matrix_sdk_test/sync_builder/
joined_room.rs

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