@@ -623,18 +623,40 @@ impl Room {
623
623
self . inner . read ( ) . active_room_call_participants ( )
624
624
}
625
625
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.
628
650
///
629
651
/// The display name is calculated according to [this algorithm][spec].
630
652
///
631
653
/// ⚠ 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 .
635
657
///
636
658
/// [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 > {
638
660
enum DisplayNameOrSummary {
639
661
Summary ( RoomSummary ) ,
640
662
DisplayName ( RoomDisplayName ) ,
@@ -871,8 +893,7 @@ impl Room {
871
893
872
894
/// Returns the cached computed display name, if available.
873
895
///
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`].
876
897
pub fn cached_display_name ( & self ) -> Option < RoomDisplayName > {
877
898
self . inner . read ( ) . cached_display_name . clone ( )
878
899
}
0 commit comments