From bc328aab5ce3cf447e540a7bc7c72ef2da35dc83 Mon Sep 17 00:00:00 2001 From: Krille Date: Thu, 2 Jan 2025 10:55:03 +0100 Subject: [PATCH] refactor: Use Event instead of EventUpdate for storing in db --- lib/encryption/encryption.dart | 8 +- lib/src/client.dart | 86 +++++-- lib/src/database/database_api.dart | 7 +- .../database/hive_collections_database.dart | 12 +- lib/src/database/matrix_sdk_database.dart | 111 ++++----- lib/src/event.dart | 2 + lib/src/room.dart | 16 +- lib/src/timeline.dart | 81 +++--- lib/src/utils/event_update.dart | 3 +- test/client_test.dart | 41 +-- test/database_api_test.dart | 53 ++-- test/matrix_database_test.dart | 201 ++++++++------- test/timeline_context_test.dart | 207 +++++++--------- test/timeline_test.dart | 234 ++++++++---------- 14 files changed, 534 insertions(+), 528 deletions(-) diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index b9387de02..176766c23 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -348,11 +348,9 @@ class Encryption { event.room.setState(event); } await client.database?.storeEventUpdate( - EventUpdate( - content: event.toJson(), - roomID: event.room.id, - type: updateType, - ), + event.room.id, + event, + updateType, client, ); } diff --git a/lib/src/client.dart b/lib/src/client.dart index ee021c96d..27af631c1 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1647,8 +1647,28 @@ class Client extends MatrixApi { /// the app receives a new synchronization, this event is called for every signal /// to update the GUI. For example, for a new message, it is called: /// onRoomEvent( "m.room.message", "!chat_id:server.com", "timeline", {sender: "@bob:server.com", body: "Hello world"} ) + // ignore: deprecated_member_use_from_same_package + @Deprecated( + 'Use `onTimelineEvent`, `onHistoryEvent` or `onNotification` instead.', + ) final CachedStreamController onEvent = CachedStreamController(); + /// A stream of all incoming timeline events for all rooms **after** + /// decryption. The events are coming in the same order as they come down from + /// the sync. + final CachedStreamController onTimelineEvent = + CachedStreamController(); + + /// A stream for all incoming historical timeline events **after** decryption + /// triggered by a `Room.requestHistory()` call or a method which calls it. + final CachedStreamController onHistoryEvent = CachedStreamController(); + + /// A stream of incoming Events **after** decryption which **should** trigger + /// a (local) notification. This includes timeline events but also + /// invite states. Excluded events are those sent by the user themself or + /// not matching the push rules. + final CachedStreamController onNotification = CachedStreamController(); + /// The onToDeviceEvent is called when there comes a new to device event. It is /// already decrypted if necessary. final CachedStreamController onToDeviceEvent = @@ -1889,11 +1909,9 @@ class Client extends MatrixApi { if (storeInDatabase) { await database?.transaction(() async { await database.storeEventUpdate( - EventUpdate( - roomID: roomId, - type: EventUpdateType.timeline, - content: event.toJson(), - ), + roomId, + event, + EventUpdateType.timeline, this, ); }); @@ -2768,12 +2786,6 @@ class Client extends MatrixApi { } } - final update = EventUpdate( - roomID: room.id, - type: type, - content: event.toJson(), - ); - // Any kind of member change? We should invalidate the profile then: if (event.type == EventTypes.RoomMember) { final userId = event.stateKey; @@ -2799,7 +2811,7 @@ class Client extends MatrixApi { } _updateRoomsByEventUpdate(room, event, type); if (store) { - await database?.storeEventUpdate(update, this); + await database?.storeEventUpdate(room.id, event, type, this); } if (event is MatrixEvent && encryptionEnabled) { await encryption?.handleEventUpdate( @@ -2807,7 +2819,53 @@ class Client extends MatrixApi { type, ); } - onEvent.add(update); + + // ignore: deprecated_member_use_from_same_package + onEvent.add( + // ignore: deprecated_member_use_from_same_package + EventUpdate( + roomID: room.id, + type: type, + content: event.toJson(), + ), + ); + if (event is MatrixEvent) { + final timelineEvent = Event.fromMatrixEvent(event, room); + switch (type) { + case EventUpdateType.timeline: + onTimelineEvent.add(timelineEvent); + if (prevBatch != null && + timelineEvent.senderId != userID && + room.notificationCount > 0 && + pushruleEvaluator.match(timelineEvent).notify) { + onNotification.add(timelineEvent); + } + break; + case EventUpdateType.history: + onHistoryEvent.add(timelineEvent); + break; + default: + break; + } + } + + // Trigger local notification for a new invite: + if (prevBatch != null && + type == EventUpdateType.inviteState && + event.type == EventTypes.RoomMember && + event.stateKey == userID) { + onNotification.add( + Event( + type: event.type, + eventId: 'invite_for_${room.id}', + senderId: event.senderId, + originServerTs: DateTime.now(), + stateKey: event.stateKey, + content: event.content, + room: room, + ), + ); + } if (prevBatch != null && (type == EventUpdateType.timeline || @@ -2931,7 +2989,7 @@ class Client extends MatrixApi { Logs().wtf( 'Passed in a ${eventUpdate.runtimeType} with $type to _updateRoomsByEventUpdate(). This should never happen!', ); - assert(true); + assert(eventUpdate is! MatrixEvent); return; } final event = Event.fromMatrixEvent(eventUpdate, room); diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index 17cde0325..d47e61fdd 100644 --- a/lib/src/database/database_api.dart +++ b/lib/src/database/database_api.dart @@ -80,7 +80,12 @@ abstract class DatabaseApi { /// Stores an EventUpdate object in the database. Must be called inside of /// [transaction]. - Future storeEventUpdate(EventUpdate eventUpdate, Client client); + Future storeEventUpdate( + String roomId, + StrippedStateEvent event, + EventUpdateType type, + Client client, + ); Future getEventById(String eventId, Room room); diff --git a/lib/src/database/hive_collections_database.dart b/lib/src/database/hive_collections_database.dart index 359a0b584..973ac330d 100644 --- a/lib/src/database/hive_collections_database.dart +++ b/lib/src/database/hive_collections_database.dart @@ -1105,7 +1105,17 @@ class HiveCollectionsDatabase extends DatabaseApi { } @override - Future storeEventUpdate(EventUpdate eventUpdate, Client client) async { + Future storeEventUpdate( + String roomId, + StrippedStateEvent event, + EventUpdateType type, + Client client, + ) async { + final eventUpdate = EventUpdate( + roomID: roomId, + content: event.toJson(), + type: type, + ); final tmpRoom = client.getRoomById(eventUpdate.roomID) ?? Room(id: eventUpdate.roomID, client: client); diff --git a/lib/src/database/matrix_sdk_database.dart b/lib/src/database/matrix_sdk_database.dart index d88b22a62..f4ded7e05 100644 --- a/lib/src/database/matrix_sdk_database.dart +++ b/lib/src/database/matrix_sdk_database.dart @@ -1093,45 +1093,37 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { } @override - Future storeEventUpdate(EventUpdate eventUpdate, Client client) async { - final tmpRoom = client.getRoomById(eventUpdate.roomID) ?? - Room(id: eventUpdate.roomID, client: client); + Future storeEventUpdate( + String roomId, + StrippedStateEvent event, + EventUpdateType type, + Client client, + ) async { + final tmpRoom = + client.getRoomById(roomId) ?? Room(id: roomId, client: client); // In case of this is a redaction event - if (eventUpdate.content['type'] == EventTypes.Redaction) { - final eventId = eventUpdate.content.tryGet('redacts'); - final event = + if (event.type == EventTypes.Redaction && event is MatrixEvent) { + final redactionEvent = Event.fromMatrixEvent(event, tmpRoom); + final eventId = redactionEvent.redacts; + final redactedEvent = eventId != null ? await getEventById(eventId, tmpRoom) : null; - if (event != null) { - event.setRedactionEvent(Event.fromJson(eventUpdate.content, tmpRoom)); + if (redactedEvent != null) { + redactedEvent.setRedactionEvent(redactionEvent); await _eventsBox.put( - TupleKey(eventUpdate.roomID, event.eventId).toString(), - event.toJson(), + TupleKey(roomId, redactedEvent.eventId).toString(), + redactedEvent.toJson(), ); - - if (tmpRoom.lastEvent?.eventId == event.eventId) { - if (client.importantStateEvents.contains(event.type)) { - await _preloadRoomStateBox.put( - TupleKey(eventUpdate.roomID, event.type, '').toString(), - event.toJson(), - ); - } else { - await _nonPreloadRoomStateBox.put( - TupleKey(eventUpdate.roomID, event.type, '').toString(), - event.toJson(), - ); - } - } } } // Store a common message event - if ({EventUpdateType.timeline, EventUpdateType.history} - .contains(eventUpdate.type)) { - final eventId = eventUpdate.content['event_id']; + if ({EventUpdateType.timeline, EventUpdateType.history}.contains(type) && + event is MatrixEvent) { + final timelineEvent = Event.fromMatrixEvent(event, tmpRoom); // Is this ID already in the store? - final prevEvent = await _eventsBox - .get(TupleKey(eventUpdate.roomID, eventId).toString()); + final prevEvent = + await _eventsBox.get(TupleKey(roomId, event.eventId).toString()); final prevStatus = prevEvent == null ? null : () { @@ -1144,13 +1136,7 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { }(); // calculate the status - final newStatus = eventStatusFromInt( - eventUpdate.content.tryGet('status') ?? - eventUpdate.content - .tryGetMap('unsigned') - ?.tryGet(messageSendingStatusKey) ?? - EventStatus.synced.intValue, - ); + final newStatus = timelineEvent.status; // Is this the response to a sending event which is already synced? Then // there is nothing to do here. @@ -1165,29 +1151,25 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { newStatus, ); - // Add the status and the sort order to the content so it get stored - eventUpdate.content['unsigned'] ??= {}; - eventUpdate.content['unsigned'][messageSendingStatusKey] = - eventUpdate.content['status'] = status.intValue; + timelineEvent.status = status; + final eventId = timelineEvent.eventId; // In case this event has sent from this account we have a transaction ID - final transactionId = eventUpdate.content - .tryGetMap('unsigned') - ?.tryGet('transaction_id'); + final transactionId = + timelineEvent.unsigned?.tryGet('transaction_id'); await _eventsBox.put( - TupleKey(eventUpdate.roomID, eventId).toString(), - eventUpdate.content, + TupleKey(roomId, eventId).toString(), + timelineEvent.toJson(), ); // Update timeline fragments - final key = TupleKey(eventUpdate.roomID, status.isSent ? '' : 'SENDING') - .toString(); + final key = TupleKey(roomId, status.isSent ? '' : 'SENDING').toString(); final eventIds = List.from(await _timelineFragmentsBox.get(key) ?? []); if (!eventIds.contains(eventId)) { - if (eventUpdate.type == EventUpdateType.history) { + if (type == EventUpdateType.history) { eventIds.add(eventId); } else { eventIds.insert(0, eventId); @@ -1196,7 +1178,7 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { } else if (status.isSynced && prevStatus != null && prevStatus.isSent && - eventUpdate.type != EventUpdateType.history) { + type != EventUpdateType.history) { // Status changes from 1 -> 2? Make sure event is correctly sorted. eventIds.remove(eventId); eventIds.insert(0, eventId); @@ -1204,7 +1186,7 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { // If event comes from server timeline, remove sending events with this ID if (status.isSent) { - final key = TupleKey(eventUpdate.roomID, 'SENDING').toString(); + final key = TupleKey(roomId, 'SENDING').toString(); final eventIds = List.from(await _timelineFragmentsBox.get(key) ?? []); final i = eventIds.indexWhere((id) => id == eventId); @@ -1215,37 +1197,38 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { // Is there a transaction id? Then delete the event with this id. if (!status.isError && !status.isSending && transactionId != null) { - await removeEvent(transactionId, eventUpdate.roomID); + await removeEvent(transactionId, roomId); } } - final stateKey = eventUpdate.content['state_key']; + final stateKey = event.stateKey; // Store a common state event if (stateKey != null && // Don't store events as state updates when paginating backwards. - (eventUpdate.type == EventUpdateType.timeline || - eventUpdate.type == EventUpdateType.state || - eventUpdate.type == EventUpdateType.inviteState)) { - if (eventUpdate.content['type'] == EventTypes.RoomMember) { + { + EventUpdateType.timeline, + EventUpdateType.state, + EventUpdateType.inviteState, + }.contains(type)) { + if (event.type == EventTypes.RoomMember) { await _roomMembersBox.put( TupleKey( - eventUpdate.roomID, - eventUpdate.content['state_key'], + roomId, + stateKey, ).toString(), - eventUpdate.content, + event.toJson(), ); } else { - final type = eventUpdate.content['type'] as String; - final roomStateBox = client.importantStateEvents.contains(type) + final roomStateBox = client.importantStateEvents.contains(event.type) ? _preloadRoomStateBox : _nonPreloadRoomStateBox; final key = TupleKey( - eventUpdate.roomID, - type, + roomId, + event.type, stateKey, ).toString(); - await roomStateBox.put(key, eventUpdate.content); + await roomStateBox.put(key, event.toJson()); } } } diff --git a/lib/src/event.dart b/lib/src/event.dart index b2f59cae7..d6c582366 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -88,6 +88,7 @@ class Event extends MatrixEvent { Map? unsigned, Map? prevContent, String? stateKey, + super.redacts, required this.room, MatrixEvent? originalSource, }) : _originalSource = originalSource, @@ -212,6 +213,7 @@ class Event extends MatrixEvent { ), unsigned: unsigned, room: room, + redacts: jsonPayload['redacts'], originalSource: originalSource.isEmpty ? null : MatrixEvent.fromJson(originalSource), ); diff --git a/lib/src/room.dart b/lib/src/room.dart index 255fdd626..29a411f96 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1674,11 +1674,9 @@ class Room { for (final user in users) { setState(user); // at *least* cache this in-memory await client.database?.storeEventUpdate( - EventUpdate( - roomID: id, - type: EventUpdateType.state, - content: user.toJson(), - ), + id, + user, + EventUpdateType.state, client, ); } @@ -1755,11 +1753,9 @@ class Room { // Store user in database: await client.database?.transaction(() async { await client.database?.storeEventUpdate( - EventUpdate( - content: foundUser.toJson(), - roomID: id, - type: EventUpdateType.state, - ), + id, + foundUser, + EventUpdateType.state, client, ); }); diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 04d3679ea..40dac5538 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -41,7 +41,8 @@ class Timeline { final void Function(int index)? onRemove; final void Function()? onNewEvent; - StreamSubscription? sub; + StreamSubscription? timelineSub; + StreamSubscription? historySub; StreamSubscription? roomSub; StreamSubscription? sessionIdReceivedSub; StreamSubscription? cancelSendEventSub; @@ -322,7 +323,18 @@ class Timeline { this.onNewEvent, required this.chunk, }) { - sub = room.client.onEvent.stream.listen(_handleEventUpdate); + timelineSub = room.client.onTimelineEvent.stream.listen( + (event) => _handleEventUpdate( + event, + EventUpdateType.timeline, + ), + ); + historySub = room.client.onHistoryEvent.stream.listen( + (event) => _handleEventUpdate( + event, + EventUpdateType.history, + ), + ); // If the timeline is limited we want to clear our events cache roomSub = room.client.onSync.stream @@ -368,7 +380,9 @@ class Timeline { /// Don't forget to call this before you dismiss this object! void cancelSubscriptions() { // ignore: discarded_futures - sub?.cancel(); + timelineSub?.cancel(); + // ignore: discarded_futures + historySub?.cancel(); // ignore: discarded_futures roomSub?.cancel(); // ignore: discarded_futures @@ -512,43 +526,35 @@ class Timeline { } } - void _handleEventUpdate(EventUpdate eventUpdate, {bool update = true}) { + void _handleEventUpdate( + Event event, + EventUpdateType type, { + bool update = true, + }) { try { - if (eventUpdate.roomID != room.id) return; + if (event.roomId != room.id) return; - if (eventUpdate.type != EventUpdateType.timeline && - eventUpdate.type != EventUpdateType.history) { + if (type != EventUpdateType.timeline && type != EventUpdateType.history) { return; } - if (eventUpdate.type == EventUpdateType.timeline) { + if (type == EventUpdateType.timeline) { onNewEvent?.call(); } if (!allowNewEvent) return; - final status = eventStatusFromInt( - eventUpdate.content['status'] ?? - (eventUpdate.content['unsigned'] is Map - ? eventUpdate.content['unsigned'][messageSendingStatusKey] - : null) ?? - EventStatus.synced.intValue, - ); + final status = event.status; final i = _findEvent( - event_id: eventUpdate.content['event_id'], - unsigned_txid: eventUpdate.content['unsigned'] is Map - ? eventUpdate.content['unsigned']['transaction_id'] - : null, + event_id: event.eventId, + unsigned_txid: event.unsigned?.tryGet('transaction_id'), ); if (i < events.length) { // if the old status is larger than the new one, we also want to preserve the old status final oldStatus = events[i].status; - events[i] = Event.fromJson( - eventUpdate.content, - room, - ); + events[i] = event; // do we preserve the status? we should allow 0 -> -1 updates and status increases if ((latestEventStatus(status, oldStatus) == oldStatus) && !(status.isError && oldStatus.isSending)) { @@ -557,34 +563,28 @@ class Timeline { addAggregatedEvent(events[i]); onChange?.call(i); } else { - final newEvent = Event.fromJson( - eventUpdate.content, - room, - ); - - if (eventUpdate.type == EventUpdateType.history && + if (type == EventUpdateType.history && events.indexWhere( - (e) => e.eventId == eventUpdate.content['event_id'], + (e) => e.eventId == event.eventId, ) != -1) { return; } var index = events.length; - if (eventUpdate.type == EventUpdateType.history) { - events.add(newEvent); + if (type == EventUpdateType.history) { + events.add(event); } else { index = events.firstIndexWhereNotError; - events.insert(index, newEvent); + events.insert(index, event); } onInsert?.call(index); - addAggregatedEvent(newEvent); + addAggregatedEvent(event); } // Handle redaction events - if (eventUpdate.content['type'] == EventTypes.Redaction) { - final index = - _findEvent(event_id: eventUpdate.content.tryGet('redacts')); + if (event.type == EventTypes.Redaction) { + final index = _findEvent(event_id: event.redacts); if (index < events.length) { removeAggregatedEvent(events[index]); @@ -598,12 +598,7 @@ class Timeline { } } - events[index].setRedactionEvent( - Event.fromJson( - eventUpdate.content, - room, - ), - ); + events[index].setRedactionEvent(event); onChange?.call(index); } } diff --git a/lib/src/utils/event_update.dart b/lib/src/utils/event_update.dart index af0926c77..f410b33a6 100644 --- a/lib/src/utils/event_update.dart +++ b/lib/src/utils/event_update.dart @@ -33,8 +33,7 @@ enum EventUpdateType { decryptedTimelineQueue, } -/// Represents a new event (e.g. a message in a room) or an update for an -/// already known event. +@Deprecated('Use `Event` class directly instead.') class EventUpdate { /// Usually 'timeline', 'state' or whatever. final EventUpdateType type; diff --git a/test/client_test.dart b/test/client_test.dart index 0f8a84dfb..50bb4e9f2 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -114,7 +114,7 @@ void main() { httpClient: FakeMatrixApi(), databaseBuilder: getDatabase, ); - final eventUpdateListFuture = matrix.onEvent.stream.toList(); + final eventUpdateListFuture = matrix.onTimelineEvent.stream.toList(); final toDeviceUpdateListFuture = matrix.onToDeviceEvent.stream.toList(); var presenceCounter = 0; var accountDataCounter = 0; @@ -278,43 +278,20 @@ void main() { null, ); - await matrix.onEvent.close(); + await matrix.onTimelineEvent.close(); final eventUpdateList = await eventUpdateListFuture; - expect(eventUpdateList.length, 13); + expect(eventUpdateList.length, 3); - expect(eventUpdateList[0].content['type'], 'm.room.member'); - expect(eventUpdateList[0].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[0].type, EventUpdateType.state); + expect(eventUpdateList[0].type, 'm.room.member'); + expect(eventUpdateList[0].roomId, '!726s6s6q:example.com'); - expect(eventUpdateList[1].content['type'], 'm.room.canonical_alias'); - expect(eventUpdateList[1].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[1].type, EventUpdateType.state); + expect(eventUpdateList[1].type, 'm.room.message'); + expect(eventUpdateList[1].roomId, '!726s6s6q:example.com'); - expect(eventUpdateList[2].content['type'], 'm.room.encryption'); - expect(eventUpdateList[2].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[2].type, EventUpdateType.state); - - expect(eventUpdateList[3].content['type'], 'm.room.pinned_events'); - expect(eventUpdateList[3].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[3].type, EventUpdateType.state); - - expect(eventUpdateList[4].content['type'], 'm.room.member'); - expect(eventUpdateList[4].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[4].type, EventUpdateType.timeline); - - expect(eventUpdateList[5].content['type'], 'm.room.message'); - expect(eventUpdateList[5].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[5].type, EventUpdateType.timeline); - - expect(eventUpdateList[6].content['type'], 'm.room.member'); - expect(eventUpdateList[6].roomID, '!calls:example.com'); - expect(eventUpdateList[6].type, EventUpdateType.state); - - expect(eventUpdateList[7].content['type'], 'm.room.member'); - expect(eventUpdateList[7].roomID, '!calls:example.com'); - expect(eventUpdateList[7].type, EventUpdateType.state); + expect(eventUpdateList[2].type, 'm.room.message'); + expect(eventUpdateList[2].roomId, '!726s6s6f:example.com'); expect( matrix diff --git a/test/database_api_test.dart b/test/database_api_test.dart index eda46236a..80c07f55c 100644 --- a/test/database_api_test.dart +++ b/test/database_api_test.dart @@ -219,10 +219,9 @@ void main() { }); test('storeEventUpdate', () async { await database.storeEventUpdate( - EventUpdate( - roomID: '!testroom:example.com', - type: EventUpdateType.timeline, - content: { + '!testroom:example.com', + MatrixEvent.fromJson( + { 'type': EventTypes.Message, 'content': { 'body': '* edit 3', @@ -236,10 +235,12 @@ void main() { 'rel_type': RelationshipTypes.edit, }, }, + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, 'event_id': '\$event:example.com', 'sender': '@bob:example.org', }, ), + EventUpdateType.timeline, Client('testclient'), ); }); @@ -263,10 +264,9 @@ void main() { ); await database.storeEventUpdate( - EventUpdate( - roomID: roomid, - type: EventUpdateType.timeline, - content: { + roomid, + MatrixEvent.fromJson( + { 'type': EventTypes.RoomName, 'content': { 'name': 'start', @@ -274,8 +274,10 @@ void main() { 'event_id': '\$eventstart:example.com', 'sender': '@bob:example.org', 'state_key': '', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, ), + EventUpdateType.timeline, client, ); @@ -288,10 +290,9 @@ void main() { expect(room?.roomAccountData['m.test']?.content, {'foo': 'bar'}); await database.storeEventUpdate( - EventUpdate( - roomID: roomid, - type: EventUpdateType.timeline, - content: { + roomid, + MatrixEvent.fromJson( + { 'type': EventTypes.RoomName, 'content': { 'name': 'update', @@ -299,8 +300,10 @@ void main() { 'event_id': '\$eventupdate:example.com', 'sender': '@bob:example.org', 'state_key': '', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, ), + EventUpdateType.timeline, client, ); @@ -309,10 +312,9 @@ void main() { expect(room?.name, 'update'); await database.storeEventUpdate( - EventUpdate( - roomID: roomid, - type: EventUpdateType.state, - content: { + roomid, + MatrixEvent.fromJson( + { 'type': EventTypes.RoomName, 'content': { 'name': 'update2', @@ -320,8 +322,10 @@ void main() { 'event_id': '\$eventupdate2:example.com', 'sender': '@bob:example.org', 'state_key': '', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, ), + EventUpdateType.state, client, ); @@ -330,10 +334,9 @@ void main() { expect(room?.name, 'update2'); await database.storeEventUpdate( - EventUpdate( - roomID: roomid, - type: EventUpdateType.inviteState, - content: { + roomid, + StrippedStateEvent.fromJson( + { 'type': EventTypes.RoomName, 'content': { 'name': 'update3', @@ -343,6 +346,7 @@ void main() { 'state_key': '', }, ), + EventUpdateType.inviteState, client, ); @@ -351,10 +355,9 @@ void main() { expect(room?.name, 'update3'); await database.storeEventUpdate( - EventUpdate( - roomID: roomid, - type: EventUpdateType.history, - content: { + roomid, + MatrixEvent.fromJson( + { 'type': EventTypes.RoomName, 'content': { 'name': 'notupdate', @@ -362,8 +365,10 @@ void main() { 'event_id': '\$eventnotupdate:example.com', 'sender': '@bob:example.org', 'state_key': '', + 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, ), + EventUpdateType.history, client, ); diff --git a/test/matrix_database_test.dart b/test/matrix_database_test.dart index 95a58d8bc..4fb9834e8 100644 --- a/test/matrix_database_test.dart +++ b/test/matrix_database_test.dart @@ -46,89 +46,105 @@ void main() { final client = Client('testclient'); final database = await getDatabase(client); // store a simple update - var update = EventUpdate( - type: EventUpdateType.timeline, - roomID: room.id, - content: { - 'type': 'm.room.message', - 'origin_server_ts': 100, - 'content': {'blah': 'blubb'}, - 'event_id': '\$event-1', - 'sender': '@blah:blubb', - }, + await database.storeEventUpdate( + room.id, + MatrixEvent.fromJson( + { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-1', + 'sender': '@blah:blubb', + }, + ), + EventUpdateType.timeline, + client, ); - await database.storeEventUpdate(update, client); var event = await database.getEventById('\$event-1', room); expect(event?.eventId, '\$event-1'); // insert a transaction id - update = EventUpdate( - type: EventUpdateType.timeline, - roomID: room.id, - content: { - 'type': 'm.room.message', - 'origin_server_ts': 100, - 'content': {'blah': 'blubb'}, - 'event_id': 'transaction-1', - 'sender': '@blah:blubb', - 'status': EventStatus.sending.intValue, - }, + await database.storeEventUpdate( + room.id, + Event.fromJson( + { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': 'transaction-1', + 'sender': '@blah:blubb', + 'status': EventStatus.sending.intValue, + }, + room, + ), + EventUpdateType.timeline, + client, ); - await database.storeEventUpdate(update, client); event = await database.getEventById('transaction-1', room); expect(event?.eventId, 'transaction-1'); - update = EventUpdate( - type: EventUpdateType.timeline, - roomID: room.id, - content: { - 'type': 'm.room.message', - 'origin_server_ts': 100, - 'content': {'blah': 'blubb'}, - 'event_id': '\$event-2', - 'sender': '@blah:blubb', - 'unsigned': { - 'transaction_id': 'transaction-1', + + await database.storeEventUpdate( + room.id, + Event.fromJson( + { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-2', + 'sender': '@blah:blubb', + 'unsigned': { + 'transaction_id': 'transaction-1', + }, + 'status': EventStatus.sent.intValue, }, - 'status': EventStatus.sent.intValue, - }, + room, + ), + EventUpdateType.timeline, + client, ); - await database.storeEventUpdate(update, client); event = await database.getEventById('transaction-1', room); expect(event, null); event = await database.getEventById('\$event-2', room); // insert a transaction id if the event id for it already exists - update = EventUpdate( - type: EventUpdateType.timeline, - roomID: room.id, - content: { - 'type': 'm.room.message', - 'origin_server_ts': 100, - 'content': {'blah': 'blubb'}, - 'event_id': '\$event-3', - 'sender': '@blah:blubb', - 'status': EventStatus.sending.intValue, - }, + await database.storeEventUpdate( + room.id, + Event.fromJson( + { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-3', + 'sender': '@blah:blubb', + 'status': EventStatus.sending.intValue, + }, + room, + ), + EventUpdateType.timeline, + client, ); - await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-3', room); expect(event?.eventId, '\$event-3'); - update = EventUpdate( - type: EventUpdateType.timeline, - roomID: room.id, - content: { - 'type': 'm.room.message', - 'origin_server_ts': 100, - 'content': {'blah': 'blubb'}, - 'event_id': '\$event-3', - 'sender': '@blah:blubb', - 'status': EventStatus.sent.intValue, - 'unsigned': { - 'transaction_id': 'transaction-2', + + await database.storeEventUpdate( + room.id, + Event.fromJson( + { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-3', + 'sender': '@blah:blubb', + 'status': EventStatus.sent.intValue, + 'unsigned': { + 'transaction_id': 'transaction-2', + }, }, - }, + room, + ), + EventUpdateType.timeline, + client, ); - await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-3', room); expect(event?.eventId, '\$event-3'); expect(event?.status, EventStatus.sent); @@ -136,37 +152,44 @@ void main() { expect(event, null); // insert transaction id and not update status - update = EventUpdate( - type: EventUpdateType.timeline, - roomID: room.id, - content: { - 'type': 'm.room.message', - 'origin_server_ts': 100, - 'content': {'blah': 'blubb'}, - 'event_id': '\$event-4', - 'sender': '@blah:blubb', - 'status': EventStatus.synced.intValue, - }, + await database.storeEventUpdate( + room.id, + Event.fromJson( + { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-4', + 'sender': '@blah:blubb', + 'status': EventStatus.synced.intValue, + }, + room, + ), + EventUpdateType.timeline, + client, ); - await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-4', room); expect(event?.eventId, '\$event-4'); - update = EventUpdate( - type: EventUpdateType.timeline, - roomID: room.id, - content: { - 'type': 'm.room.message', - 'origin_server_ts': 100, - 'content': {'blah': 'blubb'}, - 'event_id': '\$event-4', - 'sender': '@blah:blubb', - 'status': EventStatus.sent.intValue, - 'unsigned': { - 'transaction_id': 'transaction-3', + + await database.storeEventUpdate( + room.id, + Event.fromJson( + { + 'type': 'm.room.message', + 'origin_server_ts': 100, + 'content': {'blah': 'blubb'}, + 'event_id': '\$event-4', + 'sender': '@blah:blubb', + 'status': EventStatus.sent.intValue, + 'unsigned': { + 'transaction_id': 'transaction-3', + }, }, - }, + room, + ), + EventUpdateType.timeline, + client, ); - await database.storeEventUpdate(update, client); event = await database.getEventById('\$event-4', room); expect(event?.eventId, '\$event-4'); expect(event?.status, EventStatus.synced); diff --git a/test/timeline_context_test.dart b/test/timeline_context_test.dart index ae27e0905..779aa7b7c 100644 --- a/test/timeline_context_test.dart +++ b/test/timeline_context_test.dart @@ -142,11 +142,9 @@ void main() { ); // expect no new events to have been added final eventId = '1844295642248BcDkn:example.org'; - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'test'}, 'sender': '@alice:example.com', @@ -155,6 +153,7 @@ void main() { 'unsigned': {'transaction_id': '1234'}, 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); // just assume that it was on the server for this call but not for the following. @@ -208,11 +207,9 @@ void main() { expect(eventId.startsWith('\$event'), true); expect(timeline.events[0].status, EventStatus.sent); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'test'}, 'sender': '@alice:example.com', @@ -221,6 +218,7 @@ void main() { 'unsigned': {'transaction_id': '1234'}, 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); @@ -238,11 +236,9 @@ void main() { await timeline.requestFuture(); await waitForCount(6); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -250,6 +246,7 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); @@ -296,11 +293,9 @@ void main() { await timeline.requestFuture(); await timeline.requestFuture(); // send a failed message - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -308,6 +303,7 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); await waitForCount(7); @@ -327,11 +323,9 @@ void main() { test('getEventById', () async { await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -339,6 +333,7 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); await waitForCount(7); @@ -360,11 +355,9 @@ void main() { timeline.events.clear(); await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -373,6 +366,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'newresend'}, }, + room, ), ); await waitForCount(7); @@ -438,11 +432,9 @@ void main() { timeline.events.clear(); await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -450,13 +442,12 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -464,6 +455,7 @@ void main() { 'event_id': 'def', 'origin_server_ts': testTimeStamp + 5, }, + room, ), ); await waitForCount(8); @@ -475,11 +467,9 @@ void main() { timeline.events.clear(); await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -487,16 +477,15 @@ void main() { 'event_id': 'will-fail', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await waitForCount(7); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -504,6 +493,7 @@ void main() { 'event_id': 'will-fail', 'origin_server_ts': testTimeStamp, }, + room, ), ); await waitForCount(8); @@ -513,11 +503,9 @@ void main() { test('setReadMarker', () async { await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -525,6 +513,7 @@ void main() { 'event_id': 'will-work', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await waitForCount(7); @@ -539,11 +528,9 @@ void main() { await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -551,16 +538,15 @@ void main() { 'event_id': 'transaction', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await waitForCount(7); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -569,16 +555,15 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await waitForCount(8); expect(timeline.events[0].status, EventStatus.sent); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -587,6 +572,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await waitForCount(9); @@ -599,11 +585,9 @@ void main() { await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -614,16 +598,15 @@ void main() { 'transaction_id': 'transaction', }, }, + room, ), ); await waitForCount(7); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -634,16 +617,15 @@ void main() { messageSendingStatusKey: EventStatus.synced.intValue, }, }, + room, ), ); await waitForCount(8); expect(timeline.events[0].status, EventStatus.synced); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -654,6 +636,7 @@ void main() { messageSendingStatusKey: EventStatus.sent.intValue, }, }, + room, ), ); await waitForCount(9); @@ -665,11 +648,9 @@ void main() { await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -677,16 +658,15 @@ void main() { 'event_id': 'transaction', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await waitForCount(7); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -694,16 +674,15 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await waitForCount(8); expect(timeline.events[0].status, EventStatus.error); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -712,6 +691,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await waitForCount(9); @@ -723,11 +703,9 @@ void main() { await timeline.requestFuture(); await timeline.requestFuture(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -735,16 +713,15 @@ void main() { 'event_id': 'transaction', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await waitForCount(7); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -753,16 +730,15 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await waitForCount(8); expect(timeline.events[0].status, EventStatus.synced); expect(timeline.events.length, 4); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -770,6 +746,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await waitForCount(9); diff --git a/test/timeline_test.dart b/test/timeline_test.dart index d12f8fabd..a31b2068a 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -115,11 +115,9 @@ void main() { ); test('Create', () async { - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -127,13 +125,12 @@ void main() { 'event_id': '2', 'origin_server_ts': testTimeStamp - 1000, }, + room, ), ); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -141,10 +138,12 @@ void main() { 'event_id': '\$1', 'origin_server_ts': testTimeStamp, }, + room, ), ); - expect(timeline.sub != null, true); + expect(timeline.timelineSub != null, true); + expect(timeline.historySub != null, true); await waitForCount(2); @@ -202,11 +201,9 @@ void main() { expect(timeline.events[0].receipts.length, 1); expect(timeline.events[0].receipts[0].user.id, '@alice:example.com'); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.redaction', 'content': {'reason': 'spamming'}, 'sender': '@alice:example.com', @@ -214,6 +211,7 @@ void main() { 'event_id': '3', 'origin_server_ts': testTimeStamp + 1000, }, + room, ), ); @@ -269,7 +267,8 @@ void main() { ), ); - expect(timeline.sub != null, true); + expect(timeline.timelineSub != null, true); + expect(timeline.historySub != null, true); await waitForCount(3); @@ -584,7 +583,8 @@ void main() { ), ); - expect(timeline.sub != null, true); + expect(timeline.timelineSub != null, true); + expect(timeline.historySub != null, true); await waitForCount(1); @@ -604,11 +604,9 @@ void main() { expect(eventId.startsWith('\$event'), true); expect(timeline.events[0].status, EventStatus.sent); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'test'}, 'sender': '@alice:example.com', @@ -617,6 +615,7 @@ void main() { 'unsigned': {'transaction_id': '1234'}, 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); @@ -629,11 +628,9 @@ void main() { }); test('Send message with error', () async { - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': { 'msgtype': 'm.text', @@ -644,6 +641,7 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); await waitForCount(1); @@ -668,11 +666,9 @@ void main() { test('Remove message', () async { // send a failed message - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -680,6 +676,7 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); await waitForCount(1); @@ -695,11 +692,9 @@ void main() { }); test('getEventById', () async { - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -707,6 +702,7 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); await waitForCount(1); @@ -726,11 +722,9 @@ void main() { test('Resend message', () async { timeline.events.clear(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -739,6 +733,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'newresend'}, }, + room, ), ); await waitForCount(1); @@ -799,11 +794,9 @@ void main() { test('sort errors on top', () async { timeline.events.clear(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -811,13 +804,12 @@ void main() { 'event_id': 'abc', 'origin_server_ts': testTimeStamp, }, + room, ), ); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -825,6 +817,7 @@ void main() { 'event_id': 'def', 'origin_server_ts': testTimeStamp + 5, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); @@ -834,11 +827,9 @@ void main() { test('sending event to failed update', () async { timeline.events.clear(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -846,16 +837,15 @@ void main() { 'event_id': 'will-fail', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -863,6 +853,7 @@ void main() { 'event_id': 'will-fail', 'origin_server_ts': testTimeStamp, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); @@ -870,11 +861,9 @@ void main() { expect(timeline.events.length, 1); }); test('setReadMarker', () async { - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -882,6 +871,7 @@ void main() { 'event_id': 'will-work', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); @@ -892,11 +882,9 @@ void main() { test('sending an event and the http request finishes first, 0 -> 1 -> 2', () async { timeline.events.clear(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -904,16 +892,15 @@ void main() { 'event_id': 'transaction', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -922,16 +909,15 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.sent); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -940,6 +926,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); @@ -949,11 +936,9 @@ void main() { test('sending an event where the sync reply arrives first, 0 -> 2 -> 1', () async { timeline.events.clear(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -964,16 +949,15 @@ void main() { 'transaction_id': 'transaction', }, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -984,16 +968,15 @@ void main() { messageSendingStatusKey: EventStatus.synced.intValue, }, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.synced); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -1004,6 +987,7 @@ void main() { messageSendingStatusKey: EventStatus.sent.intValue, }, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); @@ -1012,11 +996,9 @@ void main() { }); test('sending an event 0 -> -1 -> 2', () async { timeline.events.clear(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -1024,16 +1006,15 @@ void main() { 'event_id': 'transaction', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -1041,16 +1022,15 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.error); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -1059,6 +1039,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); @@ -1067,11 +1048,9 @@ void main() { }); test('sending an event 0 -> 2 -> -1', () async { timeline.events.clear(); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -1079,16 +1058,15 @@ void main() { 'event_id': 'transaction', 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.sending); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -1097,16 +1075,15 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50)); expect(timeline.events[0].status, EventStatus.synced); expect(timeline.events.length, 1); - client.onEvent.add( - EventUpdate( - type: EventUpdateType.timeline, - roomID: roomID, - content: { + client.onTimelineEvent.add( + Event.fromJson( + { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, 'sender': '@alice:example.com', @@ -1114,6 +1091,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': {'transaction_id': 'transaction'}, }, + room, ), ); await Future.delayed(Duration(milliseconds: 50));