[fractal/get-account-metadata] Get display name and avatar of account at startup




commit f4a68854640c15c6ef1ae14255cc267deb4687cc
Author: Alejandro Domínguez <adomu net-c com>
Date:   Thu Jul 29 03:08:38 2021 +0200

    Get display name and avatar of account at startup

 src/session/mod.rs  | 48 +++++++++++++++++++++++++++++++++++-------------
 src/session/user.rs | 11 ++++++++++-
 2 files changed, 45 insertions(+), 14 deletions(-)
---
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 10512fc5..e6b826a6 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -303,23 +303,40 @@ impl Session {
         result: Result<(Client, StoredSession), matrix_sdk::Error>,
         store_session: bool,
     ) {
-        let priv_ = imp::Session::from_instance(self);
         match result {
             Ok((client, session)) => {
-                priv_.client.set(client).unwrap();
-                let user = User::new(self, &session.user_id);
-                priv_.user.set(user).unwrap();
-
-                if store_session {
-                    // TODO: report secret service errors
-                    secret::store_session(session).unwrap();
-                }
-
-                self.room_list().load();
-                self.sync();
+                do_async(
+                    glib::PRIORITY_LOW,
+                    async move {
+                        let display_name = client.display_name().await?;
+                        let avatar_url = client.avatar_url().await?;
+                        Ok((client, display_name, avatar_url))
+                    },
+                    clone!(@weak self as obj => move |result: matrix_sdk::Result<_>| async move {
+                        match result {
+                            Ok((client, display_name, avatar_url)) => {
+                                let priv_ = imp::Session::from_instance(&obj);
+                                priv_.client.set(client).unwrap();
+                                let user = User::new(&obj, &session.user_id);
+                                user.set_display_name(display_name);
+                                user.set_avatar_url(avatar_url);
+                                priv_.user.set(user).unwrap();
+
+                                if store_session {
+                                    // TODO: report secret service errors
+                                    secret::store_session(session).unwrap();
+                                }
+
+                                obj.room_list().load();
+                                obj.sync();
+                            }
+                            Err(error) => error!("Couldn’t fetch account metadata: {}", error),
+                        };
+                    }),
+                );
             }
             Err(error) => {
-                priv_.error.replace(Some(error));
+                self.set_error(Some(error));
             }
         }
         self.emit_by_name("prepared", &[]).unwrap();
@@ -418,6 +435,11 @@ impl Session {
         priv_.error.take().map(LoginError::from)
     }
 
+    fn set_error(&self, error: Option<matrix_sdk::Error>) {
+        let priv_ = imp::Session::from_instance(self);
+        priv_.error.replace(error);
+    }
+
     pub fn connect_prepared<F: Fn(&Self) + 'static>(&self, f: F) -> glib::SignalHandlerId {
         self.connect_local("prepared", true, move |values| {
             let obj = values[0].get::<Self>().unwrap();
diff --git a/src/session/user.rs b/src/session/user.rs
index df6c7de9..ef349518 100644
--- a/src/session/user.rs
+++ b/src/session/user.rs
@@ -4,7 +4,7 @@ use crate::session::Session;
 use matrix_sdk::{
     ruma::{
         events::{room::member::MemberEventContent, StrippedStateEvent, SyncStateEvent},
-        identifiers::UserId,
+        identifiers::{MxcUri, UserId},
     },
     RoomMember,
 };
@@ -144,11 +144,20 @@ impl User {
         }
     }
 
+    pub fn set_display_name(&self, display_name: Option<String>) {
+        let priv_ = imp::User::from_instance(&self);
+        priv_.display_name.replace(display_name);
+    }
+
     pub fn avatar(&self) -> &Avatar {
         let priv_ = imp::User::from_instance(&self);
         priv_.avatar.get().unwrap()
     }
 
+    pub fn set_avatar_url(&self, url: Option<MxcUri>) {
+        self.avatar().set_url(url);
+    }
+
     /// Update the user based on the the room member state event
     pub fn update_from_room_member(&self, member: &RoomMember) {
         let changed = {


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