Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update user permissions when room power levels change, or when they accept an invitation to a room #305

Merged
merged 17 commits into from
Dec 31, 2024
52 changes: 40 additions & 12 deletions src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use matrix_sdk::{
use matrix_sdk_ui::{
room_list_service::{self, RoomListLoadingState},
sync_service::{self, SyncService},
timeline::{AnyOtherFullStateEventContent, EventTimelineItem, RepliedToInfo, TimelineDetails, TimelineItem, TimelineItemContent},
timeline::{AnyOtherFullStateEventContent, EventTimelineItem, RepliedToInfo, TimelineDetails, TimelineItem, TimelineItemContent, MembershipChange},
Timeline,
};
use robius_open::Uri;
Expand Down Expand Up @@ -812,7 +812,7 @@ async fn async_worker(
matrix_sdk::ruma::events::MessageLikeEventType::Message
)
.await
.unwrap_or(false);
.unwrap_or(true);

if let Err(e) = sender.send(TimelineUpdate::CanUserSendMessage(can_user_send_message)) {
error!("Failed to send the result of if user can send message: {e}")
Expand Down Expand Up @@ -1312,7 +1312,7 @@ async fn update_room(
if let Some(old_latest_event) = old_room.latest_event().await {
if new_latest_event.timestamp() > old_latest_event.timestamp() {
log!("Updating latest event for room {}", new_room_id);
room_avatar_changed = update_latest_event(new_room_id.clone(), &new_latest_event);
room_avatar_changed = update_avatar_for_latest_event(new_room_id.clone(), &new_latest_event);
}
}
}
Expand Down Expand Up @@ -1888,11 +1888,16 @@ async fn timeline_subscriber_handler(
// Update the latest event for this room.
if let Some(new_latest) = new_latest_event {
if latest_event.as_ref().map_or(true, |ev| ev.timestamp() < new_latest.timestamp()) {
let room_avatar_changed = update_latest_event(room_id.clone(), &new_latest);
latest_event = Some(new_latest);
// Handle avatar changes.
let room_avatar_changed = update_avatar_for_latest_event(room_id.clone(), &new_latest);
if room_avatar_changed {
spawn_fetch_room_avatar(room.clone());
}

// Handle if can send message.
update_if_can_send_message_for_latest_event(room_id.clone(), &new_latest);

latest_event = Some(new_latest);
}
}
}
Expand All @@ -1914,32 +1919,30 @@ async fn timeline_subscriber_handler(
///
/// Returns `true` if this latest event indicates that the room's avatar has changed
/// and should also be updated.
fn update_latest_event(
fn update_avatar_for_latest_event(
aaravlu marked this conversation as resolved.
Show resolved Hide resolved
room_id: OwnedRoomId,
event_tl_item: &EventTimelineItem,
) -> bool {
let mut room_avatar_changed = false;

let (timestamp, latest_message_text) = get_latest_event_details(event_tl_item, &room_id);

// Check for relevant state events: a changed room name or avatar.
// Check for relevant state events.
if let TimelineItemContent::OtherState(other) = event_tl_item.content() {
match other.content() {
AnyOtherFullStateEventContent::RoomName(FullStateEventContent::Original { content, .. }) => {
rooms_list::enqueue_rooms_list_update(RoomsListUpdate::UpdateRoomName {
rooms_list::enqueue_rooms_list_update(RoomsListUpdate::UpdateRoomName {
room_id: room_id.clone(),
new_room_name: content.name.clone(),
});
}
AnyOtherFullStateEventContent::RoomAvatar(_avatar_event) => {
room_avatar_changed = true;
}
AnyOtherFullStateEventContent::RoomPowerLevels(_power_level_event) => {
submit_async_request(MatrixRequest::CheckCanUserSendMessage { room_id: room_id.clone() })
room_avatar_changed = true;
}
_ => { }
}
}

enqueue_rooms_list_update(RoomsListUpdate::UpdateLatestEvent {
room_id,
timestamp,
Expand All @@ -1948,6 +1951,31 @@ fn update_latest_event(
room_avatar_changed
}

/// Updates the latest event for the given room.
///
/// This function handles and checks permission of sending message.
fn update_if_can_send_message_for_latest_event(
room_id: OwnedRoomId,
event_tl_item: &EventTimelineItem,
) {
match event_tl_item.content() {
TimelineItemContent::OtherState(other) => {
// Handle RoomPowerLevels event.
// Submit a `MatrixRequest` to check if the user can send when power levels change.
if let AnyOtherFullStateEventContent::RoomPowerLevels(_room_power_levels_event) = other.content() {
submit_async_request(MatrixRequest::CheckCanUserSendMessage { room_id: room_id.clone() })
aaravlu marked this conversation as resolved.
Show resolved Hide resolved
}
}

TimelineItemContent::MembershipChange(room_membership_change) => {
// Submit a `MatrixRequest` to check if the user can send when invited to a room.
if let Some(MembershipChange::InvitationAccepted) = room_membership_change.change() {
submit_async_request(MatrixRequest::CheckCanUserSendMessage { room_id: room_id.clone() })
}
}
_ => { }
}
}

/// Spawn a new async task to fetch the room's new avatar.
fn spawn_fetch_room_avatar(room: Room) {
Expand Down
Loading