[fractal] Leave rooms through matrix-sdk
- From: Alexandre Franke <afranke src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Leave rooms through matrix-sdk
- Date: Tue, 1 Dec 2020 20:21:59 +0000 (UTC)
commit 4697673f6de90bebc6eb972a3c37ff2291c95d29
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Sat Aug 15 04:39:58 2020 +0200
Leave rooms through matrix-sdk
fractal-gtk/src/appop/invite.rs | 48 +++++++++-------------
fractal-gtk/src/appop/message.rs | 2 +-
fractal-gtk/src/appop/room.rs | 33 +++++++--------
fractal-gtk/src/backend/room.rs | 20 ++++-----
fractal-gtk/src/widgets/roomlist.rs | 20 ++++-----
fractal-matrix-api/src/meson.build | 1 -
fractal-matrix-api/src/r0/membership.rs | 1 -
fractal-matrix-api/src/r0/membership/leave_room.rs | 20 ---------
8 files changed, 53 insertions(+), 92 deletions(-)
---
diff --git a/fractal-gtk/src/appop/invite.rs b/fractal-gtk/src/appop/invite.rs
index faf45948..8942effd 100644
--- a/fractal-gtk/src/appop/invite.rs
+++ b/fractal-gtk/src/appop/invite.rs
@@ -217,43 +217,35 @@ impl AppOp {
dialog.resize(300, 200);
}
- pub fn remove_inv(&mut self, room_id: RoomId) {
- self.rooms.remove(&room_id);
+ pub fn remove_inv(&mut self, room_id: &RoomId) {
+ self.rooms.remove(room_id);
self.roomlist.remove_room(room_id);
}
pub fn accept_inv(&mut self, accept: bool) {
- 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();
+ let room_id = unwrap_or_unit_return!(self.invitation_roomid.take().clone());
+ let session_client =
+ unwrap_or_unit_return!(self.login_data.as_ref().map(|ld| ld.session_client.clone()));
+ self.remove_inv(&room_id);
+ RUNTIME.spawn(async move {
if accept {
- 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));
- APPOP!(reload_rooms);
- }
- Err(err) => {
- err.handle_error();
- }
+ match room::join_room(session_client, &room_id.into()).await {
+ Ok(jtr) => {
+ let jtr = Some(jtr);
+ APPOP!(set_join_to_room, (jtr));
+ APPOP!(reload_rooms);
}
- });
- } else {
- thread::spawn(move || {
- let query = room::leave_room(
- session_client.homeserver().clone(),
- login_data.access_token,
- room_id,
- );
- if let Err(err) = query {
+ Err(err) => {
err.handle_error();
}
- });
+ }
+ } else {
+ let query = room::leave_room(session_client, &room_id).await;
+ if let Err(err) = query {
+ err.handle_error();
+ }
}
- self.remove_inv(rid);
- }
+ });
}
/* FIXME: move to a widget */
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index f99ea05c..e757dcf6 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -423,7 +423,7 @@ impl AppOp {
}
}
- self.roomlist.moveup(msg.room.clone());
+ self.roomlist.moveup(&msg.room);
self.roomlist.set_bold(msg.room.clone(), true);
}
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index fcc07a2d..534a2758 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -41,7 +41,7 @@ impl AppOp {
pub fn remove_room(&mut self, id: RoomId) {
self.rooms.remove(&id);
self.unsent_messages.remove(&id);
- self.roomlist.remove_room(id);
+ self.roomlist.remove_room(&id);
}
pub fn set_rooms(&mut self, rooms: Vec<Room>, clear_room_list: bool) {
@@ -111,7 +111,7 @@ impl AppOp {
roomlist.push(room.clone());
} else {
self.roomlist.add_room(room.clone());
- self.roomlist.moveup(room.id.clone());
+ self.roomlist.moveup(&room.id);
}
self.rooms.insert(room.id.clone(), room);
}
@@ -330,25 +330,22 @@ impl AppOp {
// FIXME: This should be a special case in a generic
// function that leaves any room in any state.
pub fn really_leave_active_room(&mut self) {
- let login_data = unwrap_or_unit_return!(self.login_data.clone());
- let r = unwrap_or_unit_return!(self.active_room.clone());
- let room_id = r.clone();
- thread::spawn(move || {
- let query = room::leave_room(
- login_data.session_client.homeserver().clone(),
- login_data.access_token,
- room_id,
- );
- if let Err(err) = query {
- err.handle_error();
- }
- });
- self.rooms.remove(&r);
+ let room_id = unwrap_or_unit_return!(self.active_room.clone());
+ let session_client =
+ unwrap_or_unit_return!(self.login_data.as_ref().map(|ld| ld.session_client.clone()));
+
+ self.rooms.remove(&room_id);
self.active_room = None;
self.clear_tmp_msgs();
self.set_state(AppState::NoRoom);
+ self.roomlist.remove_room(&room_id);
- self.roomlist.remove_room(r);
+ RUNTIME.spawn(async move {
+ let query = room::leave_room(session_client, &room_id).await;
+ if let Err(err) = query {
+ err.handle_error();
+ }
+ });
}
pub fn leave_active_room(&self) {
@@ -580,7 +577,7 @@ impl AppOp {
}
self.roomlist.add_room(r.clone());
- self.roomlist.moveup(r.id.clone());
+ self.roomlist.moveup(&r.id);
self.set_active_room_by_id(r.id);
}
diff --git a/fractal-gtk/src/backend/room.rs b/fractal-gtk/src/backend/room.rs
index a9e09d5f..79de0e1b 100644
--- a/fractal-gtk/src/backend/room.rs
+++ b/fractal-gtk/src/backend/room.rs
@@ -40,8 +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::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;
use fractal_api::r0::message::create_message_event::Parameters as CreateMessageEventParameters;
use fractal_api::r0::message::create_message_event::Response as CreateMessageEventResponse;
@@ -425,25 +423,21 @@ pub async fn join_room(
}
#[derive(Debug)]
-pub struct LeaveRoomError(ReqwestError);
+pub struct LeaveRoomError(MatrixError);
-impl From<ReqwestError> for LeaveRoomError {
- fn from(err: ReqwestError) -> Self {
+impl From<MatrixError> for LeaveRoomError {
+ fn from(err: MatrixError) -> Self {
Self(err)
}
}
impl HandleError for LeaveRoomError {}
-pub fn leave_room(
- base: Url,
- access_token: AccessToken,
- room_id: RoomId,
+pub async fn leave_room(
+ session_client: MatrixClient,
+ room_id: &RoomId,
) -> Result<(), LeaveRoomError> {
- let params = LeaveRoomParameters { access_token };
-
- let request = leave_room_req(base, &room_id, ¶ms)?;
- HTTP_CLIENT.get_client().execute(request)?;
+ session_client.leave_room(room_id).await?;
Ok(())
}
diff --git a/fractal-gtk/src/widgets/roomlist.rs b/fractal-gtk/src/widgets/roomlist.rs
index 2408f905..f8509cb0 100644
--- a/fractal-gtk/src/widgets/roomlist.rs
+++ b/fractal-gtk/src/widgets/roomlist.rs
@@ -201,10 +201,10 @@ impl RoomListGroup {
});
}
- pub fn remove_room(&mut self, room_id: RoomId) -> Option<RoomUpdated> {
- self.rooms.remove(&room_id);
+ pub fn remove_room(&mut self, room_id: &RoomId) -> Option<RoomUpdated> {
+ self.rooms.remove(room_id);
let mut rv = self.roomvec.lock().unwrap();
- if let Some(idx) = rv.iter().position(|x| x.room.id == room_id) {
+ if let Some(idx) = rv.iter().position(|x| &x.room.id == room_id) {
if let Some(row) = self.list.get_row_at_index(idx as i32) {
self.list.remove(&row);
}
@@ -377,10 +377,10 @@ impl RoomListGroup {
}
}
- pub fn moveup(&mut self, room_id: RoomId) {
+ pub fn moveup(&mut self, room_id: &RoomId) {
let s = self.get_selected();
- self.edit_room(&room_id, move |rv| {
+ self.edit_room(room_id, move |rv| {
rv.up();
});
if let Some(r) = self.remove_room(room_id) {
@@ -618,7 +618,7 @@ impl RoomList {
let f = self.fav.clone();
let cb = acb.clone();
self.connect_drop(favw, move |room_id| {
- if let Some(room) = r.get().remove_room(room_id) {
+ if let Some(room) = r.get().remove_room(&room_id) {
cb(room.room.clone(), true);
f.get().add_room_up(room);
}
@@ -628,7 +628,7 @@ impl RoomList {
let r = self.rooms.clone();
let f = self.fav.clone();
self.connect_drop(rw, move |roomid| {
- if let Some(room) = f.get().remove_room(roomid) {
+ if let Some(room) = f.get().remove_room(&roomid) {
acb(room.room.clone(), false);
r.get().add_room_up(room);
}
@@ -649,8 +649,8 @@ impl RoomList {
run_in_group!(self, &room_id, set_room_notifications, room_id, n, h);
}
- pub fn remove_room(&mut self, room_id: RoomId) -> Option<RoomUpdated> {
- let ret = run_in_group!(self, &room_id, remove_room, room_id);
+ pub fn remove_room(&mut self, room_id: &RoomId) -> Option<RoomUpdated> {
+ let ret = run_in_group!(self, room_id, remove_room, room_id);
self.show_and_hide();
ret
}
@@ -676,7 +676,7 @@ impl RoomList {
run_in_group!(self, &room_id, rename_room, room_id, newname);
}
- pub fn moveup(&mut self, room_id: RoomId) {
+ pub fn moveup(&mut self, room_id: &RoomId) {
run_in_group!(self, &room_id, moveup, room_id);
}
diff --git a/fractal-matrix-api/src/meson.build b/fractal-matrix-api/src/meson.build
index 5aec49a9..ddfb163d 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/leave_room.rs',
'r0/message/create_message_event.rs',
'r0/message/get_message_events.rs',
'r0/profile/get_display_name.rs',
diff --git a/fractal-matrix-api/src/r0/membership.rs b/fractal-matrix-api/src/r0/membership.rs
index 10073f1d..4a77a816 100644
--- a/fractal-matrix-api/src/r0/membership.rs
+++ b/fractal-matrix-api/src/r0/membership.rs
@@ -1,2 +1 @@
pub mod invite_user;
-pub mod leave_room;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]