[fractal] Join rooms through matrix-sdk



commit 943b30064f67739543fe9f5f5264501a1dee0515
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sat Aug 15 03:42:19 2020 +0200

    Join rooms through matrix-sdk

 fractal-gtk/src/appop/invite.rs                    | 13 ++--
 fractal-gtk/src/appop/room.rs                      | 27 ++++----
 fractal-gtk/src/backend/room.rs                    | 71 ++++++----------------
 fractal-gtk/src/widgets/room.rs                    | 15 +++--
 fractal-matrix-api/src/meson.build                 |  1 -
 fractal-matrix-api/src/r0/membership.rs            |  1 -
 .../src/r0/membership/join_room_by_id_or_alias.rs  | 36 -----------
 7 files changed, 44 insertions(+), 120 deletions(-)
---
diff --git a/fractal-gtk/src/appop/invite.rs b/fractal-gtk/src/appop/invite.rs
index cd5054c0..faf45948 100644
--- a/fractal-gtk/src/appop/invite.rs
+++ b/fractal-gtk/src/appop/invite.rs
@@ -5,7 +5,7 @@ use fractal_api::identifiers::{RoomId, UserId};
 use gtk::prelude::*;
 use std::thread;
 
-use crate::app::App;
+use crate::app::{App, RUNTIME};
 use crate::appop::member::SearchType;
 use crate::appop::AppOp;
 use crate::backend::HandleError;
@@ -226,13 +226,10 @@ impl AppOp {
         let login_data = unwrap_or_unit_return!(self.login_data.clone());
         if let Some(rid) = self.invitation_roomid.take() {
             let room_id = rid.clone();
+            let session_client = login_data.session_client.clone();
             if accept {
-                thread::spawn(move || {
-                    match room::join_room(
-                        login_data.session_client.homeserver().clone(),
-                        login_data.access_token,
-                        room_id.into(),
-                    ) {
+                RUNTIME.spawn(async move {
+                    match room::join_room(session_client, &room_id.into()).await {
                         Ok(jtr) => {
                             let jtr = Some(jtr);
                             APPOP!(set_join_to_room, (jtr));
@@ -246,7 +243,7 @@ impl AppOp {
             } else {
                 thread::spawn(move || {
                     let query = room::leave_room(
-                        login_data.session_client.homeserver().clone(),
+                        session_client.homeserver().clone(),
                         login_data.access_token,
                         room_id,
                     );
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 3f889310..fcc07a2d 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -10,7 +10,7 @@ use std::thread;
 
 use gtk::prelude::*;
 
-use crate::app::App;
+use crate::app::{App, RUNTIME};
 use crate::appop::AppOp;
 use crate::backend::HandleError;
 
@@ -550,21 +550,18 @@ impl AppOp {
             .trim()
             .try_into();
 
-        thread::spawn(move || {
-            let room_id = match try_room_id {
-                Ok(rid) => rid,
-                Err(_) => {
-                    let error = i18n("The room ID is malformed");
-                    APPOP!(show_error, (error));
-                    return;
-                }
-            };
+        let room_id = match try_room_id {
+            Ok(room_id) => room_id,
+            Err(_) => {
+                let error = i18n("The room ID is malformed");
+                APPOP!(show_error, (error));
+                return;
+            }
+        };
 
-            match room::join_room(
-                login_data.session_client.homeserver().clone(),
-                login_data.access_token,
-                room_id,
-            ) {
+        let session_client = login_data.session_client.clone();
+        RUNTIME.spawn(async move {
+            match room::join_room(session_client, &room_id).await {
                 Ok(jtr) => {
                     let jtr = Some(jtr);
                     APPOP!(set_join_to_room, (jtr));
diff --git a/fractal-gtk/src/backend/room.rs b/fractal-gtk/src/backend/room.rs
index 6aa8f8d2..a9e09d5f 100644
--- a/fractal-gtk/src/backend/room.rs
+++ b/fractal-gtk/src/backend/room.rs
@@ -1,10 +1,13 @@
 use log::error;
 use serde_json::json;
 
-use fractal_api::identifiers::{Error as IdError, EventId, RoomId, RoomIdOrAliasId, UserId};
 use fractal_api::reqwest::Error as ReqwestError;
-use fractal_api::reqwest::StatusCode;
 use fractal_api::url::{ParseError as UrlError, Url};
+use fractal_api::{
+    identifiers::{Error as IdError, EventId, RoomId, RoomIdOrAliasId, UserId},
+    Client as MatrixClient, Error as MatrixError, FromHttpResponseError as RumaResponseError,
+    ServerError,
+};
 use std::fs;
 use std::io::Error as IoError;
 use std::path::{Path, PathBuf};
@@ -19,7 +22,6 @@ use crate::actions::AppState;
 use crate::backend::HTTP_CLIENT;
 use crate::util::cache_dir_path;
 
-use crate::error::StandardErrorResponse;
 use crate::model::{
     member::Member,
     message::Message,
@@ -38,9 +40,6 @@ use fractal_api::r0::media::create_content::Response as CreateContentResponse;
 use fractal_api::r0::membership::invite_user::request as invite_user;
 use fractal_api::r0::membership::invite_user::Body as InviteUserBody;
 use fractal_api::r0::membership::invite_user::Parameters as InviteUserParameters;
-use fractal_api::r0::membership::join_room_by_id_or_alias::request as join_room_req;
-use fractal_api::r0::membership::join_room_by_id_or_alias::Parameters as JoinRoomParameters;
-use fractal_api::r0::membership::join_room_by_id_or_alias::Response as JoinRoomResponse;
 use fractal_api::r0::membership::leave_room::request as leave_room_req;
 use fractal_api::r0::membership::leave_room::Parameters as LeaveRoomParameters;
 use fractal_api::r0::message::create_message_event::request as create_message_event;
@@ -387,37 +386,21 @@ pub fn redact_msg(
 }
 
 #[derive(Debug)]
-pub enum JoinRoomError {
-    Request(ReqwestError),
-    Response(StandardErrorResponse),
-}
+pub struct JoinRoomError(MatrixError);
 
-impl From<ReqwestError> for JoinRoomError {
-    fn from(err: ReqwestError) -> Self {
-        Self::Request(err)
-    }
-}
-
-impl From<StandardErrorResponse> for JoinRoomError {
-    fn from(err: StandardErrorResponse) -> Self {
-        Self::Response(err)
+impl From<MatrixError> for JoinRoomError {
+    fn from(err: MatrixError) -> Self {
+        Self(err)
     }
 }
 
 impl HandleError for JoinRoomError {
     fn handle_error(&self) {
-        let err_str: String;
-        let info: Option<String>;
-
-        match self {
-            JoinRoomError::Request(error) => {
-                err_str = format!("{:?}", error);
-                info = None;
-            }
-            JoinRoomError::Response(error) => {
-                err_str = error.error.clone();
-                info = Some(error.error.clone());
+        let (err_str, info) = match &self.0 {
+            MatrixError::RumaResponse(RumaResponseError::Http(ServerError::Known(error))) => {
+                (error.message.clone(), Some(error.message.clone()))
             }
+            error => (error.to_string(), None),
         };
 
         error!(
@@ -431,28 +414,14 @@ impl HandleError for JoinRoomError {
     }
 }
 
-pub fn join_room(
-    base: Url,
-    access_token: AccessToken,
-    room_id: RoomIdOrAliasId,
+pub async fn join_room(
+    session_client: MatrixClient,
+    room_id_or_alias_id: &RoomIdOrAliasId,
 ) -> Result<RoomId, JoinRoomError> {
-    let room_id_or_alias_id = room_id;
-
-    let params = JoinRoomParameters {
-        access_token,
-        server_name: Default::default(),
-    };
-
-    let request = join_room_req(base, &room_id_or_alias_id, &params)?;
-    let response = HTTP_CLIENT.get_client().execute(request)?;
-
-    if response.status() != StatusCode::OK {
-        let resp: StandardErrorResponse = response.json()?;
-        Err(JoinRoomError::Response(resp))
-    } else {
-        let resp: JoinRoomResponse = response.json()?;
-        Ok(resp.room_id)
-    }
+    Ok(session_client
+        .join_room_by_id_or_alias(room_id_or_alias_id, Default::default())
+        .await?
+        .room_id)
 }
 
 #[derive(Debug)]
diff --git a/fractal-gtk/src/widgets/room.rs b/fractal-gtk/src/widgets/room.rs
index 5845f9c2..c2577c62 100644
--- a/fractal-gtk/src/widgets/room.rs
+++ b/fractal-gtk/src/widgets/room.rs
@@ -2,18 +2,18 @@ use crate::util::i18n::i18n;
 
 use crate::backend::room;
 use gtk::prelude::*;
-use std::thread;
 
 use crate::model::room::Room;
 
 use crate::util::markup_text;
 
-use crate::app::App;
+use crate::app::{App, RUNTIME};
 use crate::appop::AppOp;
 use crate::backend::HandleError;
 
 use crate::widgets;
 use crate::widgets::AvatarExt;
+use fractal_api::identifiers::RoomIdOrAliasId;
 use gtk::WidgetExt;
 
 const AVATAR_SIZE: i32 = 60;
@@ -118,13 +118,12 @@ impl<'a> RoomBox<'a> {
             members_count.get_style_context().add_class("dim-label");
 
             let join_button = gtk::Button::with_label(i18n("Join").as_str());
-            let room_id = room.id.clone();
+            let room_id_or_alias: RoomIdOrAliasId = room.id.clone().into();
             join_button.connect_clicked(move |_| {
-                let server_url = login_data.session_client.homeserver().clone();
-                let access_token = login_data.access_token.clone();
-                let room_id = room_id.clone();
-                thread::spawn(move || {
-                    match room::join_room(server_url, access_token, room_id.into()) {
+                let session_client = login_data.session_client.clone();
+                let room_id_or_alias = room_id_or_alias.clone();
+                RUNTIME.spawn(async move {
+                    match room::join_room(session_client, &room_id_or_alias).await {
                         Ok(jtr) => {
                             let jtr = Some(jtr);
                             APPOP!(set_join_to_room, (jtr));
diff --git a/fractal-matrix-api/src/meson.build b/fractal-matrix-api/src/meson.build
index 1853e32a..5aec49a9 100644
--- a/fractal-matrix-api/src/meson.build
+++ b/fractal-matrix-api/src/meson.build
@@ -22,7 +22,6 @@ api_sources = files(
   'r0/media/get_content.rs',
   'r0/media/get_content_thumbnail.rs',
   'r0/membership/invite_user.rs',
-  'r0/membership/join_room_by_id_or_alias.rs',
   'r0/membership/leave_room.rs',
   'r0/message/create_message_event.rs',
   'r0/message/get_message_events.rs',
diff --git a/fractal-matrix-api/src/r0/membership.rs b/fractal-matrix-api/src/r0/membership.rs
index b20640bb..10073f1d 100644
--- a/fractal-matrix-api/src/r0/membership.rs
+++ b/fractal-matrix-api/src/r0/membership.rs
@@ -1,3 +1,2 @@
 pub mod invite_user;
-pub mod join_room_by_id_or_alias;
 pub mod leave_room;


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