From cbc66ea308dfe0cd8ea6a65d42faf22d9c83cd52 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 15 Aug 2020 18:11:21 +0200 Subject: [PATCH] Fix unencrypted calls --- lib/src/room.dart | 87 +++++++++++++++++++++------------------------ test/room_test.dart | 22 ++++++------ 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index 2ebcfd1..74664a8 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -633,10 +633,34 @@ class Room { return uploadResp; } + Future _sendContent( + String type, + Map content, { + 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, + txid, + sendMessageContent, + ); + } + /// Sends an event to this room with this json as a content. Returns the /// event ID generated from the server. - Future sendEvent(Map content, - {String type, String txid, Event inReplyTo, String editEventId}) async { + Future sendEvent( + Map content, { + String type, + String txid, + Event inReplyTo, + String editEventId, + }) async { type = type ?? EventTypes.Message; final sendType = (encrypted && client.encryptionEnabled) ? EventTypes.Encrypted : type; @@ -701,15 +725,10 @@ class Room { // Send the text and on success, store and display a *sent* event. try { - final sendMessageContent = encrypted && client.encryptionEnabled - ? await client.encryption - .encryptGroupMessagePayload(id, content, type: type) - : content; - final res = await client.sendMessage( - id, + final res = await _sendContent( sendType, - messageID, - sendMessageContent, + content, + txid: messageID, ); syncUpdate.rooms.join.values.first.timeline.events.first .unsigned[MessageSendingStatusKey] = 1; @@ -1355,16 +1374,10 @@ class Room { 'offer': {'sdp': sdp, 'type': type}, 'version': version, }; - - final sendMessageContent = encrypted && client.encryptionEnabled - ? await client.encryption.encryptGroupMessagePayload(id, content, - type: EventTypes.CallInvite) - : content; - return await client.sendMessage( - id, + return await _sendContent( EventTypes.CallInvite, - txid, - sendMessageContent, + content, + txid: txid, ); } @@ -1398,16 +1411,10 @@ class Room { 'candidates': candidates, 'version': version, }; - - final sendMessageContent = encrypted && client.encryptionEnabled - ? await client.encryption.encryptGroupMessagePayload(id, content, - type: EventTypes.CallCandidates) - : content; - return await client.sendMessage( - id, + return await _sendContent( EventTypes.CallCandidates, - txid, - sendMessageContent, + content, + txid: txid, ); } @@ -1424,16 +1431,10 @@ class Room { 'answer': {'sdp': sdp, 'type': type}, 'version': version, }; - - final sendMessageContent = encrypted && client.encryptionEnabled - ? await client.encryption.encryptGroupMessagePayload(id, content, - type: EventTypes.CallAnswer) - : content; - return await client.sendMessage( - id, + return await _sendContent( EventTypes.CallAnswer, - txid, - sendMessageContent, + content, + txid: txid, ); } @@ -1448,16 +1449,10 @@ class Room { 'call_id': callId, 'version': version, }; - - final sendMessageContent = encrypted && client.encryptionEnabled - ? await client.encryption.encryptGroupMessagePayload(id, content, - type: EventTypes.CallHangup) - : content; - return await client.sendMessage( - id, + return await _sendContent( EventTypes.CallHangup, - txid, - sendMessageContent, + content, + txid: txid, ); } diff --git a/test/room_test.dart b/test/room_test.dart index 5bad302..4ef6fa2 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -183,10 +183,6 @@ void main() { await room.sendReadReceipt('ยง1234:fakeServer.notExisting'); }); - test('enableEncryption', () async { - await room.enableEncryption(); - }); - test('requestParticipants', () async { final participants = await room.requestParticipants(); expect(participants.length, 1); @@ -455,6 +451,17 @@ void main() { expect(room.pushRuleState, PushRuleState.dont_notify); }); + test('Test call methods', () async { + await room.inviteToCall('1234', 1234, 'sdp', txid: '1234'); + await room.answerCall('1234', 'sdp', txid: '1234'); + await room.hangupCall('1234', txid: '1234'); + await room.sendCallCandidates('1234', [], txid: '1234'); + }); + + test('enableEncryption', () async { + await room.enableEncryption(); + }); + test('Enable encryption', () async { room.setState( Event( @@ -482,13 +489,6 @@ void main() { await room.setPushRuleState(PushRuleState.notify); }); - test('Test call methods', () async { - await room.inviteToCall('1234', 1234, 'sdp', txid: '1234'); - await room.answerCall('1234', 'sdp', txid: '1234'); - await room.hangupCall('1234', txid: '1234'); - await room.sendCallCandidates('1234', [], txid: '1234'); - }); - test('Test tag methods', () async { await room.addTag(TagType.Favourite, order: 0.1); await room.removeTag(TagType.Favourite);