Skip to main content

matrix_sdk_test/sync_builder/
mod.rs

1use std::collections::HashMap;
2
3use http::Response;
4use ruma::{
5    OwnedRoomId, OwnedUserId, UserId,
6    api::{
7        IncomingResponse,
8        client::sync::sync_events::v3::{
9            InvitedRoom, JoinedRoom, KnockedRoom, LeftRoom, Response as SyncResponse, State,
10        },
11    },
12    events::{
13        AnyGlobalAccountDataEvent, AnySyncStateEvent, AnyToDeviceEvent, presence::PresenceEvent,
14    },
15    serde::Raw,
16};
17use serde_json::{Value as JsonValue, from_value as from_json_value, json};
18
19mod bulk;
20mod invited_room;
21mod joined_room;
22mod knocked_room;
23mod left_room;
24
25pub use bulk::bulk_room_members;
26pub use invited_room::InvitedRoomBuilder;
27pub use joined_room::JoinedRoomBuilder;
28pub use knocked_room::KnockedRoomBuilder;
29pub use left_room::LeftRoomBuilder;
30
31/// The `SyncResponseBuilder` struct can be used to easily generate valid sync
32/// responses for testing. These can be then fed into either `Client` or `Room`.
33///
34/// It supports generated a number of canned events, such as a member entering a
35/// room, his power level and display name changing and similar. It also
36/// supports insertion of custom events in the form of `EventsJson` values.
37#[derive(Default)]
38pub struct SyncResponseBuilder {
39    /// Updates to joined `Room`s.
40    joined_rooms: HashMap<OwnedRoomId, JoinedRoom>,
41    /// Updates to invited `Room`s.
42    invited_rooms: HashMap<OwnedRoomId, InvitedRoom>,
43    /// Updates to left `Room`s.
44    left_rooms: HashMap<OwnedRoomId, LeftRoom>,
45    /// Updates to knocked `Room`s.
46    knocked_rooms: HashMap<OwnedRoomId, KnockedRoom>,
47    /// Events that determine the presence state of a user.
48    presence: Vec<Raw<PresenceEvent>>,
49    /// Global account data events.
50    account_data: Vec<Raw<AnyGlobalAccountDataEvent>>,
51    /// Internal counter to enable the `prev_batch` and `next_batch` of each
52    /// sync response to vary.
53    batch_counter: i64,
54    /// The device lists of the user.
55    changed_device_lists: Vec<OwnedUserId>,
56    to_device_events: Vec<Raw<AnyToDeviceEvent>>,
57}
58
59impl SyncResponseBuilder {
60    pub fn new() -> Self {
61        Self::default()
62    }
63
64    /// Add a joined room to the next sync response.
65    ///
66    /// If a room with the same room ID already exists, it is replaced by this
67    /// one.
68    pub fn add_joined_room(&mut self, room: JoinedRoomBuilder) -> &mut Self {
69        self.invited_rooms.remove(&room.room_id);
70        self.left_rooms.remove(&room.room_id);
71        self.knocked_rooms.remove(&room.room_id);
72        self.joined_rooms.insert(room.room_id, room.inner);
73        self
74    }
75
76    /// Add an invited room to the next sync response.
77    ///
78    /// If a room with the same room ID already exists, it is replaced by this
79    /// one.
80    pub fn add_invited_room(&mut self, room: InvitedRoomBuilder) -> &mut Self {
81        self.joined_rooms.remove(&room.room_id);
82        self.left_rooms.remove(&room.room_id);
83        self.knocked_rooms.remove(&room.room_id);
84        self.invited_rooms.insert(room.room_id, room.inner);
85        self
86    }
87
88    /// Add a left room to the next sync response.
89    ///
90    /// If a room with the same room ID already exists, it is replaced by this
91    /// one.
92    pub fn add_left_room(&mut self, room: LeftRoomBuilder) -> &mut Self {
93        self.joined_rooms.remove(&room.room_id);
94        self.invited_rooms.remove(&room.room_id);
95        self.knocked_rooms.remove(&room.room_id);
96        self.left_rooms.insert(room.room_id, room.inner);
97        self
98    }
99
100    /// Add a knocked room to the next sync response.
101    ///
102    /// If a room with the same room ID already exists, it is replaced by this
103    /// one.
104    pub fn add_knocked_room(&mut self, room: KnockedRoomBuilder) -> &mut Self {
105        self.joined_rooms.remove(&room.room_id);
106        self.invited_rooms.remove(&room.room_id);
107        self.left_rooms.remove(&room.room_id);
108        self.knocked_rooms.insert(room.room_id, room.inner);
109        self
110    }
111
112    /// Add presence in bulk.
113    pub fn add_presence_bulk<I>(&mut self, events: I) -> &mut Self
114    where
115        I: IntoIterator<Item = Raw<PresenceEvent>>,
116    {
117        self.presence.extend(events);
118        self
119    }
120
121    /// Add global account data.
122    pub fn add_global_account_data(
123        &mut self,
124        event: impl Into<Raw<AnyGlobalAccountDataEvent>>,
125    ) -> &mut Self {
126        self.account_data.push(event.into());
127        self
128    }
129
130    /// Add custom global account data based on a JSON value.
131    pub fn add_custom_global_account_data(&mut self, event: serde_json::Value) -> &mut Self {
132        self.account_data.push(Raw::new(&event).unwrap().cast_unchecked());
133        self
134    }
135
136    pub fn add_change_device(&mut self, user_id: &UserId) -> &mut Self {
137        self.changed_device_lists.push(user_id.to_owned());
138        self
139    }
140
141    /// Add a to device event.
142    pub fn add_to_device_event(&mut self, event: JsonValue) -> &mut Self {
143        self.to_device_events.push(from_json_value(event).unwrap());
144        self
145    }
146
147    /// Builds a sync response as a JSON Value containing the events we queued
148    /// so far.
149    ///
150    /// The next response returned by `build_sync_response` will then be empty
151    /// if no further events were queued.
152    ///
153    /// This method is raw JSON equivalent to
154    /// [build_sync_response()](#method.build_sync_response), use
155    /// [build_sync_response()](#method.build_sync_response) if you need a typed
156    /// response.
157    pub fn build_json_sync_response(&mut self) -> JsonValue {
158        self.batch_counter += 1;
159        let next_batch = self.generate_sync_token();
160
161        let body = json! {
162            {
163                "device_one_time_keys_count": {},
164                "next_batch": next_batch,
165                "device_lists": {
166                    "changed": self.changed_device_lists,
167                    "left": [],
168                },
169                "rooms": {
170                    "invite": self.invited_rooms,
171                    "join": self.joined_rooms,
172                    "leave": self.left_rooms,
173                    "knock": self.knocked_rooms,
174                },
175                "to_device": {
176                    "events": self.to_device_events,
177                },
178                "presence": {
179                    "events": self.presence,
180                },
181                "account_data": {
182                    "events": self.account_data,
183                },
184            }
185        };
186
187        // Clear state so that the next sync response will be empty if nothing
188        // was added.
189        self.clear();
190
191        body
192    }
193
194    /// Builds a `SyncResponse` containing the events we queued so far.
195    ///
196    /// The next response returned by `build_sync_response` will then be empty
197    /// if no further events were queued.
198    ///
199    /// This method is high level and typed equivalent to
200    /// [build_json_sync_response()](#method.build_json_sync_response), use
201    /// [build_json_sync_response()](#method.build_json_sync_response) if you
202    /// need an untyped response.
203    pub fn build_sync_response(&mut self) -> SyncResponse {
204        let body = self.build_json_sync_response();
205
206        let response = Response::builder().body(serde_json::to_vec(&body).unwrap()).unwrap();
207
208        SyncResponse::try_from_http_response(response).unwrap()
209    }
210
211    fn generate_sync_token(&self) -> String {
212        format!("t392-516_47314_0_7_1_1_1_11444_{}", self.batch_counter)
213    }
214
215    pub fn clear(&mut self) {
216        self.account_data.clear();
217        self.invited_rooms.clear();
218        self.joined_rooms.clear();
219        self.left_rooms.clear();
220        self.knocked_rooms.clear();
221        self.presence.clear();
222    }
223}
224
225/// Helper trait to mutate the data in [`State`].
226trait StateMutExt {
227    /// Use the `After` variant rather than `Before`.
228    fn use_state_after(&mut self);
229    /// Access the inner list of state events.
230    fn events_mut(&mut self) -> &mut Vec<Raw<AnySyncStateEvent>>;
231}
232
233impl StateMutExt for State {
234    fn use_state_after(&mut self) {
235        *self = Self::After(Default::default());
236    }
237
238    fn events_mut(&mut self) -> &mut Vec<Raw<AnySyncStateEvent>> {
239        match self {
240            Self::Before(state) => &mut state.events,
241            Self::After(state) => &mut state.events,
242            // We don't allow to construct another variant.
243            _ => unreachable!(),
244        }
245    }
246}