[fractal] AppOp: Change identity_url field type to Url



commit 696c2e526d1a00a470605c515ef4b7b0b2f1e27f
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sat Sep 21 03:08:05 2019 +0200

    AppOp: Change identity_url field type to Url

 fractal-gtk/src/appop/account.rs   | 10 +++++-----
 fractal-gtk/src/appop/login.rs     | 11 +++--------
 fractal-gtk/src/appop/mod.rs       |  6 +++---
 fractal-gtk/src/globals.rs         |  3 ++-
 fractal-gtk/src/passwd.rs          | 10 +++++-----
 fractal-gtk/src/widgets/address.rs |  4 ++--
 fractal-gtk/src/widgets/login.rs   |  8 ++++++--
 7 files changed, 26 insertions(+), 26 deletions(-)
---
diff --git a/fractal-gtk/src/appop/account.rs b/fractal-gtk/src/appop/account.rs
index 77a56496..47adcbd3 100644
--- a/fractal-gtk/src/appop/account.rs
+++ b/fractal-gtk/src/appop/account.rs
@@ -32,7 +32,7 @@ impl AppOp {
         if let Some(sid) = sid {
             if let Some(secret) = secret {
                 let _ = self.backend.send(BKCommand::AddThreePID(
-                    self.identity_url.clone(),
+                    self.identity_url.to_string(), // TODO: Change type to Url
                     secret.clone(),
                     sid.clone(),
                 ));
@@ -87,13 +87,13 @@ impl AppOp {
         });
 
         let value = entry.clone();
-        let id_server = self.identity_url.clone();
+        let id_server = self.identity_url.to_string();
         dialog.connect_response(move |w, r| {
             match gtk::ResponseType::from(r) {
                 gtk::ResponseType::Ok => {
                     if let Some(token) = value.get_text() {
                         let _ = backend.send(BKCommand::SubmitPhoneToken(
-                            id_server.clone(),
+                            id_server.clone(), // TODO: Change type to Url
                             secret.clone(),
                             sid.clone(),
                             token.to_string(),
@@ -124,14 +124,14 @@ impl AppOp {
             &msg,
         );
         let backend = self.backend.clone();
-        let id_server = self.identity_url.clone();
+        let id_server = self.identity_url.to_string();
         dialog.add_button(&i18n("Cancel"), gtk::ResponseType::Cancel.into());
         dialog.add_button(&i18n("Continue"), gtk::ResponseType::Ok.into());
         dialog.connect_response(move |w, r| {
             match gtk::ResponseType::from(r) {
                 gtk::ResponseType::Ok => {
                     let _ = backend.send(BKCommand::AddThreePID(
-                        id_server.clone(),
+                        id_server.clone(), // TODO: Change type to Url
                         secret.clone(),
                         sid.clone(),
                     ));
diff --git a/fractal-gtk/src/appop/login.rs b/fractal-gtk/src/appop/login.rs
index a6482c38..09f80016 100644
--- a/fractal-gtk/src/appop/login.rs
+++ b/fractal-gtk/src/appop/login.rs
@@ -1,7 +1,6 @@
 use crate::i18n::i18n;
 use log::error;
 
-use crate::globals;
 use gtk;
 use gtk::prelude::*;
 
@@ -153,20 +152,16 @@ impl AppOp {
         username: Option<String>,
         password: Option<String>,
         server: Url,
-        identity: Option<String>,
+        identity: Url,
     ) -> Option<()> {
         self.server_url = server;
-
-        self.identity_url = match identity {
-            Some(u) => u,
-            None => String::from(globals::DEFAULT_IDENTITYSERVER),
-        };
+        self.identity_url = identity;
 
         self.store_pass(
             username.clone()?,
             password.clone()?,
             self.server_url.to_string(),
-            self.identity_url.clone(),
+            self.identity_url.to_string(),
         )
         .unwrap_or_else(|_| {
             // TODO: show an error
diff --git a/fractal-gtk/src/appop/mod.rs b/fractal-gtk/src/appop/mod.rs
index fed05602..cc1f745d 100644
--- a/fractal-gtk/src/appop/mod.rs
+++ b/fractal-gtk/src/appop/mod.rs
@@ -55,7 +55,7 @@ pub struct AppOp {
     pub device_id: Option<String>,
     pub avatar: Option<String>,
     pub server_url: Url,
-    pub identity_url: String,
+    pub identity_url: Url,
 
     pub active_room: Option<String>,
     pub rooms: RoomList,
@@ -99,7 +99,7 @@ impl AppOp {
             device_id: None,
             avatar: None,
             server_url: globals::DEFAULT_HOMESERVER.clone(),
-            identity_url: String::from(globals::DEFAULT_IDENTITYSERVER),
+            identity_url: globals::DEFAULT_IDENTITYSERVER.clone(),
             syncing: false,
             msg_queue: vec![],
             sending_message: false,
@@ -137,7 +137,7 @@ impl AppOp {
             if let Ok((token, uid)) = self.get_token() {
                 self.set_token(Some(token), Some(uid), pass.2);
             } else {
-                self.connect(Some(pass.0), Some(pass.1), pass.2, Some(pass.3));
+                self.connect(Some(pass.0), Some(pass.1), pass.2, pass.3);
             }
         } else {
             self.set_state(AppState::Login);
diff --git a/fractal-gtk/src/globals.rs b/fractal-gtk/src/globals.rs
index 2165c63a..142401ba 100644
--- a/fractal-gtk/src/globals.rs
+++ b/fractal-gtk/src/globals.rs
@@ -6,7 +6,6 @@ pub static MSG_ICON_SIZE: i32 = 40;
 pub static USERLIST_ICON_SIZE: i32 = 30;
 pub static PILL_ICON_SIZE: i32 = 18;
 pub static MINUTES_TO_SPLIT_MSGS: i64 = 30;
-pub static DEFAULT_IDENTITYSERVER: &'static str = "https://vector.im";;
 pub static PLACEHOLDER_TEXT: &'static str = "Matrix username, email or phone number";
 pub static RIOT_REGISTER_URL: &'static str = "https://riot.im/app/#/register";;
 
@@ -16,4 +15,6 @@ pub static MAX_STICKER_SIZE: (i32, i32) = (200, 130);
 lazy_static! {
     pub static ref DEFAULT_HOMESERVER: Url =
         Url::parse("https://matrix.org";).expect("Malformed DEFAULT_HOMESERVER value");
+    pub static ref DEFAULT_IDENTITYSERVER: Url =
+        Url::parse("https://vector.im";).expect("Malformed DEFAULT_IDENTITYSERVER value");
 }
diff --git a/fractal-gtk/src/passwd.rs b/fractal-gtk/src/passwd.rs
index 095b5f5a..52a986ea 100644
--- a/fractal-gtk/src/passwd.rs
+++ b/fractal-gtk/src/passwd.rs
@@ -32,7 +32,7 @@ pub trait PasswordStorage {
         ss_storage::store_pass(username, password, server, identity)
     }
 
-    fn get_pass(&self) -> Result<(String, String, Url, String), Error> {
+    fn get_pass(&self) -> Result<(String, String, Url, Url), Error> {
         ss_storage::get_pass()
     }
 
@@ -187,14 +187,14 @@ mod ss_storage {
             p.delete()?;
         }
         /* Fallback to default identity server if there is none */
-        let identity = String::from(globals::DEFAULT_IDENTITYSERVER);
+        let identity = globals::DEFAULT_IDENTITYSERVER.to_string();
 
         store_pass(username, pwd, server, identity)?;
 
         Ok(())
     }
 
-    pub fn get_pass() -> Result<(String, String, Url, String), Error> {
+    pub fn get_pass() -> Result<(String, String, Url, Url), Error> {
         migrate_old_passwd()?;
 
         let ss = SecretService::new(EncryptionType::Dh)?;
@@ -230,8 +230,8 @@ mod ss_storage {
 
         /* Fallback to the vector identity server when there is none */
         let identity = match attr {
-            Some(a) => a.1.clone(),
-            None => String::from(globals::DEFAULT_IDENTITYSERVER),
+            Some(a) => Url::parse(&a.1)?,
+            None => globals::DEFAULT_IDENTITYSERVER.clone(),
         };
 
         let tup = (
diff --git a/fractal-gtk/src/widgets/address.rs b/fractal-gtk/src/widgets/address.rs
index ed191a1d..08c53506 100644
--- a/fractal-gtk/src/widgets/address.rs
+++ b/fractal-gtk/src/widgets/address.rs
@@ -159,7 +159,7 @@ impl<'a> Address<'a> {
         let action = self.action.clone();
         let entry = self.entry.clone();
         let address = self.address.clone();
-        let id_server = self.op.identity_url.clone();
+        let id_server = self.op.identity_url.to_string();
         let backend = self.op.backend.clone();
         self.signal_id = Some(self.button.clone().connect_clicked(move |w| {
             if !w.get_sensitive() || !w.is_visible() {
@@ -185,7 +185,7 @@ impl<'a> Address<'a> {
                     add_address(
                         &backend,
                         medium,
-                        id_server.clone(),
+                        id_server.clone(), // TODO: Change type to Url
                         entry.get_text().map_or(None, |gstr| Some(gstr.to_string())),
                     );
                 }
diff --git a/fractal-gtk/src/widgets/login.rs b/fractal-gtk/src/widgets/login.rs
index 5aafb6dd..71a1cf72 100644
--- a/fractal-gtk/src/widgets/login.rs
+++ b/fractal-gtk/src/widgets/login.rs
@@ -7,6 +7,7 @@ use crate::actions;
 use crate::actions::global::AppState;
 use crate::actions::login::LoginState;
 use crate::appop::AppOp;
+use crate::globals;
 use crate::i18n::i18n;
 use crate::widgets::ErrorDialog;
 
@@ -103,14 +104,17 @@ impl LoginWidget {
 
                     let mut homeserver_url =
                         hs_url.expect("hs_url must return earlier if it's Err");
-                    let mut idserver = None;
+                    let mut idserver = globals::DEFAULT_IDENTITYSERVER.clone();
                     match get_well_known(&txt) {
                         // TODO: Use Url everywhere
                         Ok(response) => {
                             info!("Got well-known response from {}: {:#?}", &txt, response);
                             homeserver_url =
                                 Url::parse(&response.homeserver.base_url).unwrap_or(homeserver_url);
-                            idserver = response.identity_server.map(|ids| ids.base_url);
+                            idserver = response
+                                .identity_server
+                                .and_then(|ids| Url::parse(&ids.base_url).ok())
+                                .unwrap_or(idserver);
                         }
                         Err(e) => info!("Failed to .well-known request: {:#?}", e),
                     };


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]