From f9b323d4c4de47243b28ae42f0812763fcce736b Mon Sep 17 00:00:00 2001 From: Daniel Salinas Date: Wed, 16 Oct 2024 18:11:45 -0400 Subject: [PATCH] Process name changes that come back on the sync api --- crates/matrix-sdk-base/src/rooms/normal.rs | 9 ++++++ .../matrix-sdk-base/src/sliding_sync/mod.rs | 29 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk-base/src/rooms/normal.rs b/crates/matrix-sdk-base/src/rooms/normal.rs index 9f790ba33f4..d798d295704 100644 --- a/crates/matrix-sdk-base/src/rooms/normal.rs +++ b/crates/matrix-sdk-base/src/rooms/normal.rs @@ -42,6 +42,7 @@ use ruma::{ history_visibility::HistoryVisibility, join_rules::JoinRule, member::{MembershipState, RoomMemberEventContent}, + name::RoomNameEventContent, pinned_events::RoomPinnedEventsEventContent, redaction::SyncRoomRedactionEvent, tombstone::RoomTombstoneEventContent, @@ -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; diff --git a/crates/matrix-sdk-base/src/sliding_sync/mod.rs b/crates/matrix-sdk-base/src/sliding_sync/mod.rs index 334336e3ba7..fa1b2f1ebf0 100644 --- a/crates/matrix-sdk-base/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk-base/src/sliding_sync/mod.rs @@ -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`]). @@ -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(), }), ); @@ -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;