matrix_sdk_test/sync_builder/
joined_room.rs

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