1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use std::collections::BTreeMap;
use ruma::{
api::client::{
push::get_notifications::v3::Notification,
sync::sync_events::v3::{
DeviceLists, Ephemeral, GlobalAccountData, InvitedRoom, Presence, RoomAccountData,
State, ToDevice, UnreadNotificationsCount as RumaUnreadNotificationsCount,
},
},
events::{
room::member::{
MembershipState, RoomMemberEvent, RoomMemberEventContent, StrippedRoomMemberEvent,
SyncRoomMemberEvent,
},
AnyRoomEvent, AnySyncRoomEvent,
},
serde::Raw,
DeviceKeyAlgorithm, OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedUserId, UserId,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AmbiguityChange {
pub member_ambiguous: bool,
pub disambiguated_member: Option<OwnedUserId>,
pub ambiguated_member: Option<OwnedUserId>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AmbiguityChanges {
pub changes: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, AmbiguityChange>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum VerificationState {
Trusted,
Untrusted,
UnknownDevice,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum AlgorithmInfo {
MegolmV1AesSha2 {
curve25519_key: String,
sender_claimed_keys: BTreeMap<DeviceKeyAlgorithm, String>,
forwarding_curve25519_key_chain: Vec<String>,
},
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EncryptionInfo {
pub sender: OwnedUserId,
pub sender_device: OwnedDeviceId,
pub algorithm_info: AlgorithmInfo,
pub verification_state: VerificationState,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SyncRoomEvent {
pub event: Raw<AnySyncRoomEvent>,
pub encryption_info: Option<EncryptionInfo>,
}
impl SyncRoomEvent {
pub fn event_id(&self) -> Option<OwnedEventId> {
self.event.get_field::<OwnedEventId>("event_id").ok().flatten()
}
}
impl From<Raw<AnySyncRoomEvent>> for SyncRoomEvent {
fn from(inner: Raw<AnySyncRoomEvent>) -> Self {
Self { encryption_info: None, event: inner }
}
}
impl From<RoomEvent> for SyncRoomEvent {
fn from(o: RoomEvent) -> Self {
Self { encryption_info: o.encryption_info, event: o.event.cast() }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct SyncResponse {
pub next_batch: String,
pub rooms: Rooms,
pub presence: Presence,
pub account_data: GlobalAccountData,
pub to_device: ToDevice,
pub device_lists: DeviceLists,
pub device_one_time_keys_count: BTreeMap<DeviceKeyAlgorithm, u64>,
pub ambiguity_changes: AmbiguityChanges,
pub notifications: BTreeMap<OwnedRoomId, Vec<Notification>>,
}
impl SyncResponse {
pub fn new(next_batch: String) -> Self {
Self { next_batch, ..Default::default() }
}
}
#[derive(Clone, Debug)]
pub struct RoomEvent {
pub event: Raw<AnyRoomEvent>,
pub encryption_info: Option<EncryptionInfo>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Rooms {
pub leave: BTreeMap<OwnedRoomId, LeftRoom>,
pub join: BTreeMap<OwnedRoomId, JoinedRoom>,
pub invite: BTreeMap<OwnedRoomId, InvitedRoom>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct JoinedRoom {
pub unread_notifications: UnreadNotificationsCount,
pub timeline: Timeline,
pub state: State,
pub account_data: RoomAccountData,
pub ephemeral: Ephemeral,
}
impl JoinedRoom {
pub fn new(
timeline: Timeline,
state: State,
account_data: RoomAccountData,
ephemeral: Ephemeral,
unread_notifications: UnreadNotificationsCount,
) -> Self {
Self { unread_notifications, timeline, state, account_data, ephemeral }
}
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub struct UnreadNotificationsCount {
pub highlight_count: u64,
pub notification_count: u64,
}
impl From<RumaUnreadNotificationsCount> for UnreadNotificationsCount {
fn from(notifications: RumaUnreadNotificationsCount) -> Self {
Self {
highlight_count: notifications.highlight_count.map(|c| c.into()).unwrap_or(0),
notification_count: notifications.notification_count.map(|c| c.into()).unwrap_or(0),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LeftRoom {
pub timeline: Timeline,
pub state: State,
pub account_data: RoomAccountData,
}
impl LeftRoom {
pub fn new(timeline: Timeline, state: State, account_data: RoomAccountData) -> Self {
Self { timeline, state, account_data }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Timeline {
pub limited: bool,
pub prev_batch: Option<String>,
pub events: Vec<SyncRoomEvent>,
}
impl Timeline {
pub fn new(limited: bool, prev_batch: Option<String>) -> Self {
Self { limited, prev_batch, ..Default::default() }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct TimelineSlice {
pub start: String,
pub end: Option<String>,
pub limited: bool,
pub events: Vec<SyncRoomEvent>,
pub sync: bool,
}
impl TimelineSlice {
pub fn new(
events: Vec<SyncRoomEvent>,
start: String,
end: Option<String>,
limited: bool,
sync: bool,
) -> Self {
Self { start, end, events, limited, sync }
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MemberEvent {
Sync(SyncRoomMemberEvent),
Stripped(StrippedRoomMemberEvent),
}
impl MemberEvent {
pub fn original_content(&self) -> Option<&RoomMemberEventContent> {
match self {
MemberEvent::Sync(e) => e.as_original().map(|e| &e.content),
MemberEvent::Stripped(e) => Some(&e.content),
}
}
pub fn membership(&self) -> &MembershipState {
match self {
MemberEvent::Sync(e) => e.membership(),
MemberEvent::Stripped(e) => &e.content.membership,
}
}
pub fn user_id(&self) -> &UserId {
match self {
MemberEvent::Sync(e) => e.state_key(),
MemberEvent::Stripped(e) => &e.state_key,
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct MembersResponse {
pub chunk: Vec<RoomMemberEvent>,
pub ambiguity_changes: AmbiguityChanges,
}
#[cfg(test)]
mod tests {
use ruma::{
event_id,
events::{
room::message::RoomMessageEventContent, AnySyncMessageLikeEvent, AnySyncRoomEvent,
MessageLikeUnsigned, OriginalMessageLikeEvent, SyncMessageLikeEvent,
},
room_id,
serde::Raw,
user_id, MilliSecondsSinceUnixEpoch,
};
use super::{RoomEvent, SyncRoomEvent};
#[test]
fn room_event_to_sync_room_event() {
let content = RoomMessageEventContent::text_plain("foobar");
let event = OriginalMessageLikeEvent {
content,
event_id: event_id!("$xxxxx:example.org").to_owned(),
room_id: room_id!("!someroom:example.com").to_owned(),
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
sender: user_id!("@carl:example.com").to_owned(),
unsigned: MessageLikeUnsigned::default(),
};
let room_event =
RoomEvent { event: Raw::new(&event).unwrap().cast(), encryption_info: None };
let converted_room_event: SyncRoomEvent = room_event.into();
let converted_event: AnySyncRoomEvent = converted_room_event.event.deserialize().unwrap();
let sync_event = AnySyncRoomEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
SyncMessageLikeEvent::Original(event.into()),
));
assert_eq!(converted_event.event_id(), sync_event.event_id());
assert_eq!(converted_event.sender(), sync_event.sender());
}
}