diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index 220e11f0..b9387de0 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -154,20 +154,20 @@ class Encryption { } } - Future handleEventUpdate(EventUpdate update) async { - if (update.type == EventUpdateType.history) { + Future handleEventUpdate(Event event, EventUpdateType type) async { + if (type == EventUpdateType.history) { return; } - 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.'))) { + if (event.type.startsWith('m.key.verification.') || + (event.type == EventTypes.Message && + event.content + .tryGet('msgtype') + ?.startsWith('m.key.verification.') == + true)) { // "just" key verification, no need to do this in sync - runInRoot(() => keyVerificationManager.handleEventUpdate(update)); + runInRoot(() => keyVerificationManager.handleEventUpdate(event)); } - if (update.content['sender'] == client.userID && - update.content['unsigned']?['transaction_id'] == null) { + if (event.senderId == client.userID && event.status.isSynced) { // maybe we need to re-try SSSS secrets runInRoot(() => ssss.periodicallyRequestMissingCache()); } diff --git a/lib/encryption/key_verification_manager.dart b/lib/encryption/key_verification_manager.dart index 517da6c3..2ff1d91c 100644 --- a/lib/encryption/key_verification_manager.dart +++ b/lib/encryption/key_verification_manager.dart @@ -88,29 +88,29 @@ class KeyVerificationManager { } } - Future handleEventUpdate(EventUpdate update) async { - final event = update.content; - final type = event['type'].startsWith('m.key.verification.') - ? event['type'] - : event['content']['msgtype']; + Future handleEventUpdate(Event update) async { + final type = update.type.startsWith('m.key.verification.') + ? update.type + : update.content.tryGet('msgtype'); if (type == null || !type.startsWith('m.key.verification.') || client.verificationMethods.isEmpty) { return; } if (type == EventTypes.KeyVerificationRequest) { - event['content']['timestamp'] = event['origin_server_ts']; + update.content['timestamp'] = + update.originServerTs.millisecondsSinceEpoch; } final transactionId = - KeyVerification.getTransactionId(event['content']) ?? event['event_id']; + KeyVerification.getTransactionId(update.content) ?? update.eventId; final req = _requests[transactionId]; if (req != null) { - final otherDeviceId = event['content']['from_device']; - if (event['sender'] != client.userID) { - await req.handlePayload(type, event['content'], event['event_id']); - } else if (event['sender'] == client.userID && + final otherDeviceId = update.content.tryGet('from_device'); + if (update.senderId != client.userID) { + await req.handlePayload(type, update.content, update.eventId); + } else if (update.senderId == client.userID && otherDeviceId != null && otherDeviceId != client.deviceID) { // okay, another of our devices answered @@ -118,22 +118,22 @@ class KeyVerificationManager { req.dispose(); _requests.remove(transactionId); } - } else if (event['sender'] != client.userID) { + } else if (update.senderId != client.userID) { if (!{EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationStart} .contains(type)) { return; // we can only start on these } - final room = client.getRoomById(update.roomID) ?? - Room(id: update.roomID, client: client); + final room = client.getRoomById(update.roomId!) ?? + Room(id: update.roomId!, client: client); final newKeyRequest = KeyVerification( encryption: encryption, - userId: event['sender'], + userId: update.senderId, room: room, ); await newKeyRequest.handlePayload( type, - event['content'], - event['event_id'], + update.content, + update.eventId, ); if (newKeyRequest.state != KeyVerificationState.askAccept) { // something went wrong, let's just dispose the request diff --git a/lib/src/client.dart b/lib/src/client.dart index b24b5abd..2e38947c 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -2801,8 +2801,11 @@ class Client extends MatrixApi { if (store) { await database?.storeEventUpdate(update, this); } - if (encryptionEnabled) { - await encryption?.handleEventUpdate(update); + if (event is MatrixEvent && encryptionEnabled) { + await encryption?.handleEventUpdate( + Event.fromMatrixEvent(event, room), + type, + ); } onEvent.add(update); diff --git a/test/encryption/key_verification_test.dart b/test/encryption/key_verification_test.dart index 7fd8edaf..6f506e12 100644 --- a/test/encryption/key_verification_test.dart +++ b/test/encryption/key_verification_test.dart @@ -28,21 +28,20 @@ import 'package:matrix/matrix.dart'; import '../fake_client.dart'; import '../fake_database.dart'; -EventUpdate getLastSentEvent(KeyVerification req) { +Event getLastSentEvent(KeyVerification req) { final entry = FakeMatrixApi.calledEndpoints.entries .firstWhere((p) => p.key.contains('/send/')); final type = entry.key.split('/')[6]; final content = json.decode(entry.value.first); - return EventUpdate( - content: { + return Event.fromJson( + { 'event_id': req.transactionId, 'type': type, 'content': content, 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, 'sender': req.client.userID, }, - type: EventUpdateType.timeline, - roomID: req.room!.id, + req.room!, ); } @@ -615,8 +614,8 @@ void main() async { await sub.cancel(); await client2.encryption!.keyVerificationManager.handleEventUpdate( - EventUpdate( - content: { + Event.fromJson( + { 'event_id': req2.transactionId, 'type': EventTypes.KeyVerificationReady, 'content': { @@ -630,8 +629,7 @@ void main() async { 'origin_server_ts': DateTime.now().millisecondsSinceEpoch, 'sender': client2.userID, }, - type: EventUpdateType.timeline, - roomID: req2.room!.id, + req2.room!, ), ); expect(req2.state, KeyVerificationState.error);