mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
embed login_modal into mtxchat.login()
This commit is contained in:
parent
50f06e2f78
commit
c3379b08e0
4 changed files with 38 additions and 16 deletions
|
|
@ -126,8 +126,8 @@
|
||||||
"zh": "WARNING: pddb not mounted *EN*"
|
"zh": "WARNING: pddb not mounted *EN*"
|
||||||
},
|
},
|
||||||
"mtxchat.login.title": {
|
"mtxchat.login.title": {
|
||||||
"en": "Matrix /login",
|
"en": "Matrix login",
|
||||||
"en-tts": "Matrix /login"
|
"en-tts": "Matrix login"
|
||||||
},
|
},
|
||||||
"mtxchat.password": {
|
"mtxchat.password": {
|
||||||
"en": "password",
|
"en": "password",
|
||||||
|
|
|
||||||
|
|
@ -245,17 +245,29 @@ impl<'a> MtxChat<'a> {
|
||||||
.expect("failed to write server");
|
.expect("failed to write server");
|
||||||
if self.token.len() > 0 {
|
if self.token.len() > 0 {
|
||||||
if let Some(user_id) = web::whoami(&server, &self.token) {
|
if let Some(user_id) = web::whoami(&server, &self.token) {
|
||||||
self.user_id = user_id;
|
let i = match user_id.find('@') {
|
||||||
|
Some(index) => { index + 1 },
|
||||||
|
None => { 0 },
|
||||||
|
};
|
||||||
|
let j = match user_id.find(':') {
|
||||||
|
Some(index) => { index },
|
||||||
|
None => { user_id.len() },
|
||||||
|
};
|
||||||
|
self.set(USER_ID_KEY, &user_id)
|
||||||
|
.expect("failed to save user id");
|
||||||
|
self.set(USER_NAME_KEY, &user_id[i..j])
|
||||||
|
.expect("failed to save user name");
|
||||||
|
self.set(USER_DOMAIN_KEY, &user_id[j+1..])
|
||||||
|
.expect("failed to save user domain");
|
||||||
self.logged_in = true;
|
self.logged_in = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !self.logged_in {
|
if !self.logged_in {
|
||||||
if web::get_login_type(&server) {
|
if web::get_login_type(&server) {
|
||||||
let user_id = self.get_or(USER_ID_KEY, USER_ID_KEY);
|
self.login_modal();
|
||||||
let password = self.get_or(PASSWORD_KEY, EMPTY);
|
let password = self.get_or(PASSWORD_KEY, EMPTY);
|
||||||
if let Some(new_token) = web::authenticate_user(&server, &user_id, &password) {
|
if let Some(new_token) = web::authenticate_user(&server, &self.user_id, &password) {
|
||||||
self.set_debug(TOKEN_KEY, &new_token);
|
self.set_debug(TOKEN_KEY, &new_token);
|
||||||
self.user_id = user_id;
|
|
||||||
self.logged_in = true;
|
self.logged_in = true;
|
||||||
} else {
|
} else {
|
||||||
log::info!(
|
log::info!(
|
||||||
|
|
@ -275,11 +287,16 @@ impl<'a> MtxChat<'a> {
|
||||||
|
|
||||||
pub fn login_modal(&mut self) {
|
pub fn login_modal(&mut self) {
|
||||||
const HIDE: &str = "*****";
|
const HIDE: &str = "*****";
|
||||||
let mut builder = self.modals.alert_builder(t!("mtxchat.login.title", locales::LANG));
|
let mut builder = self
|
||||||
|
.modals
|
||||||
|
.alert_builder(t!("mtxchat.login.title", locales::LANG));
|
||||||
let builder = match self.get(USER_NAME_KEY) {
|
let builder = match self.get(USER_NAME_KEY) {
|
||||||
// TODO add TextValidationFn
|
// TODO add TextValidationFn
|
||||||
Ok(Some(user)) => builder.field_placeholder_persist(Some(user), None),
|
Ok(Some(user)) => builder.field_placeholder_persist(Some(user), None),
|
||||||
_ => builder.field(Some(t!("mtxchat.user_name", locales::LANG).to_string()), None),
|
_ => builder.field(
|
||||||
|
Some(t!("mtxchat.user_name", locales::LANG).to_string()),
|
||||||
|
None,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
let builder = match self.get(USER_DOMAIN_KEY) {
|
let builder = match self.get(USER_DOMAIN_KEY) {
|
||||||
// TODO add TextValidationFn
|
// TODO add TextValidationFn
|
||||||
|
|
@ -288,7 +305,10 @@ impl<'a> MtxChat<'a> {
|
||||||
};
|
};
|
||||||
let builder = match self.get(PASSWORD_KEY) {
|
let builder = match self.get(PASSWORD_KEY) {
|
||||||
Ok(Some(pwd)) => builder.field_placeholder_persist(Some(HIDE.to_string()), None),
|
Ok(Some(pwd)) => builder.field_placeholder_persist(Some(HIDE.to_string()), None),
|
||||||
_ => builder.field(Some(t!("mtxchat.password", locales::LANG).to_string()), None),
|
_ => builder.field(
|
||||||
|
Some(t!("mtxchat.password", locales::LANG).to_string()),
|
||||||
|
None,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
if let Ok(payloads) = builder.build() {
|
if let Ok(payloads) = builder.build() {
|
||||||
self.unset_debug(TOKEN_KEY);
|
self.unset_debug(TOKEN_KEY);
|
||||||
|
|
@ -307,7 +327,8 @@ impl<'a> MtxChat<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut user_id = String::new();
|
let mut user_id = String::new();
|
||||||
write!(user_id, "@{}:{}", self.user_name, self.user_domain);
|
write!(user_id, "@{}:{}", self.user_name, self.user_domain)
|
||||||
|
.expect("failed to write user_id");
|
||||||
self.set(USER_ID_KEY, &user_id)
|
self.set(USER_ID_KEY, &user_id)
|
||||||
.expect("failed to save user");
|
.expect("failed to save user");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,9 +72,11 @@ fn wrapped_main() -> ! {
|
||||||
Some(Event::Focus) => {
|
Some(Event::Focus) => {
|
||||||
while !mtxchat.login() {
|
while !mtxchat.login() {
|
||||||
modals
|
modals
|
||||||
.show_notification(t!("mtxchat.login.failed", locales::LANG), None)
|
.show_notification(
|
||||||
|
t!("mtxchat.login.failed", locales::LANG),
|
||||||
|
None,
|
||||||
|
)
|
||||||
.expect("notification failed");
|
.expect("notification failed");
|
||||||
mtxchat.login_modal();
|
|
||||||
}
|
}
|
||||||
while !mtxchat.get_room_id() {
|
while !mtxchat.get_room_id() {
|
||||||
modals
|
modals
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ pub fn put_string_auth(
|
||||||
|
|
||||||
// --------------------------------
|
// --------------------------------
|
||||||
|
|
||||||
pub fn whoami(server: &str, token: &str) -> bool {
|
pub fn whoami(server: &str, token: &str) -> Option<String> {
|
||||||
let mut url = String::from(server);
|
let mut url = String::from(server);
|
||||||
url.push_str("/_matrix/client/r0/account/whoami");
|
url.push_str("/_matrix/client/r0/account/whoami");
|
||||||
if let Some(value) = handle_response(get_json_auth(&url, token)) {
|
if let Some(value) = handle_response(get_json_auth(&url, token)) {
|
||||||
|
|
@ -115,12 +115,11 @@ pub fn whoami(server: &str, token: &str) -> bool {
|
||||||
}
|
}
|
||||||
if let Some(Value::String(user_id)) = body.get("user_id") {
|
if let Some(Value::String(user_id)) = body.get("user_id") {
|
||||||
log::info!("user_id = {}", user_id);
|
log::info!("user_id = {}", user_id);
|
||||||
|
return Some(user_id.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_login_type(server: &str) -> bool {
|
pub fn get_login_type(server: &str) -> bool {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue