[fractal] Set room name through matrix-sdk



commit 007920dea42b50e7f4f4c75070edc13858c3d180
Author: Alejandro Domínguez <adomu net-c com>
Date:   Mon Sep 7 02:38:25 2020 +0200

    Set room name through matrix-sdk

 fractal-gtk/src/backend/room.rs          | 36 +++++++++++++++++++-------------
 fractal-gtk/src/widgets/room_settings.rs | 25 ++++++++++------------
 2 files changed, 32 insertions(+), 29 deletions(-)
---
diff --git a/fractal-gtk/src/backend/room.rs b/fractal-gtk/src/backend/room.rs
index ec41a1dd..d10b03f4 100644
--- a/fractal-gtk/src/backend/room.rs
+++ b/fractal-gtk/src/backend/room.rs
@@ -47,6 +47,7 @@ use fractal_api::events::room::avatar::AvatarEventContent;
 use fractal_api::events::room::history_visibility::HistoryVisibility;
 use fractal_api::events::room::history_visibility::HistoryVisibilityEventContent;
 use fractal_api::events::room::message::MessageEventContent;
+use fractal_api::events::room::name::NameEventContent;
 use fractal_api::events::tag::TagInfo;
 use fractal_api::events::AnyBasicEventContent;
 use fractal_api::events::AnyInitialStateEvent;
@@ -55,6 +56,7 @@ use fractal_api::events::AnyStateEventContent;
 use fractal_api::events::EventContent;
 use fractal_api::events::EventType;
 use fractal_api::events::InitialStateEvent;
+use fractal_api::events::InvalidInput as NameRoomEventInvalidInput;
 use fractal_api::r0::pushrules::delete_room_rules::request as delete_room_rules;
 use fractal_api::r0::pushrules::delete_room_rules::Parameters as DelRoomRulesParams;
 use fractal_api::r0::pushrules::get_room_rules::request as get_room_rules;
@@ -471,30 +473,34 @@ pub async fn mark_as_read(
     Ok((room_id, event_id))
 }
 #[derive(Debug)]
-pub struct SetRoomNameError(ReqwestError);
+pub enum SetRoomNameError {
+    Matrix(MatrixError),
+    InvalidName(NameRoomEventInvalidInput),
+}
 
-impl From<ReqwestError> for SetRoomNameError {
-    fn from(err: ReqwestError) -> Self {
-        Self(err)
+impl From<MatrixError> for SetRoomNameError {
+    fn from(err: MatrixError) -> Self {
+        Self::Matrix(err)
+    }
+}
+
+impl From<NameRoomEventInvalidInput> for SetRoomNameError {
+    fn from(err: NameRoomEventInvalidInput) -> Self {
+        Self::InvalidName(err)
     }
 }
 
 impl HandleError for SetRoomNameError {}
 
-pub fn set_room_name(
-    base: Url,
-    access_token: AccessToken,
-    room_id: RoomId,
+pub async fn set_room_name(
+    session_client: MatrixClient,
+    room_id: &RoomId,
     name: String,
 ) -> Result<(), SetRoomNameError> {
-    let params = CreateStateEventsForKeyParameters { access_token };
+    let content = &AnyStateEventContent::RoomName(NameEventContent::new(name)?);
+    let request = SendStateEventForKeyRequest::new(room_id, "m.room.name", content);
 
-    let body = json!({
-        "name": name,
-    });
-
-    let request = create_state_events_for_key(base, &params, &body, &room_id, "m.room.name")?;
-    HTTP_CLIENT.get_client().execute(request)?;
+    session_client.send(request).await?;
 
     Ok(())
 }
diff --git a/fractal-gtk/src/widgets/room_settings.rs b/fractal-gtk/src/widgets/room_settings.rs
index 3b5ed560..55bcf1db 100644
--- a/fractal-gtk/src/widgets/room_settings.rs
+++ b/fractal-gtk/src/widgets/room_settings.rs
@@ -14,7 +14,7 @@ use gtk::prelude::*;
 
 use crate::actions;
 use crate::actions::{ButtonState, StateExt};
-use crate::app::App;
+use crate::app::{App, RUNTIME};
 use crate::model::{member::Member, room::Room};
 use crate::util::markup_text;
 use crate::widgets;
@@ -88,13 +88,13 @@ impl RoomSettings {
             stack.set_visible_child_name("info")
         }
 
-        self.init_room_settings(session_client);
-        self.connect();
+        self.init_room_settings(session_client.clone());
+        self.connect(session_client);
 
         Some(page)
     }
 
-    pub fn connect(&mut self) {
+    pub fn connect(&mut self, session_client: MatrixClient) {
         let name_btn = self
             .builder
             .get_object::<gtk::Button>("room_settings_room_name_button")
@@ -145,7 +145,7 @@ impl RoomSettings {
         });
 
         name_btn.connect_clicked(clone!(@strong this => move |_| {
-            this.borrow_mut().update_room_name();
+            this.borrow_mut().update_room_name(session_client.clone());
         }));
 
         let button = topic_btn.clone();
@@ -490,7 +490,7 @@ impl RoomSettings {
         }
     }
 
-    pub fn update_room_name(&mut self) -> Option<()> {
+    pub fn update_room_name(&mut self, session_client: MatrixClient) -> Option<()> {
         let entry = self
             .builder
             .get_object::<gtk::Entry>("room_settings_room_name_entry")
@@ -501,7 +501,6 @@ impl RoomSettings {
             .expect("Can't find room_settings_name_button in ui file.");
 
         let new_name = entry.get_text().to_string();
-        let room = &self.room;
 
         let spinner = gtk::Spinner::new();
         spinner.start();
@@ -509,19 +508,17 @@ impl RoomSettings {
         button.set_sensitive(false);
         entry.set_editable(false);
 
-        let server = self.server_url.clone();
-        let access_token = self.access_token.clone();
-        let room_id = room.id.clone();
-        thread::spawn(
-            move || match room::set_room_name(server, access_token, room_id, new_name) {
+        let room_id = self.room.id.clone();
+        RUNTIME.spawn(async move {
+            match room::set_room_name(session_client, &room_id, new_name).await {
                 Ok(_) => {
                     APPOP!(show_new_room_name);
                 }
                 Err(err) => {
                     err.handle_error();
                 }
-            },
-        );
+            }
+        });
 
         None
     }


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