Skip to content

Commit 45f313e

Browse files
committed
WIP
1 parent 4e8ceaa commit 45f313e

15 files changed

Lines changed: 209 additions & 195 deletions

File tree

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,7 +2223,7 @@ async fn notification_handler(
22232223
.map(ToString::to_string)
22242224
.collect(),
22252225
active_service_members_count: room
2226-
.active_service_members()
2226+
.update_active_service_members()
22272227
.await
22282228
.ok()
22292229
.flatten()
@@ -2232,6 +2232,7 @@ async fn notification_handler(
22322232
is_encrypted: Some(room.encryption_state().is_encrypted()),
22332233
is_direct,
22342234
is_space: room.is_space(),
2235+
is_dm: room.compute_is_dm().await.ok().unwrap_or_default(),
22352236
};
22362237

22372238
listener.on_notification(

bindings/matrix-sdk-ffi/src/notification.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct NotificationRoomInfo {
5454
pub is_encrypted: Option<bool>,
5555
pub is_direct: bool,
5656
pub is_space: bool,
57+
pub is_dm: bool,
5758
}
5859

5960
#[derive(uniffi::Record)]
@@ -113,6 +114,7 @@ impl NotificationItem {
113114
is_encrypted: item.is_room_encrypted,
114115
is_direct: item.is_direct_message_room,
115116
is_space: item.is_space,
117+
is_dm: item.is_dm,
116118
},
117119
is_noisy: item.is_noisy,
118120
has_mention: item.has_mention,

bindings/matrix-sdk-ffi/src/room/room_info.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ impl RoomInfo {
147147
.ok()
148148
.map(|p| RoomPowerLevels::new(p, room.own_user_id().to_owned()));
149149

150-
let active_service_members_count =
151-
room.active_service_members().await?.unwrap_or_default().len() as u64;
152-
153150
Ok(Self {
154151
id: room.room_id().to_string(),
155152
encryption_state: room.encryption_state(),
@@ -161,7 +158,7 @@ impl RoomInfo {
161158
topic: room.topic(),
162159
avatar_url: room.avatar_url().map(Into::into),
163160
is_direct: room.is_direct().await?,
164-
is_dm: room.is_dm().await?,
161+
is_dm: room.compute_is_dm().await?,
165162
is_public: room.is_public(),
166163
is_space: room.is_space(),
167164
successor_room: room.successor_room().map(Into::into),
@@ -186,7 +183,7 @@ impl RoomInfo {
186183
active_members_count: room.active_members_count(),
187184
invited_members_count: room.invited_members_count(),
188185
joined_members_count: room.joined_members_count(),
189-
active_service_members_count,
186+
active_service_members_count: room.active_service_members_count().unwrap_or_default(),
190187
service_members: room
191188
.service_members()
192189
.iter()

bindings/matrix-sdk-ffi/src/spaces.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ pub struct SpaceRoom {
359359
pub heroes: Option<Vec<RoomHero>>,
360360
/// The via parameters of the room.
361361
pub via: Vec<String>,
362+
/// Whether this room is a DM, if known.
363+
/// Note this value can be calculated following some assumptions and is not
364+
/// guaranteed to be accurate.
365+
pub is_dm: Option<bool>,
362366
}
363367

364368
impl From<UISpaceRoom> for SpaceRoom {
@@ -380,6 +384,7 @@ impl From<UISpaceRoom> for SpaceRoom {
380384
state: room.state.map(Into::into),
381385
heroes: room.heroes.map(|heroes| heroes.into_iter().map(Into::into).collect()),
382386
via: room.via.into_iter().map(Into::into).collect(),
387+
is_dm: room.is_dm,
383388
}
384389
}
385390
}

crates/matrix-sdk-base/src/room/display_name.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ pub(crate) struct RoomSummary {
378378
pub joined_member_count: u64,
379379
/// The number of members that are considered to be invited to the room.
380380
pub invited_member_count: u64,
381+
/// The number of active (joined/invited) service members in the room, if
382+
/// known.
383+
#[serde(default, skip_serializing_if = "Option::is_none")]
384+
pub active_service_members: Option<u64>,
381385
}
382386

383387
#[cfg(test)]

crates/matrix-sdk-base/src/room/mod.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub use state::{RoomState, RoomStateFilter};
6666
pub(crate) use tags::RoomNotableTags;
6767
use tokio::sync::broadcast;
6868
pub use tombstone::{PredecessorRoom, SuccessorRoom};
69-
use tracing::{info, instrument, warn};
69+
use tracing::{info, instrument, trace, warn};
7070

7171
use 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.

crates/matrix-sdk-base/src/room/room_info.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,10 @@ impl RoomInfo {
872872
}
873873
}
874874

875+
if changed {
876+
self.summary.active_service_members = None;
877+
}
878+
875879
changed
876880
}
877881

@@ -1233,6 +1237,18 @@ impl RoomInfo {
12331237

12341238
migrated
12351239
}
1240+
1241+
/// Returns the number of active (joined/invited) service members in the
1242+
/// room, if known.
1243+
pub fn active_service_member_count(&self) -> Option<u64> {
1244+
self.summary.active_service_members
1245+
}
1246+
1247+
/// Updates the cached value for the number of active service members in the
1248+
/// room.
1249+
pub fn update_active_service_member_count(&mut self, count: Option<u64>) {
1250+
self.summary.active_service_members = count;
1251+
}
12361252
}
12371253

12381254
/// Type to represent a `RoomInfo::recency_stamp`.
@@ -1346,6 +1362,9 @@ bitflags! {
13461362
/// The display name has changed.
13471363
const DISPLAY_NAME = 0b0010_0000;
13481364

1365+
/// The active service members have changed.
1366+
const ACTIVE_SERVICE_MEMBERS = 0b0100_0000;
1367+
13491368
/// This is a temporary hack.
13501369
///
13511370
/// So here is the thing. Ideally, we DO NOT want to emit this reason. It does not
@@ -1416,6 +1435,7 @@ mod tests {
14161435
}],
14171436
joined_member_count: 5,
14181437
invited_member_count: 0,
1438+
active_service_members: None,
14191439
},
14201440
members_synced: true,
14211441
last_prev_batch: Some("pb".to_owned()),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ pub struct NotificationItem {
955955

956956
/// The push actions for this notification (notify, sound, highlight, etc.).
957957
pub actions: Option<Vec<Action>>,
958+
959+
pub is_dm: bool,
958960
}
959961

960962
impl NotificationItem {
@@ -1034,7 +1036,7 @@ impl NotificationItem {
10341036
.collect_vec();
10351037

10361038
let active_service_members_count =
1037-
room.active_service_members().await?.unwrap_or_default().len() as u64;
1039+
room.update_active_service_members().await?.unwrap_or_default().len() as u64;
10381040

10391041
let item = NotificationItem {
10401042
event,
@@ -1061,6 +1063,7 @@ impl NotificationItem {
10611063
has_mention,
10621064
thread_id,
10631065
actions: push_actions.map(|actions| actions.to_vec()),
1066+
is_dm: room.compute_is_dm().await?,
10641067
};
10651068

10661069
Ok(item)

0 commit comments

Comments
 (0)