Skip to content

Commit

Permalink
Process name changes that come back on the sync api
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Salinas committed Oct 16, 2024
1 parent a4bda1a commit f9b323d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use ruma::{
history_visibility::HistoryVisibility,
join_rules::JoinRule,
member::{MembershipState, RoomMemberEventContent},
name::RoomNameEventContent,
pinned_events::RoomPinnedEventsEventContent,
redaction::SyncRoomRedactionEvent,
tombstone::RoomTombstoneEventContent,
Expand Down Expand Up @@ -1498,6 +1499,14 @@ impl RoomInfo {
}
}

/// Update the room name.
pub fn update_name(&mut self, name: String) {
self.base_info.name = Some(MinimalStateEvent::Original(OriginalMinimalStateEvent {
content: RoomNameEventContent::new(name),
event_id: None,
}))
}

/// Get the name of this room.
pub fn name(&self) -> Option<&str> {
let name = &self.base_info.name.as_ref()?.as_original()?.content.name;
Expand Down
29 changes: 28 additions & 1 deletion crates/matrix-sdk-base/src/sliding_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,11 @@ fn process_room_properties(
JsOption::Undefined => {}
}

// If name updates come down in the payload, we need to update our cache.
if let Some(name) = &room_data.name {
room_info.update_name(name.clone());
}

// Sliding sync doesn't have a room summary, nevertheless it contains the joined
// and invited member counts, in addition to the heroes if it's been configured
// to return them (see the [`http::RequestRoomSubscription::include_heroes`]).
Expand Down Expand Up @@ -876,7 +881,7 @@ mod tests {
response.rooms.insert(
room_id.to_owned(),
assign!(http::response::Room::new(), {
unread_notifications: count.clone()
unread_notifications: count.clone(),
}),
);

Expand All @@ -894,6 +899,28 @@ mod tests {
assert_eq!(room.unread_notification_counts(), count.into());
}

#[async_test]
async fn test_name_set() {
let client = logged_in_base_client(None).await;

let mut response = http::Response::new("42".to_owned());
let room_id = room_id!("!room:example.org");
let new_name = "New name for a room".to_owned();

response.rooms.insert(
room_id.to_owned(),
assign!(http::response::Room::new(), {
name: Some(new_name.clone()),
}),
);

client.process_sliding_sync(&response, &(), true).await.expect("Failed to process sync");

// Check it's been updated in the store.
let room = client.get_room(room_id).expect("found room");
assert_eq!(room.name(), new_name.into());
}

#[async_test]
async fn test_can_process_empty_sliding_sync_response() {
let client = logged_in_base_client(None).await;
Expand Down

0 comments on commit f9b323d

Please sign in to comment.