diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index f49a175c..d04a5073 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -336,6 +336,12 @@ class Encryption { Future> encryptGroupMessagePayload( String roomId, Map payload, {String type = EventTypes.Message}) async { + final Map? mRelatesTo = payload.remove('m.relates_to'); + // Events which only contain a m.relates_to like reactions don't need to + // be encrypted. + if (payload.isEmpty && mRelatesTo != null) { + return {'m.relates_to': mRelatesTo}; + } final room = client.getRoomById(roomId); if (room == null || !room.encrypted || !enabled) { return payload; @@ -357,7 +363,6 @@ class Encryption { // we clone the payload as we do not want to remove 'm.relates_to' from the // original payload passed into this function payload = payload.copy(); - final Map? mRelatesTo = payload.remove('m.relates_to'); final payloadContent = { 'content': payload, 'type': type, diff --git a/lib/src/client.dart b/lib/src/client.dart index e607824b..6e163963 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -190,7 +190,6 @@ class Client extends MatrixApi { EventTypes.Message, EventTypes.Encrypted, EventTypes.Sticker, - EventTypes.Reaction, ]); // register all the default commands diff --git a/lib/src/room.dart b/lib/src/room.dart index f1142d18..eea53852 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -769,14 +769,18 @@ class Room { String? txid, }) async { txid ??= client.generateUniqueTransactionId(); + final mustEncrypt = encrypted && client.encryptionEnabled; + final sendMessageContent = mustEncrypt ? await client.encryption! .encryptGroupMessagePayload(id, content, type: type) : content; return await client.sendMessage( id, - mustEncrypt ? EventTypes.Encrypted : type, + sendMessageContent.containsKey('ciphertext') + ? EventTypes.Encrypted + : type, txid, sendMessageContent, ); diff --git a/test/room_test.dart b/test/room_test.dart index 6b67186f..c47ae44d 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -277,8 +277,9 @@ void main() { stateKey: '', ), ); - expect(room.lastEvent?.eventId, '123456'); - expect(room.lastEvent?.type, EventTypes.Reaction); + expect(room.lastEvent?.eventId, '5'); + expect(room.lastEvent?.body, 'edited cdc'); + expect(room.lastEvent?.status, EventStatus.sent); }); test('lastEvent when reply parent edited', () async { room.setState(