From 58b36a67a39721fd58cb8469c96342598fafc4e5 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Wed, 10 Nov 2021 10:04:03 +0100 Subject: [PATCH] refactor: Update matrix_api_lite and use SyncUpdate constructors --- lib/src/event.dart | 20 ++++-- lib/src/room.dart | 126 +++++++++++++++++++++++--------------- pubspec.yaml | 2 +- test/fake_matrix_api.dart | 8 ++- test/timeline_test.dart | 31 ++++++---- 5 files changed, 116 insertions(+), 71 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index da3b1f84..deb703d1 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -122,12 +122,20 @@ class Event extends MatrixEvent { final json = toJson(); json['unsigned'] ??= {}; json['unsigned'][messageSendingStatusKey] = EventStatus.error.intValue; - room.client.handleSync(SyncUpdate(nextBatch: '') - ..rooms = (RoomsUpdate() - ..join = ({}..[room.id] = - (JoinedRoomUpdate() - ..timeline = (TimelineUpdate() - ..events = [MatrixEvent.fromJson(json)]))))); + room.client.handleSync( + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: { + room.id: JoinedRoomUpdate( + timeline: TimelineUpdate( + events: [MatrixEvent.fromJson(json)], + ), + ) + }, + ), + ), + ); } } } diff --git a/lib/src/room.dart b/lib/src/room.dart index 39aa4a3d..71d02b9e 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -515,16 +515,24 @@ class Room { /// set a read marker! Future markUnread(bool unread) async { final content = MarkedUnread(unread).toJson(); - await _handleFakeSync(SyncUpdate(nextBatch: '') - ..rooms = (RoomsUpdate() - ..join = (({}..[id] = (JoinedRoomUpdate() - ..accountData = [ - BasicRoomEvent( - content: content, - roomId: id, - type: EventType.markedUnread, + await _handleFakeSync( + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: { + id: JoinedRoomUpdate( + accountData: [ + BasicRoomEvent( + content: content, + roomId: id, + type: EventType.markedUnread, + ), + ], ) - ]))))); + }, + ), + ), + ); await client.setAccountDataPerRoom( client.userID!, id, @@ -793,23 +801,30 @@ class Room { } } final sentDate = DateTime.now(); - final syncUpdate = SyncUpdate(nextBatch: '') - ..rooms = (RoomsUpdate() - ..join = ({}..[id] = (JoinedRoomUpdate() - ..timeline = (TimelineUpdate() - ..events = [ - MatrixEvent( - content: content, - type: type, - eventId: messageID, - senderId: client.userID!, - originServerTs: sentDate, - unsigned: { - messageSendingStatusKey: EventStatus.sending.intValue, - 'transaction_id': messageID, - }, - ) - ])))); + final syncUpdate = SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: { + id: JoinedRoomUpdate( + timeline: TimelineUpdate( + events: [ + MatrixEvent( + content: content, + type: type, + eventId: messageID, + senderId: client.userID!, + originServerTs: sentDate, + unsigned: { + messageSendingStatusKey: EventStatus.sending.intValue, + 'transaction_id': messageID, + }, + ), + ], + ), + ), + }, + ), + ); await _handleFakeSync(syncUpdate); // Send the text and on success, store and display a *sent* event. @@ -879,11 +894,14 @@ class Room { if ([MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN] .contains(exception.error)) { await _handleFakeSync( - SyncUpdate(nextBatch: '') - ..rooms = (RoomsUpdate() - ..leave = { - '$id': (LeftRoomUpdate()), - }), + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + leave: { + id: LeftRoomUpdate(), + }, + ), + ), ); } rethrow; @@ -953,24 +971,34 @@ class Room { if (!((resp.chunk?.isNotEmpty ?? false) && resp.end != null)) return; await client.handleSync( - SyncUpdate(nextBatch: '') - ..rooms = (RoomsUpdate() - ..join = membership == Membership.join - ? ({}..[id] = ((JoinedRoomUpdate() - ..state = resp.state - ..timeline = (TimelineUpdate() - ..limited = false - ..events = resp.chunk - ..prevBatch = resp.end)))) - : null - ..leave = membership != Membership.join - ? ({}..[id] = ((LeftRoomUpdate() - ..state = resp.state - ..timeline = (TimelineUpdate() - ..limited = false - ..events = resp.chunk - ..prevBatch = resp.end)))) - : null), + SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate( + join: membership == Membership.join + ? { + id: JoinedRoomUpdate( + state: resp.state, + timeline: TimelineUpdate( + limited: false, + events: resp.chunk, + prevBatch: resp.end, + ), + ) + } + : null, + leave: membership != Membership.join + ? { + id: LeftRoomUpdate( + state: resp.state, + timeline: TimelineUpdate( + limited: false, + events: resp.chunk, + prevBatch: resp.end, + ), + ), + } + : null), + ), sortAtTheEnd: true); }; diff --git a/pubspec.yaml b/pubspec.yaml index 0819f162..40246e4c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ dependencies: crypto: ^3.0.0 base58check: ^2.0.0 olm: ^2.0.0 - matrix_api_lite: ^0.4.3 + matrix_api_lite: ^0.5.1 hive: ^2.0.4 ffi: ^1.0.0 js: ^0.6.3 diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index e8564e21..93fad68b 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -122,10 +122,12 @@ class FakeMatrixApi extends MockClient { action.contains('/account_data/') && !action.contains('/room/')) { final type = Uri.decodeComponent(action.split('/').last); - final syncUpdate = sdk.SyncUpdate(nextBatch: '') - ..accountData = [ + final syncUpdate = sdk.SyncUpdate( + nextBatch: '', + accountData: [ sdk.BasicEvent(content: decodeJson(data), type: type) - ]; + ], + ); if (client?.database != null) { await client?.database?.transaction(() async { await client?.handleSync(syncUpdate); diff --git a/test/timeline_test.dart b/test/timeline_test.dart index 8c996069..084f6f65 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -306,18 +306,25 @@ void main() { }); test('Clear cache on limited timeline', () async { - client.onSync.add(SyncUpdate(nextBatch: '1234') - ..rooms = (RoomsUpdate() - ..join = { - roomID: (JoinedRoomUpdate() - ..timeline = (TimelineUpdate() - ..limited = true - ..prevBatch = 'blah') - ..unreadNotifications = UnreadNotificationCounts.fromJson({ - 'highlight_count': 0, - 'notification_count': 0, - })) - })); + client.onSync.add( + SyncUpdate( + nextBatch: '1234', + rooms: RoomsUpdate( + join: { + roomID: JoinedRoomUpdate( + timeline: TimelineUpdate( + limited: true, + prevBatch: 'blah', + ), + unreadNotifications: UnreadNotificationCounts( + highlightCount: 0, + notificationCount: 0, + ), + ), + }, + ), + ), + ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events.isEmpty, true); });