matrix_sdk/room/
mod.rs

1// Copyright 2024 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//! High-level room API
16
17use std::{
18    borrow::Borrow,
19    collections::{BTreeMap, HashMap},
20    future::Future,
21    ops::Deref,
22    sync::Arc,
23    time::Duration,
24};
25
26use async_stream::stream;
27use eyeball::SharedObservable;
28use futures_core::Stream;
29use futures_util::{
30    StreamExt, future::join_all, stream as futures_stream, stream::FuturesUnordered,
31};
32use http::StatusCode;
33#[cfg(feature = "e2e-encryption")]
34pub use identity_status_changes::IdentityStatusChanges;
35#[cfg(feature = "experimental-encrypted-state-events")]
36use matrix_sdk_base::crypto::types::events::room::encrypted::EncryptedEvent;
37#[cfg(feature = "e2e-encryption")]
38use matrix_sdk_base::crypto::{
39    IdentityStatusChange, RoomIdentityProvider, UserIdentity, types::events::CryptoContextInfo,
40};
41pub use matrix_sdk_base::store::StoredThreadSubscription;
42use matrix_sdk_base::{
43    ComposerDraft, EncryptionState, RoomInfoNotableUpdateReasons, RoomMemberships, SendOutsideWasm,
44    StateChanges, StateStoreDataKey, StateStoreDataValue,
45    deserialized_responses::{
46        RawAnySyncOrStrippedState, RawSyncOrStrippedState, SyncOrStrippedState,
47    },
48    media::{MediaThumbnailSettings, store::IgnoreMediaRetentionPolicy},
49    store::{StateStoreExt, ThreadSubscriptionStatus},
50};
51#[cfg(feature = "e2e-encryption")]
52use matrix_sdk_base::{crypto::RoomEventDecryptionResult, deserialized_responses::EncryptionInfo};
53#[cfg(feature = "e2e-encryption")]
54use matrix_sdk_common::BoxFuture;
55use matrix_sdk_common::{
56    deserialized_responses::TimelineEvent,
57    executor::{JoinHandle, spawn},
58    timeout::timeout,
59};
60#[cfg(feature = "experimental-search")]
61use matrix_sdk_search::error::IndexError;
62#[cfg(feature = "experimental-search")]
63#[cfg(doc)]
64use matrix_sdk_search::index::RoomIndex;
65use mime::Mime;
66use reply::Reply;
67#[cfg(any(feature = "experimental-search", feature = "e2e-encryption"))]
68use ruma::events::AnySyncMessageLikeEvent;
69#[cfg(feature = "experimental-encrypted-state-events")]
70use ruma::events::AnySyncStateEvent;
71#[cfg(feature = "unstable-msc4274")]
72use ruma::events::room::message::GalleryItemType;
73#[cfg(feature = "e2e-encryption")]
74use ruma::events::{
75    AnySyncTimelineEvent, SyncMessageLikeEvent, room::encrypted::OriginalSyncRoomEncryptedEvent,
76};
77use ruma::{
78    EventId, Int, MatrixToUri, MatrixUri, MxcUri, OwnedEventId, OwnedRoomId, OwnedServerName,
79    OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId,
80    api::client::{
81        config::{set_global_account_data, set_room_account_data},
82        context,
83        error::ErrorKind,
84        filter::LazyLoadOptions,
85        membership::{
86            Invite3pid, ban_user, forget_room, get_member_events,
87            invite_user::{self, v3::InvitationRecipient},
88            kick_user, leave_room, unban_user,
89        },
90        message::send_message_event,
91        read_marker::set_read_marker,
92        receipt::create_receipt,
93        redact::redact_event,
94        room::{get_room_event, report_content, report_room},
95        state::{get_state_event_for_key, send_state_event},
96        tag::{create_tag, delete_tag},
97        threads::{get_thread_subscription, subscribe_thread, unsubscribe_thread},
98        typing::create_typing_event::{self, v3::Typing},
99    },
100    assign,
101    events::{
102        AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent, AnyTimelineEvent, EmptyStateKey,
103        Mentions, MessageLikeEventContent, OriginalSyncStateEvent, RedactContent,
104        RedactedStateEventContent, RoomAccountDataEvent, RoomAccountDataEventContent,
105        RoomAccountDataEventType, StateEventContent, StateEventType, StaticEventContent,
106        StaticStateEventContent, SyncStateEvent,
107        beacon::BeaconEventContent,
108        beacon_info::BeaconInfoEventContent,
109        direct::DirectEventContent,
110        marked_unread::MarkedUnreadEventContent,
111        receipt::{Receipt, ReceiptThread, ReceiptType},
112        room::{
113            ImageInfo, MediaSource, ThumbnailInfo,
114            avatar::{self, RoomAvatarEventContent},
115            encryption::RoomEncryptionEventContent,
116            history_visibility::HistoryVisibility,
117            member::{MembershipChange, SyncRoomMemberEvent},
118            message::{
119                AudioInfo, AudioMessageEventContent, FileInfo, FileMessageEventContent,
120                ImageMessageEventContent, MessageType, RoomMessageEventContent,
121                TextMessageEventContent, UnstableAmplitude, UnstableAudioDetailsContentBlock,
122                UnstableVoiceContentBlock, VideoInfo, VideoMessageEventContent,
123            },
124            name::RoomNameEventContent,
125            pinned_events::RoomPinnedEventsEventContent,
126            power_levels::{
127                RoomPowerLevels, RoomPowerLevelsEventContent, RoomPowerLevelsSource, UserPowerLevel,
128            },
129            server_acl::RoomServerAclEventContent,
130            topic::RoomTopicEventContent,
131        },
132        space::{child::SpaceChildEventContent, parent::SpaceParentEventContent},
133        tag::{TagInfo, TagName},
134        typing::SyncTypingEvent,
135    },
136    int,
137    push::{Action, AnyPushRuleRef, PushConditionRoomCtx, Ruleset},
138    serde::Raw,
139    time::Instant,
140};
141#[cfg(feature = "experimental-encrypted-state-events")]
142use ruma::{
143    events::room::encrypted::unstable_state::OriginalSyncStateRoomEncryptedEvent,
144    serde::JsonCastable,
145};
146use serde::de::DeserializeOwned;
147use thiserror::Error;
148use tokio::{join, sync::broadcast};
149use tracing::{debug, error, info, instrument, trace, warn};
150
151use self::futures::{SendAttachment, SendMessageLikeEvent, SendRawMessageLikeEvent};
152pub use self::{
153    member::{RoomMember, RoomMemberRole},
154    messages::{
155        EventWithContextResponse, IncludeRelations, ListThreadsOptions, Messages, MessagesOptions,
156        Relations, RelationsOptions, ThreadRoots,
157    },
158};
159#[cfg(feature = "e2e-encryption")]
160use crate::encryption::backups::BackupState;
161#[cfg(doc)]
162use crate::event_cache::EventCache;
163#[cfg(feature = "experimental-encrypted-state-events")]
164use crate::room::futures::{SendRawStateEvent, SendStateEvent};
165use crate::{
166    BaseRoom, Client, Error, HttpResult, Result, RoomState, TransmissionProgress,
167    attachment::{AttachmentConfig, AttachmentInfo},
168    client::WeakClient,
169    config::RequestConfig,
170    error::{BeaconError, WrongRoomState},
171    event_cache::{self, EventCacheDropHandles, RoomEventCache},
172    event_handler::{EventHandler, EventHandlerDropGuard, EventHandlerHandle, SyncEvent},
173    live_location_share::ObservableLiveLocation,
174    media::{MediaFormat, MediaRequestParameters},
175    notification_settings::{IsEncrypted, IsOneToOne, RoomNotificationMode},
176    room::{
177        knock_requests::{KnockRequest, KnockRequestMemberInfo},
178        power_levels::{RoomPowerLevelChanges, RoomPowerLevelsExt},
179        privacy_settings::RoomPrivacySettings,
180    },
181    sync::RoomUpdate,
182    utils::{IntoRawMessageLikeEventContent, IntoRawStateEventContent},
183};
184
185pub mod edit;
186pub mod futures;
187pub mod identity_status_changes;
188/// Contains code related to requests to join a room.
189pub mod knock_requests;
190mod member;
191mod messages;
192pub mod power_levels;
193pub mod reply;
194
195pub mod calls;
196
197/// Contains all the functionality for modifying the privacy settings in a room.
198pub mod privacy_settings;
199
200#[cfg(feature = "e2e-encryption")]
201pub(crate) mod shared_room_history;
202
203/// A struct containing methods that are common for Joined, Invited and Left
204/// Rooms
205#[derive(Debug, Clone)]
206pub struct Room {
207    inner: BaseRoom,
208    pub(crate) client: Client,
209}
210
211impl Deref for Room {
212    type Target = BaseRoom;
213
214    fn deref(&self) -> &Self::Target {
215        &self.inner
216    }
217}
218
219const TYPING_NOTICE_TIMEOUT: Duration = Duration::from_secs(4);
220const TYPING_NOTICE_RESEND_TIMEOUT: Duration = Duration::from_secs(3);
221
222/// A thread subscription, according to the semantics of MSC4306.
223#[derive(Debug, Clone, Copy, PartialEq, Eq)]
224pub struct ThreadSubscription {
225    /// Whether the subscription was made automatically by a client, not by
226    /// manual user choice.
227    pub automatic: bool,
228}
229
230/// Context allowing to compute the push actions for a given event.
231#[derive(Debug)]
232pub struct PushContext {
233    /// The Ruma context used to compute the push actions.
234    push_condition_room_ctx: PushConditionRoomCtx,
235
236    /// Push rules for this room, based on the push rules state event, or the
237    /// global server default as defined by [`Ruleset::server_default`].
238    push_rules: Ruleset,
239}
240
241impl PushContext {
242    /// Create a new [`PushContext`] from its inner components.
243    pub fn new(push_condition_room_ctx: PushConditionRoomCtx, push_rules: Ruleset) -> Self {
244        Self { push_condition_room_ctx, push_rules }
245    }
246
247    /// Compute the push rules for a given event.
248    pub async fn for_event<T>(&self, event: &Raw<T>) -> Vec<Action> {
249        self.push_rules.get_actions(event, &self.push_condition_room_ctx).await.to_owned()
250    }
251
252    /// Compute the push rules for a given event, with extra logging to help
253    /// debugging.
254    #[doc(hidden)]
255    #[instrument(skip_all)]
256    pub async fn traced_for_event<T>(&self, event: &Raw<T>) -> Vec<Action> {
257        let rules = self
258            .push_rules
259            .iter()
260            .filter_map(|r| {
261                if !r.enabled() {
262                    return None;
263                }
264
265                let simplified_action = if r.actions().is_empty() { "inhibit" } else { "notify" };
266
267                let conditions = match r {
268                    AnyPushRuleRef::Override(r) => {
269                        format!("{:?}", r.conditions)
270                    }
271                    AnyPushRuleRef::Content(r) => format!("content-body-match:{}", r.pattern),
272                    AnyPushRuleRef::Room(r) => format!("room-match:{}", r.rule_id),
273                    AnyPushRuleRef::Sender(r) => format!("sender-match:{}", r.rule_id),
274                    AnyPushRuleRef::Underride(r) => format!("{:?}", r.conditions),
275                    _ => "<unknown push rule kind>".to_owned(),
276                };
277
278                Some(format!("- {}: {conditions} => {simplified_action}", r.rule_id(),))
279            })
280            .collect::<Vec<_>>()
281            .join("\n");
282        trace!("rules:\n\n{rules}\n\n");
283
284        let found = self.push_rules.get_match(event, &self.push_condition_room_ctx).await;
285
286        if let Some(found) = found {
287            trace!("rule {} matched", found.rule_id());
288            found.actions().to_owned()
289        } else {
290            trace!("no match");
291            Vec::new()
292        }
293    }
294}
295
296macro_rules! make_media_type {
297    ($t:ty, $content_type: ident, $filename: ident, $source: ident, $caption: ident, $info: ident, $thumbnail: ident) => {{
298        // If caption is set, use it as body, and filename as the file name; otherwise,
299        // body is the filename, and the filename is not set.
300        // https://github.com/matrix-org/matrix-spec-proposals/blob/main/proposals/2530-body-as-caption.md
301        let (body, formatted, filename) = match $caption {
302            Some(TextMessageEventContent { body, formatted, .. }) => (body, formatted, Some($filename)),
303            None => ($filename, None, None),
304        };
305
306        let (thumbnail_source, thumbnail_info) = $thumbnail.unzip();
307
308        match $content_type.type_() {
309            mime::IMAGE => {
310                let info = assign!($info.map(ImageInfo::from).unwrap_or_default(), {
311                    mimetype: Some($content_type.as_ref().to_owned()),
312                    thumbnail_source,
313                    thumbnail_info
314                });
315                let content = assign!(ImageMessageEventContent::new(body, $source), {
316                    info: Some(Box::new(info)),
317                    formatted,
318                    filename
319                });
320                <$t>::Image(content)
321            }
322
323            mime::AUDIO => {
324                let mut content = assign!(AudioMessageEventContent::new(body, $source), {
325                    formatted,
326                    filename
327                });
328
329                if let Some(AttachmentInfo::Audio(audio_info) | AttachmentInfo::Voice(audio_info)) = &$info &&
330                 let Some(duration) = audio_info.duration && let Some(waveform_vec) = &audio_info.waveform {
331                    let waveform = waveform_vec
332                        .iter()
333                        .map(|v| ((*v).clamp(0.0, 1.0) * UnstableAmplitude::MAX as f32) as u16)
334                        .map(Into::into)
335                        .collect();
336                    content.audio =
337                        Some(UnstableAudioDetailsContentBlock::new(duration, waveform));
338                }
339
340                if matches!($info, Some(AttachmentInfo::Voice(_))) {
341                    content.voice = Some(UnstableVoiceContentBlock::new());
342                }
343
344                let mut audio_info = $info.map(AudioInfo::from).unwrap_or_default();
345                audio_info.mimetype = Some($content_type.as_ref().to_owned());
346                let content = content.info(Box::new(audio_info));
347
348                <$t>::Audio(content)
349            }
350
351            mime::VIDEO => {
352                let info = assign!($info.map(VideoInfo::from).unwrap_or_default(), {
353                    mimetype: Some($content_type.as_ref().to_owned()),
354                    thumbnail_source,
355                    thumbnail_info
356                });
357                let content = assign!(VideoMessageEventContent::new(body, $source), {
358                    info: Some(Box::new(info)),
359                    formatted,
360                    filename
361                });
362                <$t>::Video(content)
363            }
364
365            _ => {
366                let info = assign!($info.map(FileInfo::from).unwrap_or_default(), {
367                    mimetype: Some($content_type.as_ref().to_owned()),
368                    thumbnail_source,
369                    thumbnail_info
370                });
371                let content = assign!(FileMessageEventContent::new(body, $source), {
372                    info: Some(Box::new(info)),
373                    formatted,
374                    filename,
375                });
376                <$t>::File(content)
377            }
378        }
379    }};
380}
381
382impl Room {
383    /// Create a new `Room`
384    ///
385    /// # Arguments
386    /// * `client` - The client used to make requests.
387    ///
388    /// * `room` - The underlying room.
389    pub(crate) fn new(client: Client, room: BaseRoom) -> Self {
390        Self { inner: room, client }
391    }
392
393    /// Leave this room.
394    /// If the room was in [`RoomState::Invited`] state, it'll also be forgotten
395    /// automatically.
396    ///
397    /// Only invited and joined rooms can be left.
398    #[doc(alias = "reject_invitation")]
399    #[instrument(skip_all, fields(room_id = ?self.inner.room_id()))]
400    async fn leave_impl(&self) -> (Result<()>, &Room) {
401        let state = self.state();
402        if state == RoomState::Left {
403            return (
404                Err(Error::WrongRoomState(Box::new(WrongRoomState::new(
405                    "Joined or Invited",
406                    state,
407                )))),
408                self,
409            );
410        }
411
412        // If the room was in Invited state we should also forget it when declining the
413        // invite.
414        let should_forget = matches!(self.state(), RoomState::Invited);
415
416        let request = leave_room::v3::Request::new(self.inner.room_id().to_owned());
417        let response = self.client.send(request).await;
418
419        // The server can return with an error that is acceptable to ignore. Let's find
420        // which one.
421        if let Err(error) = response {
422            #[allow(clippy::collapsible_match)]
423            let ignore_error = if let Some(error) = error.client_api_error_kind() {
424                match error {
425                    // The user is trying to leave a room but doesn't have permissions to do so.
426                    // Let's consider the user has left the room.
427                    ErrorKind::Forbidden { .. } => true,
428                    _ => false,
429                }
430            } else {
431                false
432            };
433
434            error!(?error, ignore_error, should_forget, "Failed to leave the room");
435
436            if !ignore_error {
437                return (Err(error.into()), self);
438            }
439        }
440
441        if let Err(e) = self.client.base_client().room_left(self.room_id()).await {
442            return (Err(e.into()), self);
443        }
444
445        if should_forget {
446            trace!("Trying to forget the room");
447
448            if let Err(error) = self.forget().await {
449                error!(?error, "Failed to forget the room");
450            }
451        }
452
453        (Ok(()), self)
454    }
455
456    /// Leave this room and all predecessors.
457    /// If any room was in [`RoomState::Invited`] state, it'll also be forgotten
458    /// automatically.
459    ///
460    /// Only invited and joined rooms can be left.
461    /// Will return an error if the current room fails to leave but
462    /// will only warn if a predecessor fails to leave.
463    pub async fn leave(&self) -> Result<()> {
464        let mut rooms: Vec<Room> = vec![self.clone()];
465        let mut current_room = self;
466
467        while let Some(predecessor) = current_room.predecessor_room() {
468            let maybe_predecessor_room = current_room.client.get_room(&predecessor.room_id);
469
470            if let Some(predecessor_room) = maybe_predecessor_room {
471                rooms.push(predecessor_room.clone());
472                current_room = rooms.last().expect("Room just pushed so can't be empty");
473            } else {
474                warn!("Cannot find predecessor room");
475                break;
476            }
477        }
478
479        let batch_size = 5;
480
481        let rooms_futures: Vec<_> = rooms
482            .iter()
483            .filter_map(|room| match room.state() {
484                RoomState::Joined | RoomState::Invited | RoomState::Knocked => {
485                    Some(room.leave_impl())
486                }
487                RoomState::Banned | RoomState::Left => None,
488            })
489            .collect();
490
491        let mut futures_stream = futures_stream::iter(rooms_futures).buffer_unordered(batch_size);
492
493        let mut maybe_this_room_failed_with: Option<Error> = None;
494
495        while let Some(result) = futures_stream.next().await {
496            if let (Err(e), room) = result {
497                if room.room_id() == self.room_id() {
498                    maybe_this_room_failed_with = Some(e);
499                } else {
500                    warn!("Failure while attempting to leave predecessor room: {e:?}");
501                }
502            }
503        }
504
505        maybe_this_room_failed_with.map_or(Ok(()), Err)
506    }
507
508    /// Join this room.
509    ///
510    /// Only invited and left rooms can be joined via this method.
511    #[doc(alias = "accept_invitation")]
512    pub async fn join(&self) -> Result<()> {
513        let prev_room_state = self.inner.state();
514
515        if prev_room_state == RoomState::Joined {
516            return Err(Error::WrongRoomState(Box::new(WrongRoomState::new(
517                "Invited or Left",
518                prev_room_state,
519            ))));
520        }
521
522        self.client.join_room_by_id(self.room_id()).await?;
523
524        Ok(())
525    }
526
527    /// Get the inner client saved in this room instance.
528    ///
529    /// Returns the client this room is part of.
530    pub fn client(&self) -> Client {
531        self.client.clone()
532    }
533
534    /// Get the sync state of this room, i.e. whether it was fully synced with
535    /// the server.
536    pub fn is_synced(&self) -> bool {
537        self.inner.is_state_fully_synced()
538    }
539
540    /// Gets the avatar of this room, if set.
541    ///
542    /// Returns the avatar.
543    /// If a thumbnail is requested no guarantee on the size of the image is
544    /// given.
545    ///
546    /// # Arguments
547    ///
548    /// * `format` - The desired format of the avatar.
549    ///
550    /// # Examples
551    ///
552    /// ```no_run
553    /// # use matrix_sdk::Client;
554    /// # use matrix_sdk::ruma::room_id;
555    /// # use matrix_sdk::media::MediaFormat;
556    /// # use url::Url;
557    /// # let homeserver = Url::parse("http://example.com").unwrap();
558    /// # async {
559    /// # let user = "example";
560    /// let client = Client::new(homeserver).await.unwrap();
561    /// client.matrix_auth().login_username(user, "password").send().await.unwrap();
562    /// let room_id = room_id!("!roomid:example.com");
563    /// let room = client.get_room(&room_id).unwrap();
564    /// if let Some(avatar) = room.avatar(MediaFormat::File).await.unwrap() {
565    ///     std::fs::write("avatar.png", avatar);
566    /// }
567    /// # };
568    /// ```
569    pub async fn avatar(&self, format: MediaFormat) -> Result<Option<Vec<u8>>> {
570        let Some(url) = self.avatar_url() else { return Ok(None) };
571        let request = MediaRequestParameters { source: MediaSource::Plain(url.to_owned()), format };
572        Ok(Some(self.client.media().get_media_content(&request, true).await?))
573    }
574
575    /// Sends a request to `/_matrix/client/r0/rooms/{room_id}/messages` and
576    /// returns a `Messages` struct that contains a chunk of room and state
577    /// events (`RoomEvent` and `AnyStateEvent`).
578    ///
579    /// With the encryption feature, messages are decrypted if possible. If
580    /// decryption fails for an individual message, that message is returned
581    /// undecrypted.
582    ///
583    /// # Examples
584    ///
585    /// ```no_run
586    /// use matrix_sdk::{Client, room::MessagesOptions};
587    /// # use matrix_sdk::ruma::{
588    /// #     api::client::filter::RoomEventFilter,
589    /// #     room_id,
590    /// # };
591    /// # use url::Url;
592    ///
593    /// # let homeserver = Url::parse("http://example.com").unwrap();
594    /// # async {
595    /// let options =
596    ///     MessagesOptions::backward().from("t47429-4392820_219380_26003_2265");
597    ///
598    /// let mut client = Client::new(homeserver).await.unwrap();
599    /// let room = client.get_room(room_id!("!roomid:example.com")).unwrap();
600    /// assert!(room.messages(options).await.is_ok());
601    /// # };
602    /// ```
603    #[instrument(skip_all, fields(room_id = ?self.inner.room_id(), ?options))]
604    pub async fn messages(&self, options: MessagesOptions) -> Result<Messages> {
605        let room_id = self.inner.room_id();
606        let request = options.into_request(room_id);
607        let http_response = self.client.send(request).await?;
608
609        let push_ctx = self.push_context().await?;
610        let chunk = join_all(
611            http_response.chunk.into_iter().map(|ev| self.try_decrypt_event(ev, push_ctx.as_ref())),
612        )
613        .await;
614
615        Ok(Messages {
616            start: http_response.start,
617            end: http_response.end,
618            chunk,
619            state: http_response.state,
620        })
621    }
622
623    /// Register a handler for events of a specific type, within this room.
624    ///
625    /// This method works the same way as [`Client::add_event_handler`], except
626    /// that the handler will only be called for events within this room. See
627    /// that method for more details on event handler functions.
628    ///
629    /// `room.add_event_handler(hdl)` is equivalent to
630    /// `client.add_room_event_handler(room_id, hdl)`. Use whichever one is more
631    /// convenient in your use case.
632    pub fn add_event_handler<Ev, Ctx, H>(&self, handler: H) -> EventHandlerHandle
633    where
634        Ev: SyncEvent + DeserializeOwned + Send + 'static,
635        H: EventHandler<Ev, Ctx>,
636    {
637        self.client.add_room_event_handler(self.room_id(), handler)
638    }
639
640    /// Subscribe to all updates for this room.
641    ///
642    /// The returned receiver will receive a new message for each sync response
643    /// that contains updates for this room.
644    pub fn subscribe_to_updates(&self) -> broadcast::Receiver<RoomUpdate> {
645        self.client.subscribe_to_room_updates(self.room_id())
646    }
647
648    /// Subscribe to typing notifications for this room.
649    ///
650    /// The returned receiver will receive a new vector of user IDs for each
651    /// sync response that contains 'm.typing' event. The current user ID will
652    /// be filtered out.
653    pub fn subscribe_to_typing_notifications(
654        &self,
655    ) -> (EventHandlerDropGuard, broadcast::Receiver<Vec<OwnedUserId>>) {
656        let (sender, receiver) = broadcast::channel(16);
657        let typing_event_handler_handle = self.client.add_room_event_handler(self.room_id(), {
658            let own_user_id = self.own_user_id().to_owned();
659            move |event: SyncTypingEvent| async move {
660                // Ignore typing notifications from own user.
661                let typing_user_ids = event
662                    .content
663                    .user_ids
664                    .into_iter()
665                    .filter(|user_id| *user_id != own_user_id)
666                    .collect();
667                // Ignore the result. It can only fail if there are no listeners.
668                let _ = sender.send(typing_user_ids);
669            }
670        });
671        let drop_guard = self.client().event_handler_drop_guard(typing_event_handler_handle);
672        (drop_guard, receiver)
673    }
674
675    /// Subscribe to updates about users who are in "pin violation" i.e. their
676    /// identity has changed and the user has not yet acknowledged this.
677    ///
678    /// The returned receiver will receive a new vector of
679    /// [`IdentityStatusChange`] each time a /keys/query response shows a
680    /// changed identity for a member of this room, or a sync shows a change
681    /// to the membership of an affected user. (Changes to the current user are
682    /// not directly included, but some changes to the current user's identity
683    /// can trigger changes to how we see other users' identities, which
684    /// will be included.)
685    ///
686    /// The first item in the stream provides the current state of the room:
687    /// each member of the room who is not in "pinned" or "verified" state will
688    /// be included (except the current user).
689    ///
690    /// If the `changed_to` property of an [`IdentityStatusChange`] is set to
691    /// `PinViolation` then a warning should be displayed to the user. If it is
692    /// set to `Pinned` then no warning should be displayed.
693    ///
694    /// Note that if a user who is in pin violation leaves the room, a `Pinned`
695    /// update is sent, to indicate that the warning should be removed, even
696    /// though the user's identity is not necessarily pinned.
697    #[cfg(feature = "e2e-encryption")]
698    pub async fn subscribe_to_identity_status_changes(
699        &self,
700    ) -> Result<impl Stream<Item = Vec<IdentityStatusChange>> + use<>> {
701        IdentityStatusChanges::create_stream(self.clone()).await
702    }
703
704    /// Returns a wrapping `TimelineEvent` for the input `AnyTimelineEvent`,
705    /// decrypted if needs be.
706    ///
707    /// Only logs from the crypto crate will indicate a failure to decrypt.
708    #[cfg(not(feature = "experimental-encrypted-state-events"))]
709    #[allow(clippy::unused_async)] // Used only in e2e-encryption.
710    async fn try_decrypt_event(
711        &self,
712        event: Raw<AnyTimelineEvent>,
713        push_ctx: Option<&PushContext>,
714    ) -> TimelineEvent {
715        #[cfg(feature = "e2e-encryption")]
716        if let Ok(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomEncrypted(
717            SyncMessageLikeEvent::Original(_),
718        ))) = event.deserialize_as::<AnySyncTimelineEvent>()
719            && let Ok(event) = self.decrypt_event(event.cast_ref_unchecked(), push_ctx).await
720        {
721            return event;
722        }
723
724        let mut event = TimelineEvent::from_plaintext(event.cast());
725        if let Some(push_ctx) = push_ctx {
726            event.set_push_actions(push_ctx.for_event(event.raw()).await);
727        }
728
729        event
730    }
731
732    /// Returns a wrapping `TimelineEvent` for the input `AnyTimelineEvent`,
733    /// decrypted if needs be.
734    ///
735    /// Only logs from the crypto crate will indicate a failure to decrypt.
736    #[cfg(feature = "experimental-encrypted-state-events")]
737    #[allow(clippy::unused_async)] // Used only in e2e-encryption.
738    async fn try_decrypt_event(
739        &self,
740        event: Raw<AnyTimelineEvent>,
741        push_ctx: Option<&PushContext>,
742    ) -> TimelineEvent {
743        // If we have either an encrypted message-like or state event, try to decrypt.
744        match event.deserialize_as::<AnySyncTimelineEvent>() {
745            Ok(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomEncrypted(
746                SyncMessageLikeEvent::Original(_),
747            ))) => {
748                if let Ok(event) = self
749                    .decrypt_event(
750                        event.cast_ref_unchecked::<OriginalSyncRoomEncryptedEvent>(),
751                        push_ctx,
752                    )
753                    .await
754                {
755                    return event;
756                }
757            }
758            Ok(AnySyncTimelineEvent::State(AnySyncStateEvent::RoomEncrypted(
759                SyncStateEvent::Original(_),
760            ))) => {
761                if let Ok(event) = self
762                    .decrypt_event(
763                        event.cast_ref_unchecked::<OriginalSyncStateRoomEncryptedEvent>(),
764                        push_ctx,
765                    )
766                    .await
767                {
768                    return event;
769                }
770            }
771            _ => {}
772        }
773
774        let mut event = TimelineEvent::from_plaintext(event.cast());
775        if let Some(push_ctx) = push_ctx {
776            event.set_push_actions(push_ctx.for_event(event.raw()).await);
777        }
778
779        event
780    }
781
782    /// Fetch the event with the given `EventId` in this room.
783    ///
784    /// It uses the given [`RequestConfig`] if provided, or the client's default
785    /// one otherwise.
786    pub async fn event(
787        &self,
788        event_id: &EventId,
789        request_config: Option<RequestConfig>,
790    ) -> Result<TimelineEvent> {
791        let request =
792            get_room_event::v3::Request::new(self.room_id().to_owned(), event_id.to_owned());
793
794        let raw_event = self.client.send(request).with_request_config(request_config).await?.event;
795        let push_ctx = self.push_context().await?;
796        let event = self.try_decrypt_event(raw_event, push_ctx.as_ref()).await;
797
798        // Save the event into the event cache, if it's set up.
799        if let Ok((cache, _handles)) = self.event_cache().await {
800            cache.save_events([event.clone()]).await;
801        }
802
803        Ok(event)
804    }
805
806    /// Try to load the event from the [`EventCache`][crate::event_cache], if
807    /// it's enabled, or fetch it from the homeserver.
808    ///
809    /// When running the request against the homeserver, it uses the given
810    /// [`RequestConfig`] if provided, or the client's default one
811    /// otherwise.
812    pub async fn load_or_fetch_event(
813        &self,
814        event_id: &EventId,
815        request_config: Option<RequestConfig>,
816    ) -> Result<TimelineEvent> {
817        match self.event_cache().await {
818            Ok((event_cache, _drop_handles)) => {
819                if let Some(event) = event_cache.find_event(event_id).await? {
820                    return Ok(event);
821                }
822                // Fallthrough: try with a request.
823            }
824            Err(err) => {
825                debug!("error when getting the event cache: {err}");
826            }
827        }
828        self.event(event_id, request_config).await
829    }
830
831    /// Fetch the event with the given `EventId` in this room, using the
832    /// `/context` endpoint to get more information.
833    pub async fn event_with_context(
834        &self,
835        event_id: &EventId,
836        lazy_load_members: bool,
837        context_size: UInt,
838        request_config: Option<RequestConfig>,
839    ) -> Result<EventWithContextResponse> {
840        let mut request =
841            context::get_context::v3::Request::new(self.room_id().to_owned(), event_id.to_owned());
842
843        request.limit = context_size;
844
845        if lazy_load_members {
846            request.filter.lazy_load_options =
847                LazyLoadOptions::Enabled { include_redundant_members: false };
848        }
849
850        let response = self.client.send(request).with_request_config(request_config).await?;
851
852        let push_ctx = self.push_context().await?;
853        let push_ctx = push_ctx.as_ref();
854        let target_event = if let Some(event) = response.event {
855            Some(self.try_decrypt_event(event, push_ctx).await)
856        } else {
857            None
858        };
859
860        // Note: the joined future will fail if any future failed, but
861        // [`Self::try_decrypt_event`] doesn't hard-fail when there's a
862        // decryption error, so we should prevent against most bad cases here.
863        let (events_before, events_after) = join!(
864            join_all(
865                response.events_before.into_iter().map(|ev| self.try_decrypt_event(ev, push_ctx)),
866            ),
867            join_all(
868                response.events_after.into_iter().map(|ev| self.try_decrypt_event(ev, push_ctx)),
869            ),
870        );
871
872        // Save the loaded events into the event cache, if it's set up.
873        if let Ok((cache, _handles)) = self.event_cache().await {
874            let mut events_to_save: Vec<TimelineEvent> = Vec::new();
875            if let Some(event) = &target_event {
876                events_to_save.push(event.clone());
877            }
878
879            for event in &events_before {
880                events_to_save.push(event.clone());
881            }
882
883            for event in &events_after {
884                events_to_save.push(event.clone());
885            }
886
887            cache.save_events(events_to_save).await;
888        }
889
890        Ok(EventWithContextResponse {
891            event: target_event,
892            events_before,
893            events_after,
894            state: response.state,
895            prev_batch_token: response.start,
896            next_batch_token: response.end,
897        })
898    }
899
900    pub(crate) async fn request_members(&self) -> Result<()> {
901        self.client
902            .locks()
903            .members_request_deduplicated_handler
904            .run(self.room_id().to_owned(), async move {
905                let request = get_member_events::v3::Request::new(self.inner.room_id().to_owned());
906                let response = self
907                    .client
908                    .send(request.clone())
909                    .with_request_config(
910                        // In some cases it can take longer than 30s to load:
911                        // https://github.com/element-hq/synapse/issues/16872
912                        RequestConfig::new().timeout(Duration::from_secs(60)).retry_limit(3),
913                    )
914                    .await?;
915
916                // That's a large `Future`. Let's `Box::pin` to reduce its size on the stack.
917                Box::pin(self.client.base_client().receive_all_members(
918                    self.room_id(),
919                    &request,
920                    &response,
921                ))
922                .await?;
923
924                Ok(())
925            })
926            .await
927    }
928
929    /// Request to update the encryption state for this room.
930    ///
931    /// It does nothing if the encryption state is already
932    /// [`EncryptionState::Encrypted`] or [`EncryptionState::NotEncrypted`].
933    pub async fn request_encryption_state(&self) -> Result<()> {
934        if !self.inner.encryption_state().is_unknown() {
935            return Ok(());
936        }
937
938        self.client
939            .locks()
940            .encryption_state_deduplicated_handler
941            .run(self.room_id().to_owned(), async move {
942                // Request the event from the server.
943                let request = get_state_event_for_key::v3::Request::new(
944                    self.room_id().to_owned(),
945                    StateEventType::RoomEncryption,
946                    "".to_owned(),
947                );
948                let response = match self.client.send(request).await {
949                    Ok(response) => Some(
950                        response
951                            .into_content()
952                            .deserialize_as_unchecked::<RoomEncryptionEventContent>()?,
953                    ),
954                    Err(err) if err.client_api_error_kind() == Some(&ErrorKind::NotFound) => None,
955                    Err(err) => return Err(err.into()),
956                };
957
958                let _state_store_lock = self.client.base_client().state_store_lock().lock().await;
959
960                // Persist the event and the fact that we requested it from the server in
961                // `RoomInfo`.
962                let mut room_info = self.clone_info();
963                room_info.mark_encryption_state_synced();
964                room_info.set_encryption_event(response.clone());
965                let mut changes = StateChanges::default();
966                changes.add_room(room_info.clone());
967
968                self.client.state_store().save_changes(&changes).await?;
969                self.set_room_info(room_info, RoomInfoNotableUpdateReasons::empty());
970
971                Ok(())
972            })
973            .await
974    }
975
976    /// Check the encryption state of this room.
977    ///
978    /// If the result is [`EncryptionState::Unknown`], one might want to call
979    /// [`Room::request_encryption_state`].
980    pub fn encryption_state(&self) -> EncryptionState {
981        self.inner.encryption_state()
982    }
983
984    /// Force to update the encryption state by calling
985    /// [`Room::request_encryption_state`], and then calling
986    /// [`Room::encryption_state`].
987    ///
988    /// This method is useful to ensure the encryption state is up-to-date.
989    pub async fn latest_encryption_state(&self) -> Result<EncryptionState> {
990        self.request_encryption_state().await?;
991
992        Ok(self.encryption_state())
993    }
994
995    /// Gets additional context info about the client crypto.
996    #[cfg(feature = "e2e-encryption")]
997    pub async fn crypto_context_info(&self) -> CryptoContextInfo {
998        let encryption = self.client.encryption();
999
1000        let this_device_is_verified = match encryption.get_own_device().await {
1001            Ok(Some(device)) => device.is_verified_with_cross_signing(),
1002
1003            // Should not happen, there will always be an own device
1004            _ => true,
1005        };
1006
1007        let backup_exists_on_server =
1008            encryption.backups().exists_on_server().await.unwrap_or(false);
1009
1010        CryptoContextInfo {
1011            device_creation_ts: encryption.device_creation_timestamp().await,
1012            this_device_is_verified,
1013            is_backup_configured: encryption.backups().state() == BackupState::Enabled,
1014            backup_exists_on_server,
1015        }
1016    }
1017
1018    fn are_events_visible(&self) -> bool {
1019        if let RoomState::Invited = self.inner.state() {
1020            return matches!(
1021                self.inner.history_visibility_or_default(),
1022                HistoryVisibility::WorldReadable | HistoryVisibility::Invited
1023            );
1024        }
1025
1026        true
1027    }
1028
1029    /// Sync the member list with the server.
1030    ///
1031    /// This method will de-duplicate requests if it is called multiple times in
1032    /// quick succession, in that case the return value will be `None`. This
1033    /// method does nothing if the members are already synced.
1034    pub async fn sync_members(&self) -> Result<()> {
1035        if !self.are_events_visible() {
1036            return Ok(());
1037        }
1038
1039        if !self.are_members_synced() { self.request_members().await } else { Ok(()) }
1040    }
1041
1042    /// Get a specific member of this room.
1043    ///
1044    /// *Note*: This method will fetch the members from the homeserver if the
1045    /// member list isn't synchronized due to member lazy loading. Because of
1046    /// that it might panic if it isn't run on a tokio thread.
1047    ///
1048    /// Use [get_member_no_sync()](#method.get_member_no_sync) if you want a
1049    /// method that doesn't do any requests.
1050    ///
1051    /// # Arguments
1052    ///
1053    /// * `user_id` - The ID of the user that should be fetched out of the
1054    ///   store.
1055    pub async fn get_member(&self, user_id: &UserId) -> Result<Option<RoomMember>> {
1056        self.sync_members().await?;
1057        self.get_member_no_sync(user_id).await
1058    }
1059
1060    /// Get a specific member of this room.
1061    ///
1062    /// *Note*: This method will not fetch the members from the homeserver if
1063    /// the member list isn't synchronized due to member lazy loading. Thus,
1064    /// members could be missing.
1065    ///
1066    /// Use [get_member()](#method.get_member) if you want to ensure to always
1067    /// have the full member list to chose from.
1068    ///
1069    /// # Arguments
1070    ///
1071    /// * `user_id` - The ID of the user that should be fetched out of the
1072    ///   store.
1073    pub async fn get_member_no_sync(&self, user_id: &UserId) -> Result<Option<RoomMember>> {
1074        Ok(self
1075            .inner
1076            .get_member(user_id)
1077            .await?
1078            .map(|member| RoomMember::new(self.client.clone(), member)))
1079    }
1080
1081    /// Get members for this room, with the given memberships.
1082    ///
1083    /// *Note*: This method will fetch the members from the homeserver if the
1084    /// member list isn't synchronized due to member lazy loading. Because of
1085    /// that it might panic if it isn't run on a tokio thread.
1086    ///
1087    /// Use [members_no_sync()](#method.members_no_sync) if you want a
1088    /// method that doesn't do any requests.
1089    pub async fn members(&self, memberships: RoomMemberships) -> Result<Vec<RoomMember>> {
1090        self.sync_members().await?;
1091        self.members_no_sync(memberships).await
1092    }
1093
1094    /// Get members for this room, with the given memberships.
1095    ///
1096    /// *Note*: This method will not fetch the members from the homeserver if
1097    /// the member list isn't synchronized due to member lazy loading. Thus,
1098    /// members could be missing.
1099    ///
1100    /// Use [members()](#method.members) if you want to ensure to always get
1101    /// the full member list.
1102    pub async fn members_no_sync(&self, memberships: RoomMemberships) -> Result<Vec<RoomMember>> {
1103        Ok(self
1104            .inner
1105            .members(memberships)
1106            .await?
1107            .into_iter()
1108            .map(|member| RoomMember::new(self.client.clone(), member))
1109            .collect())
1110    }
1111
1112    /// Get all state events of a given type in this room.
1113    pub async fn get_state_events(
1114        &self,
1115        event_type: StateEventType,
1116    ) -> Result<Vec<RawAnySyncOrStrippedState>> {
1117        self.client
1118            .state_store()
1119            .get_state_events(self.room_id(), event_type)
1120            .await
1121            .map_err(Into::into)
1122    }
1123
1124    /// Get all state events of a given statically-known type in this room.
1125    ///
1126    /// # Examples
1127    ///
1128    /// ```no_run
1129    /// # async {
1130    /// # let room: matrix_sdk::Room = todo!();
1131    /// use matrix_sdk::ruma::{
1132    ///     events::room::member::RoomMemberEventContent, serde::Raw,
1133    /// };
1134    ///
1135    /// let room_members =
1136    ///     room.get_state_events_static::<RoomMemberEventContent>().await?;
1137    /// # anyhow::Ok(())
1138    /// # };
1139    /// ```
1140    pub async fn get_state_events_static<C>(&self) -> Result<Vec<RawSyncOrStrippedState<C>>>
1141    where
1142        C: StaticEventContent<IsPrefix = ruma::events::False>
1143            + StaticStateEventContent
1144            + RedactContent,
1145        C::Redacted: RedactedStateEventContent,
1146    {
1147        Ok(self.client.state_store().get_state_events_static(self.room_id()).await?)
1148    }
1149
1150    /// Get the state events of a given type with the given state keys in this
1151    /// room.
1152    pub async fn get_state_events_for_keys(
1153        &self,
1154        event_type: StateEventType,
1155        state_keys: &[&str],
1156    ) -> Result<Vec<RawAnySyncOrStrippedState>> {
1157        self.client
1158            .state_store()
1159            .get_state_events_for_keys(self.room_id(), event_type, state_keys)
1160            .await
1161            .map_err(Into::into)
1162    }
1163
1164    /// Get the state events of a given statically-known type with the given
1165    /// state keys in this room.
1166    ///
1167    /// # Examples
1168    ///
1169    /// ```no_run
1170    /// # async {
1171    /// # let room: matrix_sdk::Room = todo!();
1172    /// # let user_ids: &[matrix_sdk::ruma::OwnedUserId] = &[];
1173    /// use matrix_sdk::ruma::events::room::member::RoomMemberEventContent;
1174    ///
1175    /// let room_members = room
1176    ///     .get_state_events_for_keys_static::<RoomMemberEventContent, _, _>(
1177    ///         user_ids,
1178    ///     )
1179    ///     .await?;
1180    /// # anyhow::Ok(())
1181    /// # };
1182    /// ```
1183    pub async fn get_state_events_for_keys_static<'a, C, K, I>(
1184        &self,
1185        state_keys: I,
1186    ) -> Result<Vec<RawSyncOrStrippedState<C>>>
1187    where
1188        C: StaticEventContent<IsPrefix = ruma::events::False>
1189            + StaticStateEventContent
1190            + RedactContent,
1191        C::StateKey: Borrow<K>,
1192        C::Redacted: RedactedStateEventContent,
1193        K: AsRef<str> + Sized + Sync + 'a,
1194        I: IntoIterator<Item = &'a K> + Send,
1195        I::IntoIter: Send,
1196    {
1197        Ok(self
1198            .client
1199            .state_store()
1200            .get_state_events_for_keys_static(self.room_id(), state_keys)
1201            .await?)
1202    }
1203
1204    /// Get a specific state event in this room.
1205    pub async fn get_state_event(
1206        &self,
1207        event_type: StateEventType,
1208        state_key: &str,
1209    ) -> Result<Option<RawAnySyncOrStrippedState>> {
1210        self.client
1211            .state_store()
1212            .get_state_event(self.room_id(), event_type, state_key)
1213            .await
1214            .map_err(Into::into)
1215    }
1216
1217    /// Get a specific state event of statically-known type with an empty state
1218    /// key in this room.
1219    ///
1220    /// # Examples
1221    ///
1222    /// ```no_run
1223    /// # async {
1224    /// # let room: matrix_sdk::Room = todo!();
1225    /// use matrix_sdk::ruma::events::room::power_levels::RoomPowerLevelsEventContent;
1226    ///
1227    /// let power_levels = room
1228    ///     .get_state_event_static::<RoomPowerLevelsEventContent>()
1229    ///     .await?
1230    ///     .expect("every room has a power_levels event")
1231    ///     .deserialize()?;
1232    /// # anyhow::Ok(())
1233    /// # };
1234    /// ```
1235    pub async fn get_state_event_static<C>(&self) -> Result<Option<RawSyncOrStrippedState<C>>>
1236    where
1237        C: StaticEventContent<IsPrefix = ruma::events::False>
1238            + StaticStateEventContent<StateKey = EmptyStateKey>
1239            + RedactContent,
1240        C::Redacted: RedactedStateEventContent,
1241    {
1242        self.get_state_event_static_for_key(&EmptyStateKey).await
1243    }
1244
1245    /// Get a specific state event of statically-known type in this room.
1246    ///
1247    /// # Examples
1248    ///
1249    /// ```no_run
1250    /// # async {
1251    /// # let room: matrix_sdk::Room = todo!();
1252    /// use matrix_sdk::ruma::{
1253    ///     events::room::member::RoomMemberEventContent, serde::Raw, user_id,
1254    /// };
1255    ///
1256    /// let member_event = room
1257    ///     .get_state_event_static_for_key::<RoomMemberEventContent, _>(user_id!(
1258    ///         "@alice:example.org"
1259    ///     ))
1260    ///     .await?;
1261    /// # anyhow::Ok(())
1262    /// # };
1263    /// ```
1264    pub async fn get_state_event_static_for_key<C, K>(
1265        &self,
1266        state_key: &K,
1267    ) -> Result<Option<RawSyncOrStrippedState<C>>>
1268    where
1269        C: StaticEventContent<IsPrefix = ruma::events::False>
1270            + StaticStateEventContent
1271            + RedactContent,
1272        C::StateKey: Borrow<K>,
1273        C::Redacted: RedactedStateEventContent,
1274        K: AsRef<str> + ?Sized + Sync,
1275    {
1276        Ok(self
1277            .client
1278            .state_store()
1279            .get_state_event_static_for_key(self.room_id(), state_key)
1280            .await?)
1281    }
1282
1283    /// Returns the parents this room advertises as its parents.
1284    ///
1285    /// Results are in no particular order.
1286    pub async fn parent_spaces(&self) -> Result<impl Stream<Item = Result<ParentSpace>> + '_> {
1287        // Implements this algorithm:
1288        // https://spec.matrix.org/v1.8/client-server-api/#mspaceparent-relationships
1289
1290        // Get all m.space.parent events for this room
1291        Ok(self
1292            .get_state_events_static::<SpaceParentEventContent>()
1293            .await?
1294            .into_iter()
1295            // Extract state key (ie. the parent's id) and sender
1296            .filter_map(|parent_event| match parent_event.deserialize() {
1297                Ok(SyncOrStrippedState::Sync(SyncStateEvent::Original(e))) => {
1298                    Some((e.state_key.to_owned(), e.sender))
1299                }
1300                Ok(SyncOrStrippedState::Sync(SyncStateEvent::Redacted(_))) => None,
1301                Ok(SyncOrStrippedState::Stripped(e)) => Some((e.state_key.to_owned(), e.sender)),
1302                Err(e) => {
1303                    info!(room_id = ?self.room_id(), "Could not deserialize m.space.parent: {e}");
1304                    None
1305                }
1306            })
1307            // Check whether the parent recognizes this room as its child
1308            .map(|(state_key, sender): (OwnedRoomId, OwnedUserId)| async move {
1309                let Some(parent_room) = self.client.get_room(&state_key) else {
1310                    // We are not in the room, cannot check if the relationship is reciprocal
1311                    // TODO: try peeking into the room
1312                    return Ok(ParentSpace::Unverifiable(state_key));
1313                };
1314                // Get the m.space.child state of the parent with this room's id
1315                // as state key.
1316                if let Some(child_event) = parent_room
1317                    .get_state_event_static_for_key::<SpaceChildEventContent, _>(self.room_id())
1318                    .await?
1319                {
1320                    match child_event.deserialize() {
1321                        Ok(SyncOrStrippedState::Sync(SyncStateEvent::Original(_))) => {
1322                            // There is a valid m.space.child in the parent pointing to
1323                            // this room
1324                            return Ok(ParentSpace::Reciprocal(parent_room));
1325                        }
1326                        Ok(SyncOrStrippedState::Sync(SyncStateEvent::Redacted(_))) => {}
1327                        Ok(SyncOrStrippedState::Stripped(_)) => {}
1328                        Err(e) => {
1329                            info!(
1330                                room_id = ?self.room_id(), parent_room_id = ?state_key,
1331                                "Could not deserialize m.space.child: {e}"
1332                            );
1333                        }
1334                    }
1335                    // Otherwise the event is either invalid or redacted. If
1336                    // redacted it would be missing the
1337                    // `via` key, thereby invalidating that end of the
1338                    // relationship: https://spec.matrix.org/v1.8/client-server-api/#mspacechild
1339                }
1340
1341                // No reciprocal m.space.child found, let's check if the sender has the
1342                // power to set it
1343                let Some(member) = parent_room.get_member(&sender).await? else {
1344                    // Sender is not even in the parent room
1345                    return Ok(ParentSpace::Illegitimate(parent_room));
1346                };
1347
1348                if member.can_send_state(StateEventType::SpaceChild) {
1349                    // Sender does have the power to set m.room.child
1350                    Ok(ParentSpace::WithPowerlevel(parent_room))
1351                } else {
1352                    Ok(ParentSpace::Illegitimate(parent_room))
1353                }
1354            })
1355            .collect::<FuturesUnordered<_>>())
1356    }
1357
1358    /// Read account data in this room, from storage.
1359    pub async fn account_data(
1360        &self,
1361        data_type: RoomAccountDataEventType,
1362    ) -> Result<Option<Raw<AnyRoomAccountDataEvent>>> {
1363        self.client
1364            .state_store()
1365            .get_room_account_data_event(self.room_id(), data_type)
1366            .await
1367            .map_err(Into::into)
1368    }
1369
1370    /// Get account data of a statically-known type in this room, from storage.
1371    ///
1372    /// # Examples
1373    ///
1374    /// ```no_run
1375    /// # async {
1376    /// # let room: matrix_sdk::Room = todo!();
1377    /// use matrix_sdk::ruma::events::fully_read::FullyReadEventContent;
1378    ///
1379    /// match room.account_data_static::<FullyReadEventContent>().await? {
1380    ///     Some(fully_read) => {
1381    ///         println!("Found read marker: {:?}", fully_read.deserialize()?)
1382    ///     }
1383    ///     None => println!("No read marker for this room"),
1384    /// }
1385    /// # anyhow::Ok(())
1386    /// # };
1387    /// ```
1388    pub async fn account_data_static<C>(&self) -> Result<Option<Raw<RoomAccountDataEvent<C>>>>
1389    where
1390        C: StaticEventContent<IsPrefix = ruma::events::False> + RoomAccountDataEventContent,
1391    {
1392        Ok(self.account_data(C::TYPE.into()).await?.map(Raw::cast_unchecked))
1393    }
1394
1395    /// Check if all members of this room are verified and all their devices are
1396    /// verified.
1397    ///
1398    /// Returns true if all devices in the room are verified, otherwise false.
1399    #[cfg(feature = "e2e-encryption")]
1400    pub async fn contains_only_verified_devices(&self) -> Result<bool> {
1401        let user_ids = self
1402            .client
1403            .state_store()
1404            .get_user_ids(self.room_id(), RoomMemberships::empty())
1405            .await?;
1406
1407        for user_id in user_ids {
1408            let devices = self.client.encryption().get_user_devices(&user_id).await?;
1409            let any_unverified = devices.devices().any(|d| !d.is_verified());
1410
1411            if any_unverified {
1412                return Ok(false);
1413            }
1414        }
1415
1416        Ok(true)
1417    }
1418
1419    /// Set the given account data event for this room.
1420    ///
1421    /// # Example
1422    /// ```
1423    /// # async {
1424    /// # let room: matrix_sdk::Room = todo!();
1425    /// # let event_id: ruma::OwnedEventId = todo!();
1426    /// use matrix_sdk::ruma::events::fully_read::FullyReadEventContent;
1427    /// let content = FullyReadEventContent::new(event_id);
1428    ///
1429    /// room.set_account_data(content).await?;
1430    /// # anyhow::Ok(())
1431    /// # };
1432    /// ```
1433    pub async fn set_account_data<T>(
1434        &self,
1435        content: T,
1436    ) -> Result<set_room_account_data::v3::Response>
1437    where
1438        T: RoomAccountDataEventContent,
1439    {
1440        let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
1441
1442        let request = set_room_account_data::v3::Request::new(
1443            own_user.to_owned(),
1444            self.room_id().to_owned(),
1445            &content,
1446        )?;
1447
1448        Ok(self.client.send(request).await?)
1449    }
1450
1451    /// Set the given raw account data event in this room.
1452    ///
1453    /// # Example
1454    /// ```
1455    /// # async {
1456    /// # let room: matrix_sdk::Room = todo!();
1457    /// use matrix_sdk::ruma::{
1458    ///     events::{
1459    ///         AnyRoomAccountDataEventContent, RoomAccountDataEventContent,
1460    ///         marked_unread::MarkedUnreadEventContent,
1461    ///     },
1462    ///     serde::Raw,
1463    /// };
1464    /// let marked_unread_content = MarkedUnreadEventContent::new(true);
1465    /// let full_event: AnyRoomAccountDataEventContent =
1466    ///     marked_unread_content.clone().into();
1467    /// room.set_account_data_raw(
1468    ///     marked_unread_content.event_type(),
1469    ///     Raw::new(&full_event).unwrap(),
1470    /// )
1471    /// .await?;
1472    /// # anyhow::Ok(())
1473    /// # };
1474    /// ```
1475    pub async fn set_account_data_raw(
1476        &self,
1477        event_type: RoomAccountDataEventType,
1478        content: Raw<AnyRoomAccountDataEventContent>,
1479    ) -> Result<set_room_account_data::v3::Response> {
1480        let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
1481
1482        let request = set_room_account_data::v3::Request::new_raw(
1483            own_user.to_owned(),
1484            self.room_id().to_owned(),
1485            event_type,
1486            content,
1487        );
1488
1489        Ok(self.client.send(request).await?)
1490    }
1491
1492    /// Adds a tag to the room, or updates it if it already exists.
1493    ///
1494    /// Returns the [`create_tag::v3::Response`] from the server.
1495    ///
1496    /// # Arguments
1497    /// * `tag` - The tag to add or update.
1498    ///
1499    /// * `tag_info` - Information about the tag, generally containing the
1500    ///   `order` parameter.
1501    ///
1502    /// # Examples
1503    ///
1504    /// ```no_run
1505    /// # use std::str::FromStr;
1506    /// # use ruma::events::tag::{TagInfo, TagName, UserTagName};
1507    /// # async {
1508    /// # let homeserver = url::Url::parse("http://localhost:8080")?;
1509    /// # let mut client = matrix_sdk::Client::new(homeserver).await?;
1510    /// # let room_id = matrix_sdk::ruma::room_id!("!test:localhost");
1511    /// use matrix_sdk::ruma::events::tag::TagInfo;
1512    ///
1513    /// if let Some(room) = client.get_room(&room_id) {
1514    ///     let mut tag_info = TagInfo::new();
1515    ///     tag_info.order = Some(0.9);
1516    ///     let user_tag = UserTagName::from_str("u.work")?;
1517    ///
1518    ///     room.set_tag(TagName::User(user_tag), tag_info).await?;
1519    /// }
1520    /// # anyhow::Ok(()) };
1521    /// ```
1522    pub async fn set_tag(
1523        &self,
1524        tag: TagName,
1525        tag_info: TagInfo,
1526    ) -> Result<create_tag::v3::Response> {
1527        let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
1528        let request = create_tag::v3::Request::new(
1529            user_id.to_owned(),
1530            self.inner.room_id().to_owned(),
1531            tag.to_string(),
1532            tag_info,
1533        );
1534        Ok(self.client.send(request).await?)
1535    }
1536
1537    /// Removes a tag from the room.
1538    ///
1539    /// Returns the [`delete_tag::v3::Response`] from the server.
1540    ///
1541    /// # Arguments
1542    /// * `tag` - The tag to remove.
1543    pub async fn remove_tag(&self, tag: TagName) -> Result<delete_tag::v3::Response> {
1544        let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
1545        let request = delete_tag::v3::Request::new(
1546            user_id.to_owned(),
1547            self.inner.room_id().to_owned(),
1548            tag.to_string(),
1549        );
1550        Ok(self.client.send(request).await?)
1551    }
1552
1553    /// Add or remove the `m.favourite` flag for this room.
1554    ///
1555    /// If `is_favourite` is `true`, and the `m.low_priority` tag is set on the
1556    /// room, the tag will be removed too.
1557    ///
1558    /// # Arguments
1559    ///
1560    /// * `is_favourite` - Whether to mark this room as favourite.
1561    /// * `tag_order` - The order of the tag if any.
1562    pub async fn set_is_favourite(&self, is_favourite: bool, tag_order: Option<f64>) -> Result<()> {
1563        if is_favourite {
1564            let tag_info = assign!(TagInfo::new(), { order: tag_order });
1565
1566            self.set_tag(TagName::Favorite, tag_info).await?;
1567
1568            if self.is_low_priority() {
1569                self.remove_tag(TagName::LowPriority).await?;
1570            }
1571        } else {
1572            self.remove_tag(TagName::Favorite).await?;
1573        }
1574        Ok(())
1575    }
1576
1577    /// Add or remove the `m.lowpriority` flag for this room.
1578    ///
1579    /// If `is_low_priority` is `true`, and the `m.favourite` tag is set on the
1580    /// room, the tag will be removed too.
1581    ///
1582    /// # Arguments
1583    ///
1584    /// * `is_low_priority` - Whether to mark this room as low_priority or not.
1585    /// * `tag_order` - The order of the tag if any.
1586    pub async fn set_is_low_priority(
1587        &self,
1588        is_low_priority: bool,
1589        tag_order: Option<f64>,
1590    ) -> Result<()> {
1591        if is_low_priority {
1592            let tag_info = assign!(TagInfo::new(), { order: tag_order });
1593
1594            self.set_tag(TagName::LowPriority, tag_info).await?;
1595
1596            if self.is_favourite() {
1597                self.remove_tag(TagName::Favorite).await?;
1598            }
1599        } else {
1600            self.remove_tag(TagName::LowPriority).await?;
1601        }
1602        Ok(())
1603    }
1604
1605    /// Sets whether this room is a DM.
1606    ///
1607    /// When setting this room as DM, it will be marked as DM for all active
1608    /// members of the room. When unsetting this room as DM, it will be
1609    /// unmarked as DM for all users, not just the members.
1610    ///
1611    /// # Arguments
1612    /// * `is_direct` - Whether to mark this room as direct.
1613    pub async fn set_is_direct(&self, is_direct: bool) -> Result<()> {
1614        let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
1615
1616        let mut content = self
1617            .client
1618            .account()
1619            .account_data::<DirectEventContent>()
1620            .await?
1621            .map(|c| c.deserialize())
1622            .transpose()?
1623            .unwrap_or_default();
1624
1625        let this_room_id = self.inner.room_id();
1626
1627        if is_direct {
1628            let mut room_members = self.members(RoomMemberships::ACTIVE).await?;
1629            room_members.retain(|member| member.user_id() != self.own_user_id());
1630
1631            for member in room_members {
1632                let entry = content.entry(member.user_id().into()).or_default();
1633                if !entry.iter().any(|room_id| room_id == this_room_id) {
1634                    entry.push(this_room_id.to_owned());
1635                }
1636            }
1637        } else {
1638            for (_, list) in content.iter_mut() {
1639                list.retain(|room_id| *room_id != this_room_id);
1640            }
1641
1642            // Remove user ids that don't have any room marked as DM
1643            content.retain(|_, list| !list.is_empty());
1644        }
1645
1646        let request = set_global_account_data::v3::Request::new(user_id.to_owned(), &content)?;
1647
1648        self.client.send(request).await?;
1649        Ok(())
1650    }
1651
1652    /// Tries to decrypt a room event.
1653    ///
1654    /// # Arguments
1655    /// * `event` - The room event to be decrypted.
1656    ///
1657    /// Returns the decrypted event. In the case of a decryption error, returns
1658    /// a `TimelineEvent` representing the decryption error.
1659    #[cfg(feature = "e2e-encryption")]
1660    #[cfg(not(feature = "experimental-encrypted-state-events"))]
1661    pub async fn decrypt_event(
1662        &self,
1663        event: &Raw<OriginalSyncRoomEncryptedEvent>,
1664        push_ctx: Option<&PushContext>,
1665    ) -> Result<TimelineEvent> {
1666        let machine = self.client.olm_machine().await;
1667        let machine = machine.as_ref().ok_or(Error::NoOlmMachine)?;
1668
1669        match machine
1670            .try_decrypt_room_event(
1671                event.cast_ref(),
1672                self.inner.room_id(),
1673                self.client.decryption_settings(),
1674            )
1675            .await?
1676        {
1677            RoomEventDecryptionResult::Decrypted(decrypted) => {
1678                let push_actions = if let Some(push_ctx) = push_ctx {
1679                    Some(push_ctx.for_event(&decrypted.event).await)
1680                } else {
1681                    None
1682                };
1683                Ok(TimelineEvent::from_decrypted(decrypted, push_actions))
1684            }
1685            RoomEventDecryptionResult::UnableToDecrypt(utd_info) => {
1686                self.client
1687                    .encryption()
1688                    .backups()
1689                    .maybe_download_room_key(self.room_id().to_owned(), event.clone());
1690                Ok(TimelineEvent::from_utd(event.clone().cast(), utd_info))
1691            }
1692        }
1693    }
1694
1695    /// Tries to decrypt a room event.
1696    ///
1697    /// # Arguments
1698    /// * `event` - The room event to be decrypted.
1699    ///
1700    /// Returns the decrypted event. In the case of a decryption error, returns
1701    /// a `TimelineEvent` representing the decryption error.
1702    #[cfg(feature = "experimental-encrypted-state-events")]
1703    pub async fn decrypt_event<T: JsonCastable<EncryptedEvent>>(
1704        &self,
1705        event: &Raw<T>,
1706        push_ctx: Option<&PushContext>,
1707    ) -> Result<TimelineEvent> {
1708        let machine = self.client.olm_machine().await;
1709        let machine = machine.as_ref().ok_or(Error::NoOlmMachine)?;
1710
1711        match machine
1712            .try_decrypt_room_event(
1713                event.cast_ref(),
1714                self.inner.room_id(),
1715                self.client.decryption_settings(),
1716            )
1717            .await?
1718        {
1719            RoomEventDecryptionResult::Decrypted(decrypted) => {
1720                let push_actions = if let Some(push_ctx) = push_ctx {
1721                    Some(push_ctx.for_event(&decrypted.event).await)
1722                } else {
1723                    None
1724                };
1725                Ok(TimelineEvent::from_decrypted(decrypted, push_actions))
1726            }
1727            RoomEventDecryptionResult::UnableToDecrypt(utd_info) => {
1728                self.client
1729                    .encryption()
1730                    .backups()
1731                    .maybe_download_room_key(self.room_id().to_owned(), event.clone());
1732                // Cast safety: Anything that can be cast to EncryptedEvent must be a timeline
1733                // event.
1734                Ok(TimelineEvent::from_utd(event.clone().cast_unchecked(), utd_info))
1735            }
1736        }
1737    }
1738
1739    /// Fetches the [`EncryptionInfo`] for an event decrypted with the supplied
1740    /// session_id.
1741    ///
1742    /// This may be used when we receive an update for a session, and we want to
1743    /// reflect the changes in messages we have received that were encrypted
1744    /// with that session, e.g. to remove a warning shield because a device is
1745    /// now verified.
1746    ///
1747    /// # Arguments
1748    /// * `session_id` - The ID of the Megolm session to get information for.
1749    /// * `sender` - The (claimed) sender of the event where the session was
1750    ///   used.
1751    #[cfg(feature = "e2e-encryption")]
1752    pub async fn get_encryption_info(
1753        &self,
1754        session_id: &str,
1755        sender: &UserId,
1756    ) -> Option<Arc<EncryptionInfo>> {
1757        let machine = self.client.olm_machine().await;
1758        let machine = machine.as_ref()?;
1759        machine.get_session_encryption_info(self.room_id(), session_id, sender).await.ok()
1760    }
1761
1762    /// Forces the currently active room key, which is used to encrypt messages,
1763    /// to be rotated.
1764    ///
1765    /// A new room key will be crated and shared with all the room members the
1766    /// next time a message will be sent. You don't have to call this method,
1767    /// room keys will be rotated automatically when necessary. This method is
1768    /// still useful for debugging purposes.
1769    ///
1770    /// For more info please take a look a the [`encryption`] module
1771    /// documentation.
1772    ///
1773    /// [`encryption`]: crate::encryption
1774    #[cfg(feature = "e2e-encryption")]
1775    pub async fn discard_room_key(&self) -> Result<()> {
1776        let machine = self.client.olm_machine().await;
1777        if let Some(machine) = machine.as_ref() {
1778            machine.discard_room_key(self.inner.room_id()).await?;
1779            Ok(())
1780        } else {
1781            Err(Error::NoOlmMachine)
1782        }
1783    }
1784
1785    /// Ban the user with `UserId` from this room.
1786    ///
1787    /// # Arguments
1788    ///
1789    /// * `user_id` - The user to ban with `UserId`.
1790    ///
1791    /// * `reason` - The reason for banning this user.
1792    #[instrument(skip_all)]
1793    pub async fn ban_user(&self, user_id: &UserId, reason: Option<&str>) -> Result<()> {
1794        let request = assign!(
1795            ban_user::v3::Request::new(self.room_id().to_owned(), user_id.to_owned()),
1796            { reason: reason.map(ToOwned::to_owned) }
1797        );
1798        self.client.send(request).await?;
1799        Ok(())
1800    }
1801
1802    /// Unban the user with `UserId` from this room.
1803    ///
1804    /// # Arguments
1805    ///
1806    /// * `user_id` - The user to unban with `UserId`.
1807    ///
1808    /// * `reason` - The reason for unbanning this user.
1809    #[instrument(skip_all)]
1810    pub async fn unban_user(&self, user_id: &UserId, reason: Option<&str>) -> Result<()> {
1811        let request = assign!(
1812            unban_user::v3::Request::new(self.room_id().to_owned(), user_id.to_owned()),
1813            { reason: reason.map(ToOwned::to_owned) }
1814        );
1815        self.client.send(request).await?;
1816        Ok(())
1817    }
1818
1819    /// Kick a user out of this room.
1820    ///
1821    /// # Arguments
1822    ///
1823    /// * `user_id` - The `UserId` of the user that should be kicked out of the
1824    ///   room.
1825    ///
1826    /// * `reason` - Optional reason why the room member is being kicked out.
1827    #[instrument(skip_all)]
1828    pub async fn kick_user(&self, user_id: &UserId, reason: Option<&str>) -> Result<()> {
1829        let request = assign!(
1830            kick_user::v3::Request::new(self.room_id().to_owned(), user_id.to_owned()),
1831            { reason: reason.map(ToOwned::to_owned) }
1832        );
1833        self.client.send(request).await?;
1834        Ok(())
1835    }
1836
1837    /// Invite the specified user by `UserId` to this room.
1838    ///
1839    /// # Arguments
1840    ///
1841    /// * `user_id` - The `UserId` of the user to invite to the room.
1842    #[instrument(skip_all)]
1843    pub async fn invite_user_by_id(&self, user_id: &UserId) -> Result<()> {
1844        #[cfg(feature = "e2e-encryption")]
1845        if self.client.inner.enable_share_history_on_invite {
1846            shared_room_history::share_room_history(self, user_id.to_owned()).await?;
1847        }
1848
1849        let recipient = InvitationRecipient::UserId { user_id: user_id.to_owned() };
1850        let request = invite_user::v3::Request::new(self.room_id().to_owned(), recipient);
1851        self.client.send(request).await?;
1852
1853        // Force a future room members reload before sending any event to prevent UTDs
1854        // that can happen when some event is sent after a room member has been invited
1855        // but before the /sync request could fetch the membership change event.
1856        self.mark_members_missing();
1857
1858        Ok(())
1859    }
1860
1861    /// Invite the specified user by third party id to this room.
1862    ///
1863    /// # Arguments
1864    ///
1865    /// * `invite_id` - A third party id of a user to invite to the room.
1866    #[instrument(skip_all)]
1867    pub async fn invite_user_by_3pid(&self, invite_id: Invite3pid) -> Result<()> {
1868        let recipient = InvitationRecipient::ThirdPartyId(invite_id);
1869        let request = invite_user::v3::Request::new(self.room_id().to_owned(), recipient);
1870        self.client.send(request).await?;
1871
1872        // Force a future room members reload before sending any event to prevent UTDs
1873        // that can happen when some event is sent after a room member has been invited
1874        // but before the /sync request could fetch the membership change event.
1875        self.mark_members_missing();
1876
1877        Ok(())
1878    }
1879
1880    /// Activate typing notice for this room.
1881    ///
1882    /// The typing notice remains active for 4s. It can be deactivate at any
1883    /// point by setting typing to `false`. If this method is called while
1884    /// the typing notice is active nothing will happen. This method can be
1885    /// called on every key stroke, since it will do nothing while typing is
1886    /// active.
1887    ///
1888    /// # Arguments
1889    ///
1890    /// * `typing` - Whether the user is typing or has stopped typing.
1891    ///
1892    /// # Examples
1893    ///
1894    /// ```no_run
1895    /// use std::time::Duration;
1896    ///
1897    /// use matrix_sdk::ruma::api::client::typing::create_typing_event::v3::Typing;
1898    /// # use matrix_sdk::{
1899    /// #     Client, config::SyncSettings,
1900    /// #     ruma::room_id,
1901    /// # };
1902    /// # use url::Url;
1903    ///
1904    /// # async {
1905    /// # let homeserver = Url::parse("http://localhost:8080")?;
1906    /// # let client = Client::new(homeserver).await?;
1907    /// let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost");
1908    ///
1909    /// if let Some(room) = client.get_room(&room_id) {
1910    ///     room.typing_notice(true).await?
1911    /// }
1912    /// # anyhow::Ok(()) };
1913    /// ```
1914    pub async fn typing_notice(&self, typing: bool) -> Result<()> {
1915        self.ensure_room_joined()?;
1916
1917        // Only send a request to the homeserver if the old timeout has elapsed
1918        // or the typing notice changed state within the `TYPING_NOTICE_TIMEOUT`
1919        let send = if let Some(typing_time) =
1920            self.client.inner.typing_notice_times.read().unwrap().get(self.room_id())
1921        {
1922            if typing_time.elapsed() > TYPING_NOTICE_RESEND_TIMEOUT {
1923                // We always reactivate the typing notice if typing is true or
1924                // we may need to deactivate it if it's
1925                // currently active if typing is false
1926                typing || typing_time.elapsed() <= TYPING_NOTICE_TIMEOUT
1927            } else {
1928                // Only send a request when we need to deactivate typing
1929                !typing
1930            }
1931        } else {
1932            // Typing notice is currently deactivated, therefore, send a request
1933            // only when it's about to be activated
1934            typing
1935        };
1936
1937        if send {
1938            self.send_typing_notice(typing).await?;
1939        }
1940
1941        Ok(())
1942    }
1943
1944    #[instrument(name = "typing_notice", skip(self))]
1945    async fn send_typing_notice(&self, typing: bool) -> Result<()> {
1946        let typing = if typing {
1947            self.client
1948                .inner
1949                .typing_notice_times
1950                .write()
1951                .unwrap()
1952                .insert(self.room_id().to_owned(), Instant::now());
1953            Typing::Yes(TYPING_NOTICE_TIMEOUT)
1954        } else {
1955            self.client.inner.typing_notice_times.write().unwrap().remove(self.room_id());
1956            Typing::No
1957        };
1958
1959        let request = create_typing_event::v3::Request::new(
1960            self.own_user_id().to_owned(),
1961            self.room_id().to_owned(),
1962            typing,
1963        );
1964
1965        self.client.send(request).await?;
1966
1967        Ok(())
1968    }
1969
1970    /// Send a request to set a single receipt.
1971    ///
1972    /// If an unthreaded receipt is sent, this will also unset the unread flag
1973    /// of the room if necessary.
1974    ///
1975    /// # Arguments
1976    ///
1977    /// * `receipt_type` - The type of the receipt to set. Note that it is
1978    ///   possible to set the fully-read marker although it is technically not a
1979    ///   receipt.
1980    ///
1981    /// * `thread` - The thread where this receipt should apply, if any. Note
1982    ///   that this must be [`ReceiptThread::Unthreaded`] when sending a
1983    ///   [`ReceiptType::FullyRead`][create_receipt::v3::ReceiptType::FullyRead].
1984    ///
1985    /// * `event_id` - The `EventId` of the event to set the receipt on.
1986    #[instrument(skip_all)]
1987    pub async fn send_single_receipt(
1988        &self,
1989        receipt_type: create_receipt::v3::ReceiptType,
1990        thread: ReceiptThread,
1991        event_id: OwnedEventId,
1992    ) -> Result<()> {
1993        // Since the receipt type and the thread aren't Hash/Ord, flatten then as a
1994        // string key.
1995        let request_key = format!("{}|{}", receipt_type, thread.as_str().unwrap_or("<unthreaded>"));
1996
1997        self.client
1998            .inner
1999            .locks
2000            .read_receipt_deduplicated_handler
2001            .run((request_key, event_id.clone()), async {
2002                // We will unset the unread flag if we send an unthreaded receipt.
2003                let is_unthreaded = thread == ReceiptThread::Unthreaded;
2004
2005                let mut request = create_receipt::v3::Request::new(
2006                    self.room_id().to_owned(),
2007                    receipt_type,
2008                    event_id,
2009                );
2010                request.thread = thread;
2011
2012                self.client.send(request).await?;
2013
2014                if is_unthreaded {
2015                    self.set_unread_flag(false).await?;
2016                }
2017
2018                Ok(())
2019            })
2020            .await
2021    }
2022
2023    /// Send a request to set multiple receipts at once.
2024    ///
2025    /// This will also unset the unread flag of the room if necessary.
2026    ///
2027    /// # Arguments
2028    ///
2029    /// * `receipts` - The `Receipts` to send.
2030    ///
2031    /// If `receipts` is empty, this is a no-op.
2032    #[instrument(skip_all)]
2033    pub async fn send_multiple_receipts(&self, receipts: Receipts) -> Result<()> {
2034        if receipts.is_empty() {
2035            return Ok(());
2036        }
2037
2038        let Receipts { fully_read, public_read_receipt, private_read_receipt } = receipts;
2039        let request = assign!(set_read_marker::v3::Request::new(self.room_id().to_owned()), {
2040            fully_read,
2041            read_receipt: public_read_receipt,
2042            private_read_receipt,
2043        });
2044
2045        self.client.send(request).await?;
2046
2047        self.set_unread_flag(false).await?;
2048
2049        Ok(())
2050    }
2051
2052    /// Helper function to enable End-to-end encryption in this room.
2053    /// `encrypted_state_events` is not used unless the
2054    /// `experimental-encrypted-state-events` feature is enabled.
2055    #[allow(unused_variables, unused_mut)]
2056    async fn enable_encryption_inner(&self, encrypted_state_events: bool) -> Result<()> {
2057        use ruma::{
2058            EventEncryptionAlgorithm, events::room::encryption::RoomEncryptionEventContent,
2059        };
2060        const SYNC_WAIT_TIME: Duration = Duration::from_secs(3);
2061
2062        if !self.latest_encryption_state().await?.is_encrypted() {
2063            let mut content =
2064                RoomEncryptionEventContent::new(EventEncryptionAlgorithm::MegolmV1AesSha2);
2065            #[cfg(feature = "experimental-encrypted-state-events")]
2066            if encrypted_state_events {
2067                content = content.with_encrypted_state();
2068            }
2069            self.send_state_event(content).await?;
2070
2071            // Spin on the sync beat event, since the first sync we receive might not
2072            // include the encryption event.
2073            //
2074            // TODO do we want to return an error here if we time out? This
2075            // could be quite useful if someone wants to enable encryption and
2076            // send a message right after it's enabled.
2077            let res = timeout(
2078                async {
2079                    loop {
2080                        // Listen for sync events, then check if the encryption state is known.
2081                        self.client.inner.sync_beat.listen().await;
2082                        let _state_store_lock =
2083                            self.client.base_client().state_store_lock().lock().await;
2084
2085                        if !self.inner.encryption_state().is_unknown() {
2086                            break;
2087                        }
2088                    }
2089                },
2090                SYNC_WAIT_TIME,
2091            )
2092            .await;
2093
2094            let _state_store_lock = self.client.base_client().state_store_lock().lock().await;
2095
2096            // If encryption was enabled, return.
2097            #[cfg(not(feature = "experimental-encrypted-state-events"))]
2098            if res.is_ok() && self.inner.encryption_state().is_encrypted() {
2099                debug!("room successfully marked as encrypted");
2100                return Ok(());
2101            }
2102
2103            // If encryption with state event encryption was enabled, return.
2104            #[cfg(feature = "experimental-encrypted-state-events")]
2105            if res.is_ok() && {
2106                if encrypted_state_events {
2107                    self.inner.encryption_state().is_state_encrypted()
2108                } else {
2109                    self.inner.encryption_state().is_encrypted()
2110                }
2111            } {
2112                debug!("room successfully marked as encrypted");
2113                return Ok(());
2114            }
2115
2116            // If after waiting for multiple syncs, we don't have the encryption state we
2117            // expect, assume the local encryption state is incorrect; this will
2118            // cause the SDK to re-request it later for confirmation, instead of
2119            // assuming it's sync'd and correct (and not encrypted).
2120            debug!("still not marked as encrypted, marking encryption state as missing");
2121
2122            let mut room_info = self.clone_info();
2123            room_info.mark_encryption_state_missing();
2124            let mut changes = StateChanges::default();
2125            changes.add_room(room_info.clone());
2126
2127            self.client.state_store().save_changes(&changes).await?;
2128            self.set_room_info(room_info, RoomInfoNotableUpdateReasons::empty());
2129        }
2130
2131        Ok(())
2132    }
2133
2134    /// Enable End-to-end encryption in this room.
2135    ///
2136    /// This method will be a noop if encryption is already enabled, otherwise
2137    /// sends a `m.room.encryption` state event to the room. This might fail if
2138    /// you don't have the appropriate power level to enable end-to-end
2139    /// encryption.
2140    ///
2141    /// A sync needs to be received to update the local room state. This method
2142    /// will wait for a sync to be received, this might time out if no
2143    /// sync loop is running or if the server is slow.
2144    ///
2145    /// # Examples
2146    ///
2147    /// ```no_run
2148    /// # use matrix_sdk::{
2149    /// #     Client, config::SyncSettings,
2150    /// #     ruma::room_id,
2151    /// # };
2152    /// # use url::Url;
2153    /// #
2154    /// # async {
2155    /// # let homeserver = Url::parse("http://localhost:8080")?;
2156    /// # let client = Client::new(homeserver).await?;
2157    /// # let room_id = room_id!("!test:localhost");
2158    /// let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost");
2159    ///
2160    /// if let Some(room) = client.get_room(&room_id) {
2161    ///     room.enable_encryption().await?
2162    /// }
2163    /// # anyhow::Ok(()) };
2164    /// ```
2165    #[instrument(skip_all)]
2166    pub async fn enable_encryption(&self) -> Result<()> {
2167        self.enable_encryption_inner(false).await
2168    }
2169
2170    /// Enable End-to-end encryption in this room, opting into experimental
2171    /// state event encryption.
2172    ///
2173    /// This method will be a noop if encryption is already enabled, otherwise
2174    /// sends a `m.room.encryption` state event to the room. This might fail if
2175    /// you don't have the appropriate power level to enable end-to-end
2176    /// encryption.
2177    ///
2178    /// A sync needs to be received to update the local room state. This method
2179    /// will wait for a sync to be received, this might time out if no
2180    /// sync loop is running or if the server is slow.
2181    ///
2182    /// # Examples
2183    ///
2184    /// ```no_run
2185    /// # use matrix_sdk::{
2186    /// #     Client, config::SyncSettings,
2187    /// #     ruma::room_id,
2188    /// # };
2189    /// # use url::Url;
2190    /// #
2191    /// # async {
2192    /// # let homeserver = Url::parse("http://localhost:8080")?;
2193    /// # let client = Client::new(homeserver).await?;
2194    /// # let room_id = room_id!("!test:localhost");
2195    /// let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost");
2196    ///
2197    /// if let Some(room) = client.get_room(&room_id) {
2198    ///     room.enable_encryption_with_state_event_encryption().await?
2199    /// }
2200    /// # anyhow::Ok(()) };
2201    /// ```
2202    #[instrument(skip_all)]
2203    #[cfg(feature = "experimental-encrypted-state-events")]
2204    pub async fn enable_encryption_with_state_event_encryption(&self) -> Result<()> {
2205        self.enable_encryption_inner(true).await
2206    }
2207
2208    /// Share a room key with users in the given room.
2209    ///
2210    /// This will create Olm sessions with all the users/device pairs in the
2211    /// room if necessary and share a room key that can be shared with them.
2212    ///
2213    /// Does nothing if no room key needs to be shared.
2214    // TODO: expose this publicly so people can pre-share a group session if
2215    // e.g. a user starts to type a message for a room.
2216    #[cfg(feature = "e2e-encryption")]
2217    #[instrument(skip_all, fields(room_id = ?self.room_id(), store_generation))]
2218    async fn preshare_room_key(&self) -> Result<()> {
2219        self.ensure_room_joined()?;
2220
2221        // Take and release the lock on the store, if needs be.
2222        let guard = self.client.encryption().spin_lock_store(Some(60000)).await?;
2223        tracing::Span::current().record("store_generation", guard.map(|guard| guard.generation()));
2224
2225        self.client
2226            .locks()
2227            .group_session_deduplicated_handler
2228            .run(self.room_id().to_owned(), async move {
2229                {
2230                    let members = self
2231                        .client
2232                        .state_store()
2233                        .get_user_ids(self.room_id(), RoomMemberships::ACTIVE)
2234                        .await?;
2235                    self.client.claim_one_time_keys(members.iter().map(Deref::deref)).await?;
2236                };
2237
2238                let response = self.share_room_key().await;
2239
2240                // If one of the responses failed invalidate the group
2241                // session as using it would end up in undecryptable
2242                // messages.
2243                if let Err(r) = response {
2244                    let machine = self.client.olm_machine().await;
2245                    if let Some(machine) = machine.as_ref() {
2246                        machine.discard_room_key(self.room_id()).await?;
2247                    }
2248                    return Err(r);
2249                }
2250
2251                Ok(())
2252            })
2253            .await
2254    }
2255
2256    /// Share a group session for a room.
2257    ///
2258    /// # Panics
2259    ///
2260    /// Panics if the client isn't logged in.
2261    #[cfg(feature = "e2e-encryption")]
2262    #[instrument(skip_all)]
2263    async fn share_room_key(&self) -> Result<()> {
2264        self.ensure_room_joined()?;
2265
2266        let requests = self.client.base_client().share_room_key(self.room_id()).await?;
2267
2268        for request in requests {
2269            let response = self.client.send_to_device(&request).await?;
2270            self.client.mark_request_as_sent(&request.txn_id, &response).await?;
2271        }
2272
2273        Ok(())
2274    }
2275
2276    /// Wait for the room to be fully synced.
2277    ///
2278    /// This method makes sure the room that was returned when joining a room
2279    /// has been echoed back in the sync.
2280    ///
2281    /// Warning: This waits until a sync happens and does not return if no sync
2282    /// is happening. It can also return early when the room is not a joined
2283    /// room anymore.
2284    #[instrument(skip_all)]
2285    pub async fn sync_up(&self) {
2286        while !self.is_synced() && self.state() == RoomState::Joined {
2287            let wait_for_beat = self.client.inner.sync_beat.listen();
2288            // We don't care whether it's a timeout or a sync beat.
2289            let _ = timeout(wait_for_beat, Duration::from_millis(1000)).await;
2290        }
2291    }
2292
2293    /// Send a message-like event to this room.
2294    ///
2295    /// Returns the parsed response from the server.
2296    ///
2297    /// If the encryption feature is enabled this method will transparently
2298    /// encrypt the event if this room is encrypted (except for `m.reaction`
2299    /// events, which are never encrypted).
2300    ///
2301    /// **Note**: If you just want to send an event with custom JSON content to
2302    /// a room, you can use the [`send_raw()`][Self::send_raw] method for that.
2303    ///
2304    /// If you want to set a transaction ID for the event, use
2305    /// [`.with_transaction_id()`][SendMessageLikeEvent::with_transaction_id]
2306    /// on the returned value before `.await`ing it.
2307    ///
2308    /// # Arguments
2309    ///
2310    /// * `content` - The content of the message event.
2311    ///
2312    /// # Examples
2313    ///
2314    /// ```no_run
2315    /// # use std::sync::{Arc, RwLock};
2316    /// # use matrix_sdk::{Client, config::SyncSettings};
2317    /// # use url::Url;
2318    /// # use matrix_sdk::ruma::room_id;
2319    /// # use serde::{Deserialize, Serialize};
2320    /// use matrix_sdk::ruma::{
2321    ///     MilliSecondsSinceUnixEpoch, TransactionId,
2322    ///     events::{
2323    ///         macros::EventContent,
2324    ///         room::message::{RoomMessageEventContent, TextMessageEventContent},
2325    ///     },
2326    ///     uint,
2327    /// };
2328    ///
2329    /// # async {
2330    /// # let homeserver = Url::parse("http://localhost:8080")?;
2331    /// # let mut client = Client::new(homeserver).await?;
2332    /// # let room_id = room_id!("!test:localhost");
2333    /// let content = RoomMessageEventContent::text_plain("Hello world");
2334    /// let txn_id = TransactionId::new();
2335    ///
2336    /// if let Some(room) = client.get_room(&room_id) {
2337    ///     room.send(content).with_transaction_id(txn_id).await?;
2338    /// }
2339    ///
2340    /// // Custom events work too:
2341    /// #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
2342    /// #[ruma_event(type = "org.shiny_new_2fa.token", kind = MessageLike)]
2343    /// struct TokenEventContent {
2344    ///     token: String,
2345    ///     #[serde(rename = "exp")]
2346    ///     expires_at: MilliSecondsSinceUnixEpoch,
2347    /// }
2348    ///
2349    /// # fn generate_token() -> String { todo!() }
2350    /// let content = TokenEventContent {
2351    ///     token: generate_token(),
2352    ///     expires_at: {
2353    ///         let now = MilliSecondsSinceUnixEpoch::now();
2354    ///         MilliSecondsSinceUnixEpoch(now.0 + uint!(30_000))
2355    ///     },
2356    /// };
2357    ///
2358    /// if let Some(room) = client.get_room(&room_id) {
2359    ///     room.send(content).await?;
2360    /// }
2361    /// # anyhow::Ok(()) };
2362    /// ```
2363    pub fn send(&self, content: impl MessageLikeEventContent) -> SendMessageLikeEvent<'_> {
2364        SendMessageLikeEvent::new(self, content)
2365    }
2366
2367    /// Run /keys/query requests for all the non-tracked users, and for users
2368    /// with an out-of-date device list.
2369    #[cfg(feature = "e2e-encryption")]
2370    async fn query_keys_for_untracked_or_dirty_users(&self) -> Result<()> {
2371        let olm = self.client.olm_machine().await;
2372        let olm = olm.as_ref().expect("Olm machine wasn't started");
2373
2374        let members =
2375            self.client.state_store().get_user_ids(self.room_id(), RoomMemberships::ACTIVE).await?;
2376
2377        let tracked: HashMap<_, _> = olm
2378            .store()
2379            .load_tracked_users()
2380            .await?
2381            .into_iter()
2382            .map(|tracked| (tracked.user_id, tracked.dirty))
2383            .collect();
2384
2385        // A member has no unknown devices iff it was tracked *and* the tracking is
2386        // not considered dirty.
2387        let members_with_unknown_devices =
2388            members.iter().filter(|member| tracked.get(*member).is_none_or(|dirty| *dirty));
2389
2390        let (req_id, request) =
2391            olm.query_keys_for_users(members_with_unknown_devices.map(|owned| owned.borrow()));
2392
2393        if !request.device_keys.is_empty() {
2394            self.client.keys_query(&req_id, request.device_keys).await?;
2395        }
2396
2397        Ok(())
2398    }
2399
2400    /// Send a message-like event with custom JSON content to this room.
2401    ///
2402    /// Returns the parsed response from the server.
2403    ///
2404    /// If the encryption feature is enabled this method will transparently
2405    /// encrypt the event if this room is encrypted (except for `m.reaction`
2406    /// events, which are never encrypted).
2407    ///
2408    /// This method is equivalent to the [`send()`][Self::send] method but
2409    /// allows sending custom JSON payloads, e.g. constructed using the
2410    /// [`serde_json::json!()`] macro.
2411    ///
2412    /// If you want to set a transaction ID for the event, use
2413    /// [`.with_transaction_id()`][SendRawMessageLikeEvent::with_transaction_id]
2414    /// on the returned value before `.await`ing it.
2415    ///
2416    /// # Arguments
2417    ///
2418    /// * `event_type` - The type of the event.
2419    ///
2420    /// * `content` - The content of the event as a raw JSON value. The argument
2421    ///   type can be `serde_json::Value`, but also other raw JSON types; for
2422    ///   the full list check the documentation of
2423    ///   [`IntoRawMessageLikeEventContent`].
2424    ///
2425    /// # Examples
2426    ///
2427    /// ```no_run
2428    /// # use std::sync::{Arc, RwLock};
2429    /// # use matrix_sdk::{Client, config::SyncSettings};
2430    /// # use url::Url;
2431    /// # use matrix_sdk::ruma::room_id;
2432    /// # async {
2433    /// # let homeserver = Url::parse("http://localhost:8080")?;
2434    /// # let mut client = Client::new(homeserver).await?;
2435    /// # let room_id = room_id!("!test:localhost");
2436    /// use serde_json::json;
2437    ///
2438    /// if let Some(room) = client.get_room(&room_id) {
2439    ///     room.send_raw("m.room.message", json!({ "body": "Hello world" })).await?;
2440    /// }
2441    /// # anyhow::Ok(()) };
2442    /// ```
2443    #[instrument(skip_all, fields(event_type, room_id = ?self.room_id(), transaction_id, is_room_encrypted, event_id))]
2444    pub fn send_raw<'a>(
2445        &'a self,
2446        event_type: &'a str,
2447        content: impl IntoRawMessageLikeEventContent,
2448    ) -> SendRawMessageLikeEvent<'a> {
2449        // Note: the recorded instrument fields are saved in
2450        // `SendRawMessageLikeEvent::into_future`.
2451        SendRawMessageLikeEvent::new(self, event_type, content)
2452    }
2453
2454    /// Send an attachment to this room.
2455    ///
2456    /// This will upload the given data that the reader produces using the
2457    /// [`upload()`] method and post an event to the given room.
2458    /// If the room is encrypted and the encryption feature is enabled the
2459    /// upload will be encrypted.
2460    ///
2461    /// This is a convenience method that calls the
2462    /// [`upload()`] and afterwards the [`send()`].
2463    ///
2464    /// # Arguments
2465    /// * `filename` - The file name.
2466    ///
2467    /// * `content_type` - The type of the media, this will be used as the
2468    /// content-type header.
2469    ///
2470    /// * `reader` - A `Reader` that will be used to fetch the raw bytes of the
2471    /// media.
2472    ///
2473    /// * `config` - Metadata and configuration for the attachment.
2474    ///
2475    /// # Examples
2476    ///
2477    /// ```no_run
2478    /// # use std::fs;
2479    /// # use matrix_sdk::{Client, ruma::room_id, attachment::AttachmentConfig};
2480    /// # use url::Url;
2481    /// # use mime;
2482    /// # async {
2483    /// # let homeserver = Url::parse("http://localhost:8080")?;
2484    /// # let mut client = Client::new(homeserver).await?;
2485    /// # let room_id = room_id!("!test:localhost");
2486    /// let mut image = fs::read("/home/example/my-cat.jpg")?;
2487    ///
2488    /// if let Some(room) = client.get_room(&room_id) {
2489    ///     room.send_attachment(
2490    ///         "my_favorite_cat.jpg",
2491    ///         &mime::IMAGE_JPEG,
2492    ///         image,
2493    ///         AttachmentConfig::new(),
2494    ///     ).await?;
2495    /// }
2496    /// # anyhow::Ok(()) };
2497    /// ```
2498    ///
2499    /// [`upload()`]: crate::Media::upload
2500    /// [`send()`]: Self::send
2501    #[instrument(skip_all)]
2502    pub fn send_attachment<'a>(
2503        &'a self,
2504        filename: impl Into<String>,
2505        content_type: &'a Mime,
2506        data: Vec<u8>,
2507        config: AttachmentConfig,
2508    ) -> SendAttachment<'a> {
2509        SendAttachment::new(self, filename.into(), content_type, data, config)
2510    }
2511
2512    /// Prepare and send an attachment to this room.
2513    ///
2514    /// This will upload the given data that the reader produces using the
2515    /// [`upload()`](#method.upload) method and post an event to the given room.
2516    /// If the room is encrypted and the encryption feature is enabled the
2517    /// upload will be encrypted.
2518    ///
2519    /// This is a convenience method that calls the
2520    /// [`Client::upload()`](#Client::method.upload) and afterwards the
2521    /// [`send()`](#method.send).
2522    ///
2523    /// # Arguments
2524    /// * `filename` - The file name.
2525    ///
2526    /// * `content_type` - The type of the media, this will be used as the
2527    ///   content-type header.
2528    ///
2529    /// * `reader` - A `Reader` that will be used to fetch the raw bytes of the
2530    ///   media.
2531    ///
2532    /// * `config` - Metadata and configuration for the attachment.
2533    ///
2534    /// * `send_progress` - An observable to transmit forward progress about the
2535    ///   upload.
2536    ///
2537    /// * `store_in_cache` - A boolean defining whether the uploaded media will
2538    ///   be stored in the cache immediately after a successful upload.
2539    #[instrument(skip_all)]
2540    pub(super) async fn prepare_and_send_attachment<'a>(
2541        &'a self,
2542        filename: String,
2543        content_type: &'a Mime,
2544        data: Vec<u8>,
2545        mut config: AttachmentConfig,
2546        send_progress: SharedObservable<TransmissionProgress>,
2547        store_in_cache: bool,
2548    ) -> Result<send_message_event::v3::Response> {
2549        self.ensure_room_joined()?;
2550
2551        let txn_id = config.txn_id.take();
2552        let mentions = config.mentions.take();
2553
2554        let thumbnail = config.thumbnail.take();
2555
2556        // If necessary, store caching data for the thumbnail ahead of time.
2557        let thumbnail_cache_info = if store_in_cache {
2558            thumbnail
2559                .as_ref()
2560                .map(|thumbnail| (thumbnail.data.clone(), thumbnail.height, thumbnail.width))
2561        } else {
2562            None
2563        };
2564
2565        #[cfg(feature = "e2e-encryption")]
2566        let (media_source, thumbnail) = if self.latest_encryption_state().await?.is_encrypted() {
2567            self.client
2568                .upload_encrypted_media_and_thumbnail(&data, thumbnail, send_progress)
2569                .await?
2570        } else {
2571            self.client
2572                .media()
2573                .upload_plain_media_and_thumbnail(
2574                    content_type,
2575                    // TODO: get rid of this clone; wait for Ruma to use `Bytes` or something
2576                    // similar.
2577                    data.clone(),
2578                    thumbnail,
2579                    send_progress,
2580                )
2581                .await?
2582        };
2583
2584        #[cfg(not(feature = "e2e-encryption"))]
2585        let (media_source, thumbnail) = self
2586            .client
2587            .media()
2588            .upload_plain_media_and_thumbnail(content_type, data.clone(), thumbnail, send_progress)
2589            .await?;
2590
2591        if store_in_cache {
2592            let media_store_lock_guard = self.client.media_store().lock().await?;
2593
2594            // A failure to cache shouldn't prevent the whole upload from finishing
2595            // properly, so only log errors during caching.
2596
2597            debug!("caching the media");
2598            let request =
2599                MediaRequestParameters { source: media_source.clone(), format: MediaFormat::File };
2600
2601            if let Err(err) = media_store_lock_guard
2602                .add_media_content(&request, data, IgnoreMediaRetentionPolicy::No)
2603                .await
2604            {
2605                warn!("unable to cache the media after uploading it: {err}");
2606            }
2607
2608            if let Some(((data, height, width), source)) =
2609                thumbnail_cache_info.zip(thumbnail.as_ref().map(|tuple| &tuple.0))
2610            {
2611                debug!("caching the thumbnail");
2612
2613                let request = MediaRequestParameters {
2614                    source: source.clone(),
2615                    format: MediaFormat::Thumbnail(MediaThumbnailSettings::new(width, height)),
2616                };
2617
2618                if let Err(err) = media_store_lock_guard
2619                    .add_media_content(&request, data, IgnoreMediaRetentionPolicy::No)
2620                    .await
2621                {
2622                    warn!("unable to cache the media after uploading it: {err}");
2623                }
2624            }
2625        }
2626
2627        let content = self
2628            .make_media_event(
2629                Room::make_attachment_type(
2630                    content_type,
2631                    filename,
2632                    media_source,
2633                    config.caption,
2634                    config.info,
2635                    thumbnail,
2636                ),
2637                mentions,
2638                config.reply,
2639            )
2640            .await?;
2641
2642        let mut fut = self.send(content);
2643        if let Some(txn_id) = txn_id {
2644            fut = fut.with_transaction_id(txn_id);
2645        }
2646
2647        fut.await.map(|result| result.response)
2648    }
2649
2650    /// Creates the inner [`MessageType`] for an already-uploaded media file
2651    /// provided by its source.
2652    #[allow(clippy::too_many_arguments)]
2653    pub(crate) fn make_attachment_type(
2654        content_type: &Mime,
2655        filename: String,
2656        source: MediaSource,
2657        caption: Option<TextMessageEventContent>,
2658        info: Option<AttachmentInfo>,
2659        thumbnail: Option<(MediaSource, Box<ThumbnailInfo>)>,
2660    ) -> MessageType {
2661        make_media_type!(MessageType, content_type, filename, source, caption, info, thumbnail)
2662    }
2663
2664    /// Creates the [`RoomMessageEventContent`] based on the message type,
2665    /// mentions and reply information.
2666    pub(crate) async fn make_media_event(
2667        &self,
2668        msg_type: MessageType,
2669        mentions: Option<Mentions>,
2670        reply: Option<Reply>,
2671    ) -> Result<RoomMessageEventContent> {
2672        let mut content = RoomMessageEventContent::new(msg_type);
2673        if let Some(mentions) = mentions {
2674            content = content.add_mentions(mentions);
2675        }
2676        if let Some(reply) = reply {
2677            // Since we just created the event, there is no relation attached to it. Thus,
2678            // it is safe to add the reply relation without overriding anything.
2679            content = self.make_reply_event(content.into(), reply).await?;
2680        }
2681        Ok(content)
2682    }
2683
2684    /// Creates the inner [`GalleryItemType`] for an already-uploaded media file
2685    /// provided by its source.
2686    #[cfg(feature = "unstable-msc4274")]
2687    #[allow(clippy::too_many_arguments)]
2688    pub(crate) fn make_gallery_item_type(
2689        content_type: &Mime,
2690        filename: String,
2691        source: MediaSource,
2692        caption: Option<TextMessageEventContent>,
2693        info: Option<AttachmentInfo>,
2694        thumbnail: Option<(MediaSource, Box<ThumbnailInfo>)>,
2695    ) -> GalleryItemType {
2696        make_media_type!(GalleryItemType, content_type, filename, source, caption, info, thumbnail)
2697    }
2698
2699    /// Update the power levels of a select set of users of this room.
2700    ///
2701    /// Issue a `power_levels` state event request to the server, changing the
2702    /// given UserId -> Int levels. May fail if the `power_levels` aren't
2703    /// locally known yet or the server rejects the state event update, e.g.
2704    /// because of insufficient permissions. Neither permissions to update
2705    /// nor whether the data might be stale is checked prior to issuing the
2706    /// request.
2707    pub async fn update_power_levels(
2708        &self,
2709        updates: Vec<(&UserId, Int)>,
2710    ) -> Result<send_state_event::v3::Response> {
2711        let mut power_levels = self.power_levels().await?;
2712
2713        for (user_id, new_level) in updates {
2714            if new_level == power_levels.users_default {
2715                power_levels.users.remove(user_id);
2716            } else {
2717                power_levels.users.insert(user_id.to_owned(), new_level);
2718            }
2719        }
2720
2721        self.send_state_event(RoomPowerLevelsEventContent::try_from(power_levels)?).await
2722    }
2723
2724    /// Applies a set of power level changes to this room.
2725    ///
2726    /// Any values that are `None` in the given `RoomPowerLevelChanges` will
2727    /// remain unchanged.
2728    pub async fn apply_power_level_changes(&self, changes: RoomPowerLevelChanges) -> Result<()> {
2729        let mut power_levels = self.power_levels().await?;
2730        power_levels.apply(changes)?;
2731        self.send_state_event(RoomPowerLevelsEventContent::try_from(power_levels)?).await?;
2732        Ok(())
2733    }
2734
2735    /// Resets the room's power levels to the default values
2736    ///
2737    /// [spec]: https://spec.matrix.org/v1.9/client-server-api/#mroompower_levels
2738    pub async fn reset_power_levels(&self) -> Result<RoomPowerLevels> {
2739        let creators = self.creators().unwrap_or_default();
2740        let rules = self.clone_info().room_version_rules_or_default();
2741
2742        let default_power_levels =
2743            RoomPowerLevels::new(RoomPowerLevelsSource::None, &rules.authorization, creators);
2744        let changes = RoomPowerLevelChanges::from(default_power_levels);
2745        self.apply_power_level_changes(changes).await?;
2746        Ok(self.power_levels().await?)
2747    }
2748
2749    /// Gets the suggested role for the user with the provided `user_id`.
2750    ///
2751    /// This method checks the `RoomPowerLevels` events instead of loading the
2752    /// member list and looking for the member.
2753    pub async fn get_suggested_user_role(&self, user_id: &UserId) -> Result<RoomMemberRole> {
2754        let power_level = self.get_user_power_level(user_id).await?;
2755        Ok(RoomMemberRole::suggested_role_for_power_level(power_level))
2756    }
2757
2758    /// Gets the power level the user with the provided `user_id`.
2759    ///
2760    /// This method checks the `RoomPowerLevels` events instead of loading the
2761    /// member list and looking for the member.
2762    pub async fn get_user_power_level(&self, user_id: &UserId) -> Result<UserPowerLevel> {
2763        let event = self.power_levels().await?;
2764        Ok(event.for_user(user_id))
2765    }
2766
2767    /// Gets a map with the `UserId` of users with power levels other than `0`
2768    /// and this power level.
2769    pub async fn users_with_power_levels(&self) -> HashMap<OwnedUserId, i64> {
2770        let power_levels = self.power_levels().await.ok();
2771        let mut user_power_levels = HashMap::<OwnedUserId, i64>::new();
2772        if let Some(power_levels) = power_levels {
2773            for (id, level) in power_levels.users.into_iter() {
2774                user_power_levels.insert(id, level.into());
2775            }
2776        }
2777        user_power_levels
2778    }
2779
2780    /// Sets the name of this room.
2781    pub async fn set_name(&self, name: String) -> Result<send_state_event::v3::Response> {
2782        self.send_state_event(RoomNameEventContent::new(name)).await
2783    }
2784
2785    /// Sets a new topic for this room.
2786    pub async fn set_room_topic(&self, topic: &str) -> Result<send_state_event::v3::Response> {
2787        self.send_state_event(RoomTopicEventContent::new(topic.into())).await
2788    }
2789
2790    /// Sets the new avatar url for this room.
2791    ///
2792    /// # Arguments
2793    /// * `avatar_url` - The owned Matrix uri that represents the avatar
2794    /// * `info` - The optional image info that can be provided for the avatar
2795    pub async fn set_avatar_url(
2796        &self,
2797        url: &MxcUri,
2798        info: Option<avatar::ImageInfo>,
2799    ) -> Result<send_state_event::v3::Response> {
2800        self.ensure_room_joined()?;
2801
2802        let mut room_avatar_event = RoomAvatarEventContent::new();
2803        room_avatar_event.url = Some(url.to_owned());
2804        room_avatar_event.info = info.map(Box::new);
2805
2806        self.send_state_event(room_avatar_event).await
2807    }
2808
2809    /// Removes the avatar from the room
2810    pub async fn remove_avatar(&self) -> Result<send_state_event::v3::Response> {
2811        self.send_state_event(RoomAvatarEventContent::new()).await
2812    }
2813
2814    /// Uploads a new avatar for this room.
2815    ///
2816    /// # Arguments
2817    /// * `mime` - The mime type describing the data
2818    /// * `data` - The data representation of the avatar
2819    /// * `info` - The optional image info provided for the avatar, the blurhash
2820    ///   and the mimetype will always be updated
2821    pub async fn upload_avatar(
2822        &self,
2823        mime: &Mime,
2824        data: Vec<u8>,
2825        info: Option<avatar::ImageInfo>,
2826    ) -> Result<send_state_event::v3::Response> {
2827        self.ensure_room_joined()?;
2828
2829        let upload_response = self.client.media().upload(mime, data, None).await?;
2830        let mut info = info.unwrap_or_default();
2831        info.blurhash = upload_response.blurhash;
2832        info.mimetype = Some(mime.to_string());
2833
2834        self.set_avatar_url(&upload_response.content_uri, Some(info)).await
2835    }
2836
2837    /// Send a state event with an empty state key to the homeserver.
2838    ///
2839    /// For state events with a non-empty state key, see
2840    /// [`send_state_event_for_key`][Self::send_state_event_for_key].
2841    ///
2842    /// Returns the parsed response from the server.
2843    ///
2844    /// # Arguments
2845    ///
2846    /// * `content` - The content of the state event.
2847    ///
2848    /// # Examples
2849    ///
2850    /// ```no_run
2851    /// # use serde::{Deserialize, Serialize};
2852    /// # async {
2853    /// # let joined_room: matrix_sdk::Room = todo!();
2854    /// use matrix_sdk::ruma::{
2855    ///     EventEncryptionAlgorithm,
2856    ///     events::{
2857    ///         EmptyStateKey, macros::EventContent,
2858    ///         room::encryption::RoomEncryptionEventContent,
2859    ///     },
2860    /// };
2861    ///
2862    /// let encryption_event_content = RoomEncryptionEventContent::new(
2863    ///     EventEncryptionAlgorithm::MegolmV1AesSha2,
2864    /// );
2865    /// joined_room.send_state_event(encryption_event_content).await?;
2866    ///
2867    /// // Custom event:
2868    /// #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
2869    /// #[ruma_event(
2870    ///     type = "org.matrix.msc_9000.xxx",
2871    ///     kind = State,
2872    ///     state_key_type = EmptyStateKey,
2873    /// )]
2874    /// struct XxxStateEventContent {/* fields... */}
2875    ///
2876    /// let content: XxxStateEventContent = todo!();
2877    /// joined_room.send_state_event(content).await?;
2878    /// # anyhow::Ok(()) };
2879    /// ```
2880    #[cfg(not(feature = "experimental-encrypted-state-events"))]
2881    #[instrument(skip_all)]
2882    pub async fn send_state_event(
2883        &self,
2884        content: impl StateEventContent<StateKey = EmptyStateKey>,
2885    ) -> Result<send_state_event::v3::Response> {
2886        self.send_state_event_for_key(&EmptyStateKey, content).await
2887    }
2888
2889    /// Send a state event with an empty state key to the homeserver.
2890    ///
2891    /// For state events with a non-empty state key, see
2892    /// [`send_state_event_for_key`][Self::send_state_event_for_key].
2893    ///
2894    /// If the experimental state event encryption feature is enabled, this
2895    /// method will transparently encrypt the event if this room is
2896    /// encrypted (except if the event type is considered critical for the room
2897    /// to function, as outlined in [MSC4362][msc4362]).
2898    ///
2899    /// Returns the parsed response from the server.
2900    ///
2901    /// # Arguments
2902    ///
2903    /// * `content` - The content of the state event.
2904    ///
2905    /// # Examples
2906    ///
2907    /// ```no_run
2908    /// # use serde::{Deserialize, Serialize};
2909    /// # async {
2910    /// # let joined_room: matrix_sdk::Room = todo!();
2911    /// use matrix_sdk::ruma::{
2912    ///     EventEncryptionAlgorithm,
2913    ///     events::{
2914    ///         EmptyStateKey, macros::EventContent,
2915    ///         room::encryption::RoomEncryptionEventContent,
2916    ///     },
2917    /// };
2918    ///
2919    /// let encryption_event_content = RoomEncryptionEventContent::new(
2920    ///     EventEncryptionAlgorithm::MegolmV1AesSha2,
2921    /// );
2922    /// joined_room.send_state_event(encryption_event_content).await?;
2923    ///
2924    /// // Custom event:
2925    /// #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
2926    /// #[ruma_event(
2927    ///     type = "org.matrix.msc_9000.xxx",
2928    ///     kind = State,
2929    ///     state_key_type = EmptyStateKey,
2930    /// )]
2931    /// struct XxxStateEventContent {/* fields... */}
2932    ///
2933    /// let content: XxxStateEventContent = todo!();
2934    /// joined_room.send_state_event(content).await?;
2935    /// # anyhow::Ok(()) };
2936    /// ```
2937    ///
2938    /// [msc4362]: https://github.com/matrix-org/matrix-spec-proposals/blob/travis/msc/encrypted-state/proposals/4362-encrypted-state.md
2939    #[cfg(feature = "experimental-encrypted-state-events")]
2940    #[instrument(skip_all)]
2941    pub fn send_state_event<'a>(
2942        &'a self,
2943        content: impl StateEventContent<StateKey = EmptyStateKey>,
2944    ) -> SendStateEvent<'a> {
2945        self.send_state_event_for_key(&EmptyStateKey, content)
2946    }
2947
2948    /// Send a state event to the homeserver.
2949    ///
2950    /// Returns the parsed response from the server.
2951    ///
2952    /// # Arguments
2953    ///
2954    /// * `content` - The content of the state event.
2955    ///
2956    /// * `state_key` - A unique key which defines the overwriting semantics for
2957    ///   this piece of room state.
2958    ///
2959    /// # Examples
2960    ///
2961    /// ```no_run
2962    /// # use serde::{Deserialize, Serialize};
2963    /// # async {
2964    /// # let joined_room: matrix_sdk::Room = todo!();
2965    /// use matrix_sdk::ruma::{
2966    ///     events::{
2967    ///         macros::EventContent,
2968    ///         room::member::{RoomMemberEventContent, MembershipState},
2969    ///     },
2970    ///     mxc_uri,
2971    /// };
2972    ///
2973    /// let avatar_url = mxc_uri!("mxc://example.org/avatar").to_owned();
2974    /// let mut content = RoomMemberEventContent::new(MembershipState::Join);
2975    /// content.avatar_url = Some(avatar_url);
2976    ///
2977    /// joined_room.send_state_event_for_key(ruma::user_id!("@foo:bar.com"), content).await?;
2978    ///
2979    /// // Custom event:
2980    /// #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
2981    /// #[ruma_event(type = "org.matrix.msc_9000.xxx", kind = State, state_key_type = String)]
2982    /// struct XxxStateEventContent { /* fields... */ }
2983    ///
2984    /// let content: XxxStateEventContent = todo!();
2985    /// joined_room.send_state_event_for_key("foo", content).await?;
2986    /// # anyhow::Ok(()) };
2987    /// ```
2988    #[cfg(not(feature = "experimental-encrypted-state-events"))]
2989    pub async fn send_state_event_for_key<C, K>(
2990        &self,
2991        state_key: &K,
2992        content: C,
2993    ) -> Result<send_state_event::v3::Response>
2994    where
2995        C: StateEventContent,
2996        C::StateKey: Borrow<K>,
2997        K: AsRef<str> + ?Sized,
2998    {
2999        self.ensure_room_joined()?;
3000        let request =
3001            send_state_event::v3::Request::new(self.room_id().to_owned(), state_key, &content)?;
3002        let response = self.client.send(request).await?;
3003        Ok(response)
3004    }
3005
3006    /// Send a state event to the homeserver. If state encryption is enabled in
3007    /// this room, the event will be encrypted.
3008    ///
3009    /// If the experimental state event encryption feature is enabled, this
3010    /// method will transparently encrypt the event if this room is
3011    /// encrypted (except if the event type is considered critical for the room
3012    /// to function, as outlined in [MSC4362][msc4362]).
3013    ///
3014    /// Returns the parsed response from the server.
3015    ///
3016    /// # Arguments
3017    ///
3018    /// * `content` - The content of the state event.
3019    ///
3020    /// * `state_key` - A unique key which defines the overwriting semantics for
3021    ///   this piece of room state.
3022    ///
3023    /// # Examples
3024    ///
3025    /// ```no_run
3026    /// # use serde::{Deserialize, Serialize};
3027    /// # async {
3028    /// # let joined_room: matrix_sdk::Room = todo!();
3029    /// use matrix_sdk::ruma::{
3030    ///     events::{
3031    ///         macros::EventContent,
3032    ///         room::member::{RoomMemberEventContent, MembershipState},
3033    ///     },
3034    ///     mxc_uri,
3035    /// };
3036    ///
3037    /// let avatar_url = mxc_uri!("mxc://example.org/avatar").to_owned();
3038    /// let mut content = RoomMemberEventContent::new(MembershipState::Join);
3039    /// content.avatar_url = Some(avatar_url);
3040    ///
3041    /// joined_room.send_state_event_for_key(ruma::user_id!("@foo:bar.com"), content).await?;
3042    ///
3043    /// // Custom event:
3044    /// #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
3045    /// #[ruma_event(type = "org.matrix.msc_9000.xxx", kind = State, state_key_type = String)]
3046    /// struct XxxStateEventContent { /* fields... */ }
3047    ///
3048    /// let content: XxxStateEventContent = todo!();
3049    /// joined_room.send_state_event_for_key("foo", content).await?;
3050    /// # anyhow::Ok(()) };
3051    /// ```
3052    ///
3053    /// [msc4362]: https://github.com/matrix-org/matrix-spec-proposals/pull/4362
3054    #[cfg(feature = "experimental-encrypted-state-events")]
3055    pub fn send_state_event_for_key<'a, C, K>(
3056        &'a self,
3057        state_key: &K,
3058        content: C,
3059    ) -> SendStateEvent<'a>
3060    where
3061        C: StateEventContent,
3062        C::StateKey: Borrow<K>,
3063        K: AsRef<str> + ?Sized,
3064    {
3065        SendStateEvent::new(self, state_key, content)
3066    }
3067
3068    /// Send a raw room state event to the homeserver.
3069    ///
3070    /// Returns the parsed response from the server.
3071    ///
3072    /// # Arguments
3073    ///
3074    /// * `event_type` - The type of the event that we're sending out.
3075    ///
3076    /// * `state_key` - A unique key which defines the overwriting semantics for
3077    /// this piece of room state. This value is often a zero-length string.
3078    ///
3079    /// * `content` - The content of the event as a raw JSON value. The argument
3080    ///   type can be `serde_json::Value`, but also other raw JSON types; for
3081    ///   the full list check the documentation of [`IntoRawStateEventContent`].
3082    ///
3083    /// # Examples
3084    ///
3085    /// ```no_run
3086    /// use serde_json::json;
3087    ///
3088    /// # async {
3089    /// # let homeserver = url::Url::parse("http://localhost:8080")?;
3090    /// # let mut client = matrix_sdk::Client::new(homeserver).await?;
3091    /// # let room_id = matrix_sdk::ruma::room_id!("!test:localhost");
3092    ///
3093    /// if let Some(room) = client.get_room(&room_id) {
3094    ///     room.send_state_event_raw("m.room.member", "", json!({
3095    ///         "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
3096    ///         "displayname": "Alice Margatroid",
3097    ///         "membership": "join",
3098    ///     })).await?;
3099    /// }
3100    /// # anyhow::Ok(()) };
3101    /// ```
3102    #[cfg(not(feature = "experimental-encrypted-state-events"))]
3103    #[instrument(skip_all)]
3104    pub async fn send_state_event_raw(
3105        &self,
3106        event_type: &str,
3107        state_key: &str,
3108        content: impl IntoRawStateEventContent,
3109    ) -> Result<send_state_event::v3::Response> {
3110        self.ensure_room_joined()?;
3111
3112        let request = send_state_event::v3::Request::new_raw(
3113            self.room_id().to_owned(),
3114            event_type.into(),
3115            state_key.to_owned(),
3116            content.into_raw_state_event_content(),
3117        );
3118
3119        Ok(self.client.send(request).await?)
3120    }
3121
3122    /// Send a raw room state event to the homeserver.
3123    ///
3124    /// If the experimental state event encryption feature is enabled, this
3125    /// method will transparently encrypt the event if this room is
3126    /// encrypted (except if the event type is considered critical for the room
3127    /// to function, as outlined in [MSC4362][msc4362]).
3128    ///
3129    /// Returns the parsed response from the server.
3130    ///
3131    /// # Arguments
3132    ///
3133    /// * `event_type` - The type of the event that we're sending out.
3134    ///
3135    /// * `state_key` - A unique key which defines the overwriting semantics for
3136    /// this piece of room state. This value is often a zero-length string.
3137    ///
3138    /// * `content` - The content of the event as a raw JSON value. The argument
3139    ///   type can be `serde_json::Value`, but also other raw JSON types; for
3140    ///   the full list check the documentation of [`IntoRawStateEventContent`].
3141    ///
3142    /// # Examples
3143    ///
3144    /// ```no_run
3145    /// use serde_json::json;
3146    ///
3147    /// # async {
3148    /// # let homeserver = url::Url::parse("http://localhost:8080")?;
3149    /// # let mut client = matrix_sdk::Client::new(homeserver).await?;
3150    /// # let room_id = matrix_sdk::ruma::room_id!("!test:localhost");
3151    ///
3152    /// if let Some(room) = client.get_room(&room_id) {
3153    ///     room.send_state_event_raw("m.room.member", "", json!({
3154    ///         "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
3155    ///         "displayname": "Alice Margatroid",
3156    ///         "membership": "join",
3157    ///     })).await?;
3158    /// }
3159    /// # anyhow::Ok(()) };
3160    /// ```
3161    ///
3162    /// [msc4362]: https://github.com/matrix-org/matrix-spec-proposals/pull/4362
3163    #[cfg(feature = "experimental-encrypted-state-events")]
3164    #[instrument(skip_all)]
3165    pub fn send_state_event_raw<'a>(
3166        &'a self,
3167        event_type: &'a str,
3168        state_key: &'a str,
3169        content: impl IntoRawStateEventContent,
3170    ) -> SendRawStateEvent<'a> {
3171        SendRawStateEvent::new(self, event_type, state_key, content)
3172    }
3173
3174    /// Strips all information out of an event of the room.
3175    ///
3176    /// Returns the [`redact_event::v3::Response`] from the server.
3177    ///
3178    /// This cannot be undone. Users may redact their own events, and any user
3179    /// with a power level greater than or equal to the redact power level of
3180    /// the room may redact events there.
3181    ///
3182    /// # Arguments
3183    ///
3184    /// * `event_id` - The ID of the event to redact
3185    ///
3186    /// * `reason` - The reason for the event being redacted.
3187    ///
3188    /// * `txn_id` - A unique ID that can be attached to this event as
3189    /// its transaction ID. If not given one is created for the message.
3190    ///
3191    /// # Examples
3192    ///
3193    /// ```no_run
3194    /// use matrix_sdk::ruma::event_id;
3195    ///
3196    /// # async {
3197    /// # let homeserver = url::Url::parse("http://localhost:8080")?;
3198    /// # let mut client = matrix_sdk::Client::new(homeserver).await?;
3199    /// # let room_id = matrix_sdk::ruma::room_id!("!test:localhost");
3200    /// #
3201    /// if let Some(room) = client.get_room(&room_id) {
3202    ///     let event_id = event_id!("$xxxxxx:example.org");
3203    ///     let reason = Some("Indecent material");
3204    ///     room.redact(&event_id, reason, None).await?;
3205    /// }
3206    /// # anyhow::Ok(()) };
3207    /// ```
3208    #[instrument(skip_all)]
3209    pub async fn redact(
3210        &self,
3211        event_id: &EventId,
3212        reason: Option<&str>,
3213        txn_id: Option<OwnedTransactionId>,
3214    ) -> HttpResult<redact_event::v3::Response> {
3215        let txn_id = txn_id.unwrap_or_else(TransactionId::new);
3216        let request = assign!(
3217            redact_event::v3::Request::new(self.room_id().to_owned(), event_id.to_owned(), txn_id),
3218            { reason: reason.map(ToOwned::to_owned) }
3219        );
3220
3221        self.client.send(request).await
3222    }
3223
3224    /// Get a list of servers that should know this room.
3225    ///
3226    /// Uses the synced members of the room and the suggested [routing
3227    /// algorithm] from the Matrix spec.
3228    ///
3229    /// Returns at most three servers.
3230    ///
3231    /// [routing algorithm]: https://spec.matrix.org/v1.3/appendices/#routing
3232    pub async fn route(&self) -> Result<Vec<OwnedServerName>> {
3233        let acl_ev = self
3234            .get_state_event_static::<RoomServerAclEventContent>()
3235            .await?
3236            .and_then(|ev| ev.deserialize().ok());
3237        let acl = acl_ev.as_ref().and_then(|ev| match ev {
3238            SyncOrStrippedState::Sync(ev) => ev.as_original().map(|ev| &ev.content),
3239            SyncOrStrippedState::Stripped(ev) => Some(&ev.content),
3240        });
3241
3242        // Filter out server names that:
3243        // - Are blocked due to server ACLs
3244        // - Are IP addresses
3245        let members: Vec<_> = self
3246            .members_no_sync(RoomMemberships::JOIN)
3247            .await?
3248            .into_iter()
3249            .filter(|member| {
3250                let server = member.user_id().server_name();
3251                acl.filter(|acl| !acl.is_allowed(server)).is_none() && !server.is_ip_literal()
3252            })
3253            .collect();
3254
3255        // Get the server of the highest power level user in the room, provided
3256        // they are at least power level 50.
3257        let max = members
3258            .iter()
3259            .max_by_key(|member| member.power_level())
3260            .filter(|max| max.power_level() >= int!(50))
3261            .map(|member| member.user_id().server_name());
3262
3263        // Sort the servers by population.
3264        let servers = members
3265            .iter()
3266            .map(|member| member.user_id().server_name())
3267            .filter(|server| max.filter(|max| max == server).is_none())
3268            .fold(BTreeMap::<_, u32>::new(), |mut servers, server| {
3269                *servers.entry(server).or_default() += 1;
3270                servers
3271            });
3272        let mut servers: Vec<_> = servers.into_iter().collect();
3273        servers.sort_unstable_by(|(_, count_a), (_, count_b)| count_b.cmp(count_a));
3274
3275        Ok(max
3276            .into_iter()
3277            .chain(servers.into_iter().map(|(name, _)| name))
3278            .take(3)
3279            .map(ToOwned::to_owned)
3280            .collect())
3281    }
3282
3283    /// Get a `matrix.to` permalink to this room.
3284    ///
3285    /// If this room has an alias, we use it. Otherwise, we try to use the
3286    /// synced members in the room for [routing] the room ID.
3287    ///
3288    /// [routing]: https://spec.matrix.org/v1.3/appendices/#routing
3289    pub async fn matrix_to_permalink(&self) -> Result<MatrixToUri> {
3290        if let Some(alias) = self.canonical_alias().or_else(|| self.alt_aliases().pop()) {
3291            return Ok(alias.matrix_to_uri());
3292        }
3293
3294        let via = self.route().await?;
3295        Ok(self.room_id().matrix_to_uri_via(via))
3296    }
3297
3298    /// Get a `matrix:` permalink to this room.
3299    ///
3300    /// If this room has an alias, we use it. Otherwise, we try to use the
3301    /// synced members in the room for [routing] the room ID.
3302    ///
3303    /// # Arguments
3304    ///
3305    /// * `join` - Whether the user should join the room.
3306    ///
3307    /// [routing]: https://spec.matrix.org/v1.3/appendices/#routing
3308    pub async fn matrix_permalink(&self, join: bool) -> Result<MatrixUri> {
3309        if let Some(alias) = self.canonical_alias().or_else(|| self.alt_aliases().pop()) {
3310            return Ok(alias.matrix_uri(join));
3311        }
3312
3313        let via = self.route().await?;
3314        Ok(self.room_id().matrix_uri_via(via, join))
3315    }
3316
3317    /// Get a `matrix.to` permalink to an event in this room.
3318    ///
3319    /// We try to use the synced members in the room for [routing] the room ID.
3320    ///
3321    /// *Note*: This method does not check if the given event ID is actually
3322    /// part of this room. It needs to be checked before calling this method
3323    /// otherwise the permalink won't work.
3324    ///
3325    /// # Arguments
3326    ///
3327    /// * `event_id` - The ID of the event.
3328    ///
3329    /// [routing]: https://spec.matrix.org/v1.3/appendices/#routing
3330    pub async fn matrix_to_event_permalink(
3331        &self,
3332        event_id: impl Into<OwnedEventId>,
3333    ) -> Result<MatrixToUri> {
3334        // Don't use the alias because an event is tied to a room ID, but an
3335        // alias might point to another room, e.g. after a room upgrade.
3336        let via = self.route().await?;
3337        Ok(self.room_id().matrix_to_event_uri_via(event_id, via))
3338    }
3339
3340    /// Get a `matrix:` permalink to an event in this room.
3341    ///
3342    /// We try to use the synced members in the room for [routing] the room ID.
3343    ///
3344    /// *Note*: This method does not check if the given event ID is actually
3345    /// part of this room. It needs to be checked before calling this method
3346    /// otherwise the permalink won't work.
3347    ///
3348    /// # Arguments
3349    ///
3350    /// * `event_id` - The ID of the event.
3351    ///
3352    /// [routing]: https://spec.matrix.org/v1.3/appendices/#routing
3353    pub async fn matrix_event_permalink(
3354        &self,
3355        event_id: impl Into<OwnedEventId>,
3356    ) -> Result<MatrixUri> {
3357        // Don't use the alias because an event is tied to a room ID, but an
3358        // alias might point to another room, e.g. after a room upgrade.
3359        let via = self.route().await?;
3360        Ok(self.room_id().matrix_event_uri_via(event_id, via))
3361    }
3362
3363    /// Get the latest receipt of a user in this room.
3364    ///
3365    /// # Arguments
3366    ///
3367    /// * `receipt_type` - The type of receipt to get.
3368    ///
3369    /// * `thread` - The thread containing the event of the receipt, if any.
3370    ///
3371    /// * `user_id` - The ID of the user.
3372    ///
3373    /// Returns the ID of the event on which the receipt applies and the
3374    /// receipt.
3375    pub async fn load_user_receipt(
3376        &self,
3377        receipt_type: ReceiptType,
3378        thread: ReceiptThread,
3379        user_id: &UserId,
3380    ) -> Result<Option<(OwnedEventId, Receipt)>> {
3381        self.inner.load_user_receipt(receipt_type, thread, user_id).await.map_err(Into::into)
3382    }
3383
3384    /// Load the receipts for an event in this room from storage.
3385    ///
3386    /// # Arguments
3387    ///
3388    /// * `receipt_type` - The type of receipt to get.
3389    ///
3390    /// * `thread` - The thread containing the event of the receipt, if any.
3391    ///
3392    /// * `event_id` - The ID of the event.
3393    ///
3394    /// Returns a list of IDs of users who have sent a receipt for the event and
3395    /// the corresponding receipts.
3396    pub async fn load_event_receipts(
3397        &self,
3398        receipt_type: ReceiptType,
3399        thread: ReceiptThread,
3400        event_id: &EventId,
3401    ) -> Result<Vec<(OwnedUserId, Receipt)>> {
3402        self.inner.load_event_receipts(receipt_type, thread, event_id).await.map_err(Into::into)
3403    }
3404
3405    /// Get the push-condition context for this room.
3406    ///
3407    /// Returns `None` if some data couldn't be found. This should only happen
3408    /// in brand new rooms, while we process its state.
3409    pub async fn push_condition_room_ctx(&self) -> Result<Option<PushConditionRoomCtx>> {
3410        self.push_condition_room_ctx_internal(self.client.enabled_thread_subscriptions()).await
3411    }
3412
3413    /// Get the push-condition context for this room, with a choice to include
3414    /// thread subscriptions or not, based on the extra
3415    /// `with_threads_subscriptions` parameter.
3416    ///
3417    /// Returns `None` if some data couldn't be found. This should only happen
3418    /// in brand new rooms, while we process its state.
3419    pub(crate) async fn push_condition_room_ctx_internal(
3420        &self,
3421        with_threads_subscriptions: bool,
3422    ) -> Result<Option<PushConditionRoomCtx>> {
3423        let room_id = self.room_id();
3424        let user_id = self.own_user_id();
3425        let room_info = self.clone_info();
3426        let member_count = room_info.active_members_count();
3427
3428        let user_display_name = if let Some(member) = self.get_member_no_sync(user_id).await? {
3429            member.name().to_owned()
3430        } else {
3431            return Ok(None);
3432        };
3433
3434        let power_levels = match self.power_levels().await {
3435            Ok(power_levels) => Some(power_levels.into()),
3436            Err(error) => {
3437                if matches!(room_info.state(), RoomState::Joined) {
3438                    // It's normal to not have the power levels in a non-joined room, so don't log
3439                    // the error if the room is not joined
3440                    error!("Could not compute power levels for push conditions: {error}");
3441                }
3442                None
3443            }
3444        };
3445
3446        let mut ctx = assign!(PushConditionRoomCtx::new(
3447            room_id.to_owned(),
3448            UInt::new(member_count).unwrap_or(UInt::MAX),
3449            user_id.to_owned(),
3450            user_display_name,
3451        ),
3452        {
3453            power_levels,
3454        });
3455
3456        if with_threads_subscriptions {
3457            let this = self.clone();
3458            ctx = ctx.with_has_thread_subscription_fn(move |event_id: &EventId| {
3459                let room = this.clone();
3460                Box::pin(async move {
3461                    if let Ok(maybe_sub) = room.load_or_fetch_thread_subscription(event_id).await {
3462                        maybe_sub.is_some()
3463                    } else {
3464                        false
3465                    }
3466                })
3467            });
3468        }
3469
3470        Ok(Some(ctx))
3471    }
3472
3473    /// Retrieves a [`PushContext`] that can be used to compute the push
3474    /// actions for events.
3475    pub async fn push_context(&self) -> Result<Option<PushContext>> {
3476        self.push_context_internal(self.client.enabled_thread_subscriptions()).await
3477    }
3478
3479    /// Retrieves a [`PushContext`] that can be used to compute the push actions
3480    /// for events, with a choice to include thread subscriptions or not,
3481    /// based on the extra `with_threads_subscriptions` parameter.
3482    #[instrument(skip(self))]
3483    pub(crate) async fn push_context_internal(
3484        &self,
3485        with_threads_subscriptions: bool,
3486    ) -> Result<Option<PushContext>> {
3487        let Some(push_condition_room_ctx) =
3488            self.push_condition_room_ctx_internal(with_threads_subscriptions).await?
3489        else {
3490            debug!("Could not aggregate push context");
3491            return Ok(None);
3492        };
3493        let push_rules = self.client().account().push_rules().await?;
3494        Ok(Some(PushContext::new(push_condition_room_ctx, push_rules)))
3495    }
3496
3497    /// Get the push actions for the given event with the current room state.
3498    ///
3499    /// Note that it is possible that no push action is returned because the
3500    /// current room state does not have all the required state events.
3501    pub async fn event_push_actions<T>(&self, event: &Raw<T>) -> Result<Option<Vec<Action>>> {
3502        if let Some(ctx) = self.push_context().await? {
3503            Ok(Some(ctx.for_event(event).await))
3504        } else {
3505            Ok(None)
3506        }
3507    }
3508
3509    /// The membership details of the (latest) invite for the logged-in user in
3510    /// this room.
3511    pub async fn invite_details(&self) -> Result<Invite> {
3512        let state = self.state();
3513
3514        if state != RoomState::Invited {
3515            return Err(Error::WrongRoomState(Box::new(WrongRoomState::new("Invited", state))));
3516        }
3517
3518        let invitee = self
3519            .get_member_no_sync(self.own_user_id())
3520            .await?
3521            .ok_or_else(|| Error::UnknownError(Box::new(InvitationError::EventMissing)))?;
3522        let event = invitee.event();
3523        let inviter_id = event.sender();
3524        let inviter = self.get_member_no_sync(inviter_id).await?;
3525        Ok(Invite { invitee, inviter })
3526    }
3527
3528    /// Get the membership details for the current user.
3529    ///
3530    /// Returns:
3531    ///     - If the user was present in the room, a
3532    ///       [`RoomMemberWithSenderInfo`] containing both the user info and the
3533    ///       member info of the sender of the `m.room.member` event.
3534    ///     - If the current user is not present, an error.
3535    pub async fn member_with_sender_info(
3536        &self,
3537        user_id: &UserId,
3538    ) -> Result<RoomMemberWithSenderInfo> {
3539        let Some(member) = self.get_member_no_sync(user_id).await? else {
3540            return Err(Error::InsufficientData);
3541        };
3542
3543        let sender_member =
3544            if let Some(member) = self.get_member_no_sync(member.event().sender()).await? {
3545                // If the sender room member info is already available, return it
3546                Some(member)
3547            } else if self.are_members_synced() {
3548                // The room members are synced and we couldn't find the sender info
3549                None
3550            } else if self.sync_members().await.is_ok() {
3551                // Try getting the sender room member info again after syncing
3552                self.get_member_no_sync(member.event().sender()).await?
3553            } else {
3554                None
3555            };
3556
3557        Ok(RoomMemberWithSenderInfo { room_member: member, sender_info: sender_member })
3558    }
3559
3560    /// Forget this room.
3561    ///
3562    /// This communicates to the homeserver that it should forget the room.
3563    ///
3564    /// Only left or banned-from rooms can be forgotten.
3565    pub async fn forget(&self) -> Result<()> {
3566        let state = self.state();
3567        match state {
3568            RoomState::Joined | RoomState::Invited | RoomState::Knocked => {
3569                return Err(Error::WrongRoomState(Box::new(WrongRoomState::new(
3570                    "Left / Banned",
3571                    state,
3572                ))));
3573            }
3574            RoomState::Left | RoomState::Banned => {}
3575        }
3576
3577        let request = forget_room::v3::Request::new(self.inner.room_id().to_owned());
3578        let _response = self.client.send(request).await?;
3579
3580        // If it was a DM, remove the room from the `m.direct` global account data.
3581        if self.inner.direct_targets_length() != 0
3582            && let Err(e) = self.set_is_direct(false).await
3583        {
3584            // It is not important whether we managed to remove the room, it will not have
3585            // any consequences, so just log the error.
3586            warn!(room_id = ?self.room_id(), "failed to remove room from m.direct account data: {e}");
3587        }
3588
3589        self.client.base_client().forget_room(self.inner.room_id()).await?;
3590
3591        Ok(())
3592    }
3593
3594    fn ensure_room_joined(&self) -> Result<()> {
3595        let state = self.state();
3596        if state == RoomState::Joined {
3597            Ok(())
3598        } else {
3599            Err(Error::WrongRoomState(Box::new(WrongRoomState::new("Joined", state))))
3600        }
3601    }
3602
3603    /// Get the notification mode.
3604    pub async fn notification_mode(&self) -> Option<RoomNotificationMode> {
3605        if !matches!(self.state(), RoomState::Joined) {
3606            return None;
3607        }
3608
3609        let notification_settings = self.client().notification_settings().await;
3610
3611        // Get the user-defined mode if available
3612        let notification_mode =
3613            notification_settings.get_user_defined_room_notification_mode(self.room_id()).await;
3614
3615        if notification_mode.is_some() {
3616            notification_mode
3617        } else if let Ok(is_encrypted) =
3618            self.latest_encryption_state().await.map(|state| state.is_encrypted())
3619        {
3620            // Otherwise, if encrypted status is available, get the default mode for this
3621            // type of room.
3622            // From the point of view of notification settings, a `one-to-one` room is one
3623            // that involves exactly two people.
3624            let is_one_to_one = IsOneToOne::from(self.active_members_count() == 2);
3625            let default_mode = notification_settings
3626                .get_default_room_notification_mode(IsEncrypted::from(is_encrypted), is_one_to_one)
3627                .await;
3628            Some(default_mode)
3629        } else {
3630            None
3631        }
3632    }
3633
3634    /// Get the user-defined notification mode.
3635    ///
3636    /// The result is cached for fast and non-async call. To read the cached
3637    /// result, use
3638    /// [`matrix_sdk_base::Room::cached_user_defined_notification_mode`].
3639    //
3640    // Note for maintainers:
3641    //
3642    // The fact the result is cached is an important property. If you change that in
3643    // the future, please review all calls to this method.
3644    pub async fn user_defined_notification_mode(&self) -> Option<RoomNotificationMode> {
3645        if !matches!(self.state(), RoomState::Joined) {
3646            return None;
3647        }
3648
3649        let notification_settings = self.client().notification_settings().await;
3650
3651        // Get the user-defined mode if available.
3652        let mode =
3653            notification_settings.get_user_defined_room_notification_mode(self.room_id()).await;
3654
3655        if let Some(mode) = mode {
3656            self.update_cached_user_defined_notification_mode(mode);
3657        }
3658
3659        mode
3660    }
3661
3662    /// Report an event as inappropriate to the homeserver's administrator.
3663    ///
3664    /// # Arguments
3665    ///
3666    /// * `event_id` - The ID of the event to report.
3667    /// * `score` - The score to rate this content.
3668    /// * `reason` - The reason the content is being reported.
3669    ///
3670    /// # Errors
3671    ///
3672    /// Returns an error if the room is not joined or if an error occurs with
3673    /// the request.
3674    pub async fn report_content(
3675        &self,
3676        event_id: OwnedEventId,
3677        score: Option<ReportedContentScore>,
3678        reason: Option<String>,
3679    ) -> Result<report_content::v3::Response> {
3680        let state = self.state();
3681        if state != RoomState::Joined {
3682            return Err(Error::WrongRoomState(Box::new(WrongRoomState::new("Joined", state))));
3683        }
3684
3685        let request = report_content::v3::Request::new(
3686            self.inner.room_id().to_owned(),
3687            event_id,
3688            score.map(Into::into),
3689            reason,
3690        );
3691        Ok(self.client.send(request).await?)
3692    }
3693
3694    /// Reports a room as inappropriate to the server.
3695    /// The caller is not required to be joined to the room to report it.
3696    ///
3697    /// # Arguments
3698    ///
3699    /// * `reason` - The reason the room is being reported.
3700    ///
3701    /// # Errors
3702    ///
3703    /// Returns an error if the room is not found or on rate limit
3704    pub async fn report_room(&self, reason: String) -> Result<report_room::v3::Response> {
3705        let request = report_room::v3::Request::new(self.inner.room_id().to_owned(), reason);
3706
3707        Ok(self.client.send(request).await?)
3708    }
3709
3710    /// Set a flag on the room to indicate that the user has explicitly marked
3711    /// it as (un)read.
3712    ///
3713    /// This is a no-op if [`BaseRoom::is_marked_unread()`] returns the same
3714    /// value as `unread`.
3715    pub async fn set_unread_flag(&self, unread: bool) -> Result<()> {
3716        if self.is_marked_unread() == unread {
3717            // The request is not necessary.
3718            return Ok(());
3719        }
3720
3721        let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
3722
3723        let content = MarkedUnreadEventContent::new(unread);
3724
3725        let request = set_room_account_data::v3::Request::new(
3726            user_id.to_owned(),
3727            self.inner.room_id().to_owned(),
3728            &content,
3729        )?;
3730
3731        self.client.send(request).await?;
3732        Ok(())
3733    }
3734
3735    /// Returns the [`RoomEventCache`] associated to this room, assuming the
3736    /// global [`EventCache`] has been enabled for subscription.
3737    pub async fn event_cache(
3738        &self,
3739    ) -> event_cache::Result<(RoomEventCache, Arc<EventCacheDropHandles>)> {
3740        self.client.event_cache().for_room(self.room_id()).await
3741    }
3742
3743    /// Get the beacon information event in the room for the `user_id`.
3744    ///
3745    /// # Errors
3746    ///
3747    /// Returns an error if the event is redacted, stripped, not found or could
3748    /// not be deserialized.
3749    pub(crate) async fn get_user_beacon_info(
3750        &self,
3751        user_id: &UserId,
3752    ) -> Result<OriginalSyncStateEvent<BeaconInfoEventContent>, BeaconError> {
3753        let raw_event = self
3754            .get_state_event_static_for_key::<BeaconInfoEventContent, _>(user_id)
3755            .await?
3756            .ok_or(BeaconError::NotFound)?;
3757
3758        match raw_event.deserialize()? {
3759            SyncOrStrippedState::Sync(SyncStateEvent::Original(beacon_info)) => Ok(beacon_info),
3760            SyncOrStrippedState::Sync(SyncStateEvent::Redacted(_)) => Err(BeaconError::Redacted),
3761            SyncOrStrippedState::Stripped(_) => Err(BeaconError::Stripped),
3762        }
3763    }
3764
3765    /// Start sharing live location in the room.
3766    ///
3767    /// # Arguments
3768    ///
3769    /// * `duration_millis` - The duration for which the live location is
3770    ///   shared, in milliseconds.
3771    /// * `description` - An optional description for the live location share.
3772    ///
3773    /// # Errors
3774    ///
3775    /// Returns an error if the room is not joined or if the state event could
3776    /// not be sent.
3777    pub async fn start_live_location_share(
3778        &self,
3779        duration_millis: u64,
3780        description: Option<String>,
3781    ) -> Result<send_state_event::v3::Response> {
3782        self.ensure_room_joined()?;
3783
3784        self.send_state_event_for_key(
3785            self.own_user_id(),
3786            BeaconInfoEventContent::new(
3787                description,
3788                Duration::from_millis(duration_millis),
3789                true,
3790                None,
3791            ),
3792        )
3793        .await
3794    }
3795
3796    /// Stop sharing live location in the room.
3797    ///
3798    /// # Errors
3799    ///
3800    /// Returns an error if the room is not joined, if the beacon information
3801    /// is redacted or stripped, or if the state event is not found.
3802    pub async fn stop_live_location_share(
3803        &self,
3804    ) -> Result<send_state_event::v3::Response, BeaconError> {
3805        self.ensure_room_joined()?;
3806
3807        let mut beacon_info_event = self.get_user_beacon_info(self.own_user_id()).await?;
3808        beacon_info_event.content.stop();
3809        Ok(self.send_state_event_for_key(self.own_user_id(), beacon_info_event.content).await?)
3810    }
3811
3812    /// Send a location beacon event in the current room.
3813    ///
3814    /// # Arguments
3815    ///
3816    /// * `geo_uri` - The geo URI of the location beacon.
3817    ///
3818    /// # Errors
3819    ///
3820    /// Returns an error if the room is not joined, if the beacon information
3821    /// is redacted or stripped, if the location share is no longer live,
3822    /// or if the state event is not found.
3823    pub async fn send_location_beacon(
3824        &self,
3825        geo_uri: String,
3826    ) -> Result<send_message_event::v3::Response, BeaconError> {
3827        self.ensure_room_joined()?;
3828
3829        let beacon_info_event = self.get_user_beacon_info(self.own_user_id()).await?;
3830
3831        if beacon_info_event.content.is_live() {
3832            let content = BeaconEventContent::new(beacon_info_event.event_id, geo_uri, None);
3833            Ok(self.send(content).await?.response)
3834        } else {
3835            Err(BeaconError::NotLive)
3836        }
3837    }
3838
3839    /// Store the given `ComposerDraft` in the state store using the current
3840    /// room id and optional thread root id as identifier.
3841    pub async fn save_composer_draft(
3842        &self,
3843        draft: ComposerDraft,
3844        thread_root: Option<&EventId>,
3845    ) -> Result<()> {
3846        self.client
3847            .state_store()
3848            .set_kv_data(
3849                StateStoreDataKey::ComposerDraft(self.room_id(), thread_root),
3850                StateStoreDataValue::ComposerDraft(draft),
3851            )
3852            .await?;
3853        Ok(())
3854    }
3855
3856    /// Retrieve the `ComposerDraft` stored in the state store for this room
3857    /// and given thread, if any.
3858    pub async fn load_composer_draft(
3859        &self,
3860        thread_root: Option<&EventId>,
3861    ) -> Result<Option<ComposerDraft>> {
3862        let data = self
3863            .client
3864            .state_store()
3865            .get_kv_data(StateStoreDataKey::ComposerDraft(self.room_id(), thread_root))
3866            .await?;
3867        Ok(data.and_then(|d| d.into_composer_draft()))
3868    }
3869
3870    /// Remove the `ComposerDraft` stored in the state store for this room
3871    /// and given thread, if any.
3872    pub async fn clear_composer_draft(&self, thread_root: Option<&EventId>) -> Result<()> {
3873        self.client
3874            .state_store()
3875            .remove_kv_data(StateStoreDataKey::ComposerDraft(self.room_id(), thread_root))
3876            .await?;
3877        Ok(())
3878    }
3879
3880    /// Load pinned state events for a room from the `/state` endpoint in the
3881    /// home server.
3882    pub async fn load_pinned_events(&self) -> Result<Option<Vec<OwnedEventId>>> {
3883        let response = self
3884            .client
3885            .send(get_state_event_for_key::v3::Request::new(
3886                self.room_id().to_owned(),
3887                StateEventType::RoomPinnedEvents,
3888                "".to_owned(),
3889            ))
3890            .await;
3891
3892        match response {
3893            Ok(response) => Ok(Some(
3894                response
3895                    .into_content()
3896                    .deserialize_as_unchecked::<RoomPinnedEventsEventContent>()?
3897                    .pinned,
3898            )),
3899            Err(http_error) => match http_error.as_client_api_error() {
3900                Some(error) if error.status_code == StatusCode::NOT_FOUND => Ok(None),
3901                _ => Err(http_error.into()),
3902            },
3903        }
3904    }
3905
3906    /// Observe live location sharing events for this room.
3907    ///
3908    /// The returned observable will receive the newest event for each sync
3909    /// response that contains an `m.beacon` event.
3910    ///
3911    /// Returns a stream of [`ObservableLiveLocation`] events from other users
3912    /// in the room, excluding the live location events of the room's own user.
3913    pub fn observe_live_location_shares(&self) -> ObservableLiveLocation {
3914        ObservableLiveLocation::new(&self.client, self.room_id())
3915    }
3916
3917    /// Subscribe to knock requests in this `Room`.
3918    ///
3919    /// The current requests to join the room will be emitted immediately
3920    /// when subscribing.
3921    ///
3922    /// A new set of knock requests will be emitted whenever:
3923    /// - A new member event is received.
3924    /// - A knock request is marked as seen.
3925    /// - A sync is gappy (limited), so room membership information may be
3926    ///   outdated.
3927    ///
3928    /// Returns both a stream of knock requests and a handle for a task that
3929    /// will clean up the seen knock request ids when possible.
3930    pub async fn subscribe_to_knock_requests(
3931        &self,
3932    ) -> Result<(impl Stream<Item = Vec<KnockRequest>> + use<>, JoinHandle<()>)> {
3933        let this = Arc::new(self.clone());
3934
3935        let room_member_events_observer =
3936            self.client.observe_room_events::<SyncRoomMemberEvent, (Client, Room)>(this.room_id());
3937
3938        let current_seen_ids = self.get_seen_knock_request_ids().await?;
3939        let mut seen_request_ids_stream = self
3940            .seen_knock_request_ids_map
3941            .subscribe()
3942            .await
3943            .map(|values| values.unwrap_or_default());
3944
3945        let mut room_info_stream = self.subscribe_info();
3946
3947        // Spawn a task that will clean up the seen knock request ids when updated room
3948        // members are received
3949        let clear_seen_ids_handle = spawn({
3950            let this = self.clone();
3951            async move {
3952                let mut member_updates_stream = this.room_member_updates_sender.subscribe();
3953                while member_updates_stream.recv().await.is_ok() {
3954                    // If room members were updated, try to remove outdated seen knock request ids
3955                    if let Err(err) = this.remove_outdated_seen_knock_requests_ids().await {
3956                        warn!("Failed to remove seen knock requests: {err}")
3957                    }
3958                }
3959            }
3960        });
3961
3962        let combined_stream = stream! {
3963            // Emit current requests to join
3964            match this.get_current_join_requests(&current_seen_ids).await {
3965                Ok(initial_requests) => yield initial_requests,
3966                Err(err) => warn!("Failed to get initial requests to join: {err}")
3967            }
3968
3969            let mut requests_stream = room_member_events_observer.subscribe();
3970            let mut seen_ids = current_seen_ids.clone();
3971
3972            loop {
3973                // This is equivalent to a combine stream operation, triggering a new emission
3974                // when any of the branches changes
3975                tokio::select! {
3976                    Some((event, _)) = requests_stream.next() => {
3977                        if let Some(event) = event.as_original() {
3978                            // If we can calculate the membership change, try to emit only when needed
3979                            let emit = if event.prev_content().is_some() {
3980                                matches!(event.membership_change(),
3981                                    MembershipChange::Banned |
3982                                    MembershipChange::Knocked |
3983                                    MembershipChange::KnockAccepted |
3984                                    MembershipChange::KnockDenied |
3985                                    MembershipChange::KnockRetracted
3986                                )
3987                            } else {
3988                                // If we can't calculate the membership change, assume we need to
3989                                // emit updated values
3990                                true
3991                            };
3992
3993                            if emit {
3994                                match this.get_current_join_requests(&seen_ids).await {
3995                                    Ok(requests) => yield requests,
3996                                    Err(err) => {
3997                                        warn!("Failed to get updated knock requests on new member event: {err}")
3998                                    }
3999                                }
4000                            }
4001                        }
4002                    }
4003
4004                    Some(new_seen_ids) = seen_request_ids_stream.next() => {
4005                        // Update the current seen ids
4006                        seen_ids = new_seen_ids;
4007
4008                        // If seen requests have changed we need to recalculate
4009                        // all the knock requests
4010                        match this.get_current_join_requests(&seen_ids).await {
4011                            Ok(requests) => yield requests,
4012                            Err(err) => {
4013                                warn!("Failed to get updated knock requests on seen ids changed: {err}")
4014                            }
4015                        }
4016                    }
4017
4018                    Some(room_info) = room_info_stream.next() => {
4019                        // We need to emit new items when we may have missing room members:
4020                        // this usually happens after a gappy (limited) sync
4021                        if !room_info.are_members_synced() {
4022                            match this.get_current_join_requests(&seen_ids).await {
4023                                Ok(requests) => yield requests,
4024                                Err(err) => {
4025                                    warn!("Failed to get updated knock requests on gappy (limited) sync: {err}")
4026                                }
4027                            }
4028                        }
4029                    }
4030                    // If the streams in all branches are closed, stop the loop
4031                    else => break,
4032                }
4033            }
4034        };
4035
4036        Ok((combined_stream, clear_seen_ids_handle))
4037    }
4038
4039    async fn get_current_join_requests(
4040        &self,
4041        seen_request_ids: &BTreeMap<OwnedEventId, OwnedUserId>,
4042    ) -> Result<Vec<KnockRequest>> {
4043        Ok(self
4044            .members(RoomMemberships::KNOCK)
4045            .await?
4046            .into_iter()
4047            .filter_map(|member| {
4048                let event_id = member.event().event_id()?;
4049                Some(KnockRequest::new(
4050                    self,
4051                    event_id,
4052                    member.event().timestamp(),
4053                    KnockRequestMemberInfo::from_member(&member),
4054                    seen_request_ids.contains_key(event_id),
4055                ))
4056            })
4057            .collect())
4058    }
4059
4060    /// Access the room settings related to privacy and visibility.
4061    pub fn privacy_settings(&self) -> RoomPrivacySettings<'_> {
4062        RoomPrivacySettings::new(&self.inner, &self.client)
4063    }
4064
4065    /// Retrieve a list of all the threads for the current room.
4066    ///
4067    /// Since this client-server API is paginated, the return type may include a
4068    /// token used to resuming back-pagination into the list of results, in
4069    /// [`ThreadRoots::prev_batch_token`]. This token can be fed back into
4070    /// [`ListThreadsOptions::from`] to continue the pagination
4071    /// from the previous position.
4072    pub async fn list_threads(&self, opts: ListThreadsOptions) -> Result<ThreadRoots> {
4073        let request = opts.into_request(self.room_id());
4074
4075        let response = self.client.send(request).await?;
4076
4077        let push_ctx = self.push_context().await?;
4078        let chunk = join_all(
4079            response.chunk.into_iter().map(|ev| self.try_decrypt_event(ev, push_ctx.as_ref())),
4080        )
4081        .await;
4082
4083        Ok(ThreadRoots { chunk, prev_batch_token: response.next_batch })
4084    }
4085
4086    /// Retrieve a list of relations for the given event, according to the given
4087    /// options.
4088    ///
4089    /// Since this client-server API is paginated, the return type may include a
4090    /// token used to resuming back-pagination into the list of results, in
4091    /// [`Relations::prev_batch_token`]. This token can be fed back into
4092    /// [`RelationsOptions::from`] to continue the pagination from the previous
4093    /// position.
4094    ///
4095    /// **Note**: if [`RelationsOptions::from`] is set for a subsequent request,
4096    /// then it must be used with the same
4097    /// [`RelationsOptions::include_relations`] value as the request that
4098    /// returns the `from` token, otherwise the server behavior is undefined.
4099    pub async fn relations(
4100        &self,
4101        event_id: OwnedEventId,
4102        opts: RelationsOptions,
4103    ) -> Result<Relations> {
4104        let relations = opts.send(self, event_id).await;
4105
4106        // Save any new related events to the cache.
4107        if let Ok(Relations { chunk, .. }) = &relations
4108            && let Ok((cache, _handles)) = self.event_cache().await
4109        {
4110            cache.save_events(chunk.clone()).await;
4111        }
4112
4113        relations
4114    }
4115
4116    /// Search this room's [`RoomIndex`] for query and return at most
4117    /// max_number_of_results results.
4118    #[cfg(feature = "experimental-search")]
4119    pub async fn search(
4120        &self,
4121        query: &str,
4122        max_number_of_results: usize,
4123        pagination_offset: Option<usize>,
4124    ) -> Result<Vec<OwnedEventId>, IndexError> {
4125        let mut search_index_guard = self.client.search_index().lock().await;
4126        search_index_guard.search(query, max_number_of_results, pagination_offset, self.room_id())
4127    }
4128
4129    /// Subscribe to a given thread in this room.
4130    ///
4131    /// This will subscribe the user to the thread, so that they will receive
4132    /// notifications for that thread specifically.
4133    ///
4134    /// # Arguments
4135    ///
4136    /// - `thread_root`: The ID of the thread root event to subscribe to.
4137    /// - `automatic`: Whether the subscription was made automatically by a
4138    ///   client, not by manual user choice. If set, must include the latest
4139    ///   event ID that's known in the thread and that is causing the automatic
4140    ///   subscription. If unset (i.e. we're now subscribing manually) and there
4141    ///   was a previous automatic subscription, the subscription will be
4142    ///   overridden to a manual one instead.
4143    ///
4144    /// # Returns
4145    ///
4146    /// - A 404 error if the event isn't known, or isn't a thread root.
4147    /// - An `Ok` result if the subscription was successful, or if the server
4148    ///   skipped an automatic subscription (as the user unsubscribed from the
4149    ///   thread after the event causing the automatic subscription).
4150    #[instrument(skip(self), fields(room_id = %self.room_id()))]
4151    pub async fn subscribe_thread(
4152        &self,
4153        thread_root: OwnedEventId,
4154        automatic: Option<OwnedEventId>,
4155    ) -> Result<()> {
4156        let is_automatic = automatic.is_some();
4157
4158        match self
4159            .client
4160            .send(subscribe_thread::unstable::Request::new(
4161                self.room_id().to_owned(),
4162                thread_root.clone(),
4163                automatic,
4164            ))
4165            .await
4166        {
4167            Ok(_response) => {
4168                trace!("Server acknowledged the thread subscription; saving in db");
4169
4170                // Immediately save the result into the database.
4171                self.client
4172                    .state_store()
4173                    .upsert_thread_subscription(
4174                        self.room_id(),
4175                        &thread_root,
4176                        StoredThreadSubscription {
4177                            status: ThreadSubscriptionStatus::Subscribed {
4178                                automatic: is_automatic,
4179                            },
4180                            bump_stamp: None,
4181                        },
4182                    )
4183                    .await?;
4184
4185                Ok(())
4186            }
4187
4188            Err(err) => {
4189                if let Some(ErrorKind::ConflictingUnsubscription) = err.client_api_error_kind() {
4190                    // In this case: the server indicates that the user unsubscribed *after* the
4191                    // event ID we've used in an automatic subscription; don't
4192                    // save the subscription state in the database, as the
4193                    // previous one should be more correct.
4194                    trace!("Thread subscription skipped: {err}");
4195                    Ok(())
4196                } else {
4197                    // Forward the error to the caller.
4198                    Err(err.into())
4199                }
4200            }
4201        }
4202    }
4203
4204    /// Subscribe to a thread if needed, based on a current subscription to it.
4205    ///
4206    /// This is like [`Self::subscribe_thread`], but it first checks if the user
4207    /// has already subscribed to a thread, so as to minimize sending
4208    /// unnecessary subscriptions which would be ignored by the server.
4209    pub async fn subscribe_thread_if_needed(
4210        &self,
4211        thread_root: &EventId,
4212        automatic: Option<OwnedEventId>,
4213    ) -> Result<()> {
4214        if let Some(prev_sub) = self.load_or_fetch_thread_subscription(thread_root).await? {
4215            // If we have a previous subscription, we should only send the new one if it's
4216            // manual and the previous one was automatic.
4217            if !prev_sub.automatic || automatic.is_some() {
4218                // Either we had already a manual subscription, or we had an automatic one and
4219                // the new one is automatic too: nothing to do!
4220                return Ok(());
4221            }
4222        }
4223        self.subscribe_thread(thread_root.to_owned(), automatic).await
4224    }
4225
4226    /// Unsubscribe from a given thread in this room.
4227    ///
4228    /// # Arguments
4229    ///
4230    /// - `thread_root`: The ID of the thread root event to unsubscribe to.
4231    ///
4232    /// # Returns
4233    ///
4234    /// - An `Ok` result if the unsubscription was successful, or the thread was
4235    ///   already unsubscribed.
4236    /// - A 404 error if the event isn't known, or isn't a thread root.
4237    #[instrument(skip(self), fields(room_id = %self.room_id()))]
4238    pub async fn unsubscribe_thread(&self, thread_root: OwnedEventId) -> Result<()> {
4239        self.client
4240            .send(unsubscribe_thread::unstable::Request::new(
4241                self.room_id().to_owned(),
4242                thread_root.clone(),
4243            ))
4244            .await?;
4245
4246        trace!("Server acknowledged the thread subscription removal; removed it from db too");
4247
4248        // Immediately save the result into the database.
4249        self.client
4250            .state_store()
4251            .upsert_thread_subscription(
4252                self.room_id(),
4253                &thread_root,
4254                StoredThreadSubscription {
4255                    status: ThreadSubscriptionStatus::Unsubscribed,
4256                    bump_stamp: None,
4257                },
4258            )
4259            .await?;
4260
4261        Ok(())
4262    }
4263
4264    /// Return the current thread subscription for the given thread root in this
4265    /// room.
4266    ///
4267    /// # Arguments
4268    ///
4269    /// - `thread_root`: The ID of the thread root event to get the subscription
4270    ///   for.
4271    ///
4272    /// # Returns
4273    ///
4274    /// - An `Ok` result with `Some(ThreadSubscription)` if we have some
4275    ///   subscription information.
4276    /// - An `Ok` result with `None` if the subscription does not exist, or the
4277    ///   event couldn't be found, or the event isn't a thread.
4278    /// - An error if the request fails for any other reason, such as a network
4279    ///   error.
4280    #[instrument(skip(self), fields(room_id = %self.room_id()))]
4281    pub async fn fetch_thread_subscription(
4282        &self,
4283        thread_root: OwnedEventId,
4284    ) -> Result<Option<ThreadSubscription>> {
4285        let result = self
4286            .client
4287            .send(get_thread_subscription::unstable::Request::new(
4288                self.room_id().to_owned(),
4289                thread_root.clone(),
4290            ))
4291            .await;
4292
4293        let subscription = match result {
4294            Ok(response) => Some(ThreadSubscription { automatic: response.automatic }),
4295            Err(http_error) => match http_error.as_client_api_error() {
4296                Some(error) if error.status_code == StatusCode::NOT_FOUND => None,
4297                _ => return Err(http_error.into()),
4298            },
4299        };
4300
4301        // Keep the database in sync.
4302        if let Some(sub) = &subscription {
4303            self.client
4304                .state_store()
4305                .upsert_thread_subscription(
4306                    self.room_id(),
4307                    &thread_root,
4308                    StoredThreadSubscription {
4309                        status: ThreadSubscriptionStatus::Subscribed { automatic: sub.automatic },
4310                        bump_stamp: None,
4311                    },
4312                )
4313                .await?;
4314        } else {
4315            // If the subscription was not found, remove it from the database.
4316            self.client
4317                .state_store()
4318                .remove_thread_subscription(self.room_id(), &thread_root)
4319                .await?;
4320        }
4321
4322        Ok(subscription)
4323    }
4324
4325    /// Return the current thread subscription for the given thread root in this
4326    /// room, by getting it from storage if possible, or fetching it from
4327    /// network otherwise.
4328    ///
4329    /// See also [`Self::fetch_thread_subscription`] for the exact semantics of
4330    /// this method.
4331    pub async fn load_or_fetch_thread_subscription(
4332        &self,
4333        thread_root: &EventId,
4334    ) -> Result<Option<ThreadSubscription>> {
4335        // If the thread subscriptions list is outdated, fetch from the server.
4336        if self.client.thread_subscription_catchup().is_outdated() {
4337            return self.fetch_thread_subscription(thread_root.to_owned()).await;
4338        }
4339
4340        // Otherwise, we can rely on the store information.
4341        Ok(self
4342            .client
4343            .state_store()
4344            .load_thread_subscription(self.room_id(), thread_root)
4345            .await
4346            .map(|maybe_sub| {
4347                maybe_sub.and_then(|stored| match stored.status {
4348                    ThreadSubscriptionStatus::Unsubscribed => None,
4349                    ThreadSubscriptionStatus::Subscribed { automatic } => {
4350                        Some(ThreadSubscription { automatic })
4351                    }
4352                })
4353            })?)
4354    }
4355}
4356
4357#[cfg(feature = "e2e-encryption")]
4358impl RoomIdentityProvider for Room {
4359    fn is_member<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, bool> {
4360        Box::pin(async { self.get_member(user_id).await.unwrap_or(None).is_some() })
4361    }
4362
4363    fn member_identities(&self) -> BoxFuture<'_, Vec<UserIdentity>> {
4364        Box::pin(async {
4365            let members = self
4366                .members(RoomMemberships::JOIN | RoomMemberships::INVITE)
4367                .await
4368                .unwrap_or_else(|_| Default::default());
4369
4370            let mut ret: Vec<UserIdentity> = Vec::new();
4371            for member in members {
4372                if let Some(i) = self.user_identity(member.user_id()).await {
4373                    ret.push(i);
4374                }
4375            }
4376            ret
4377        })
4378    }
4379
4380    fn user_identity<'a>(&'a self, user_id: &'a UserId) -> BoxFuture<'a, Option<UserIdentity>> {
4381        Box::pin(async {
4382            self.client
4383                .encryption()
4384                .get_user_identity(user_id)
4385                .await
4386                .unwrap_or(None)
4387                .map(|u| u.underlying_identity())
4388        })
4389    }
4390}
4391
4392/// A wrapper for a weak client and a room id that allows to lazily retrieve a
4393/// room, only when needed.
4394#[derive(Clone, Debug)]
4395pub(crate) struct WeakRoom {
4396    client: WeakClient,
4397    room_id: OwnedRoomId,
4398}
4399
4400impl WeakRoom {
4401    /// Create a new `WeakRoom` given its weak components.
4402    pub fn new(client: WeakClient, room_id: OwnedRoomId) -> Self {
4403        Self { client, room_id }
4404    }
4405
4406    /// Attempts to reconstruct the room.
4407    pub fn get(&self) -> Option<Room> {
4408        self.client.get().and_then(|client| client.get_room(&self.room_id))
4409    }
4410
4411    /// The room id for that room.
4412    pub fn room_id(&self) -> &RoomId {
4413        &self.room_id
4414    }
4415}
4416
4417/// Details of the (latest) invite.
4418#[derive(Debug, Clone)]
4419pub struct Invite {
4420    /// Who has been invited.
4421    pub invitee: RoomMember,
4422    /// Who sent the invite.
4423    pub inviter: Option<RoomMember>,
4424}
4425
4426#[derive(Error, Debug)]
4427enum InvitationError {
4428    #[error("No membership event found")]
4429    EventMissing,
4430}
4431
4432/// Receipts to send all at once.
4433#[derive(Debug, Clone, Default)]
4434#[non_exhaustive]
4435pub struct Receipts {
4436    /// Fully-read marker (room account data).
4437    pub fully_read: Option<OwnedEventId>,
4438    /// Read receipt (public ephemeral room event).
4439    pub public_read_receipt: Option<OwnedEventId>,
4440    /// Read receipt (private ephemeral room event).
4441    pub private_read_receipt: Option<OwnedEventId>,
4442}
4443
4444impl Receipts {
4445    /// Create an empty `Receipts`.
4446    pub fn new() -> Self {
4447        Self::default()
4448    }
4449
4450    /// Set the last event the user has read.
4451    ///
4452    /// It means that the user has read all the events before this event.
4453    ///
4454    /// This is a private marker only visible by the user.
4455    ///
4456    /// Note that this is technically not a receipt as it is persisted in the
4457    /// room account data.
4458    pub fn fully_read_marker(mut self, event_id: impl Into<Option<OwnedEventId>>) -> Self {
4459        self.fully_read = event_id.into();
4460        self
4461    }
4462
4463    /// Set the last event presented to the user and forward it to the other
4464    /// users in the room.
4465    ///
4466    /// This is used to reset the unread messages/notification count and
4467    /// advertise to other users the last event that the user has likely seen.
4468    pub fn public_read_receipt(mut self, event_id: impl Into<Option<OwnedEventId>>) -> Self {
4469        self.public_read_receipt = event_id.into();
4470        self
4471    }
4472
4473    /// Set the last event presented to the user and don't forward it.
4474    ///
4475    /// This is used to reset the unread messages/notification count.
4476    pub fn private_read_receipt(mut self, event_id: impl Into<Option<OwnedEventId>>) -> Self {
4477        self.private_read_receipt = event_id.into();
4478        self
4479    }
4480
4481    /// Whether this `Receipts` is empty.
4482    pub fn is_empty(&self) -> bool {
4483        self.fully_read.is_none()
4484            && self.public_read_receipt.is_none()
4485            && self.private_read_receipt.is_none()
4486    }
4487}
4488
4489/// [Parent space](https://spec.matrix.org/v1.8/client-server-api/#mspaceparent-relationships)
4490/// listed by a room, possibly validated by checking the space's state.
4491#[derive(Debug)]
4492pub enum ParentSpace {
4493    /// The room recognizes the given room as its parent, and the parent
4494    /// recognizes it as its child.
4495    Reciprocal(Room),
4496    /// The room recognizes the given room as its parent, but the parent does
4497    /// not recognizes it as its child. However, the author of the
4498    /// `m.space.parent` event in the room has a sufficient power level in the
4499    /// parent to create the child event.
4500    WithPowerlevel(Room),
4501    /// The room recognizes the given room as its parent, but the parent does
4502    /// not recognizes it as its child.
4503    Illegitimate(Room),
4504    /// The room recognizes the given id as its parent room, but we cannot check
4505    /// whether the parent recognizes it as its child.
4506    Unverifiable(OwnedRoomId),
4507}
4508
4509/// The score to rate an inappropriate content.
4510///
4511/// Must be a value between `0`, inoffensive, and `-100`, very offensive.
4512#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
4513pub struct ReportedContentScore(i8);
4514
4515impl ReportedContentScore {
4516    /// The smallest value that can be represented by this type.
4517    ///
4518    /// This is for very offensive content.
4519    pub const MIN: Self = Self(-100);
4520
4521    /// The largest value that can be represented by this type.
4522    ///
4523    /// This is for inoffensive content.
4524    pub const MAX: Self = Self(0);
4525
4526    /// Try to create a `ReportedContentScore` from the provided `i8`.
4527    ///
4528    /// Returns `None` if it is smaller than [`ReportedContentScore::MIN`] or
4529    /// larger than [`ReportedContentScore::MAX`] .
4530    ///
4531    /// This is the same as the `TryFrom<i8>` implementation for
4532    /// `ReportedContentScore`, except that it returns an `Option` instead
4533    /// of a `Result`.
4534    pub fn new(value: i8) -> Option<Self> {
4535        value.try_into().ok()
4536    }
4537
4538    /// Create a `ReportedContentScore` from the provided `i8` clamped to the
4539    /// acceptable interval.
4540    ///
4541    /// The given value gets clamped into the closed interval between
4542    /// [`ReportedContentScore::MIN`] and [`ReportedContentScore::MAX`].
4543    pub fn new_saturating(value: i8) -> Self {
4544        if value > Self::MAX {
4545            Self::MAX
4546        } else if value < Self::MIN {
4547            Self::MIN
4548        } else {
4549            Self(value)
4550        }
4551    }
4552
4553    /// The value of this score.
4554    pub fn value(&self) -> i8 {
4555        self.0
4556    }
4557}
4558
4559impl PartialEq<i8> for ReportedContentScore {
4560    fn eq(&self, other: &i8) -> bool {
4561        self.0.eq(other)
4562    }
4563}
4564
4565impl PartialEq<ReportedContentScore> for i8 {
4566    fn eq(&self, other: &ReportedContentScore) -> bool {
4567        self.eq(&other.0)
4568    }
4569}
4570
4571impl PartialOrd<i8> for ReportedContentScore {
4572    fn partial_cmp(&self, other: &i8) -> Option<std::cmp::Ordering> {
4573        self.0.partial_cmp(other)
4574    }
4575}
4576
4577impl PartialOrd<ReportedContentScore> for i8 {
4578    fn partial_cmp(&self, other: &ReportedContentScore) -> Option<std::cmp::Ordering> {
4579        self.partial_cmp(&other.0)
4580    }
4581}
4582
4583impl From<ReportedContentScore> for Int {
4584    fn from(value: ReportedContentScore) -> Self {
4585        value.0.into()
4586    }
4587}
4588
4589impl TryFrom<i8> for ReportedContentScore {
4590    type Error = TryFromReportedContentScoreError;
4591
4592    fn try_from(value: i8) -> std::prelude::v1::Result<Self, Self::Error> {
4593        if value > Self::MAX || value < Self::MIN {
4594            Err(TryFromReportedContentScoreError(()))
4595        } else {
4596            Ok(Self(value))
4597        }
4598    }
4599}
4600
4601impl TryFrom<i16> for ReportedContentScore {
4602    type Error = TryFromReportedContentScoreError;
4603
4604    fn try_from(value: i16) -> std::prelude::v1::Result<Self, Self::Error> {
4605        let value = i8::try_from(value).map_err(|_| TryFromReportedContentScoreError(()))?;
4606        value.try_into()
4607    }
4608}
4609
4610impl TryFrom<i32> for ReportedContentScore {
4611    type Error = TryFromReportedContentScoreError;
4612
4613    fn try_from(value: i32) -> std::prelude::v1::Result<Self, Self::Error> {
4614        let value = i8::try_from(value).map_err(|_| TryFromReportedContentScoreError(()))?;
4615        value.try_into()
4616    }
4617}
4618
4619impl TryFrom<i64> for ReportedContentScore {
4620    type Error = TryFromReportedContentScoreError;
4621
4622    fn try_from(value: i64) -> std::prelude::v1::Result<Self, Self::Error> {
4623        let value = i8::try_from(value).map_err(|_| TryFromReportedContentScoreError(()))?;
4624        value.try_into()
4625    }
4626}
4627
4628impl TryFrom<Int> for ReportedContentScore {
4629    type Error = TryFromReportedContentScoreError;
4630
4631    fn try_from(value: Int) -> std::prelude::v1::Result<Self, Self::Error> {
4632        let value = i8::try_from(value).map_err(|_| TryFromReportedContentScoreError(()))?;
4633        value.try_into()
4634    }
4635}
4636
4637trait EventSource {
4638    fn get_event(
4639        &self,
4640        event_id: &EventId,
4641    ) -> impl Future<Output = Result<TimelineEvent, Error>> + SendOutsideWasm;
4642}
4643
4644impl EventSource for &Room {
4645    async fn get_event(&self, event_id: &EventId) -> Result<TimelineEvent, Error> {
4646        self.load_or_fetch_event(event_id, None).await
4647    }
4648}
4649
4650/// The error type returned when a checked `ReportedContentScore` conversion
4651/// fails.
4652#[derive(Debug, Clone, Error)]
4653#[error("out of range conversion attempted")]
4654pub struct TryFromReportedContentScoreError(());
4655
4656/// Contains the current user's room member info and the optional room member
4657/// info of the sender of the `m.room.member` event that this info represents.
4658#[derive(Debug)]
4659pub struct RoomMemberWithSenderInfo {
4660    /// The actual room member.
4661    pub room_member: RoomMember,
4662    /// The info of the sender of the event `room_member` is based on, if
4663    /// available.
4664    pub sender_info: Option<RoomMember>,
4665}
4666
4667#[cfg(all(test, not(target_family = "wasm")))]
4668mod tests {
4669    use std::collections::BTreeMap;
4670
4671    use matrix_sdk_base::{ComposerDraft, DraftAttachment, store::ComposerDraftType};
4672    use matrix_sdk_test::{
4673        JoinedRoomBuilder, StateTestEvent, SyncResponseBuilder, async_test,
4674        event_factory::EventFactory, test_json,
4675    };
4676    use ruma::{
4677        RoomVersionId, event_id,
4678        events::{relation::RelationType, room::member::MembershipState},
4679        int, owned_event_id, room_id, user_id,
4680    };
4681    use wiremock::{
4682        Mock, MockServer, ResponseTemplate,
4683        matchers::{header, method, path_regex},
4684    };
4685
4686    use super::ReportedContentScore;
4687    use crate::{
4688        Client,
4689        config::RequestConfig,
4690        room::messages::{IncludeRelations, ListThreadsOptions, RelationsOptions},
4691        test_utils::{
4692            client::mock_matrix_session,
4693            logged_in_client,
4694            mocks::{MatrixMockServer, RoomRelationsResponseTemplate},
4695        },
4696    };
4697
4698    #[cfg(all(feature = "sqlite", feature = "e2e-encryption"))]
4699    #[async_test]
4700    async fn test_cache_invalidation_while_encrypt() {
4701        use matrix_sdk_base::store::RoomLoadSettings;
4702        use matrix_sdk_test::{DEFAULT_TEST_ROOM_ID, message_like_event_content};
4703
4704        let sqlite_path = std::env::temp_dir().join("cache_invalidation_while_encrypt.db");
4705        let session = mock_matrix_session();
4706
4707        let client = Client::builder()
4708            .homeserver_url("http://localhost:1234")
4709            .request_config(RequestConfig::new().disable_retry())
4710            .sqlite_store(&sqlite_path, None)
4711            .build()
4712            .await
4713            .unwrap();
4714        client
4715            .matrix_auth()
4716            .restore_session(session.clone(), RoomLoadSettings::default())
4717            .await
4718            .unwrap();
4719
4720        client.encryption().enable_cross_process_store_lock("client1".to_owned()).await.unwrap();
4721
4722        // Mock receiving an event to create an internal room.
4723        let server = MockServer::start().await;
4724        {
4725            Mock::given(method("GET"))
4726                .and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.*room.*encryption.?"))
4727                .and(header("authorization", "Bearer 1234"))
4728                .respond_with(
4729                    ResponseTemplate::new(200)
4730                        .set_body_json(&*test_json::sync_events::ENCRYPTION_CONTENT),
4731                )
4732                .mount(&server)
4733                .await;
4734            let response = SyncResponseBuilder::default()
4735                .add_joined_room(
4736                    JoinedRoomBuilder::default()
4737                        .add_state_event(StateTestEvent::Member)
4738                        .add_state_event(StateTestEvent::PowerLevels)
4739                        .add_state_event(StateTestEvent::Encryption),
4740                )
4741                .build_sync_response();
4742            client.base_client().receive_sync_response(response).await.unwrap();
4743        }
4744
4745        let room = client.get_room(&DEFAULT_TEST_ROOM_ID).expect("Room should exist");
4746
4747        // Step 1, preshare the room keys.
4748        room.preshare_room_key().await.unwrap();
4749
4750        // Step 2, force lock invalidation by pretending another client obtained the
4751        // lock.
4752        {
4753            let client = Client::builder()
4754                .homeserver_url("http://localhost:1234")
4755                .request_config(RequestConfig::new().disable_retry())
4756                .sqlite_store(&sqlite_path, None)
4757                .build()
4758                .await
4759                .unwrap();
4760            client
4761                .matrix_auth()
4762                .restore_session(session.clone(), RoomLoadSettings::default())
4763                .await
4764                .unwrap();
4765            client
4766                .encryption()
4767                .enable_cross_process_store_lock("client2".to_owned())
4768                .await
4769                .unwrap();
4770
4771            let guard = client.encryption().spin_lock_store(None).await.unwrap();
4772            assert!(guard.is_some());
4773        }
4774
4775        // Step 3, take the crypto-store lock.
4776        let guard = client.encryption().spin_lock_store(None).await.unwrap();
4777        assert!(guard.is_some());
4778
4779        // Step 4, try to encrypt a message.
4780        let olm = client.olm_machine().await;
4781        let olm = olm.as_ref().expect("Olm machine wasn't started");
4782
4783        // Now pretend we're encrypting an event; the olm machine shouldn't rely on
4784        // caching the outgoing session before.
4785        let _encrypted_content = olm
4786            .encrypt_room_event_raw(room.room_id(), "test-event", &message_like_event_content!({}))
4787            .await
4788            .unwrap();
4789    }
4790
4791    #[test]
4792    fn reported_content_score() {
4793        // i8
4794        let score = ReportedContentScore::new(0).unwrap();
4795        assert_eq!(score.value(), 0);
4796        let score = ReportedContentScore::new(-50).unwrap();
4797        assert_eq!(score.value(), -50);
4798        let score = ReportedContentScore::new(-100).unwrap();
4799        assert_eq!(score.value(), -100);
4800        assert_eq!(ReportedContentScore::new(10), None);
4801        assert_eq!(ReportedContentScore::new(-110), None);
4802
4803        let score = ReportedContentScore::new_saturating(0);
4804        assert_eq!(score.value(), 0);
4805        let score = ReportedContentScore::new_saturating(-50);
4806        assert_eq!(score.value(), -50);
4807        let score = ReportedContentScore::new_saturating(-100);
4808        assert_eq!(score.value(), -100);
4809        let score = ReportedContentScore::new_saturating(10);
4810        assert_eq!(score, ReportedContentScore::MAX);
4811        let score = ReportedContentScore::new_saturating(-110);
4812        assert_eq!(score, ReportedContentScore::MIN);
4813
4814        // i16
4815        let score = ReportedContentScore::try_from(0i16).unwrap();
4816        assert_eq!(score.value(), 0);
4817        let score = ReportedContentScore::try_from(-100i16).unwrap();
4818        assert_eq!(score.value(), -100);
4819        ReportedContentScore::try_from(10i16).unwrap_err();
4820        ReportedContentScore::try_from(-110i16).unwrap_err();
4821
4822        // i32
4823        let score = ReportedContentScore::try_from(0i32).unwrap();
4824        assert_eq!(score.value(), 0);
4825        let score = ReportedContentScore::try_from(-100i32).unwrap();
4826        assert_eq!(score.value(), -100);
4827        ReportedContentScore::try_from(10i32).unwrap_err();
4828        ReportedContentScore::try_from(-110i32).unwrap_err();
4829
4830        // i64
4831        let score = ReportedContentScore::try_from(0i64).unwrap();
4832        assert_eq!(score.value(), 0);
4833        let score = ReportedContentScore::try_from(-100i64).unwrap();
4834        assert_eq!(score.value(), -100);
4835        ReportedContentScore::try_from(10i64).unwrap_err();
4836        ReportedContentScore::try_from(-110i64).unwrap_err();
4837
4838        // Int
4839        let score = ReportedContentScore::try_from(int!(0)).unwrap();
4840        assert_eq!(score.value(), 0);
4841        let score = ReportedContentScore::try_from(int!(-100)).unwrap();
4842        assert_eq!(score.value(), -100);
4843        ReportedContentScore::try_from(int!(10)).unwrap_err();
4844        ReportedContentScore::try_from(int!(-110)).unwrap_err();
4845    }
4846
4847    #[async_test]
4848    async fn test_composer_draft() {
4849        use matrix_sdk_test::DEFAULT_TEST_ROOM_ID;
4850
4851        let client = logged_in_client(None).await;
4852
4853        let response = SyncResponseBuilder::default()
4854            .add_joined_room(JoinedRoomBuilder::default())
4855            .build_sync_response();
4856        client.base_client().receive_sync_response(response).await.unwrap();
4857        let room = client.get_room(&DEFAULT_TEST_ROOM_ID).expect("Room should exist");
4858
4859        assert_eq!(room.load_composer_draft(None).await.unwrap(), None);
4860
4861        // Save 2 drafts, one for the room and one for a thread.
4862
4863        let draft = ComposerDraft {
4864            plain_text: "Hello, world!".to_owned(),
4865            html_text: Some("<strong>Hello</strong>, world!".to_owned()),
4866            draft_type: ComposerDraftType::NewMessage,
4867            attachments: vec![DraftAttachment {
4868                filename: "cat.txt".to_owned(),
4869                content: matrix_sdk_base::DraftAttachmentContent::File {
4870                    data: b"meow".to_vec(),
4871                    mimetype: Some("text/plain".to_owned()),
4872                    size: Some(5),
4873                },
4874            }],
4875        };
4876
4877        room.save_composer_draft(draft.clone(), None).await.unwrap();
4878
4879        let thread_root = owned_event_id!("$thread_root:b.c");
4880        let thread_draft = ComposerDraft {
4881            plain_text: "Hello, thread!".to_owned(),
4882            html_text: Some("<strong>Hello</strong>, thread!".to_owned()),
4883            draft_type: ComposerDraftType::NewMessage,
4884            attachments: vec![DraftAttachment {
4885                filename: "dog.txt".to_owned(),
4886                content: matrix_sdk_base::DraftAttachmentContent::File {
4887                    data: b"wuv".to_vec(),
4888                    mimetype: Some("text/plain".to_owned()),
4889                    size: Some(4),
4890                },
4891            }],
4892        };
4893
4894        room.save_composer_draft(thread_draft.clone(), Some(&thread_root)).await.unwrap();
4895
4896        // Check that the room draft was saved correctly
4897        assert_eq!(room.load_composer_draft(None).await.unwrap(), Some(draft));
4898
4899        // Check that the thread draft was saved correctly
4900        assert_eq!(
4901            room.load_composer_draft(Some(&thread_root)).await.unwrap(),
4902            Some(thread_draft.clone())
4903        );
4904
4905        // Clear the room draft
4906        room.clear_composer_draft(None).await.unwrap();
4907        assert_eq!(room.load_composer_draft(None).await.unwrap(), None);
4908
4909        // Check that the thread one is still there
4910        assert_eq!(room.load_composer_draft(Some(&thread_root)).await.unwrap(), Some(thread_draft));
4911
4912        // Clear the thread draft as well
4913        room.clear_composer_draft(Some(&thread_root)).await.unwrap();
4914        assert_eq!(room.load_composer_draft(Some(&thread_root)).await.unwrap(), None);
4915    }
4916
4917    #[async_test]
4918    async fn test_mark_join_requests_as_seen() {
4919        let server = MatrixMockServer::new().await;
4920        let client = server.client_builder().build().await;
4921        let event_id = event_id!("$a:b.c");
4922        let room_id = room_id!("!a:b.c");
4923        let user_id = user_id!("@alice:b.c");
4924
4925        let f = EventFactory::new().room(room_id);
4926        let joined_room_builder = JoinedRoomBuilder::new(room_id).add_state_bulk(vec![
4927            f.member(user_id).membership(MembershipState::Knock).event_id(event_id).into(),
4928        ]);
4929        let room = server.sync_room(&client, joined_room_builder).await;
4930
4931        // When loading the initial seen ids, there are none
4932        let seen_ids =
4933            room.get_seen_knock_request_ids().await.expect("Couldn't load seen join request ids");
4934        assert!(seen_ids.is_empty());
4935
4936        // We mark a random event id as seen
4937        room.mark_knock_requests_as_seen(&[user_id.to_owned()])
4938            .await
4939            .expect("Couldn't mark join request as seen");
4940
4941        // Then we can check it was successfully marked as seen
4942        let seen_ids =
4943            room.get_seen_knock_request_ids().await.expect("Couldn't load seen join request ids");
4944        assert_eq!(seen_ids.len(), 1);
4945        assert_eq!(
4946            seen_ids.into_iter().next().expect("No next value"),
4947            (event_id.to_owned(), user_id.to_owned())
4948        )
4949    }
4950
4951    #[async_test]
4952    async fn test_own_room_membership_with_no_own_member_event() {
4953        let server = MatrixMockServer::new().await;
4954        let client = server.client_builder().build().await;
4955        let room_id = room_id!("!a:b.c");
4956
4957        let room = server.sync_joined_room(&client, room_id).await;
4958
4959        // Since there is no member event for the own user, the method fails.
4960        // This should never happen in an actual room.
4961        let error = room.member_with_sender_info(client.user_id().unwrap()).await.err();
4962        assert!(error.is_some());
4963    }
4964
4965    #[async_test]
4966    async fn test_own_room_membership_with_own_member_event_but_unknown_sender() {
4967        let server = MatrixMockServer::new().await;
4968        let client = server.client_builder().build().await;
4969        let room_id = room_id!("!a:b.c");
4970        let user_id = user_id!("@example:localhost");
4971
4972        let f = EventFactory::new().room(room_id).sender(user_id!("@alice:b.c"));
4973        let joined_room_builder =
4974            JoinedRoomBuilder::new(room_id).add_state_bulk(vec![f.member(user_id).into()]);
4975        let room = server.sync_room(&client, joined_room_builder).await;
4976
4977        // When we load the membership details
4978        let ret = room
4979            .member_with_sender_info(client.user_id().unwrap())
4980            .await
4981            .expect("Room member info should be available");
4982
4983        // We get the member info for the current user
4984        assert_eq!(ret.room_member.event().user_id(), user_id);
4985
4986        // But there is no info for the sender
4987        assert!(ret.sender_info.is_none());
4988    }
4989
4990    #[async_test]
4991    async fn test_own_room_membership_with_own_member_event_and_own_sender() {
4992        let server = MatrixMockServer::new().await;
4993        let client = server.client_builder().build().await;
4994        let room_id = room_id!("!a:b.c");
4995        let user_id = user_id!("@example:localhost");
4996
4997        let f = EventFactory::new().room(room_id).sender(user_id);
4998        let joined_room_builder =
4999            JoinedRoomBuilder::new(room_id).add_state_bulk(vec![f.member(user_id).into()]);
5000        let room = server.sync_room(&client, joined_room_builder).await;
5001
5002        // When we load the membership details
5003        let ret = room
5004            .member_with_sender_info(client.user_id().unwrap())
5005            .await
5006            .expect("Room member info should be available");
5007
5008        // We get the current user's member info
5009        assert_eq!(ret.room_member.event().user_id(), user_id);
5010
5011        // And the sender has the same info, since it's also the current user
5012        assert!(ret.sender_info.is_some());
5013        assert_eq!(ret.sender_info.unwrap().event().user_id(), user_id);
5014    }
5015
5016    #[async_test]
5017    async fn test_own_room_membership_with_own_member_event_and_known_sender() {
5018        let server = MatrixMockServer::new().await;
5019        let client = server.client_builder().build().await;
5020        let room_id = room_id!("!a:b.c");
5021        let user_id = user_id!("@example:localhost");
5022        let sender_id = user_id!("@alice:b.c");
5023
5024        let f = EventFactory::new().room(room_id).sender(sender_id);
5025        let joined_room_builder = JoinedRoomBuilder::new(room_id).add_state_bulk(vec![
5026            f.member(user_id).into(),
5027            // The sender info comes from the sync
5028            f.member(sender_id).into(),
5029        ]);
5030        let room = server.sync_room(&client, joined_room_builder).await;
5031
5032        // When we load the membership details
5033        let ret = room
5034            .member_with_sender_info(client.user_id().unwrap())
5035            .await
5036            .expect("Room member info should be available");
5037
5038        // We get the current user's member info
5039        assert_eq!(ret.room_member.event().user_id(), user_id);
5040
5041        // And also the sender info from the events received in the sync
5042        assert!(ret.sender_info.is_some());
5043        assert_eq!(ret.sender_info.unwrap().event().user_id(), sender_id);
5044    }
5045
5046    #[async_test]
5047    async fn test_own_room_membership_with_own_member_event_and_unknown_but_available_sender() {
5048        let server = MatrixMockServer::new().await;
5049        let client = server.client_builder().build().await;
5050        let room_id = room_id!("!a:b.c");
5051        let user_id = user_id!("@example:localhost");
5052        let sender_id = user_id!("@alice:b.c");
5053
5054        let f = EventFactory::new().room(room_id).sender(sender_id);
5055        let joined_room_builder =
5056            JoinedRoomBuilder::new(room_id).add_state_bulk(vec![f.member(user_id).into()]);
5057        let room = server.sync_room(&client, joined_room_builder).await;
5058
5059        // We'll receive the member info through the /members endpoint
5060        server
5061            .mock_get_members()
5062            .ok(vec![f.member(sender_id).into_raw()])
5063            .mock_once()
5064            .mount()
5065            .await;
5066
5067        // We get the current user's member info
5068        let ret = room
5069            .member_with_sender_info(client.user_id().unwrap())
5070            .await
5071            .expect("Room member info should be available");
5072
5073        // We get the current user's member info
5074        assert_eq!(ret.room_member.event().user_id(), user_id);
5075
5076        // And also the sender info from the /members endpoint
5077        assert!(ret.sender_info.is_some());
5078        assert_eq!(ret.sender_info.unwrap().event().user_id(), sender_id);
5079    }
5080
5081    #[async_test]
5082    async fn test_list_threads() {
5083        let server = MatrixMockServer::new().await;
5084        let client = server.client_builder().build().await;
5085
5086        let room_id = room_id!("!a:b.c");
5087        let sender_id = user_id!("@alice:b.c");
5088        let f = EventFactory::new().room(room_id).sender(sender_id);
5089
5090        let eid1 = event_id!("$1");
5091        let eid2 = event_id!("$2");
5092        let batch1 = vec![f.text_msg("Thread root 1").event_id(eid1).into_raw()];
5093        let batch2 = vec![f.text_msg("Thread root 2").event_id(eid2).into_raw()];
5094
5095        server
5096            .mock_room_threads()
5097            .ok(batch1.clone(), Some("prev_batch".to_owned()))
5098            .mock_once()
5099            .mount()
5100            .await;
5101        server
5102            .mock_room_threads()
5103            .match_from("prev_batch")
5104            .ok(batch2, None)
5105            .mock_once()
5106            .mount()
5107            .await;
5108
5109        let room = server.sync_joined_room(&client, room_id).await;
5110        let result =
5111            room.list_threads(ListThreadsOptions::default()).await.expect("Failed to list threads");
5112        assert_eq!(result.chunk.len(), 1);
5113        assert_eq!(result.chunk[0].event_id().unwrap(), eid1);
5114        assert!(result.prev_batch_token.is_some());
5115
5116        let opts = ListThreadsOptions { from: result.prev_batch_token, ..Default::default() };
5117        let result = room.list_threads(opts).await.expect("Failed to list threads");
5118        assert_eq!(result.chunk.len(), 1);
5119        assert_eq!(result.chunk[0].event_id().unwrap(), eid2);
5120        assert!(result.prev_batch_token.is_none());
5121    }
5122
5123    #[async_test]
5124    async fn test_relations() {
5125        let server = MatrixMockServer::new().await;
5126        let client = server.client_builder().build().await;
5127
5128        let room_id = room_id!("!a:b.c");
5129        let sender_id = user_id!("@alice:b.c");
5130        let f = EventFactory::new().room(room_id).sender(sender_id);
5131
5132        let target_event_id = owned_event_id!("$target");
5133        let eid1 = event_id!("$1");
5134        let eid2 = event_id!("$2");
5135        let batch1 = vec![f.text_msg("Related event 1").event_id(eid1).into_raw()];
5136        let batch2 = vec![f.text_msg("Related event 2").event_id(eid2).into_raw()];
5137
5138        server
5139            .mock_room_relations()
5140            .match_target_event(target_event_id.clone())
5141            .ok(RoomRelationsResponseTemplate::default().events(batch1).next_batch("next_batch"))
5142            .mock_once()
5143            .mount()
5144            .await;
5145
5146        server
5147            .mock_room_relations()
5148            .match_target_event(target_event_id.clone())
5149            .match_from("next_batch")
5150            .ok(RoomRelationsResponseTemplate::default().events(batch2))
5151            .mock_once()
5152            .mount()
5153            .await;
5154
5155        let room = server.sync_joined_room(&client, room_id).await;
5156
5157        // Main endpoint: no relation type filtered out.
5158        let mut opts = RelationsOptions {
5159            include_relations: IncludeRelations::AllRelations,
5160            ..Default::default()
5161        };
5162        let result = room
5163            .relations(target_event_id.clone(), opts.clone())
5164            .await
5165            .expect("Failed to list relations the first time");
5166        assert_eq!(result.chunk.len(), 1);
5167        assert_eq!(result.chunk[0].event_id().unwrap(), eid1);
5168        assert!(result.prev_batch_token.is_none());
5169        assert!(result.next_batch_token.is_some());
5170        assert!(result.recursion_depth.is_none());
5171
5172        opts.from = result.next_batch_token;
5173        let result = room
5174            .relations(target_event_id, opts)
5175            .await
5176            .expect("Failed to list relations the second time");
5177        assert_eq!(result.chunk.len(), 1);
5178        assert_eq!(result.chunk[0].event_id().unwrap(), eid2);
5179        assert!(result.prev_batch_token.is_none());
5180        assert!(result.next_batch_token.is_none());
5181        assert!(result.recursion_depth.is_none());
5182    }
5183
5184    #[async_test]
5185    async fn test_relations_with_reltype() {
5186        let server = MatrixMockServer::new().await;
5187        let client = server.client_builder().build().await;
5188
5189        let room_id = room_id!("!a:b.c");
5190        let sender_id = user_id!("@alice:b.c");
5191        let f = EventFactory::new().room(room_id).sender(sender_id);
5192
5193        let target_event_id = owned_event_id!("$target");
5194        let eid1 = event_id!("$1");
5195        let eid2 = event_id!("$2");
5196        let batch1 = vec![f.text_msg("In-thread event 1").event_id(eid1).into_raw()];
5197        let batch2 = vec![f.text_msg("In-thread event 2").event_id(eid2).into_raw()];
5198
5199        server
5200            .mock_room_relations()
5201            .match_target_event(target_event_id.clone())
5202            .match_subrequest(IncludeRelations::RelationsOfType(RelationType::Thread))
5203            .ok(RoomRelationsResponseTemplate::default().events(batch1).next_batch("next_batch"))
5204            .mock_once()
5205            .mount()
5206            .await;
5207
5208        server
5209            .mock_room_relations()
5210            .match_target_event(target_event_id.clone())
5211            .match_from("next_batch")
5212            .match_subrequest(IncludeRelations::RelationsOfType(RelationType::Thread))
5213            .ok(RoomRelationsResponseTemplate::default().events(batch2))
5214            .mock_once()
5215            .mount()
5216            .await;
5217
5218        let room = server.sync_joined_room(&client, room_id).await;
5219
5220        // Reltype-filtered endpoint, for threads \o/
5221        let mut opts = RelationsOptions {
5222            include_relations: IncludeRelations::RelationsOfType(RelationType::Thread),
5223            ..Default::default()
5224        };
5225        let result = room
5226            .relations(target_event_id.clone(), opts.clone())
5227            .await
5228            .expect("Failed to list relations the first time");
5229        assert_eq!(result.chunk.len(), 1);
5230        assert_eq!(result.chunk[0].event_id().unwrap(), eid1);
5231        assert!(result.prev_batch_token.is_none());
5232        assert!(result.next_batch_token.is_some());
5233        assert!(result.recursion_depth.is_none());
5234
5235        opts.from = result.next_batch_token;
5236        let result = room
5237            .relations(target_event_id, opts)
5238            .await
5239            .expect("Failed to list relations the second time");
5240        assert_eq!(result.chunk.len(), 1);
5241        assert_eq!(result.chunk[0].event_id().unwrap(), eid2);
5242        assert!(result.prev_batch_token.is_none());
5243        assert!(result.next_batch_token.is_none());
5244        assert!(result.recursion_depth.is_none());
5245    }
5246
5247    #[async_test]
5248    async fn test_power_levels_computation() {
5249        let server = MatrixMockServer::new().await;
5250        let client = server.client_builder().build().await;
5251
5252        let room_id = room_id!("!a:b.c");
5253        let sender_id = client.user_id().expect("No session id");
5254        let f = EventFactory::new().room(room_id).sender(sender_id);
5255        let mut user_map = BTreeMap::from([(sender_id.into(), 50.into())]);
5256
5257        // Computing the power levels will need these 3 state events:
5258        let room_create_event = f.create(sender_id, RoomVersionId::V1).state_key("").into();
5259        let power_levels_event = f.power_levels(&mut user_map).state_key("").into();
5260        let room_member_event = f.member(sender_id).into();
5261
5262        // With only the room member event
5263        let room = server
5264            .sync_room(&client, JoinedRoomBuilder::new(room_id).add_state_bulk([room_member_event]))
5265            .await;
5266        let ctx = room
5267            .push_condition_room_ctx()
5268            .await
5269            .expect("Failed to get push condition context")
5270            .expect("Could not get push condition context");
5271
5272        // The internal power levels couldn't be computed
5273        assert!(ctx.power_levels.is_none());
5274
5275        // Adding the room creation event
5276        let room = server
5277            .sync_room(&client, JoinedRoomBuilder::new(room_id).add_state_bulk([room_create_event]))
5278            .await;
5279        let ctx = room
5280            .push_condition_room_ctx()
5281            .await
5282            .expect("Failed to get push condition context")
5283            .expect("Could not get push condition context");
5284
5285        // The internal power levels still couldn't be computed
5286        assert!(ctx.power_levels.is_none());
5287
5288        // With the room member, room creation and the power levels events
5289        let room = server
5290            .sync_room(
5291                &client,
5292                JoinedRoomBuilder::new(room_id).add_state_bulk([power_levels_event]),
5293            )
5294            .await;
5295        let ctx = room
5296            .push_condition_room_ctx()
5297            .await
5298            .expect("Failed to get push condition context")
5299            .expect("Could not get push condition context");
5300
5301        // The internal power levels can finally be computed
5302        assert!(ctx.power_levels.is_some());
5303    }
5304}