@@ -110,26 +110,36 @@ pub struct Room {
110110/// calculate the room display name.
111111#[ derive( Clone , Debug , Default , Serialize , Deserialize ) ]
112112pub struct RoomSummary {
113- /// The heroes of the room, members that should be used for the room display
114- /// name.
113+ /// The heroes of the room, members that can be used as a fallback for the
114+ /// room's display name or avatar if these haven't been set .
115115 ///
116116 /// This was called `heroes` and contained raw `String`s of the `UserId`
117- /// before; changing the field's name helped with avoiding a migration.
117+ /// before. Following this it was called `heroes_user_ids` and a
118+ /// complimentary `heroes_names` existed too; changing the field's name
119+ /// helped with avoiding a migration.
118120 #[ serde( default , skip_serializing_if = "Vec::is_empty" ) ]
119- pub ( crate ) heroes_user_ids : Vec < OwnedUserId > ,
120- /// The heroes names, as returned by a server, if available.
121- #[ serde( default , skip_serializing_if = "Vec::is_empty" ) ]
122- pub ( crate ) heroes_names : Vec < String > ,
121+ pub ( crate ) room_heroes : Vec < RoomHero > ,
123122 /// The number of members that are considered to be joined to the room.
124123 pub ( crate ) joined_member_count : u64 ,
125124 /// The number of members that are considered to be invited to the room.
126125 pub ( crate ) invited_member_count : u64 ,
127126}
128127
128+ /// Information about a member considered to be a room hero.
129+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
130+ pub struct RoomHero {
131+ /// The user id of the hero.
132+ pub user_id : OwnedUserId ,
133+ /// The display name of the hero.
134+ pub display_name : Option < String > ,
135+ /// The avatar url of the hero.
136+ pub avatar_url : Option < OwnedMxcUri > ,
137+ }
138+
129139#[ cfg( test) ]
130140impl RoomSummary {
131- pub ( crate ) fn heroes ( & self ) -> & [ OwnedUserId ] {
132- & self . heroes_user_ids
141+ pub ( crate ) fn heroes ( & self ) -> & [ RoomHero ] {
142+ & self . room_heroes
133143 }
134144}
135145
@@ -517,21 +527,26 @@ impl Room {
517527 // From here, use some heroes to compute the room's name.
518528 let own_user_id = self . own_user_id ( ) . as_str ( ) ;
519529
520- let ( heroes, num_joined_guess) : ( Vec < String > , _ ) = if !summary. heroes_names . is_empty ( ) {
521- // Straightforward path: pass through the heroes names, don't give a guess of
522- // the number of members.
523- ( summary. heroes_names , None )
524- } else if !summary. heroes_user_ids . is_empty ( ) {
525- // Use the heroes, if available.
526- let heroes = summary. heroes_user_ids ;
527-
528- let mut names = Vec :: with_capacity ( heroes. len ( ) ) ;
529- for user_id in heroes {
530- if user_id == own_user_id {
530+ let ( heroes, num_joined_guess) : ( Vec < String > , _ ) = if !summary. room_heroes . is_empty ( ) {
531+ let mut names = Vec :: with_capacity ( summary. room_heroes . len ( ) ) ;
532+ for hero in & summary. room_heroes {
533+ if hero. user_id == own_user_id {
534+ continue ;
535+ }
536+ if let Some ( display_name) = & hero. display_name {
537+ names. push ( display_name. clone ( ) ) ;
531538 continue ;
532539 }
533- if let Some ( member) = self . get_member ( & user_id) . await ? {
534- names. push ( member. name ( ) . to_owned ( ) ) ;
540+ match self . get_member ( & hero. user_id ) . await {
541+ Ok ( Some ( member) ) => {
542+ names. push ( member. name ( ) . to_owned ( ) ) ;
543+ }
544+ Ok ( None ) => {
545+ warn ! ( "Ignoring hero, no member info for {}" , hero. user_id) ;
546+ }
547+ Err ( error) => {
548+ warn ! ( "Ignoring hero, error getting member: {}" , error) ;
549+ }
535550 }
536551 }
537552
@@ -694,6 +709,11 @@ impl Room {
694709 Ok ( members)
695710 }
696711
712+ /// Get the heroes for this room.
713+ pub fn heroes ( & self ) -> Vec < RoomHero > {
714+ self . inner . read ( ) . heroes ( ) . to_vec ( )
715+ }
716+
697717 /// Get the list of `RoomMember`s that are considered to be joined members
698718 /// of this room.
699719 #[ deprecated = "Use members with RoomMemberships::JOIN instead" ]
@@ -1128,7 +1148,16 @@ impl RoomInfo {
11281148
11291149 if !summary. is_empty ( ) {
11301150 if !summary. heroes . is_empty ( ) {
1131- self . summary . heroes_user_ids = summary. heroes . clone ( ) ;
1151+ self . summary . room_heroes = summary
1152+ . heroes
1153+ . iter ( )
1154+ . map ( |hero_id| RoomHero {
1155+ user_id : hero_id. to_owned ( ) ,
1156+ display_name : None ,
1157+ avatar_url : None ,
1158+ } )
1159+ . collect ( ) ;
1160+
11321161 changed = true ;
11331162 }
11341163
@@ -1158,11 +1187,15 @@ impl RoomInfo {
11581187 self . summary . invited_member_count = count;
11591188 }
11601189
1161- /// Updates the heroes user ids .
1190+ /// Updates the room heroes .
11621191 #[ cfg( feature = "experimental-sliding-sync" ) ]
1163- pub ( crate ) fn update_heroes ( & mut self , heroes : Vec < OwnedUserId > , names : Vec < String > ) {
1164- self . summary . heroes_user_ids = heroes;
1165- self . summary . heroes_names = names;
1192+ pub ( crate ) fn update_heroes ( & mut self , heroes : Vec < RoomHero > ) {
1193+ self . summary . room_heroes = heroes;
1194+ }
1195+
1196+ /// The heroes for this room.
1197+ pub fn heroes ( & self ) -> & [ RoomHero ] {
1198+ & self . summary . room_heroes
11661199 }
11671200
11681201 /// The number of active members (invited + joined) in the room.
@@ -1509,7 +1542,7 @@ mod tests {
15091542
15101543 #[ cfg( feature = "experimental-sliding-sync" ) ]
15111544 use super :: SyncInfo ;
1512- use super :: { compute_display_name_from_heroes, Room , RoomInfo , RoomState } ;
1545+ use super :: { compute_display_name_from_heroes, Room , RoomHero , RoomInfo , RoomState } ;
15131546 #[ cfg( any( feature = "experimental-sliding-sync" , feature = "e2e-encryption" ) ) ]
15141547 use crate :: latest_event:: LatestEvent ;
15151548 use crate :: {
@@ -1536,8 +1569,11 @@ mod tests {
15361569 notification_count : 2 ,
15371570 } ,
15381571 summary : RoomSummary {
1539- heroes_user_ids : vec ! [ owned_user_id!( "@somebody:example.org" ) ] ,
1540- heroes_names : vec ! [ ] ,
1572+ room_heroes : vec ! [ RoomHero {
1573+ user_id: owned_user_id!( "@somebody:example.org" ) ,
1574+ display_name: None ,
1575+ avatar_url: None ,
1576+ } ] ,
15411577 joined_member_count : 5 ,
15421578 invited_member_count : 0 ,
15431579 } ,
@@ -1562,7 +1598,11 @@ mod tests {
15621598 "notification_count" : 2 ,
15631599 } ,
15641600 "summary" : {
1565- "heroes_user_ids" : [ "@somebody:example.org" ] ,
1601+ "room_heroes" : [ {
1602+ "user_id" : "@somebody:example.org" ,
1603+ "display_name" : null,
1604+ "avatar_url" : null
1605+ } ] ,
15661606 "joined_member_count" : 5 ,
15671607 "invited_member_count" : 0 ,
15681608 } ,
@@ -1614,7 +1654,7 @@ mod tests {
16141654 // The following JSON should never change if we want to be able to read in old
16151655 // cached state
16161656
1617- use ruma:: owned_user_id;
1657+ use ruma:: { owned_mxc_uri , owned_user_id} ;
16181658 let info_json = json ! ( {
16191659 "room_id" : "!gda78o:server.tld" ,
16201660 "room_state" : "Invited" ,
@@ -1623,8 +1663,11 @@ mod tests {
16231663 "notification_count" : 2 ,
16241664 } ,
16251665 "summary" : {
1626- "heroes_user_ids" : [ "@somebody:example.org" ] ,
1627- "heroes_names" : [ "Somebody" ] ,
1666+ "room_heroes" : [ {
1667+ "user_id" : "@somebody:example.org" ,
1668+ "display_name" : "Somebody" ,
1669+ "avatar_url" : "mxc://example.org/abc"
1670+ } ] ,
16281671 "joined_member_count" : 5 ,
16291672 "invited_member_count" : 0 ,
16301673 } ,
@@ -1655,8 +1698,14 @@ mod tests {
16551698 assert_eq ! ( info. room_state, RoomState :: Invited ) ;
16561699 assert_eq ! ( info. notification_counts. highlight_count, 1 ) ;
16571700 assert_eq ! ( info. notification_counts. notification_count, 2 ) ;
1658- assert_eq ! ( info. summary. heroes_user_ids, vec![ owned_user_id!( "@somebody:example.org" ) ] ) ;
1659- assert_eq ! ( info. summary. heroes_names, vec![ "Somebody" . to_owned( ) ] ) ;
1701+ assert_eq ! (
1702+ info. summary. room_heroes,
1703+ vec![ RoomHero {
1704+ user_id: owned_user_id!( "@somebody:example.org" ) ,
1705+ display_name: Some ( "Somebody" . to_owned( ) ) ,
1706+ avatar_url: Some ( owned_mxc_uri!( "mxc://example.org/abc" ) ) ,
1707+ } ]
1708+ ) ;
16601709 assert_eq ! ( info. summary. joined_member_count, 5 ) ;
16611710 assert_eq ! ( info. summary. invited_member_count, 0 ) ;
16621711 assert ! ( info. members_synced) ;
0 commit comments