[fractal] Set display name through matrix-sdk



commit 3acf859ea6548ff24f5a0620e7ad0e8a97607950
Author: Alejandro Domínguez <adomu net-c com>
Date:   Thu Sep 10 00:14:16 2020 +0200

    Set display name through matrix-sdk

 fractal-gtk/src/appop/account.rs                   | 23 ++++++++-------
 fractal-gtk/src/backend/user.rs                    | 30 +++++++------------
 fractal-matrix-api/src/meson.build                 |  2 --
 fractal-matrix-api/src/r0.rs                       |  1 -
 fractal-matrix-api/src/r0/profile.rs               |  1 -
 .../src/r0/profile/set_display_name.rs             | 34 ----------------------
 6 files changed, 23 insertions(+), 68 deletions(-)
---
diff --git a/fractal-gtk/src/appop/account.rs b/fractal-gtk/src/appop/account.rs
index 1dc28dbe..bd39b8db 100644
--- a/fractal-gtk/src/appop/account.rs
+++ b/fractal-gtk/src/appop/account.rs
@@ -4,7 +4,7 @@ use log::info;
 use std::path::PathBuf;
 use std::thread;
 
-use crate::app::App;
+use crate::app::{App, RUNTIME};
 use crate::appop::AppOp;
 use crate::appop::AppState;
 use crate::backend::HandleError;
@@ -597,7 +597,7 @@ impl AppOp {
             .expect("Can't find account_settings_name_button in ui file.");
 
         let old_username = login_data.username.clone().unwrap_or_default();
-        let username = name.get_text().to_string();
+        let username: String = name.get_text().into();
 
         if old_username != username {
             let spinner = gtk::Spinner::new();
@@ -605,16 +605,17 @@ impl AppOp {
             button.set_image(Some(&spinner));
             button.set_sensitive(false);
             name.set_editable(false);
-            thread::spawn(move || {
-                match user::set_username(
-                    login_data.session_client.homeserver().clone(),
-                    login_data.access_token,
-                    login_data.uid,
-                    username,
-                ) {
+            RUNTIME.spawn(async move {
+                let query = user::set_username(
+                    login_data.session_client,
+                    &login_data.uid,
+                    Some(username).filter(|u| !u.is_empty()),
+                )
+                .await;
+
+                match query {
                     Ok(username) => {
-                        let u = Some(username);
-                        APPOP!(show_new_username, (u));
+                        APPOP!(show_new_username, (username));
                     }
                     Err(err) => {
                         err.handle_error();
diff --git a/fractal-gtk/src/backend/user.rs b/fractal-gtk/src/backend/user.rs
index c2972efc..8ac3d430 100644
--- a/fractal-gtk/src/backend/user.rs
+++ b/fractal-gtk/src/backend/user.rs
@@ -18,6 +18,7 @@ use crate::model::member::Member;
 use fractal_api::api::r0::profile::get_display_name::Request as GetDisplayNameRequest;
 use fractal_api::api::r0::profile::get_profile::Request as GetProfileRequest;
 use fractal_api::api::r0::profile::set_avatar_url::Request as SetAvatarUrlRequest;
+use fractal_api::api::r0::profile::set_display_name::Request as SetDisplayNameRequest;
 use fractal_api::api::r0::user_directory::search_users::Request as UserDirectoryRequest;
 use fractal_api::identity::r0::association::msisdn::submit_token::request as submit_phone_token_req;
 use fractal_api::identity::r0::association::msisdn::submit_token::Body as SubmitPhoneTokenBody;
@@ -49,9 +50,6 @@ use fractal_api::r0::contact::request_verification_token_msisdn::request as requ
 use fractal_api::r0::contact::request_verification_token_msisdn::Body as PhoneTokenBody;
 use fractal_api::r0::contact::request_verification_token_msisdn::Parameters as PhoneTokenParameters;
 use fractal_api::r0::contact::request_verification_token_msisdn::Response as PhoneTokenResponse;
-use fractal_api::r0::profile::set_display_name::request as set_display_name;
-use fractal_api::r0::profile::set_display_name::Body as SetDisplayNameBody;
-use fractal_api::r0::profile::set_display_name::Parameters as SetDisplayNameParameters;
 use fractal_api::r0::AccessToken;
 use fractal_api::r0::Medium;
 use fractal_api::r0::ThreePIDCredentials;
@@ -87,29 +85,23 @@ pub async fn get_username(
 }
 
 #[derive(Debug)]
-pub struct SetUserNameError(ReqwestError);
+pub struct SetUserNameError(MatrixError);
 
-impl From<ReqwestError> for SetUserNameError {
-    fn from(err: ReqwestError) -> Self {
+impl From<MatrixError> for SetUserNameError {
+    fn from(err: MatrixError) -> Self {
         Self(err)
     }
 }
 
 impl HandleError for SetUserNameError {}
 
-pub fn set_username(
-    base: Url,
-    access_token: AccessToken,
-    uid: UserId,
-    username: String,
-) -> Result<String, SetUserNameError> {
-    let params = SetDisplayNameParameters { access_token };
-    let body = SetDisplayNameBody {
-        displayname: Some(username.clone()),
-    };
-
-    let request = set_display_name(base, &params, &body, &uid)?;
-    HTTP_CLIENT.get_client().execute(request)?;
+pub async fn set_username(
+    session_client: MatrixClient,
+    user_id: &UserId,
+    username: Option<String>,
+) -> Result<Option<String>, SetUserNameError> {
+    let request = SetDisplayNameRequest::new(user_id, username.as_ref().map(String::as_str));
+    session_client.send(request).await?;
 
     Ok(username)
 }
diff --git a/fractal-matrix-api/src/meson.build b/fractal-matrix-api/src/meson.build
index 05fa5cbf..7cea3e92 100644
--- a/fractal-matrix-api/src/meson.build
+++ b/fractal-matrix-api/src/meson.build
@@ -13,14 +13,12 @@ api_sources = files(
   'r0/contact/get_identifiers.rs',
   'r0/contact/request_verification_token_email.rs',
   'r0/contact/request_verification_token_msisdn.rs',
-  'r0/profile/set_display_name.rs',
   'r0/server/domain_info.rs',
   'r0/sync/sync_events.rs',
   'r0/thirdparty/get_supported_protocols.rs',
   'r0/account.rs',
   'r0/contact.rs',
   'r0/filter.rs',
-  'r0/profile.rs',
   'r0/server.rs',
   'r0/sync.rs',
   'r0/thirdparty.rs',
diff --git a/fractal-matrix-api/src/r0.rs b/fractal-matrix-api/src/r0.rs
index 9784f349..e658cd7d 100644
--- a/fractal-matrix-api/src/r0.rs
+++ b/fractal-matrix-api/src/r0.rs
@@ -1,7 +1,6 @@
 pub mod account;
 pub mod contact;
 pub mod filter;
-pub mod profile;
 pub mod server;
 pub mod sync;
 pub mod thirdparty;


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