Line data Source code
1 : /* MIT License
2 : *
3 : * Copyright (C) 2019, 2020, 2021 Famedly GmbH
4 : *
5 : * Permission is hereby granted, free of charge, to any person obtaining a copy
6 : * of this software and associated documentation files (the "Software"), to deal
7 : * in the Software without restriction, including without limitation the rights
8 : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : * copies of the Software, and to permit persons to whom the Software is
10 : * furnished to do so, subject to the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included in all
13 : * copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 : * SOFTWARE.
22 : */
23 :
24 : import 'package:matrix/matrix_api_lite.dart';
25 :
26 : class SyncUpdate {
27 : String nextBatch;
28 : RoomsUpdate? rooms;
29 : List<Presence>? presence;
30 : List<BasicEvent>? accountData;
31 : List<BasicEventWithSender>? toDevice;
32 : DeviceListsUpdate? deviceLists;
33 : Map<String, int>? deviceOneTimeKeysCount;
34 : List<String>? deviceUnusedFallbackKeyTypes;
35 :
36 16 : SyncUpdate({
37 : required this.nextBatch,
38 : this.rooms,
39 : this.presence,
40 : this.accountData,
41 : this.toDevice,
42 : this.deviceLists,
43 : this.deviceOneTimeKeysCount,
44 : this.deviceUnusedFallbackKeyTypes,
45 : });
46 :
47 34 : SyncUpdate.fromJson(Map<String, Object?> json)
48 34 : : nextBatch = json.tryGet<String>('next_batch') ?? '',
49 34 : rooms = (() {
50 34 : final temp = json.tryGetMap<String, Object?>('rooms');
51 34 : return temp != null ? RoomsUpdate.fromJson(temp) : null;
52 34 : }()),
53 : presence = json
54 68 : .tryGetMap<String, List<Object?>>('presence')?['events']
55 102 : ?.map((i) => Presence.fromJson(i as Map<String, Object?>))
56 34 : .toList(),
57 : accountData = json
58 68 : .tryGetMap<String, List<Object?>>('account_data')?['events']
59 102 : ?.map((i) => BasicEvent.fromJson(i as Map<String, Object?>))
60 34 : .toList(),
61 : toDevice = json
62 68 : .tryGetMap<String, List<Object?>>('to_device')?['events']
63 34 : ?.map(
64 68 : (i) => BasicEventWithSender.fromJson(i as Map<String, Object?>))
65 34 : .toList(),
66 34 : deviceLists = (() {
67 34 : final temp = json.tryGetMap<String, Object?>('device_lists');
68 32 : return temp != null ? DeviceListsUpdate.fromJson(temp) : null;
69 34 : }()),
70 : deviceOneTimeKeysCount =
71 34 : json.tryGetMap<String, int>('device_one_time_keys_count'),
72 : deviceUnusedFallbackKeyTypes =
73 34 : json.tryGetList<String>('device_unused_fallback_key_types') ??
74 34 : json.tryGetList<String>(
75 : 'org.matrix.msc2732.device_unused_fallback_key_types');
76 :
77 0 : Map<String, Object?> toJson() {
78 0 : final data = <String, Object?>{};
79 0 : data['next_batch'] = nextBatch;
80 0 : if (rooms != null) {
81 0 : data['rooms'] = rooms!.toJson();
82 : }
83 0 : if (presence != null) {
84 0 : data['presence'] = {
85 0 : 'events': presence!.map((i) => i.toJson()).toList(),
86 : };
87 : }
88 0 : if (accountData != null) {
89 0 : data['account_data'] = {
90 0 : 'events': accountData!.map((i) => i.toJson()).toList(),
91 : };
92 : }
93 0 : if (toDevice != null) {
94 0 : data['to_device'] = {
95 0 : 'events': toDevice!.map((i) => i.toJson()).toList(),
96 : };
97 : }
98 0 : if (deviceLists != null) {
99 0 : data['device_lists'] = deviceLists!.toJson();
100 : }
101 0 : if (deviceOneTimeKeysCount != null) {
102 0 : data['device_one_time_keys_count'] = deviceOneTimeKeysCount;
103 : }
104 0 : if (deviceUnusedFallbackKeyTypes != null) {
105 0 : data['device_unused_fallback_key_types'] = deviceUnusedFallbackKeyTypes;
106 0 : data['org.matrix.msc2732.device_unused_fallback_key_types'] =
107 0 : deviceUnusedFallbackKeyTypes;
108 : }
109 : return data;
110 : }
111 : }
112 :
113 : class RoomsUpdate {
114 : Map<String, JoinedRoomUpdate>? join;
115 : Map<String, InvitedRoomUpdate>? invite;
116 : Map<String, LeftRoomUpdate>? leave;
117 : Map<String, KnockRoomUpdate>? knock;
118 :
119 14 : RoomsUpdate({
120 : this.join,
121 : this.invite,
122 : this.leave,
123 : this.knock,
124 : });
125 :
126 34 : RoomsUpdate.fromJson(Map<String, Object?> json) {
127 136 : join = json.tryGetMap<String, Object?>('join')?.catchMap((k, v) =>
128 68 : MapEntry(k, JoinedRoomUpdate.fromJson(v as Map<String, Object?>)));
129 136 : invite = json.tryGetMap<String, Object?>('invite')?.catchMap((k, v) =>
130 68 : MapEntry(k, InvitedRoomUpdate.fromJson(v as Map<String, Object?>)));
131 136 : leave = json.tryGetMap<String, Object?>('leave')?.catchMap((k, v) =>
132 68 : MapEntry(k, LeftRoomUpdate.fromJson(v as Map<String, Object?>)));
133 68 : knock = json.tryGetMap<String, Object?>('knock')?.catchMap((k, v) =>
134 0 : MapEntry(k, KnockRoomUpdate.fromJson(v as Map<String, Object?>)));
135 : }
136 :
137 0 : Map<String, Object?> toJson() {
138 0 : final data = <String, Object?>{};
139 0 : if (join != null) {
140 0 : data['join'] = join!.map((k, v) => MapEntry(k, v.toJson()));
141 : }
142 0 : if (invite != null) {
143 0 : data['invite'] = invite!.map((k, v) => MapEntry(k, v.toJson()));
144 : }
145 0 : if (leave != null) {
146 0 : data['leave'] = leave!.map((k, v) => MapEntry(k, v.toJson()));
147 : }
148 0 : if (knock != null) {
149 0 : data['knock'] = knock!.map((k, v) => MapEntry(k, v.toJson()));
150 : }
151 : return data;
152 : }
153 : }
154 :
155 : abstract class SyncRoomUpdate {}
156 :
157 : class JoinedRoomUpdate extends SyncRoomUpdate {
158 : RoomSummary? summary;
159 : List<MatrixEvent>? state;
160 : TimelineUpdate? timeline;
161 : List<BasicRoomEvent>? ephemeral;
162 : List<BasicRoomEvent>? accountData;
163 : UnreadNotificationCounts? unreadNotifications;
164 :
165 13 : JoinedRoomUpdate({
166 : this.summary,
167 : this.state,
168 : this.timeline,
169 : this.ephemeral,
170 : this.accountData,
171 : this.unreadNotifications,
172 : });
173 :
174 35 : JoinedRoomUpdate.fromJson(Map<String, Object?> json)
175 35 : : summary = json.tryGetFromJson('summary', RoomSummary.fromJson),
176 : state = json
177 69 : .tryGetMap<String, List<Object?>>('state')?['events']
178 98 : ?.map((i) => MatrixEvent.fromJson(i as Map<String, Object?>))
179 34 : .toList(),
180 35 : timeline = json.tryGetFromJson('timeline', TimelineUpdate.fromJson),
181 : ephemeral = json
182 69 : .tryGetMap<String, List<Object?>>('ephemeral')?['events']
183 98 : ?.map((i) => BasicRoomEvent.fromJson(i as Map<String, Object?>))
184 34 : .toList(),
185 : accountData = json
186 69 : .tryGetMap<String, List<Object?>>('account_data')?['events']
187 98 : ?.map((i) => BasicRoomEvent.fromJson(i as Map<String, Object?>))
188 34 : .toList(),
189 35 : unreadNotifications = json.tryGetFromJson(
190 : 'unread_notifications', UnreadNotificationCounts.fromJson);
191 :
192 0 : Map<String, Object?> toJson() {
193 0 : final data = <String, Object?>{};
194 0 : if (summary != null) {
195 0 : data['summary'] = summary!.toJson();
196 : }
197 0 : if (state != null) {
198 0 : data['state'] = {
199 0 : 'events': state!.map((i) => i.toJson()).toList(),
200 : };
201 : }
202 0 : if (timeline != null) {
203 0 : data['timeline'] = timeline!.toJson();
204 : }
205 0 : if (ephemeral != null) {
206 0 : data['ephemeral'] = {
207 0 : 'events': ephemeral!.map((i) => i.toJson()).toList(),
208 : };
209 : }
210 0 : if (accountData != null) {
211 0 : data['account_data'] = {
212 0 : 'events': accountData!.map((i) => i.toJson()).toList(),
213 : };
214 : }
215 0 : if (unreadNotifications != null) {
216 0 : data['unread_notifications'] = unreadNotifications!.toJson();
217 : }
218 : return data;
219 : }
220 : }
221 :
222 : class InvitedRoomUpdate extends SyncRoomUpdate {
223 : List<StrippedStateEvent>? inviteState;
224 :
225 3 : InvitedRoomUpdate({this.inviteState});
226 :
227 34 : InvitedRoomUpdate.fromJson(Map<String, Object?> json)
228 : : inviteState = json
229 68 : .tryGetMap<String, List<Object?>>('invite_state')?['events']
230 98 : ?.map((i) => StrippedStateEvent.fromJson(i as Map<String, Object?>))
231 34 : .toList();
232 :
233 0 : Map<String, Object?> toJson() {
234 0 : final data = <String, Object?>{};
235 0 : if (inviteState != null) {
236 0 : data['invite_state'] = {
237 0 : 'events': inviteState!.map((i) => i.toJson()).toList(),
238 : };
239 : }
240 : return data;
241 : }
242 : }
243 :
244 : class KnockRoomUpdate extends SyncRoomUpdate {
245 : List<StrippedStateEvent>? knockState;
246 :
247 0 : KnockRoomUpdate({this.knockState});
248 :
249 0 : KnockRoomUpdate.fromJson(Map<String, Object?> json)
250 : : knockState = json
251 0 : .tryGetMap<String, List<Object?>>('knock_state')?['events']
252 0 : ?.map((i) => StrippedStateEvent.fromJson(i as Map<String, Object?>))
253 0 : .toList();
254 :
255 0 : Map<String, Object?> toJson() {
256 0 : final data = <String, Object?>{};
257 0 : if (knockState != null) {
258 0 : data['knock_state'] = {
259 0 : 'events': knockState!.map((i) => i.toJson()).toList(),
260 : };
261 : }
262 : return data;
263 : }
264 : }
265 :
266 : class LeftRoomUpdate extends SyncRoomUpdate {
267 : List<MatrixEvent>? state;
268 : TimelineUpdate? timeline;
269 : List<BasicRoomEvent>? accountData;
270 :
271 3 : LeftRoomUpdate({
272 : this.state,
273 : this.timeline,
274 : this.accountData,
275 : });
276 :
277 34 : LeftRoomUpdate.fromJson(Map<String, Object?> json)
278 : : state = json
279 66 : .tryGetMap<String, List<Object?>>('state')?['events']
280 96 : ?.map((i) => MatrixEvent.fromJson(i as Map<String, Object?>))
281 32 : .toList(),
282 34 : timeline = json.tryGetFromJson('timeline', TimelineUpdate.fromJson),
283 : accountData = json
284 66 : .tryGetMap<String, List<Object?>>('account_data')?['events']
285 96 : ?.map((i) => BasicRoomEvent.fromJson(i as Map<String, Object?>))
286 32 : .toList();
287 :
288 0 : Map<String, Object?> toJson() {
289 0 : final data = <String, Object?>{};
290 0 : if (state != null) {
291 0 : data['state'] = {
292 0 : 'events': state!.map((i) => i.toJson()).toList(),
293 : };
294 : }
295 0 : if (timeline != null) {
296 0 : data['timeline'] = timeline!.toJson();
297 : }
298 0 : if (accountData != null) {
299 0 : data['account_data'] = {
300 0 : 'events': accountData!.map((i) => i.toJson()).toList(),
301 : };
302 : }
303 : return data;
304 : }
305 : }
306 :
307 : class TimelineUpdate {
308 : List<MatrixEvent>? events;
309 : bool? limited;
310 : String? prevBatch;
311 :
312 14 : TimelineUpdate({
313 : this.events,
314 : this.limited,
315 : this.prevBatch,
316 : });
317 :
318 34 : TimelineUpdate.fromJson(Map<String, Object?> json)
319 : : events = json
320 34 : .tryGetList<Map<String, Object?>>('events')
321 98 : ?.map((v) => MatrixEvent.fromJson(v))
322 34 : .toList(),
323 34 : limited = json.tryGet<bool>('limited'),
324 34 : prevBatch = json.tryGet<String>('prev_batch');
325 :
326 0 : Map<String, Object?> toJson() {
327 0 : final data = <String, Object?>{};
328 0 : if (events != null) {
329 0 : data['events'] = events!.map((i) => i.toJson()).toList();
330 : }
331 0 : if (limited != null) {
332 0 : data['limited'] = limited;
333 : }
334 0 : if (prevBatch != null) {
335 0 : data['prev_batch'] = prevBatch;
336 : }
337 : return data;
338 : }
339 : }
340 :
341 : class UnreadNotificationCounts {
342 : int? highlightCount;
343 : int? notificationCount;
344 :
345 2 : UnreadNotificationCounts({
346 : this.notificationCount,
347 : this.highlightCount,
348 : });
349 :
350 34 : UnreadNotificationCounts.fromJson(Map<String, Object?> json)
351 34 : : highlightCount = json.tryGet<int>('highlight_count'),
352 34 : notificationCount = json.tryGet<int>('notification_count');
353 :
354 0 : Map<String, Object?> toJson() {
355 0 : final data = <String, Object?>{};
356 0 : if (highlightCount != null) {
357 0 : data['highlight_count'] = highlightCount;
358 : }
359 0 : if (notificationCount != null) {
360 0 : data['notification_count'] = notificationCount;
361 : }
362 : return data;
363 : }
364 : }
365 :
366 : class DeviceListsUpdate {
367 : List<String>? changed;
368 : List<String>? left;
369 :
370 0 : DeviceListsUpdate({
371 : this.changed,
372 : this.left,
373 : });
374 :
375 32 : DeviceListsUpdate.fromJson(Map<String, Object?> json)
376 32 : : changed = json.tryGetList<String>('changed') ?? [],
377 32 : left = json.tryGetList<String>('left') ?? [];
378 :
379 0 : Map<String, Object?> toJson() {
380 0 : final data = <String, Object?>{};
381 0 : if (changed != null) {
382 0 : data['changed'] = changed;
383 : }
384 0 : if (left != null) {
385 0 : data['left'] = left;
386 : }
387 : return data;
388 : }
389 : }
|