matrix_sdk_test/sync_builder/
mod.rsuse std::collections::HashMap;
use http::Response;
use ruma::{
api::{
client::sync::sync_events::v3::{
InvitedRoom, JoinedRoom, KnockedRoom, LeftRoom, Response as SyncResponse,
},
IncomingResponse,
},
events::{presence::PresenceEvent, AnyGlobalAccountDataEvent},
serde::Raw,
OwnedRoomId, OwnedUserId, UserId,
};
use serde_json::{from_value as from_json_value, json, Value as JsonValue};
use super::test_json;
mod bulk;
mod invited_room;
mod joined_room;
mod knocked_room;
mod left_room;
mod test_event;
pub use bulk::bulk_room_members;
pub use invited_room::InvitedRoomBuilder;
pub use joined_room::JoinedRoomBuilder;
pub use knocked_room::KnockedRoomBuilder;
pub use left_room::LeftRoomBuilder;
pub use test_event::{
EphemeralTestEvent, GlobalAccountDataTestEvent, PresenceTestEvent, RoomAccountDataTestEvent,
StateTestEvent, StrippedStateTestEvent,
};
#[derive(Default)]
pub struct SyncResponseBuilder {
joined_rooms: HashMap<OwnedRoomId, JoinedRoom>,
invited_rooms: HashMap<OwnedRoomId, InvitedRoom>,
left_rooms: HashMap<OwnedRoomId, LeftRoom>,
knocked_rooms: HashMap<OwnedRoomId, KnockedRoom>,
presence: Vec<Raw<PresenceEvent>>,
account_data: Vec<Raw<AnyGlobalAccountDataEvent>>,
batch_counter: i64,
changed_device_lists: Vec<OwnedUserId>,
}
impl SyncResponseBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn add_joined_room(&mut self, room: JoinedRoomBuilder) -> &mut Self {
self.invited_rooms.remove(&room.room_id);
self.left_rooms.remove(&room.room_id);
self.knocked_rooms.remove(&room.room_id);
self.joined_rooms.insert(room.room_id, room.inner);
self
}
pub fn add_invited_room(&mut self, room: InvitedRoomBuilder) -> &mut Self {
self.joined_rooms.remove(&room.room_id);
self.left_rooms.remove(&room.room_id);
self.knocked_rooms.remove(&room.room_id);
self.invited_rooms.insert(room.room_id, room.inner);
self
}
pub fn add_left_room(&mut self, room: LeftRoomBuilder) -> &mut Self {
self.joined_rooms.remove(&room.room_id);
self.invited_rooms.remove(&room.room_id);
self.knocked_rooms.remove(&room.room_id);
self.left_rooms.insert(room.room_id, room.inner);
self
}
pub fn add_knocked_room(&mut self, room: KnockedRoomBuilder) -> &mut Self {
self.joined_rooms.remove(&room.room_id);
self.invited_rooms.remove(&room.room_id);
self.left_rooms.remove(&room.room_id);
self.knocked_rooms.insert(room.room_id, room.inner);
self
}
pub fn add_presence_event(&mut self, event: PresenceTestEvent) -> &mut Self {
let val = match event {
PresenceTestEvent::Presence => test_json::PRESENCE.to_owned(),
PresenceTestEvent::Custom(json) => json,
};
self.presence.push(from_json_value(val).unwrap());
self
}
pub fn add_presence_bulk<I>(&mut self, events: I) -> &mut Self
where
I: IntoIterator<Item = Raw<PresenceEvent>>,
{
self.presence.extend(events);
self
}
pub fn add_global_account_data_event(
&mut self,
event: GlobalAccountDataTestEvent,
) -> &mut Self {
let val = match event {
GlobalAccountDataTestEvent::Direct => test_json::DIRECT.to_owned(),
GlobalAccountDataTestEvent::PushRules => test_json::PUSH_RULES.to_owned(),
GlobalAccountDataTestEvent::Custom(json) => json,
};
self.account_data.push(from_json_value(val).unwrap());
self
}
pub fn add_global_account_data_bulk<I>(&mut self, events: I) -> &mut Self
where
I: IntoIterator<Item = Raw<AnyGlobalAccountDataEvent>>,
{
self.account_data.extend(events);
self
}
pub fn add_change_device(&mut self, user_id: &UserId) -> &mut Self {
self.changed_device_lists.push(user_id.to_owned());
self
}
pub fn build_json_sync_response(&mut self) -> JsonValue {
self.batch_counter += 1;
let next_batch = self.generate_sync_token();
let body = json! {
{
"device_one_time_keys_count": {},
"next_batch": next_batch,
"device_lists": {
"changed": self.changed_device_lists,
"left": [],
},
"rooms": {
"invite": self.invited_rooms,
"join": self.joined_rooms,
"leave": self.left_rooms,
"knock": self.knocked_rooms,
},
"to_device": {
"events": []
},
"presence": {
"events": self.presence,
},
"account_data": {
"events": self.account_data,
},
}
};
self.clear();
body
}
pub fn build_sync_response(&mut self) -> SyncResponse {
let body = self.build_json_sync_response();
let response = Response::builder().body(serde_json::to_vec(&body).unwrap()).unwrap();
SyncResponse::try_from_http_response(response).unwrap()
}
fn generate_sync_token(&self) -> String {
format!("t392-516_47314_0_7_1_1_1_11444_{}", self.batch_counter)
}
pub fn clear(&mut self) {
self.account_data.clear();
self.invited_rooms.clear();
self.joined_rooms.clear();
self.left_rooms.clear();
self.knocked_rooms.clear();
self.presence.clear();
}
}