From 3c386391cf0a50725d8b6d66a41f9589a15814b5 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 15 Feb 2020 08:58:08 +0000 Subject: [PATCH] [Timeline] Try to decrypt but not block --- lib/src/client.dart | 15 +-------------- lib/src/room.dart | 8 ++++---- lib/src/sync/event_update.dart | 21 +++++++++++++++++++++ lib/src/timeline.dart | 12 ------------ 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 1e08fc3..3b0309d 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1178,20 +1178,7 @@ class Client { ); this.store?.storeEventUpdate(update); if (event["type"] == "m.room.encrypted") { - Room room = getRoomById(roomID); - try { - Event decrpytedEvent = - room.decryptGroupMessage(Event.fromJson(event, room)); - event = decrpytedEvent.toJson(); - update = EventUpdate( - eventType: event["type"], - roomID: roomID, - type: type, - content: event, - ); - } catch (e) { - print("[LibOlm] Could not decrypt megolm event: " + e.toString()); - } + update = update.decrypt(this.getRoomById(update.roomID)); } _updateRoomsByEventUpdate(update); onEvent.add(update); diff --git a/lib/src/room.dart b/lib/src/room.dart index db65cfd..bbc5459 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -698,7 +698,7 @@ class Room { eventType: resp["state"][i]["type"], content: resp["state"][i], ); - client.onEvent.add(eventUpdate); + client.onEvent.add(eventUpdate.decrypt(this)); client.store.storeEventUpdate(eventUpdate); } return; @@ -711,7 +711,7 @@ class Room { eventType: resp["state"][i]["type"], content: resp["state"][i], ); - client.onEvent.add(eventUpdate); + client.onEvent.add(eventUpdate.decrypt(this)); } } } @@ -725,7 +725,7 @@ class Room { eventType: history[i]["type"], content: history[i], ); - client.onEvent.add(eventUpdate); + client.onEvent.add(eventUpdate.decrypt(this)); client.store.storeEventUpdate(eventUpdate); client.store.setRoomPrevBatch(id, resp["end"]); } @@ -739,7 +739,7 @@ class Room { eventType: history[i]["type"], content: history[i], ); - client.onEvent.add(eventUpdate); + client.onEvent.add(eventUpdate.decrypt(this)); } } client.onRoomUpdate.add( diff --git a/lib/src/sync/event_update.dart b/lib/src/sync/event_update.dart index 4de7339..504807d 100644 --- a/lib/src/sync/event_update.dart +++ b/lib/src/sync/event_update.dart @@ -21,6 +21,8 @@ * along with famedlysdk. If not, see . */ +import '../../famedlysdk.dart'; + /// Represents a new event (e.g. a message in a room) or an update for an /// already known event. class EventUpdate { @@ -39,4 +41,23 @@ class EventUpdate { final Map content; EventUpdate({this.eventType, this.roomID, this.type, this.content}); + + EventUpdate decrypt(Room room) { + if (eventType != "m.room.encrypted") { + return this; + } + try { + Event decrpytedEvent = + room.decryptGroupMessage(Event.fromJson(content, room)); + return EventUpdate( + eventType: eventType, + roomID: roomID, + type: type, + content: decrpytedEvent.toJson(), + ); + } catch (e) { + print("[LibOlm] Could not decrypt megolm event: " + e.toString()); + return this; + } + } } diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index f5aca8e..0e18c0c 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -92,18 +92,6 @@ class Timeline { try { if (eventUpdate.roomID != room.id) return; - if (eventUpdate.eventType == "m.room.encrypted") { - Event decrypted = - room.decryptGroupMessage(Event.fromJson(eventUpdate.content, room)); - eventUpdate = EventUpdate( - eventType: decrypted.typeKey, - content: eventUpdate.content, - type: eventUpdate.type, - roomID: eventUpdate.roomID, - ); - eventUpdate.content["content"] = decrypted.content; - } - if (eventUpdate.type == "timeline" || eventUpdate.type == "history") { // Redaction events are handled as modification for existing events. if (eventUpdate.eventType == "m.room.redaction") {