Skip to content

Commit

Permalink
Fix send permission
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravlu committed Nov 15, 2024
1 parent 7929b7f commit 4b1b354
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
56 changes: 28 additions & 28 deletions src/home/room_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,30 +613,7 @@ live_design! {

// A jump to bottom button (with an unread message badge) that is shown
// when the timeline is not at the bottom.
<View> {
flow: Down

jump_to_bottom = <JumpToBottomButton> { }

no_send_permisson_notice = <View> {
visible: false
show_bg: true
draw_bg: {
color: #xFF000030
}
padding: {left: 90}
align: {y: 0.5}
width: Fill, height: 28,

<Label> {
draw_text: {
color: #111111
text_style: <THEME_FONT_ITALIC>{font_size: 13.5}
}
text: "You have no permission to send messages in this room"
}
}
}
jump_to_bottom = <JumpToBottomButton> { }
}

LocationPreview = {{LocationPreview}} {
Expand Down Expand Up @@ -932,6 +909,24 @@ live_design! {
icon_walk: {width: 18.0, height: Fit},
}
}
no_send_permisson_notice = <View> {
visible: false
show_bg: true
draw_bg: {
color: #EFEFEF
}
padding: {left: 75}
align: {y: 0.5}
width: Fill, height: 31,

<Label> {
draw_text: {
color: #111111
text_style: <THEME_FONT_ITALIC>{font_size: 12.2}
}
text: "You don't have permission to post to this room"
}
}
}

// The top space should be displayed on top of the timeline
Expand Down Expand Up @@ -1173,8 +1168,7 @@ impl Widget for RoomScreen {
}
}
}
// Check if the user has permission to send message in this room.
self.check_self_has_send_permission();

// Set visibility of loading message banner based of pagination logic
self.send_pagination_request_based_on_scroll_pos(cx, actions, &portal_list);
// Handle sending any read receipts for the current logged-in user.
Expand Down Expand Up @@ -1590,6 +1584,10 @@ impl RoomScreen {
bottom_input.set_visible(false);
no_send_permisson_notice.set_visible(true)
}
else {
bottom_input.set_visible(true);
no_send_permisson_notice.set_visible(false)
}
}

}
Expand Down Expand Up @@ -1764,6 +1762,8 @@ impl RoomScreen {
submit_async_request(MatrixRequest::FetchRoomMembers { room_id });
}

// Check if the user has permission to send message in this room.
self.check_user_permission();
// Now, restore the visual state of this timeline from its previously-saved state.
self.restore_state(cx, &mut tl_state);

Expand Down Expand Up @@ -1963,9 +1963,9 @@ impl RoomScreen {
}
tl.last_scrolled_index = first_index;
}
fn check_self_has_send_permission(&self) {
fn check_user_permission(&self) {
if let Some(room_id) = self.room_id.clone() {
submit_async_request(MatrixRequest::CheckIfSelfCanSendMessage { room_id })
submit_async_request(MatrixRequest::CheckUserSendPermission { room_id })
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub enum MatrixRequest {
room_id: OwnedRoomId,
event_id: OwnedEventId,
},
CheckIfSelfCanSendMessage{
CheckUserSendPermission{
room_id: OwnedRoomId,
}
}
Expand Down Expand Up @@ -700,22 +700,28 @@ async fn async_worker(
});
}

MatrixRequest::CheckIfSelfCanSendMessage { room_id } => {
let sender = {
MatrixRequest::CheckUserSendPermission { room_id } => {
let (timeline, sender) = {
let all_room_info = ALL_ROOM_INFO.lock().unwrap();
let Some(room_info) = all_room_info.get(&room_id) else {
log!("BUG: room info not found for fetch members request {room_id}");
continue;
};

room_info.timeline_update_sender.clone()
(room_info.timeline.clone(), room_info.timeline_update_sender.clone())
};

let Some(client) = CLIENT.get() else { continue };
let user_id = client.user_id().unwrap();
let room = client.get_room(&room_id).unwrap();
let can_send = room.can_user_send_message(user_id, matrix_sdk::ruma::events::MessageLikeEventType::Message).await.unwrap_or(true);
sender.send(TimelineUpdate::SendPermission(can_send)).unwrap();
let Some(user_id) = client.user_id() else { continue };

let _send_frr_task = Handle::current().spawn(async move {
let room = timeline.room();
let can_send = room.can_user_send_message(user_id, matrix_sdk::ruma::events::MessageLikeEventType::Message).await.unwrap_or(false);

if let Err(_) = sender.send(TimelineUpdate::SendPermission(can_send)) {
error!("Failed to send the result of user send permission")
}
});
}
}
}
Expand Down

0 comments on commit 4b1b354

Please sign in to comment.