LCOV - code coverage report
Current view: top level - lib/matrix_api_lite/model - sync_update.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 74 157 47.1 %
Date: 2024-05-13 12:56:47 Functions: 0 0 -

          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          15 :   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          33 :   SyncUpdate.fromJson(Map<String, Object?> json)
      48          33 :       : nextBatch = json.tryGet<String>('next_batch') ?? '',
      49          33 :         rooms = (() {
      50          33 :           final temp = json.tryGetMap<String, Object?>('rooms');
      51          33 :           return temp != null ? RoomsUpdate.fromJson(temp) : null;
      52          33 :         }()),
      53             :         presence = json
      54          66 :             .tryGetMap<String, List<Object?>>('presence')?['events']
      55          99 :             ?.map((i) => Presence.fromJson(i as Map<String, Object?>))
      56          33 :             .toList(),
      57             :         accountData = json
      58          66 :             .tryGetMap<String, List<Object?>>('account_data')?['events']
      59          99 :             ?.map((i) => BasicEvent.fromJson(i as Map<String, Object?>))
      60          33 :             .toList(),
      61             :         toDevice = json
      62          66 :             .tryGetMap<String, List<Object?>>('to_device')?['events']
      63          33 :             ?.map(
      64          66 :                 (i) => BasicEventWithSender.fromJson(i as Map<String, Object?>))
      65          33 :             .toList(),
      66          33 :         deviceLists = (() {
      67          33 :           final temp = json.tryGetMap<String, Object?>('device_lists');
      68          31 :           return temp != null ? DeviceListsUpdate.fromJson(temp) : null;
      69          33 :         }()),
      70             :         deviceOneTimeKeysCount =
      71          33 :             json.tryGetMap<String, int>('device_one_time_keys_count'),
      72             :         deviceUnusedFallbackKeyTypes =
      73          33 :             json.tryGetList<String>('device_unused_fallback_key_types') ??
      74          33 :                 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             : 
     118          13 :   RoomsUpdate({
     119             :     this.join,
     120             :     this.invite,
     121             :     this.leave,
     122             :   });
     123             : 
     124          33 :   RoomsUpdate.fromJson(Map<String, Object?> json) {
     125         132 :     join = json.tryGetMap<String, Object?>('join')?.catchMap((k, v) =>
     126          66 :         MapEntry(k, JoinedRoomUpdate.fromJson(v as Map<String, Object?>)));
     127         132 :     invite = json.tryGetMap<String, Object?>('invite')?.catchMap((k, v) =>
     128          66 :         MapEntry(k, InvitedRoomUpdate.fromJson(v as Map<String, Object?>)));
     129         132 :     leave = json.tryGetMap<String, Object?>('leave')?.catchMap((k, v) =>
     130          66 :         MapEntry(k, LeftRoomUpdate.fromJson(v as Map<String, Object?>)));
     131             :   }
     132             : 
     133           0 :   Map<String, Object?> toJson() {
     134           0 :     final data = <String, Object?>{};
     135           0 :     if (join != null) {
     136           0 :       data['join'] = join!.map((k, v) => MapEntry(k, v.toJson()));
     137             :     }
     138           0 :     if (invite != null) {
     139           0 :       data['invite'] = invite!.map((k, v) => MapEntry(k, v.toJson()));
     140             :     }
     141           0 :     if (leave != null) {
     142           0 :       data['leave'] = leave!.map((k, v) => MapEntry(k, v.toJson()));
     143             :     }
     144             :     return data;
     145             :   }
     146             : }
     147             : 
     148             : abstract class SyncRoomUpdate {}
     149             : 
     150             : class JoinedRoomUpdate extends SyncRoomUpdate {
     151             :   RoomSummary? summary;
     152             :   List<MatrixEvent>? state;
     153             :   TimelineUpdate? timeline;
     154             :   List<BasicRoomEvent>? ephemeral;
     155             :   List<BasicRoomEvent>? accountData;
     156             :   UnreadNotificationCounts? unreadNotifications;
     157             : 
     158          11 :   JoinedRoomUpdate({
     159             :     this.summary,
     160             :     this.state,
     161             :     this.timeline,
     162             :     this.ephemeral,
     163             :     this.accountData,
     164             :     this.unreadNotifications,
     165             :   });
     166             : 
     167          34 :   JoinedRoomUpdate.fromJson(Map<String, Object?> json)
     168          34 :       : summary = json.tryGetFromJson('summary', RoomSummary.fromJson),
     169             :         state = json
     170          67 :             .tryGetMap<String, List<Object?>>('state')?['events']
     171          95 :             ?.map((i) => MatrixEvent.fromJson(i as Map<String, Object?>))
     172          33 :             .toList(),
     173          34 :         timeline = json.tryGetFromJson('timeline', TimelineUpdate.fromJson),
     174             :         ephemeral = json
     175          67 :             .tryGetMap<String, List<Object?>>('ephemeral')?['events']
     176          95 :             ?.map((i) => BasicRoomEvent.fromJson(i as Map<String, Object?>))
     177          33 :             .toList(),
     178             :         accountData = json
     179          67 :             .tryGetMap<String, List<Object?>>('account_data')?['events']
     180          95 :             ?.map((i) => BasicRoomEvent.fromJson(i as Map<String, Object?>))
     181          33 :             .toList(),
     182          34 :         unreadNotifications = json.tryGetFromJson(
     183             :             'unread_notifications', UnreadNotificationCounts.fromJson);
     184             : 
     185           0 :   Map<String, Object?> toJson() {
     186           0 :     final data = <String, Object?>{};
     187           0 :     if (summary != null) {
     188           0 :       data['summary'] = summary!.toJson();
     189             :     }
     190           0 :     if (state != null) {
     191           0 :       data['state'] = {
     192           0 :         'events': state!.map((i) => i.toJson()).toList(),
     193             :       };
     194             :     }
     195           0 :     if (timeline != null) {
     196           0 :       data['timeline'] = timeline!.toJson();
     197             :     }
     198           0 :     if (ephemeral != null) {
     199           0 :       data['ephemeral'] = {
     200           0 :         'events': ephemeral!.map((i) => i.toJson()).toList(),
     201             :       };
     202             :     }
     203           0 :     if (accountData != null) {
     204           0 :       data['account_data'] = {
     205           0 :         'events': accountData!.map((i) => i.toJson()).toList(),
     206             :       };
     207             :     }
     208           0 :     if (unreadNotifications != null) {
     209           0 :       data['unread_notifications'] = unreadNotifications!.toJson();
     210             :     }
     211             :     return data;
     212             :   }
     213             : }
     214             : 
     215             : class InvitedRoomUpdate extends SyncRoomUpdate {
     216             :   List<StrippedStateEvent>? inviteState;
     217             : 
     218           2 :   InvitedRoomUpdate({this.inviteState});
     219             : 
     220          33 :   InvitedRoomUpdate.fromJson(Map<String, Object?> json)
     221             :       : inviteState = json
     222          66 :             .tryGetMap<String, List<Object?>>('invite_state')?['events']
     223          95 :             ?.map((i) => StrippedStateEvent.fromJson(i as Map<String, Object?>))
     224          33 :             .toList();
     225             : 
     226           0 :   Map<String, Object?> toJson() {
     227           0 :     final data = <String, Object?>{};
     228           0 :     if (inviteState != null) {
     229           0 :       data['invite_state'] = {
     230           0 :         'events': inviteState!.map((i) => i.toJson()).toList(),
     231             :       };
     232             :     }
     233             :     return data;
     234             :   }
     235             : }
     236             : 
     237             : class LeftRoomUpdate extends SyncRoomUpdate {
     238             :   List<MatrixEvent>? state;
     239             :   TimelineUpdate? timeline;
     240             :   List<BasicRoomEvent>? accountData;
     241             : 
     242           2 :   LeftRoomUpdate({
     243             :     this.state,
     244             :     this.timeline,
     245             :     this.accountData,
     246             :   });
     247             : 
     248          33 :   LeftRoomUpdate.fromJson(Map<String, Object?> json)
     249             :       : state = json
     250          64 :             .tryGetMap<String, List<Object?>>('state')?['events']
     251          93 :             ?.map((i) => MatrixEvent.fromJson(i as Map<String, Object?>))
     252          31 :             .toList(),
     253          33 :         timeline = json.tryGetFromJson('timeline', TimelineUpdate.fromJson),
     254             :         accountData = json
     255          64 :             .tryGetMap<String, List<Object?>>('account_data')?['events']
     256          93 :             ?.map((i) => BasicRoomEvent.fromJson(i as Map<String, Object?>))
     257          31 :             .toList();
     258             : 
     259           0 :   Map<String, Object?> toJson() {
     260           0 :     final data = <String, Object?>{};
     261           0 :     if (state != null) {
     262           0 :       data['state'] = {
     263           0 :         'events': state!.map((i) => i.toJson()).toList(),
     264             :       };
     265             :     }
     266           0 :     if (timeline != null) {
     267           0 :       data['timeline'] = timeline!.toJson();
     268             :     }
     269           0 :     if (accountData != null) {
     270           0 :       data['account_data'] = {
     271           0 :         'events': accountData!.map((i) => i.toJson()).toList(),
     272             :       };
     273             :     }
     274             :     return data;
     275             :   }
     276             : }
     277             : 
     278             : class TimelineUpdate {
     279             :   List<MatrixEvent>? events;
     280             :   bool? limited;
     281             :   String? prevBatch;
     282             : 
     283          12 :   TimelineUpdate({
     284             :     this.events,
     285             :     this.limited,
     286             :     this.prevBatch,
     287             :   });
     288             : 
     289          33 :   TimelineUpdate.fromJson(Map<String, Object?> json)
     290             :       : events = json
     291          33 :             .tryGetList<Map<String, Object?>>('events')
     292          95 :             ?.map((v) => MatrixEvent.fromJson(v))
     293          33 :             .toList(),
     294          33 :         limited = json.tryGet<bool>('limited'),
     295          33 :         prevBatch = json.tryGet<String>('prev_batch');
     296             : 
     297           0 :   Map<String, Object?> toJson() {
     298           0 :     final data = <String, Object?>{};
     299           0 :     if (events != null) {
     300           0 :       data['events'] = events!.map((i) => i.toJson()).toList();
     301             :     }
     302           0 :     if (limited != null) {
     303           0 :       data['limited'] = limited;
     304             :     }
     305           0 :     if (prevBatch != null) {
     306           0 :       data['prev_batch'] = prevBatch;
     307             :     }
     308             :     return data;
     309             :   }
     310             : }
     311             : 
     312             : class UnreadNotificationCounts {
     313             :   int? highlightCount;
     314             :   int? notificationCount;
     315             : 
     316           2 :   UnreadNotificationCounts({
     317             :     this.notificationCount,
     318             :     this.highlightCount,
     319             :   });
     320             : 
     321          33 :   UnreadNotificationCounts.fromJson(Map<String, Object?> json)
     322          33 :       : highlightCount = json.tryGet<int>('highlight_count'),
     323          33 :         notificationCount = json.tryGet<int>('notification_count');
     324             : 
     325           0 :   Map<String, Object?> toJson() {
     326           0 :     final data = <String, Object?>{};
     327           0 :     if (highlightCount != null) {
     328           0 :       data['highlight_count'] = highlightCount;
     329             :     }
     330           0 :     if (notificationCount != null) {
     331           0 :       data['notification_count'] = notificationCount;
     332             :     }
     333             :     return data;
     334             :   }
     335             : }
     336             : 
     337             : class DeviceListsUpdate {
     338             :   List<String>? changed;
     339             :   List<String>? left;
     340             : 
     341           0 :   DeviceListsUpdate({
     342             :     this.changed,
     343             :     this.left,
     344             :   });
     345             : 
     346          31 :   DeviceListsUpdate.fromJson(Map<String, Object?> json)
     347          31 :       : changed = json.tryGetList<String>('changed') ?? [],
     348          31 :         left = json.tryGetList<String>('left') ?? [];
     349             : 
     350           0 :   Map<String, Object?> toJson() {
     351           0 :     final data = <String, Object?>{};
     352           0 :     if (changed != null) {
     353           0 :       data['changed'] = changed;
     354             :     }
     355           0 :     if (left != null) {
     356           0 :       data['left'] = left;
     357             :     }
     358             :     return data;
     359             :   }
     360             : }

Generated by: LCOV version 1.14