@@ -66,7 +66,7 @@ pub use state::{RoomState, RoomStateFilter};
6666pub ( crate ) use tags:: RoomNotableTags ;
6767use tokio:: sync:: broadcast;
6868pub use tombstone:: { PredecessorRoom , SuccessorRoom } ;
69- use tracing:: { info, instrument, warn} ;
69+ use tracing:: { info, instrument, trace , warn} ;
7070
7171use crate :: {
7272 DmRoomDefinition , Error ,
@@ -304,9 +304,9 @@ impl Room {
304304 }
305305 }
306306
307- /// Checks if the current room is a DM based on the rules from the
308- /// [`DmRoomDefinition`].
309- pub async fn is_dm ( & self , dm_room_definition : & DmRoomDefinition ) -> StoreResult < bool > {
307+ /// Computes if the current room is a DM based on the rules from the
308+ /// [`DmRoomDefinition`], updating the active service members .
309+ pub async fn compute_is_dm ( & self , dm_room_definition : & DmRoomDefinition ) -> StoreResult < bool > {
310310 let is_direct = self . is_direct ( ) . await ?;
311311
312312 match * dm_room_definition {
@@ -316,7 +316,7 @@ impl Room {
316316 return Ok ( false ) ;
317317 }
318318 let active_service_member_count =
319- self . active_service_members ( ) . await ?. unwrap_or_default ( ) . len ( ) as u64 ;
319+ self . update_active_service_members ( ) . await ?. unwrap_or_default ( ) . len ( ) as u64 ;
320320 let has_at_most_two_members =
321321 self . active_members_count ( ) . saturating_sub ( active_service_member_count) <= 2 ;
322322 Ok ( has_at_most_two_members)
@@ -534,7 +534,7 @@ impl Room {
534534 /// Returns the list of service members that are either in a joined or
535535 /// invited state in this room, checking the service member list against the
536536 /// locally available room members.
537- pub async fn active_service_members ( & self ) -> StoreResult < Option < Vec < RoomMember > > > {
537+ pub async fn update_active_service_members ( & self ) -> StoreResult < Option < Vec < RoomMember > > > {
538538 if let Some ( service_members) = self . service_members ( ) {
539539 let mut found = Vec :: new ( ) ;
540540 for user_id in service_members {
@@ -553,11 +553,44 @@ impl Room {
553553 }
554554 }
555555
556+ trace ! (
557+ "Updating active service members ({}) in room {:?}" ,
558+ found. len( ) ,
559+ self . room_id( )
560+ ) ;
561+
562+ let new_active_service_member_count = found. len ( ) as u64 ;
563+ let current_active_service_member_count =
564+ self . info . read ( ) . summary . active_service_members . unwrap_or_default ( ) ;
565+ if new_active_service_member_count != current_active_service_member_count {
566+ let mut new_room_info = self . clone_info ( ) ;
567+ new_room_info
568+ . update_active_service_member_count ( Some ( new_active_service_member_count) ) ;
569+ self . set_room_info (
570+ new_room_info,
571+ RoomInfoNotableUpdateReasons :: ACTIVE_SERVICE_MEMBERS ,
572+ ) ;
573+ }
574+
556575 Ok ( Some ( found) )
557576 } else {
577+ if self . info . read ( ) . summary . active_service_members . is_some ( ) {
578+ let mut new_room_info = self . clone_info ( ) ;
579+ new_room_info. update_active_service_member_count ( None ) ;
580+ self . set_room_info (
581+ new_room_info,
582+ RoomInfoNotableUpdateReasons :: ACTIVE_SERVICE_MEMBERS ,
583+ ) ;
584+ }
558585 Ok ( None )
559586 }
560587 }
588+
589+ /// Returns a cached value containing the active (joined/invited) service
590+ /// member count, if known.
591+ pub fn active_service_members_count ( & self ) -> Option < u64 > {
592+ self . info . read ( ) . summary . active_service_members
593+ }
561594}
562595
563596// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823.
0 commit comments