Skip to content

Commit 618e472

Browse files
committed
feat!(base): reintroduce Room::display_name
`compute_display_name` is made private again, and used only within the base crate. A new public counterpart `Room::display_name` is introduced, which returns a cached value for, or computes (and fills in cache) the display name. This is simpler to use, and likely what most users expect anyways.
1 parent 5110aa6 commit 618e472

File tree

7 files changed

+49
-21
lines changed

7 files changed

+49
-21
lines changed

crates/matrix-sdk-base/CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ All notable changes to this project will be documented in this file.
66

77
## [Unreleased] - ReleaseDate
88

9+
### Breaking changes
10+
11+
- Replaced `Room::compute_display_name` with the reintroduced `Room::display_name()`. The new
12+
method computes a display name, or return a cached value from the previous successful computation.
13+
If you need a sync variant, consider using `Room::cached_display_name()`.
14+
15+
### Features
16+
17+
### Bug Fixes
18+
919
## [0.9.0] - 2024-12-18
1020

1121
### Features

crates/matrix-sdk-base/src/rooms/normal.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -623,18 +623,40 @@ impl Room {
623623
self.inner.read().active_room_call_participants()
624624
}
625625

626-
/// Calculate a room's display name, taking into account its name, aliases
627-
/// and members.
626+
/// Calculate a room's display name, or return the cached value, taking into
627+
/// account its name, aliases and members.
628+
///
629+
/// The display name is calculated according to [this algorithm][spec].
630+
///
631+
/// While the underlying computation can be slow, the result is cached and
632+
/// returned on the following calls. The cache is also filled on every
633+
/// successful sync, since a sync may cause a change in the display
634+
/// name.
635+
///
636+
/// If you need a variant that's sync (but with the drawback that it returns
637+
/// an `Option`), consider using [`Room::cached_display_name`].
638+
///
639+
/// [spec]: <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room>
640+
pub async fn display_name(&self) -> StoreResult<RoomDisplayName> {
641+
if let Some(name) = self.cached_display_name() {
642+
Ok(name)
643+
} else {
644+
self.compute_display_name().await
645+
}
646+
}
647+
648+
/// Force recalculating a room's display name, taking into account its name,
649+
/// aliases and members.
628650
///
629651
/// The display name is calculated according to [this algorithm][spec].
630652
///
631653
/// ⚠ This may be slowish to compute. As such, the result is cached and can
632-
/// be retrieved via [`Room::cached_display_name`], which should be
633-
/// preferred in general. Indeed, the cached value is automatically
634-
/// recomputed on every sync.
654+
/// be retrieved via [`Room::cached_display_name`] (sync, returns an option)
655+
/// or [`Room::display_name`] (async, always returns a value), which should
656+
/// be preferred in general.
635657
///
636658
/// [spec]: <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room>
637-
pub async fn compute_display_name(&self) -> StoreResult<RoomDisplayName> {
659+
pub(crate) async fn compute_display_name(&self) -> StoreResult<RoomDisplayName> {
638660
enum DisplayNameOrSummary {
639661
Summary(RoomSummary),
640662
DisplayName(RoomDisplayName),
@@ -871,8 +893,7 @@ impl Room {
871893

872894
/// Returns the cached computed display name, if available.
873895
///
874-
/// This cache is refilled every time we call
875-
/// [`Self::compute_display_name`].
896+
/// This cache is refilled every time we call [`Self::display_name`].
876897
pub fn cached_display_name(&self) -> Option<RoomDisplayName> {
877898
self.inner.read().cached_display_name.clone()
878899
}

crates/matrix-sdk-ui/src/notification_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ impl NotificationItem {
740740
sender_display_name,
741741
sender_avatar_url,
742742
is_sender_name_ambiguous,
743-
room_computed_display_name: room.compute_display_name().await?.to_string(),
743+
room_computed_display_name: room.display_name().await?.to_string(),
744744
room_avatar_url: room.avatar_url().map(|s| s.to_string()),
745745
room_canonical_alias: room.canonical_alias().map(|c| c.to_string()),
746746
is_direct_message_room: room.is_direct().await?,

crates/matrix-sdk/src/room_preview.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl RoomPreview {
130130
pub(crate) async fn from_joined(room: &Room) -> Self {
131131
let is_direct = room.is_direct().await.ok();
132132

133-
let display_name = room.compute_display_name().await.ok().map(|name| name.to_string());
133+
let display_name = room.display_name().await.ok().map(|name| name.to_string());
134134

135135
Self::from_room_info(
136136
room.clone_info(),

crates/matrix-sdk/tests/integration/room/common.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async fn test_calculate_room_names_from_summary() {
6565

6666
assert_eq!(
6767
RoomDisplayName::Calculated("example2".to_owned()),
68-
room.compute_display_name().await.unwrap()
68+
room.display_name().await.unwrap()
6969
);
7070
}
7171

@@ -83,10 +83,7 @@ async fn test_room_names() {
8383
assert_eq!(client.rooms().len(), 1);
8484
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();
8585

86-
assert_eq!(
87-
RoomDisplayName::Aliased("tutorial".to_owned()),
88-
room.compute_display_name().await.unwrap()
89-
);
86+
assert_eq!(RoomDisplayName::Aliased("tutorial".to_owned()), room.display_name().await.unwrap());
9087

9188
// Room with a name.
9289
mock_sync(&server, &*test_json::INVITE_SYNC, None).await;
@@ -99,7 +96,7 @@ async fn test_room_names() {
9996

10097
assert_eq!(
10198
RoomDisplayName::Named("My Room Name".to_owned()),
102-
invited_room.compute_display_name().await.unwrap()
99+
invited_room.display_name().await.unwrap()
103100
);
104101

105102
let mut sync_builder = SyncResponseBuilder::new();
@@ -137,7 +134,7 @@ async fn test_room_names() {
137134
RoomDisplayName::Calculated(
138135
"user_0, user_1, user_10, user_11, user_12, and 10 others".to_owned()
139136
),
140-
room.compute_display_name().await.unwrap()
137+
room.display_name().await.unwrap()
141138
);
142139

143140
// Room with joined and invited members.
@@ -185,7 +182,7 @@ async fn test_room_names() {
185182

186183
assert_eq!(
187184
RoomDisplayName::Calculated("Bob, example1".to_owned()),
188-
room.compute_display_name().await.unwrap()
185+
room.display_name().await.unwrap()
189186
);
190187

191188
// Room with only left members.
@@ -205,7 +202,7 @@ async fn test_room_names() {
205202

206203
assert_eq!(
207204
RoomDisplayName::EmptyWas("user_0, user_1, user_2".to_owned()),
208-
room.compute_display_name().await.unwrap()
205+
room.display_name().await.unwrap()
209206
);
210207
}
211208

examples/oidc_cli/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
935935
}
936936
let MessageType::Text(text_content) = &event.content.msgtype else { return };
937937

938-
let room_name = match room.compute_display_name().await {
938+
let room_name = match room.display_name().await {
939939
Ok(room_name) => room_name.to_string(),
940940
Err(error) => {
941941
println!("Error getting room display name: {error}");

examples/persist_session/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
297297
}
298298
let MessageType::Text(text_content) = &event.content.msgtype else { return };
299299

300-
let room_name = match room.compute_display_name().await {
300+
let room_name = match room.display_name().await {
301301
Ok(room_name) => room_name.to_string(),
302302
Err(error) => {
303303
println!("Error getting room display name: {error}");

0 commit comments

Comments
 (0)