Skip to main content

matrix_sdk_base/room/
mod.rs

1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![allow(clippy::assign_op_pattern)] // Triggered by bitflags! usage
16
17mod call;
18mod create;
19mod display_name;
20mod encryption;
21mod knock;
22mod latest_event;
23mod members;
24mod room_info;
25mod state;
26mod tags;
27mod tombstone;
28
29use std::{
30    collections::{BTreeMap, BTreeSet, HashSet},
31    sync::Arc,
32};
33
34pub use call::CallIntentConsensus;
35pub use create::*;
36pub use display_name::{RoomDisplayName, RoomHero};
37pub(crate) use display_name::{RoomSummary, UpdatedRoomDisplayName};
38pub use encryption::EncryptionState;
39use eyeball::{AsyncLock, SharedObservable};
40use futures_util::{Stream, StreamExt};
41pub use members::{RoomMember, RoomMembersUpdate, RoomMemberships};
42pub(crate) use room_info::SyncInfo;
43pub use room_info::{
44    BaseRoomInfo, RoomInfo, RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomRecencyStamp,
45    apply_redaction,
46};
47use ruma::{
48    EventId, OwnedEventId, OwnedMxcUri, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomId,
49    RoomVersionId, UserId,
50    events::{
51        direct::OwnedDirectUserIdentifier,
52        receipt::{Receipt, ReceiptThread, ReceiptType},
53        room::{
54            avatar,
55            guest_access::GuestAccess,
56            history_visibility::HistoryVisibility,
57            join_rules::JoinRule,
58            power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent, RoomPowerLevelsSource},
59        },
60    },
61    room::RoomType,
62};
63use serde::{Deserialize, Serialize};
64pub use state::{RoomState, RoomStateFilter};
65pub(crate) use tags::RoomNotableTags;
66use tokio::sync::broadcast;
67pub use tombstone::{PredecessorRoom, SuccessorRoom};
68use tracing::{info, instrument, warn};
69
70use crate::{
71    Error,
72    deserialized_responses::MemberEvent,
73    notification_settings::RoomNotificationMode,
74    read_receipts::RoomReadReceipts,
75    store::{DynStateStore, Result as StoreResult, StateStoreExt},
76    sync::UnreadNotificationsCount,
77};
78
79/// The underlying room data structure collecting state for joined, left and
80/// invited rooms.
81#[derive(Debug, Clone)]
82pub struct Room {
83    /// The room ID.
84    pub(super) room_id: OwnedRoomId,
85
86    /// Our own user ID.
87    pub(super) own_user_id: OwnedUserId,
88
89    pub(super) info: SharedObservable<RoomInfo>,
90
91    /// A clone of the [`BaseStateStore::room_info_notable_update_sender`].
92    ///
93    /// [`BaseStateStore::room_info_notable_update_sender`]: crate::store::BaseStateStore::room_info_notable_update_sender
94    pub(super) room_info_notable_update_sender: broadcast::Sender<RoomInfoNotableUpdate>,
95
96    /// A clone of the state store.
97    pub(super) store: Arc<DynStateStore>,
98
99    /// A map for ids of room membership events in the knocking state linked to
100    /// the user id of the user affected by the member event, that the current
101    /// user has marked as seen so they can be ignored.
102    pub seen_knock_request_ids_map:
103        SharedObservable<Option<BTreeMap<OwnedEventId, OwnedUserId>>, AsyncLock>,
104
105    /// A sender that will notify receivers when room member updates happen.
106    pub room_member_updates_sender: broadcast::Sender<RoomMembersUpdate>,
107}
108
109impl Room {
110    pub(crate) fn new(
111        own_user_id: &UserId,
112        store: Arc<DynStateStore>,
113        room_id: &RoomId,
114        room_state: RoomState,
115        room_info_notable_update_sender: broadcast::Sender<RoomInfoNotableUpdate>,
116    ) -> Self {
117        let room_info = RoomInfo::new(room_id, room_state);
118        Self::restore(own_user_id, store, room_info, room_info_notable_update_sender)
119    }
120
121    pub(crate) fn restore(
122        own_user_id: &UserId,
123        store: Arc<DynStateStore>,
124        room_info: RoomInfo,
125        room_info_notable_update_sender: broadcast::Sender<RoomInfoNotableUpdate>,
126    ) -> Self {
127        let (room_member_updates_sender, _) = broadcast::channel(10);
128        Self {
129            own_user_id: own_user_id.into(),
130            room_id: room_info.room_id.clone(),
131            store,
132            info: SharedObservable::new(room_info),
133            room_info_notable_update_sender,
134            seen_knock_request_ids_map: SharedObservable::new_async(None),
135            room_member_updates_sender,
136        }
137    }
138
139    /// Get the unique room id of the room.
140    pub fn room_id(&self) -> &RoomId {
141        &self.room_id
142    }
143
144    /// Get a copy of the room creators.
145    pub fn creators(&self) -> Option<Vec<OwnedUserId>> {
146        self.info.read().creators()
147    }
148
149    /// Get our own user id.
150    pub fn own_user_id(&self) -> &UserId {
151        &self.own_user_id
152    }
153
154    /// Whether this room's [`RoomType`] is `m.space`.
155    pub fn is_space(&self) -> bool {
156        self.info.read().room_type().is_some_and(|t| *t == RoomType::Space)
157    }
158
159    /// Whether this room is a Call room as defined by [MSC3417].
160    ///
161    /// [MSC3417]: <https://github.com/matrix-org/matrix-spec-proposals/pull/3417>
162    pub fn is_call(&self) -> bool {
163        self.info.read().room_type().is_some_and(|t| *t == RoomType::Call)
164    }
165
166    /// Returns the room's type as defined in its creation event
167    /// (`m.room.create`).
168    pub fn room_type(&self) -> Option<RoomType> {
169        self.info.read().room_type().map(ToOwned::to_owned)
170    }
171
172    /// Get the unread notification counts computed server-side.
173    ///
174    /// Note: these might be incorrect for encrypted rooms, since the server
175    /// doesn't know which events are relevant standalone messages or not,
176    /// nor can it inspect mentions. If you need more precise counts for
177    /// encrypted rooms, consider using the client-side computed counts in
178    /// [`Self::num_unread_messages`], [`Self::num_unread_notifications`] and
179    /// [`Self::num_unread_mentions`].
180    pub fn unread_notification_counts(&self) -> UnreadNotificationsCount {
181        self.info.read().notification_counts
182    }
183
184    /// Get the number of unread messages (computed client-side).
185    ///
186    /// This might be more precise than [`Self::unread_notification_counts`] for
187    /// encrypted rooms.
188    pub fn num_unread_messages(&self) -> u64 {
189        self.info.read().read_receipts.num_unread
190    }
191
192    /// Get the number of unread notifications (computed client-side).
193    ///
194    /// This might be more precise than [`Self::unread_notification_counts`] for
195    /// encrypted rooms.
196    pub fn num_unread_notifications(&self) -> u64 {
197        self.info.read().read_receipts.num_notifications
198    }
199
200    /// Get the number of unread mentions (computed client-side), that is,
201    /// messages causing a highlight in a room.
202    ///
203    /// This might be more precise than [`Self::unread_notification_counts`] for
204    /// encrypted rooms.
205    pub fn num_unread_mentions(&self) -> u64 {
206        self.info.read().read_receipts.num_mentions
207    }
208
209    /// Get the detailed information about read receipts for the room.
210    pub fn read_receipts(&self) -> RoomReadReceipts {
211        self.info.read().read_receipts.clone()
212    }
213
214    /// Check if the room states have been synced
215    ///
216    /// States might be missing if we have only seen the room_id of this Room
217    /// so far, for example as the response for a `create_room` request without
218    /// being synced yet.
219    ///
220    /// Returns true if the state is fully synced, false otherwise.
221    pub fn is_state_fully_synced(&self) -> bool {
222        self.info.read().sync_info == SyncInfo::FullySynced
223    }
224
225    /// Check if the room state has been at least partially synced.
226    ///
227    /// See [`Room::is_state_fully_synced`] for more info.
228    pub fn is_state_partially_or_fully_synced(&self) -> bool {
229        self.info.read().sync_info != SyncInfo::NoState
230    }
231
232    /// Get the `prev_batch` token that was received from the last sync. May be
233    /// `None` if the last sync contained the full room history.
234    pub fn last_prev_batch(&self) -> Option<String> {
235        self.info.read().last_prev_batch.clone()
236    }
237
238    /// Get the avatar url of this room.
239    pub fn avatar_url(&self) -> Option<OwnedMxcUri> {
240        self.info.read().avatar_url().map(ToOwned::to_owned)
241    }
242
243    /// Get information about the avatar of this room.
244    pub fn avatar_info(&self) -> Option<avatar::ImageInfo> {
245        self.info.read().avatar_info().map(ToOwned::to_owned)
246    }
247
248    /// Get the canonical alias of this room.
249    pub fn canonical_alias(&self) -> Option<OwnedRoomAliasId> {
250        self.info.read().canonical_alias().map(ToOwned::to_owned)
251    }
252
253    /// Get the canonical alias of this room.
254    pub fn alt_aliases(&self) -> Vec<OwnedRoomAliasId> {
255        self.info.read().alt_aliases().to_owned()
256    }
257
258    /// Get the `m.room.create` content of this room.
259    ///
260    /// This usually isn't optional but some servers might not send an
261    /// `m.room.create` event as the first event for a given room, thus this can
262    /// be optional.
263    ///
264    /// For room versions earlier than room version 11, if the event is
265    /// redacted, all fields except `creator` will be set to their default
266    /// value.
267    pub fn create_content(&self) -> Option<RoomCreateWithCreatorEventContent> {
268        Some(self.info.read().base_info.create.as_ref()?.content.clone())
269    }
270
271    /// Is this room considered a direct message.
272    ///
273    /// Async because it can read room info from storage.
274    #[instrument(skip_all, fields(room_id = ?self.room_id))]
275    pub async fn is_direct(&self) -> StoreResult<bool> {
276        match self.state() {
277            RoomState::Joined | RoomState::Left | RoomState::Banned => {
278                Ok(!self.info.read().base_info.dm_targets.is_empty())
279            }
280
281            RoomState::Invited => {
282                let member = self.get_member(self.own_user_id()).await?;
283
284                match member {
285                    None => {
286                        info!("RoomMember not found for the user's own id");
287                        Ok(false)
288                    }
289                    Some(member) => match member.event.as_ref() {
290                        MemberEvent::Sync(_) => {
291                            warn!("Got MemberEvent::Sync in an invited room");
292                            Ok(false)
293                        }
294                        MemberEvent::Stripped(event) => {
295                            Ok(event.content.is_direct.unwrap_or(false))
296                        }
297                    },
298                }
299            }
300
301            // TODO: implement logic once we have the stripped events as we'd have with an Invite
302            RoomState::Knocked => Ok(false),
303        }
304    }
305
306    /// If this room is a direct message, get the members that we're sharing the
307    /// room with.
308    ///
309    /// *Note*: The member list might have been modified in the meantime and
310    /// the targets might not even be in the room anymore. This setting should
311    /// only be considered as guidance. We leave members in this list to allow
312    /// us to re-find a DM with a user even if they have left, since we may
313    /// want to re-invite them.
314    pub fn direct_targets(&self) -> HashSet<OwnedDirectUserIdentifier> {
315        self.info.read().base_info.dm_targets.clone()
316    }
317
318    /// If this room is a direct message, returns the number of members that
319    /// we're sharing the room with.
320    pub fn direct_targets_length(&self) -> usize {
321        self.info.read().base_info.dm_targets.len()
322    }
323
324    /// Get the guest access policy of this room.
325    pub fn guest_access(&self) -> GuestAccess {
326        self.info.read().guest_access().clone()
327    }
328
329    /// Get the history visibility policy of this room.
330    pub fn history_visibility(&self) -> Option<HistoryVisibility> {
331        self.info.read().history_visibility().cloned()
332    }
333
334    /// Get the history visibility policy of this room, or a sensible default if
335    /// the event is missing.
336    pub fn history_visibility_or_default(&self) -> HistoryVisibility {
337        self.info.read().history_visibility_or_default().clone()
338    }
339
340    /// Is the room considered to be public.
341    ///
342    /// May return `None` if the join rule event is not available.
343    pub fn is_public(&self) -> Option<bool> {
344        self.info.read().join_rule().map(|join_rule| matches!(join_rule, JoinRule::Public))
345    }
346
347    /// Get the join rule policy of this room, if available.
348    pub fn join_rule(&self) -> Option<JoinRule> {
349        self.info.read().join_rule().cloned()
350    }
351
352    /// Get the maximum power level that this room contains.
353    ///
354    /// This is useful if one wishes to normalize the power levels, e.g. from
355    /// 0-100 where 100 would be the max power level.
356    pub fn max_power_level(&self) -> i64 {
357        self.info.read().base_info.max_power_level
358    }
359
360    /// Get the service members in this room, if available.
361    pub fn service_members(&self) -> Option<BTreeSet<OwnedUserId>> {
362        self.info.read().service_members().cloned()
363    }
364
365    /// Get the current power levels of this room.
366    pub async fn power_levels(&self) -> Result<RoomPowerLevels, Error> {
367        let power_levels_content = self
368            .store
369            .get_state_event_static::<RoomPowerLevelsEventContent>(self.room_id())
370            .await?
371            .ok_or(Error::InsufficientData)?
372            .deserialize()?;
373        let creators = self.creators().ok_or(Error::InsufficientData)?;
374        let rules = self.info.read().room_version_rules_or_default();
375
376        Ok(power_levels_content.power_levels(&rules.authorization, creators))
377    }
378
379    /// Get the current power levels of this room, or a sensible default if they
380    /// are not known.
381    pub async fn power_levels_or_default(&self) -> RoomPowerLevels {
382        if let Ok(power_levels) = self.power_levels().await {
383            return power_levels;
384        }
385
386        // As a fallback, create the default power levels of a room.
387        let rules = self.info.read().room_version_rules_or_default();
388        RoomPowerLevels::new(
389            RoomPowerLevelsSource::None,
390            &rules.authorization,
391            self.creators().into_iter().flatten(),
392        )
393    }
394
395    /// Get the `m.room.name` of this room.
396    ///
397    /// The returned string may be empty if the event has been redacted, or it's
398    /// missing from storage.
399    pub fn name(&self) -> Option<String> {
400        self.info.read().name().map(ToOwned::to_owned)
401    }
402
403    /// Get the topic of the room.
404    pub fn topic(&self) -> Option<String> {
405        self.info.read().topic().map(ToOwned::to_owned)
406    }
407
408    /// Update the cached user defined notification mode.
409    ///
410    /// This is automatically recomputed on every successful sync, and the
411    /// cached result can be retrieved in
412    /// [`Self::cached_user_defined_notification_mode`].
413    pub fn update_cached_user_defined_notification_mode(&self, mode: RoomNotificationMode) {
414        self.info.update_if(|info| {
415            if info.cached_user_defined_notification_mode.as_ref() != Some(&mode) {
416                info.cached_user_defined_notification_mode = Some(mode);
417
418                true
419            } else {
420                false
421            }
422        });
423    }
424
425    /// Returns the cached user defined notification mode, if available.
426    ///
427    /// This cache is refilled every time we call
428    /// [`Self::update_cached_user_defined_notification_mode`].
429    pub fn cached_user_defined_notification_mode(&self) -> Option<RoomNotificationMode> {
430        self.info.read().cached_user_defined_notification_mode
431    }
432
433    /// Removes any existing cached value for the user defined notification
434    /// mode.
435    pub fn clear_user_defined_notification_mode(&self) {
436        self.info.update_if(|info| {
437            if info.cached_user_defined_notification_mode.is_some() {
438                info.cached_user_defined_notification_mode = None;
439                true
440            } else {
441                false
442            }
443        })
444    }
445
446    /// Get the list of users ids that are considered to be joined members of
447    /// this room.
448    pub async fn joined_user_ids(&self) -> StoreResult<Vec<OwnedUserId>> {
449        self.store.get_user_ids(self.room_id(), RoomMemberships::JOIN).await
450    }
451
452    /// Get the heroes for this room.
453    pub fn heroes(&self) -> Vec<RoomHero> {
454        self.info.read().heroes().to_vec()
455    }
456
457    /// Get the receipt as an `OwnedEventId` and `Receipt` tuple for the given
458    /// `receipt_type`, `thread` and `user_id` in this room.
459    pub async fn load_user_receipt(
460        &self,
461        receipt_type: ReceiptType,
462        thread: ReceiptThread,
463        user_id: &UserId,
464    ) -> StoreResult<Option<(OwnedEventId, Receipt)>> {
465        self.store.get_user_room_receipt_event(self.room_id(), receipt_type, thread, user_id).await
466    }
467
468    /// Load from storage the receipts as a list of `OwnedUserId` and `Receipt`
469    /// tuples for the given `receipt_type`, `thread` and `event_id` in this
470    /// room.
471    pub async fn load_event_receipts(
472        &self,
473        receipt_type: ReceiptType,
474        thread: ReceiptThread,
475        event_id: &EventId,
476    ) -> StoreResult<Vec<(OwnedUserId, Receipt)>> {
477        self.store
478            .get_event_room_receipt_events(self.room_id(), receipt_type, thread, event_id)
479            .await
480    }
481
482    /// Returns a boolean indicating if this room has been manually marked as
483    /// unread
484    pub fn is_marked_unread(&self) -> bool {
485        self.info.read().base_info.is_marked_unread
486    }
487
488    /// Returns the [`RoomVersionId`] of the room, if known.
489    pub fn version(&self) -> Option<RoomVersionId> {
490        self.info.read().room_version().cloned()
491    }
492
493    /// Returns the recency stamp of the room.
494    ///
495    /// Please read `RoomInfo::recency_stamp` to learn more.
496    pub fn recency_stamp(&self) -> Option<RoomRecencyStamp> {
497        self.info.read().recency_stamp
498    }
499
500    /// Get a `Stream` of loaded pinned events for this room.
501    /// If no pinned events are found a single empty `Vec` will be returned.
502    pub fn pinned_event_ids_stream(&self) -> impl Stream<Item = Vec<OwnedEventId>> + use<> {
503        self.info
504            .subscribe()
505            .map(|i| i.base_info.pinned_events.and_then(|c| c.pinned).unwrap_or_default())
506    }
507
508    /// Returns the current pinned event ids for this room.
509    pub fn pinned_event_ids(&self) -> Option<Vec<OwnedEventId>> {
510        self.info.read().pinned_event_ids()
511    }
512}
513
514// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823.
515#[cfg(not(feature = "test-send-sync"))]
516unsafe impl Send for Room {}
517
518// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823.
519#[cfg(not(feature = "test-send-sync"))]
520unsafe impl Sync for Room {}
521
522#[cfg(feature = "test-send-sync")]
523#[test]
524// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823.
525fn test_send_sync_for_room() {
526    fn assert_send_sync<
527        T: matrix_sdk_common::SendOutsideWasm + matrix_sdk_common::SyncOutsideWasm,
528    >() {
529    }
530
531    assert_send_sync::<Room>();
532}
533
534/// The possible sources of an account data type.
535#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
536pub(crate) enum AccountDataSource {
537    /// The source is account data with the stable prefix.
538    Stable,
539
540    /// The source is account data with the unstable prefix.
541    #[default]
542    Unstable,
543}