[fractal/fractal-next] room: Load power levels on initialization



commit addcd499654f87e0f434ca58e6b33004833e2192
Author: Kai A. Hiller <V02460 gmail com>
Date:   Fri Aug 13 23:47:59 2021 +0200

    room: Load power levels on initialization

 src/session/room/room.rs | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
---
diff --git a/src/session/room/room.rs b/src/session/room/room.rs
index 17f4ef81..11c57b6d 100644
--- a/src/session/room/room.rs
+++ b/src/session/room/room.rs
@@ -14,11 +14,12 @@ use matrix_sdk::{
                     TextMessageEventContent,
                 },
                 name::NameEventContent,
+                power_levels::PowerLevelsEventContent,
                 topic::TopicEventContent,
             },
             tag::TagName,
             AnyRoomAccountDataEvent, AnyStateEventContent, AnyStrippedStateEvent,
-            AnySyncMessageEvent, AnySyncRoomEvent, AnySyncStateEvent, SyncMessageEvent,
+            AnySyncMessageEvent, AnySyncRoomEvent, AnySyncStateEvent, EventType, SyncMessageEvent,
             SyncStateEvent, Unsigned,
         },
         identifiers::{EventId, RoomId, UserId},
@@ -240,6 +241,8 @@ mod imp {
                 .set(Avatar::new(obj.session(), obj.matrix_room().avatar_url()))
                 .unwrap();
 
+            obj.load_power_levels();
+
             obj.bind_property("display-name", obj.avatar(), "display-name")
                 .flags(glib::BindingFlags::SYNC_CREATE)
                 .build()
@@ -761,6 +764,40 @@ impl Room {
         */
     }
 
+    fn load_power_levels(&self) {
+        let matrix_room = self.matrix_room();
+        do_async(
+            glib::PRIORITY_DEFAULT_IDLE,
+            async move {
+                let state_event = match matrix_room
+                    .get_state_event(EventType::RoomPowerLevels, "")
+                    .await
+                {
+                    Ok(state_event) => state_event,
+                    Err(e) => {
+                        error!("Initial load of room power levels failed: {}", e);
+                        return None;
+                    }
+                };
+
+                state_event
+                    .and_then(|e| e.deserialize().ok())
+                    .and_then(|e| {
+                        if let AnySyncStateEvent::RoomPowerLevels(e) = e {
+                            Some(e)
+                        } else {
+                            None
+                        }
+                    })
+            },
+            clone!(@weak self as obj => move |event: Option<SyncStateEvent<PowerLevelsEventContent>>| async 
move {
+                if let Some(event) = event {
+                    obj.power_levels().update_from_event(event);
+                }
+            }),
+        );
+    }
+
     pub fn send_text_message(&self, body: &str, markdown_enabled: bool) {
         let content = if let Some(body) = body.strip_prefix("/me ") {
             let emote = if markdown_enabled {


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