From c3379b08e0a024ba7b96b09591d4acd51af1c4f9 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 7 Aug 2023 08:30:54 +1000 Subject: [PATCH] embed login_modal into mtxchat.login() --- apps/mtxchat/locales/i18n.json | 4 ++-- apps/mtxchat/src/lib.rs | 37 ++++++++++++++++++++++++++-------- apps/mtxchat/src/main.rs | 6 ++++-- apps/mtxchat/src/web.rs | 7 +++---- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/apps/mtxchat/locales/i18n.json b/apps/mtxchat/locales/i18n.json index 987b2cc25..0c0ca5e97 100644 --- a/apps/mtxchat/locales/i18n.json +++ b/apps/mtxchat/locales/i18n.json @@ -126,8 +126,8 @@ "zh": "WARNING: pddb not mounted *EN*" }, "mtxchat.login.title": { - "en": "Matrix /login", - "en-tts": "Matrix /login" + "en": "Matrix login", + "en-tts": "Matrix login" }, "mtxchat.password": { "en": "password", diff --git a/apps/mtxchat/src/lib.rs b/apps/mtxchat/src/lib.rs index bc3541ee9..72588914d 100644 --- a/apps/mtxchat/src/lib.rs +++ b/apps/mtxchat/src/lib.rs @@ -245,17 +245,29 @@ impl<'a> MtxChat<'a> { .expect("failed to write server"); if self.token.len() > 0 { 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; } } if !self.logged_in { 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); - 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.user_id = user_id; self.logged_in = true; } else { log::info!( @@ -275,11 +287,16 @@ impl<'a> MtxChat<'a> { pub fn login_modal(&mut self) { 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) { // TODO add TextValidationFn 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) { // TODO add TextValidationFn @@ -288,7 +305,10 @@ impl<'a> MtxChat<'a> { }; let builder = match self.get(PASSWORD_KEY) { 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() { self.unset_debug(TOKEN_KEY); @@ -307,7 +327,8 @@ impl<'a> MtxChat<'a> { } } 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) .expect("failed to save user"); } diff --git a/apps/mtxchat/src/main.rs b/apps/mtxchat/src/main.rs index 907126e92..9c41cd4b6 100644 --- a/apps/mtxchat/src/main.rs +++ b/apps/mtxchat/src/main.rs @@ -72,9 +72,11 @@ fn wrapped_main() -> ! { Some(Event::Focus) => { while !mtxchat.login() { modals - .show_notification(t!("mtxchat.login.failed", locales::LANG), None) + .show_notification( + t!("mtxchat.login.failed", locales::LANG), + None, + ) .expect("notification failed"); - mtxchat.login_modal(); } while !mtxchat.get_room_id() { modals diff --git a/apps/mtxchat/src/web.rs b/apps/mtxchat/src/web.rs index 1a8d85640..0bb44a45d 100644 --- a/apps/mtxchat/src/web.rs +++ b/apps/mtxchat/src/web.rs @@ -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 { let mut url = String::from(server); url.push_str("/_matrix/client/r0/account/whoami"); 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") { log::info!("user_id = {}", user_id); + return Some(user_id.to_string()); } } - true - } else { - false } + None } pub fn get_login_type(server: &str) -> bool {