From 2781d2d91c2d860d3e502a14d3312961495049f5 Mon Sep 17 00:00:00 2001 From: Demolemon11 Date: Wed, 20 Nov 2024 20:06:58 +0800 Subject: [PATCH] Give notice if user donnot have permission to post --- src/home/room_screen.rs | 18 +++++++++--------- src/sliding_sync.rs | 12 +++++++----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/home/room_screen.rs b/src/home/room_screen.rs index 840cfff5..92159b39 100644 --- a/src/home/room_screen.rs +++ b/src/home/room_screen.rs @@ -1434,8 +1434,6 @@ impl RoomScreen { /// /// Redraws this RoomScreen view if any updates were applied. fn process_timeline_updates(&mut self, cx: &mut Cx, portal_list: &PortalListRef) { - // Check if the user has permission to send message in this room. - self.check_user_permission(); let top_space = self.view(id!(top_space)); let bottom_input = self.view(id!(bottom_input)); let no_send_permisson_notice = self.view(id!(no_send_permisson_notice)); @@ -1582,14 +1580,14 @@ impl RoomScreen { typing_users = users; } TimelineUpdate::SendPermission(can_send) => { - if can_send == -1 { - bottom_input.set_visible(false); - no_send_permisson_notice.set_visible(true) - } - else { + if can_send { bottom_input.set_visible(true); no_send_permisson_notice.set_visible(false) } + else { + bottom_input.set_visible(false); + no_send_permisson_notice.set_visible(true) + } } } @@ -1705,6 +1703,8 @@ impl RoomScreen { /// Invoke this when this timeline is being shown, /// e.g., when the user navigates to this timeline. fn show_timeline(&mut self, cx: &mut Cx) { + // Check if the user has permission to send message in this room. + self.check_user_send_permission(); let room_id = self.room_id.clone() .expect("BUG: Timeline::show_timeline(): no room_id was set."); // just an optional sanity check @@ -1963,7 +1963,7 @@ impl RoomScreen { } tl.last_scrolled_index = first_index; } - fn check_user_permission(&self) { + fn check_user_send_permission(&self) { if let Some(room_id) = self.room_id.clone() { submit_async_request(MatrixRequest::CheckUserSendPermission { room_id }) } @@ -2031,7 +2031,7 @@ pub enum TimelineUpdate { users: Vec, }, /// if the user has permission to send messages in this room - SendPermission(i64), + SendPermission(bool), } /// The global set of all timeline states, one entry per room. diff --git a/src/sliding_sync.rs b/src/sliding_sync.rs index be58dc21..6ea76f02 100644 --- a/src/sliding_sync.rs +++ b/src/sliding_sync.rs @@ -716,12 +716,11 @@ async fn async_worker( let _check_user_send_permission_task = Handle::current().spawn(async move { let room = timeline.room(); - let can_send = room.get_user_power_level(user_id).await.unwrap_or(0); - - if let Err(_) = sender.send(TimelineUpdate::SendPermission(can_send)) { - error!("Failed to send the result of user send permission") + if let Ok(can_send) = room.can_user_send_message(user_id, matrix_sdk::ruma::events::MessageLikeEventType::Message).await { + if let Err(_) = sender.send(TimelineUpdate::SendPermission(can_send)) { + error!("Failed check user send permission") + } } - SignalToUI::set_ui_signal(); }); } } @@ -1629,6 +1628,9 @@ fn update_latest_event( AnyOtherFullStateEventContent::RoomAvatar(_avatar_event) => { room_avatar_changed = true; } + AnyOtherFullStateEventContent::RoomPowerLevels(_user_power_level_event) => { + submit_async_request(MatrixRequest::CheckUserSendPermission { room_id: room_id.clone() }); + } _ => { } } _ => { }