matrix_sdk_test/sync_builder/
joined_room.rs

1use ruma::{
2    api::client::sync::sync_events::v3::JoinedRoom,
3    events::{
4        receipt::ReceiptEventContent, typing::TypingEventContent, AnyRoomAccountDataEvent,
5        AnySyncStateEvent, AnySyncTimelineEvent,
6    },
7    serde::Raw,
8    OwnedRoomId, RoomId,
9};
10use serde_json::{from_value as from_json_value, Value as JsonValue};
11
12use super::{RoomAccountDataTestEvent, StateTestEvent};
13use crate::{event_factory::EventBuilder, DEFAULT_TEST_ROOM_ID};
14
15pub struct JoinedRoomBuilder {
16    pub(super) room_id: OwnedRoomId,
17    pub(super) inner: JoinedRoom,
18}
19
20impl JoinedRoomBuilder {
21    /// Create a new `JoinedRoomBuilder` for the given room ID.
22    ///
23    /// If the room ID is [`DEFAULT_TEST_ROOM_ID`],
24    /// [`JoinedRoomBuilder::default()`] can be used instead.
25    pub fn new(room_id: &RoomId) -> Self {
26        Self { room_id: room_id.to_owned(), inner: Default::default() }
27    }
28
29    /// Get the room ID of this [`JoinedRoomBuilder`].
30    pub fn room_id(&self) -> &RoomId {
31        &self.room_id
32    }
33
34    /// Add an event to the timeline.
35    ///
36    /// The raw event can be created with the
37    /// [`sync_timeline_event`](crate::sync_timeline_event) macro.
38    pub fn add_timeline_event(mut self, event: impl Into<Raw<AnySyncTimelineEvent>>) -> Self {
39        self.inner.timeline.events.push(event.into());
40        self
41    }
42
43    /// Add events in bulk to the timeline.
44    pub fn add_timeline_bulk<I>(mut self, events: I) -> Self
45    where
46        I: IntoIterator<Item = Raw<AnySyncTimelineEvent>>,
47    {
48        self.inner.timeline.events.extend(events);
49        self
50    }
51
52    /// Add state events in bulk to the timeline.
53    ///
54    /// This is a convenience method that casts `Raw<AnySyncStateEvent>` to
55    /// `Raw<AnySyncTimelineEvent>` and calls `JoinedRoom::add_timeline_bulk()`.
56    pub fn add_timeline_state_bulk<I>(self, events: I) -> Self
57    where
58        I: IntoIterator<Item = Raw<AnySyncStateEvent>>,
59    {
60        let events = events.into_iter().map(|event| event.cast());
61        self.add_timeline_bulk(events)
62    }
63
64    /// Set the timeline as limited.
65    pub fn set_timeline_limited(mut self) -> Self {
66        self.inner.timeline.limited = true;
67        self
68    }
69
70    /// Set the `prev_batch` of the timeline.
71    pub fn set_timeline_prev_batch(mut self, prev_batch: impl Into<String>) -> Self {
72        self.inner.timeline.prev_batch = Some(prev_batch.into());
73        self
74    }
75
76    /// Add an event to the state.
77    pub fn add_state_event(mut self, event: StateTestEvent) -> Self {
78        self.inner.state.events.push(event.into_raw_event());
79        self
80    }
81
82    /// Add events in bulk to the state.
83    pub fn add_state_bulk<I>(mut self, events: I) -> Self
84    where
85        I: IntoIterator<Item = Raw<AnySyncStateEvent>>,
86    {
87        self.inner.state.events.extend(events);
88        self
89    }
90
91    /// Add a single read receipt to the joined room's ephemeral events.
92    pub fn add_receipt(mut self, f: EventBuilder<ReceiptEventContent>) -> Self {
93        self.inner.ephemeral.events.push(f.into_raw());
94        self
95    }
96
97    /// Add a typing notification event for this sync.
98    pub fn add_typing(mut self, f: EventBuilder<TypingEventContent>) -> Self {
99        self.inner.ephemeral.events.push(f.into_raw());
100        self
101    }
102
103    /// Add room account data.
104    pub fn add_account_data(mut self, event: RoomAccountDataTestEvent) -> Self {
105        self.inner.account_data.events.push(event.into_raw_event());
106        self
107    }
108
109    /// Add room account data in bulk.
110    pub fn add_account_data_bulk<I>(mut self, events: I) -> Self
111    where
112        I: IntoIterator<Item = Raw<AnyRoomAccountDataEvent>>,
113    {
114        self.inner.account_data.events.extend(events);
115        self
116    }
117
118    /// Set the room summary.
119    pub fn set_room_summary(mut self, summary: JsonValue) -> Self {
120        self.inner.summary = from_json_value(summary).unwrap();
121        self
122    }
123
124    /// Set the unread notifications count.
125    pub fn set_unread_notifications_count(mut self, unread_notifications: JsonValue) -> Self {
126        self.inner.unread_notifications = from_json_value(unread_notifications).unwrap();
127        self
128    }
129}
130
131impl Default for JoinedRoomBuilder {
132    fn default() -> Self {
133        Self::new(&DEFAULT_TEST_ROOM_ID)
134    }
135}