diff --git a/commet/lib/client/matrix_background/matrix_background_room.dart b/commet/lib/client/matrix_background/matrix_background_room.dart index 67513dafd..4d57179e0 100644 --- a/commet/lib/client/matrix_background/matrix_background_room.dart +++ b/commet/lib/client/matrix_background/matrix_background_room.dart @@ -23,6 +23,8 @@ import 'package:flutter/src/widgets/icon_data.dart'; import 'package:matrix_dart_sdk_drift_db/database.dart'; import 'package:matrix/matrix.dart' as matrix; +import 'package:vodozemac/vodozemac.dart' as vod; + class MatrixBackgroundRoom implements Room { MatrixBackgroundClient backgroundClient; RoomDataData data; @@ -161,12 +163,72 @@ class MatrixBackgroundRoom implements Room { throw UnimplementedError(); } + vod.InboundGroupSession? _createSession(String sessionKey) { + try { + return vod.InboundGroupSession(sessionKey); + } catch (e, s) { + Log.onError(e, s); + } + + Log.i("Attempting import instead"); + try { + return vod.InboundGroupSession.import(sessionKey); + } catch (e, s) { + Log.onError(e, s); + } + + Log.i("Could not import key"); + + return null; + } + @override Future?> getEvent(String eventId) async { var result = await backgroundClient.api.getOneRoomEvent(identifier, eventId); Log.i("Received event: ${result}"); + if (result.type == matrix.EventTypes.Encrypted) { + Log.i("Attempting to decrypt incoming notification content"); + var ciphertext = result.content["ciphertext"]; + + var sessionId = result.content["session_id"]; + + var session = await backgroundClient.database + .getInboundGroupSession(result.roomId!, sessionId as String); + + var senderKey = result.content["sender_key"]; + + if (session != null) { + if (session.senderKey == senderKey) { + var key = jsonDecode(session.content); + var sessionKey = key["session_key"]; + + if (vod.isInitialized() == false) { + await vod.init(); + } + + var sess = _createSession(sessionKey); + + if (sess != null) { + var decrypted = sess.decrypt(ciphertext as String); + + Log.i("Got decrypted: ${decrypted}"); + + result = matrix.MatrixEvent.fromJson({ + ...jsonDecode(decrypted.plaintext), + "event_id": result.eventId, + "room_id": result.roomId, + "origin_server_ts": result.originServerTs.millisecondsSinceEpoch, + "sender": result.senderId, + }); + } else { + Log.w("Failed to create session to decrypt event!"); + } + } + } + } + if ([ matrix.EventTypes.Encrypted, matrix.EventTypes.Message, diff --git a/commet/lib/service/background_service_notifications/background_service_task_notification2.dart b/commet/lib/service/background_service_notifications/background_service_task_notification2.dart index d736323c2..735c80ac0 100644 --- a/commet/lib/service/background_service_notifications/background_service_task_notification2.dart +++ b/commet/lib/service/background_service_notifications/background_service_task_notification2.dart @@ -122,6 +122,22 @@ class BackgroundNotificationsManager2 { title: "Room Invite", ); + Log.i("Failed to find a client for room: $roomId"); + + Log.i("Clients: ${clientManager!.clients.length}"); + clientManager!.clients.forEach((i) => Log.i(i.identifier)); + + for (var c in clientManager!.clients) { + Log.i("Client Id: ${c.identifier}"); + Log.i(" Start Room List:"); + var client = c as MatrixBackgroundClient; + for (var room in client.allRooms) { + Log.i(" ${room.roomId}"); + } + + Log.i(" End Room List"); + } + await NotificationManager.notify(content); return; }