Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions commet/lib/client/matrix_background/matrix_background_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TimelineEvent<Client>?> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading