[fractal] Redact messages through matrix-sdk



commit 106d767b4bc44bc143bdd6b37cb1022eae8a5389
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Sep 6 01:49:45 2020 +0200

    Redact messages through matrix-sdk

 fractal-gtk/src/actions/message.rs               | 14 +++-----
 fractal-gtk/src/appop/media_viewer.rs            |  8 ++---
 fractal-gtk/src/appop/room.rs                    |  1 -
 fractal-gtk/src/backend/room.rs                  | 40 ++++++++---------------
 fractal-matrix-api/src/meson.build               |  2 --
 fractal-matrix-api/src/r0.rs                     |  1 -
 fractal-matrix-api/src/r0/redact.rs              |  1 -
 fractal-matrix-api/src/r0/redact/redact_event.rs | 41 ------------------------
 8 files changed, 19 insertions(+), 89 deletions(-)
---
diff --git a/fractal-gtk/src/actions/message.rs b/fractal-gtk/src/actions/message.rs
index f135c77c..3d1e480e 100644
--- a/fractal-gtk/src/actions/message.rs
+++ b/fractal-gtk/src/actions/message.rs
@@ -1,6 +1,5 @@
 use crate::backend::{dw_media, media, room, ContentType};
 use fractal_api::identifiers::RoomId;
-use fractal_api::r0::AccessToken;
 use fractal_api::Client as MatrixClient;
 use glib::clone;
 use log::error;
@@ -8,7 +7,6 @@ use std::cell::RefCell;
 use std::fs;
 use std::process::Command;
 use std::rc::Rc;
-use std::thread;
 
 use crate::actions::AppState;
 use crate::app::RUNTIME;
@@ -32,11 +30,9 @@ use crate::widgets::SourceDialog;
 /* This creates all actions the room history can perform */
 pub fn new(
     session_client: MatrixClient,
-    access_token: AccessToken,
     ui: UI,
     back_history: Rc<RefCell<Vec<AppState>>>,
 ) -> gio::SimpleActionGroup {
-    let server_url = session_client.homeserver().clone();
     let actions = SimpleActionGroup::new();
     /* Action for each message */
     let reply = SimpleAction::new("reply", glib::VariantTy::new("s").ok());
@@ -197,9 +193,8 @@ pub fn new(
         }
     });
 
-    let s = server_url.clone();
-    let tk = access_token.clone();
     delete.connect_activate(clone!(
+    @strong session_client,
     @weak back_history,
     @weak window
     => move |_, data| {
@@ -212,10 +207,9 @@ pub fn new(
             }
         }
         if let Some(msg) = get_message(data) {
-            let server = s.clone();
-            let access_token = tk.clone();
-            thread::spawn(move || {
-                let query = room::redact_msg(server, access_token, msg);
+            let session_client = session_client.clone();
+            RUNTIME.spawn(async move {
+                let query = room::redact_msg(session_client, msg).await;
                 if let Err(err) = query {
                     err.handle_error();
                 }
diff --git a/fractal-gtk/src/appop/media_viewer.rs b/fractal-gtk/src/appop/media_viewer.rs
index 98145f04..728529d4 100644
--- a/fractal-gtk/src/appop/media_viewer.rs
+++ b/fractal-gtk/src/appop/media_viewer.rs
@@ -45,12 +45,8 @@ impl AppOp {
 
             if let Some(login_data) = self.login_data.clone() {
                 let back_history = self.room_back_history.clone();
-                let actions = actions::Message::new(
-                    login_data.session_client,
-                    login_data.access_token,
-                    self.ui.clone(),
-                    back_history,
-                );
+                let actions =
+                    actions::Message::new(login_data.session_client, self.ui.clone(), back_history);
                 header.insert_action_group("message", Some(&actions));
                 body.insert_action_group("message", Some(&actions));
             } else {
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index d8990f63..b79b7dd1 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -303,7 +303,6 @@ impl AppOp {
         let back_history = self.room_back_history.clone();
         let actions = actions::Message::new(
             login_data.session_client.clone(),
-            login_data.access_token,
             self.ui.clone(),
             back_history,
         );
diff --git a/fractal-gtk/src/backend/room.rs b/fractal-gtk/src/backend/room.rs
index 57ecd3cf..229db6eb 100644
--- a/fractal-gtk/src/backend/room.rs
+++ b/fractal-gtk/src/backend/room.rs
@@ -34,6 +34,7 @@ use fractal_api::api::r0::filter::RoomEventFilter;
 use fractal_api::api::r0::media::create_content::Request as CreateContentRequest;
 use fractal_api::api::r0::media::create_content::Response as CreateContentResponse;
 use fractal_api::api::r0::message::get_message_events::Request as GetMessagesEventsRequest;
+use fractal_api::api::r0::redact::redact_event::Request as RedactEventRequest;
 use fractal_api::api::r0::room::create_room::Request as CreateRoomRequest;
 use fractal_api::api::r0::room::create_room::RoomPreset;
 use fractal_api::api::r0::room::Visibility;
@@ -57,10 +58,6 @@ use fractal_api::r0::pushrules::get_room_rules::request as get_room_rules;
 use fractal_api::r0::pushrules::get_room_rules::Parameters as GetRoomRulesParams;
 use fractal_api::r0::pushrules::set_room_rules::request as set_room_rules;
 use fractal_api::r0::pushrules::set_room_rules::Parameters as SetRoomRulesParams;
-use fractal_api::r0::redact::redact_event::request as redact_event;
-use fractal_api::r0::redact::redact_event::Body as RedactEventBody;
-use fractal_api::r0::redact::redact_event::Parameters as RedactEventParameters;
-use fractal_api::r0::redact::redact_event::Response as RedactEventResponse;
 use fractal_api::r0::state::create_state_events_for_key::request as create_state_events_for_key;
 use fractal_api::r0::state::create_state_events_for_key::Parameters as CreateStateEventsForKeyParameters;
 use fractal_api::r0::state::get_state_events_for_key::request as get_state_events_for_key;
@@ -364,12 +361,12 @@ pub async fn send_typing(
 #[derive(Debug)]
 pub enum SendMsgRedactionError {
     MessageNotSent,
-    Reqwest(ReqwestError),
+    Matrix(MatrixError),
 }
 
-impl From<ReqwestError> for SendMsgRedactionError {
-    fn from(err: ReqwestError) -> Self {
-        Self::Reqwest(err)
+impl From<MatrixError> for SendMsgRedactionError {
+    fn from(err: MatrixError) -> Self {
+        Self::Matrix(err)
     }
 }
 
@@ -381,28 +378,17 @@ impl HandleError for SendMsgRedactionError {
     }
 }
 
-pub fn redact_msg(
-    base: Url,
-    access_token: AccessToken,
+pub async fn redact_msg(
+    session_client: MatrixClient,
     msg: Message,
-) -> Result<(EventId, Option<EventId>), SendMsgRedactionError> {
-    let room_id = &msg.room;
-    let txn_id = msg.get_txn_id();
-    let event_id = msg
-        .id
-        .as_ref()
-        .ok_or(SendMsgRedactionError::MessageNotSent)?;
-
-    let params = RedactEventParameters { access_token };
-
-    let body = RedactEventBody {
-        reason: "Deletion requested by the sender".into(),
-    };
+) -> Result<(EventId, EventId), SendMsgRedactionError> {
+    let ref txn_id = msg.get_txn_id();
+    let event_id = msg.id.ok_or(SendMsgRedactionError::MessageNotSent)?;
 
-    let request = redact_event(base, &params, &body, room_id, event_id, &txn_id)?;
-    let response: RedactEventResponse = HTTP_CLIENT.get_client().execute(request)?.json()?;
+    let request = RedactEventRequest::new(&msg.room, &event_id, txn_id);
+    let response = session_client.send(request).await?;
 
-    Ok((event_id.clone(), response.event_id))
+    Ok((event_id, response.event_id))
 }
 
 #[derive(Debug)]
diff --git a/fractal-matrix-api/src/meson.build b/fractal-matrix-api/src/meson.build
index 68c1beda..386bdfba 100644
--- a/fractal-matrix-api/src/meson.build
+++ b/fractal-matrix-api/src/meson.build
@@ -18,7 +18,6 @@ api_sources = files(
   'r0/profile/get_profile.rs',
   'r0/profile/set_avatar_url.rs',
   'r0/profile/set_display_name.rs',
-  'r0/redact/redact_event.rs',
   'r0/server/domain_info.rs',
   'r0/state/create_state_events_for_key.rs',
   'r0/state/get_state_events_for_key.rs',
@@ -32,7 +31,6 @@ api_sources = files(
   'r0/filter.rs',
   'r0/media.rs',
   'r0/profile.rs',
-  'r0/redact.rs',
   'r0/server.rs',
   'r0/state.rs',
   'r0/sync.rs',
diff --git a/fractal-matrix-api/src/r0.rs b/fractal-matrix-api/src/r0.rs
index ae62186a..ef6e3c07 100644
--- a/fractal-matrix-api/src/r0.rs
+++ b/fractal-matrix-api/src/r0.rs
@@ -4,7 +4,6 @@ pub mod filter;
 pub mod media;
 pub mod profile;
 pub mod pushrules;
-pub mod redact;
 pub mod server;
 pub mod state;
 pub mod sync;


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