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




commit e9197f077ed6a412ce365653000555ef6af99cbb
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Aug 1 19:04:16 2021 +0200

    Get display name and avatar of account at startup

 src/session/mod.rs  | 22 ++++++++++++--
 src/session/user.rs | 86 +++++++++++++++++++++++------------------------------
 2 files changed, 58 insertions(+), 50 deletions(-)
---
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 10512fc5..311bb445 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -306,9 +306,27 @@ impl Session {
         let priv_ = imp::Session::from_instance(self);
         match result {
             Ok((client, session)) => {
-                priv_.client.set(client).unwrap();
+                priv_.client.set(client.clone()).unwrap();
                 let user = User::new(self, &session.user_id);
-                priv_.user.set(user).unwrap();
+                priv_.user.set(user.clone()).unwrap();
+
+                do_async(
+                    glib::PRIORITY_LOW,
+                    async move {
+                        let display_name = client.display_name().await?;
+                        let avatar_url = client.avatar_url().await?;
+                        Ok((display_name, avatar_url))
+                    },
+                    move |result: matrix_sdk::Result<_>| async move {
+                        match result {
+                            Ok((display_name, avatar_url)) => {
+                                user.set_display_name(display_name);
+                                user.set_avatar_url(avatar_url);
+                            }
+                            Err(error) => error!("Couldn’t fetch account metadata: {}", error),
+                        };
+                    },
+                );
 
                 if store_session {
                     // TODO: report secret service errors
diff --git a/src/session/user.rs b/src/session/user.rs
index ec5731f2..8045d1a3 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,
 };
@@ -141,71 +141,61 @@ 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);
+        self.notify("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 = {
-            let priv_ = imp::User::from_instance(&self);
-
-            let user_id = priv_.user_id.get().unwrap();
-            if member.user_id().as_str() != user_id {
-                return;
-            };
-
-            //let content = event.content;
-            let display_name = member.display_name().map(|name| name.to_owned());
-            self.avatar().set_url(member.avatar_url().cloned());
+        let priv_ = imp::User::from_instance(&self);
 
-            let mut current_display_name = priv_.display_name.borrow_mut();
-            if *current_display_name != display_name {
-                *current_display_name = display_name;
-                true
-            } else {
-                false
-            }
+        let user_id = priv_.user_id.get().unwrap();
+        if member.user_id().as_str() != user_id {
+            return;
         };
 
-        if changed {
-            self.notify("display-name");
+        //let content = event.content;
+        let display_name = member.display_name().map(|name| name.to_owned());
+        self.avatar().set_url(member.avatar_url().cloned());
+
+        if *priv_.display_name.borrow() != display_name {
+            self.set_display_name(display_name);
         }
     }
 
     /// Update the user based on the the room member state event
     pub fn update_from_member_event(&self, event: &SyncStateEvent<MemberEventContent>) {
-        let changed = {
-            let priv_ = imp::User::from_instance(&self);
-            let user_id = priv_.user_id.get().unwrap();
-            if event.sender.as_str() != user_id {
-                return;
-            };
-
-            let display_name = if let Some(display_name) = &event.content.displayname {
-                Some(display_name.to_owned())
-            } else {
-                event
-                    .content
-                    .third_party_invite
-                    .as_ref()
-                    .map(|i| i.display_name.to_owned())
-            };
-
-            self.avatar().set_url(event.content.avatar_url.to_owned());
+        let priv_ = imp::User::from_instance(&self);
+        let user_id = priv_.user_id.get().unwrap();
+        if event.sender.as_str() != user_id {
+            return;
+        };
 
-            let mut current_display_name = priv_.display_name.borrow_mut();
-            if *current_display_name != display_name {
-                *current_display_name = display_name;
-                true
-            } else {
-                false
-            }
+        let display_name = if let Some(display_name) = &event.content.displayname {
+            Some(display_name.to_owned())
+        } else {
+            event
+                .content
+                .third_party_invite
+                .as_ref()
+                .map(|i| i.display_name.to_owned())
         };
 
-        if changed {
-            self.notify("display-name");
+        self.avatar().set_url(event.content.avatar_url.to_owned());
+
+        if *priv_.display_name.borrow() != display_name {
+            self.set_display_name(display_name);
         }
     }
 


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