From c9d3c327f67c67a1511fce51c5ad79da4a6beff4 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 26 Feb 2021 12:53:28 +0100 Subject: [PATCH] refactor: Deprecate eventType in EventUpdate --- lib/encryption/encryption.dart | 5 ++-- lib/src/client.dart | 5 ++-- lib/src/database/database.dart | 4 ++-- lib/src/event.dart | 1 - lib/src/room.dart | 1 - lib/src/timeline.dart | 2 +- lib/src/utils/event_update.dart | 13 ++++------- test/client_test.dart | 27 +++++++++++----------- test/encryption/key_verification_test.dart | 2 -- test/matrix_database_test.dart | 7 ------ test/timeline_test.dart | 22 ------------------ 11 files changed, 26 insertions(+), 63 deletions(-) diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index 4a75f1dd..f391883e 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -118,8 +118,8 @@ class Encryption { if (update.type == EventUpdateType.ephemeral) { return; } - if (update.eventType.startsWith('m.key.verification.') || - (update.eventType == EventTypes.Message && + if (update.content['type'].startsWith('m.key.verification.') || + (update.content['type'] == EventTypes.Message && (update.content['content']['msgtype'] is String) && update.content['content']['msgtype'] .startsWith('m.key.verification.'))) { @@ -281,7 +281,6 @@ class Encryption { await client.database?.storeEventUpdate( client.id, EventUpdate( - eventType: event.type, content: event.toJson(), roomID: event.roomId, type: updateType, diff --git a/lib/src/client.dart b/lib/src/client.dart index 7dfc3014..3d57efb9 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1271,7 +1271,6 @@ class Client extends MatrixApi { ? (sortAtTheEnd ? room.oldSortOrder : room.newSortOrder) : 0.0; var update = EventUpdate( - eventType: event['type'], roomID: roomID, type: type, content: event, @@ -1421,11 +1420,11 @@ sort order of ${prevState.sortOrder}. This should never happen...'''); } break; case EventUpdateType.accountData: - room.roomAccountData[eventUpdate.eventType] = + room.roomAccountData[eventUpdate.content['type']] = BasicRoomEvent.fromJson(eventUpdate.content); break; case EventUpdateType.ephemeral: - room.ephemerals[eventUpdate.eventType] = + room.ephemerals[eventUpdate.content['type']] = BasicRoomEvent.fromJson(eventUpdate.content); break; case EventUpdateType.history: diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index 20c6233c..0a5b004e 100644 --- a/lib/src/database/database.dart +++ b/lib/src/database/database.dart @@ -461,7 +461,7 @@ class Database extends _$Database { stateKey = eventContent['state_key']; } - if (eventUpdate.eventType == api.EventTypes.Redaction) { + if (eventUpdate.content['type'] == api.EventTypes.Redaction) { await redactMessage(clientId, eventUpdate); } @@ -556,7 +556,7 @@ class Database extends _$Database { api.EventTypes.Message, api.EventTypes.Sticker, api.EventTypes.Encrypted - ].contains(eventUpdate.eventType))) { + ].contains(eventUpdate.content['type']))) { final now = DateTime.now(); await storeRoomState( clientId, diff --git a/lib/src/event.dart b/lib/src/event.dart index f839ad23..7b6d630a 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -320,7 +320,6 @@ class Event extends MatrixEvent { room.client.onEvent.add(EventUpdate( roomID: room.id, type: EventUpdateType.timeline, - eventType: type, content: { 'event_id': eventId, 'status': -2, diff --git a/lib/src/room.dart b/lib/src/room.dart index 3cb63453..d9c170d0 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1301,7 +1301,6 @@ class Room { content: content, roomID: id, type: EventUpdateType.state, - eventType: EventTypes.RoomMember, sortOrder: 0.0), ); }); diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index f58677c3..0688822e 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -263,7 +263,7 @@ class Timeline { : null) ?? 2; // Redaction events are handled as modification for existing events. - if (eventUpdate.eventType == EventTypes.Redaction) { + if (eventUpdate.content['type'] == EventTypes.Redaction) { final eventId = _findEvent(event_id: eventUpdate.content['redacts']); if (eventId < events.length) { removeAggregatedEvent(events[eventId]); diff --git a/lib/src/utils/event_update.dart b/lib/src/utils/event_update.dart index de3e0194..772eeebe 100644 --- a/lib/src/utils/event_update.dart +++ b/lib/src/utils/event_update.dart @@ -36,10 +36,8 @@ class EventUpdate { /// Most events belong to a room. If not, this equals to eventType. final String roomID; - /// See (Matrix Room Events)[https://matrix.org/docs/spec/client_server/r0.4.0.html#room-events] - /// and (Matrix Events)[https://matrix.org/docs/spec/client_server/r0.4.0.html#id89] for more - /// informations. - final String eventType; + @Deprecated("Use `content['eventType']` instead.") + String get eventType => content['type']; // The json payload of the content of this event. final Map content; @@ -47,11 +45,11 @@ class EventUpdate { // the order where to stort this event final double sortOrder; - EventUpdate( - {this.eventType, this.roomID, this.type, this.content, this.sortOrder}); + EventUpdate({this.roomID, this.type, this.content, this.sortOrder}); Future decrypt(Room room, {bool store = false}) async { - if (eventType != EventTypes.Encrypted || !room.client.encryptionEnabled) { + if (content['type'] != EventTypes.Encrypted || + !room.client.encryptionEnabled) { return this; } try { @@ -59,7 +57,6 @@ class EventUpdate { room.id, Event.fromJson(content, room, sortOrder), store: store, updateType: type); return EventUpdate( - eventType: decrpytedEvent.type, roomID: roomID, type: type, content: decrpytedEvent.toJson(), diff --git a/test/client_test.dart b/test/client_test.dart index 65ab1199..6f996a6b 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -245,55 +245,56 @@ void main() { expect(eventUpdateList.length, 14); - expect(eventUpdateList[0].eventType, 'm.room.member'); + expect(eventUpdateList[0].content['type'], 'm.room.member'); expect(eventUpdateList[0].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[0].type, EventUpdateType.state); - expect(eventUpdateList[1].eventType, 'm.room.canonical_alias'); + expect(eventUpdateList[1].content['type'], 'm.room.canonical_alias'); expect(eventUpdateList[1].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[1].type, EventUpdateType.state); - expect(eventUpdateList[2].eventType, 'm.room.encryption'); + expect(eventUpdateList[2].content['type'], 'm.room.encryption'); expect(eventUpdateList[2].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[2].type, EventUpdateType.state); - expect(eventUpdateList[3].eventType, 'm.room.pinned_events'); + 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].eventType, 'm.room.member'); + expect(eventUpdateList[4].content['type'], 'm.room.member'); expect(eventUpdateList[4].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[4].type, EventUpdateType.timeline); - expect(eventUpdateList[5].eventType, 'm.room.message'); + expect(eventUpdateList[5].content['type'], 'm.room.message'); expect(eventUpdateList[5].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[5].type, EventUpdateType.timeline); - expect(eventUpdateList[6].eventType, 'm.typing'); + expect(eventUpdateList[6].content['type'], 'm.typing'); expect(eventUpdateList[6].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[6].type, EventUpdateType.ephemeral); - expect(eventUpdateList[7].eventType, 'm.receipt'); + expect(eventUpdateList[7].content['type'], 'm.receipt'); expect(eventUpdateList[7].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[7].type, EventUpdateType.ephemeral); - expect(eventUpdateList[8].eventType, 'm.receipt'); + expect(eventUpdateList[8].content['type'], 'm.receipt'); expect(eventUpdateList[8].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[8].type, EventUpdateType.accountData); - expect(eventUpdateList[9].eventType, 'm.tag'); + expect(eventUpdateList[9].content['type'], 'm.tag'); expect(eventUpdateList[9].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[9].type, EventUpdateType.accountData); - expect(eventUpdateList[10].eventType, 'org.example.custom.room.config'); + expect(eventUpdateList[10].content['type'], + 'org.example.custom.room.config'); expect(eventUpdateList[10].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[10].type, EventUpdateType.accountData); - expect(eventUpdateList[11].eventType, 'm.room.name'); + expect(eventUpdateList[11].content['type'], 'm.room.name'); expect(eventUpdateList[11].roomID, '!696r7674:example.com'); expect(eventUpdateList[11].type, EventUpdateType.inviteState); - expect(eventUpdateList[12].eventType, 'm.room.member'); + expect(eventUpdateList[12].content['type'], 'm.room.member'); expect(eventUpdateList[12].roomID, '!696r7674:example.com'); expect(eventUpdateList[12].type, EventUpdateType.inviteState); }); diff --git a/test/encryption/key_verification_test.dart b/test/encryption/key_verification_test.dart index 8bbf3e8e..3a77f1b4 100644 --- a/test/encryption/key_verification_test.dart +++ b/test/encryption/key_verification_test.dart @@ -53,7 +53,6 @@ EventUpdate getLastSentEvent(KeyVerification req) { 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, 'sender': req.client.userID, }, - eventType: type, type: EventUpdateType.timeline, roomID: req.room.id, ); @@ -448,7 +447,6 @@ void main() { 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, 'sender': client2.userID, }, - eventType: 'm.key.verification.ready', type: EventUpdateType.timeline, roomID: req2.room.id, )); diff --git a/test/matrix_database_test.dart b/test/matrix_database_test.dart index 8ecd8c8a..e8ceca60 100644 --- a/test/matrix_database_test.dart +++ b/test/matrix_database_test.dart @@ -45,7 +45,6 @@ void main() { var update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'origin_server_ts': 100, @@ -63,7 +62,6 @@ void main() { update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'origin_server_ts': 100, @@ -80,7 +78,6 @@ void main() { update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'origin_server_ts': 100, @@ -103,7 +100,6 @@ void main() { update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'origin_server_ts': 100, @@ -120,7 +116,6 @@ void main() { update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'origin_server_ts': 100, @@ -145,7 +140,6 @@ void main() { update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'origin_server_ts': 100, @@ -162,7 +156,6 @@ void main() { update = EventUpdate( type: EventUpdateType.timeline, roomID: room.id, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'origin_server_ts': 100, diff --git a/test/timeline_test.dart b/test/timeline_test.dart index 2a67644e..762a30d6 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -71,7 +71,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -84,7 +83,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -133,7 +131,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.redaction', content: { 'type': 'm.room.redaction', 'content': {'reason': 'spamming'}, @@ -168,7 +165,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'test'}, @@ -193,7 +189,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -257,7 +252,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -314,7 +308,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -327,7 +320,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -347,7 +339,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -363,7 +354,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -383,7 +373,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -399,7 +388,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -416,7 +404,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -437,7 +424,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -456,7 +442,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -475,7 +460,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -497,7 +481,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -513,7 +496,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -529,7 +511,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -549,7 +530,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -565,7 +545,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'}, @@ -582,7 +561,6 @@ void main() { client.onEvent.add(EventUpdate( type: EventUpdateType.timeline, roomID: roomID, - eventType: 'm.room.message', content: { 'type': 'm.room.message', 'content': {'msgtype': 'm.text', 'body': 'Testcase'},