From 39717f591765b6642ad47b862c682d6a7e8be59f Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 6 Mar 2020 12:04:27 +0000 Subject: [PATCH] Sendtodevice enhance performance --- lib/src/client.dart | 80 ++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index eb4c15c8..f639a14f 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -814,7 +814,7 @@ class Client { if (this.debug) { print( - "[REQUEST ${type.toString().split('.').last}] Action: $action, Data: $data"); + "[REQUEST ${type.toString().split('.').last}] Action: $action, Data: ${jsonEncode(data)}"); } http.Response resp; @@ -1711,45 +1711,6 @@ class Client { Map sendToDeviceMessage = message; - if (encrypted) { - // Create new sessions with devices if there is no existing session yet. - List deviceKeysWithoutSession = - List.from(deviceKeys); - deviceKeysWithoutSession.removeWhere((DeviceKeys deviceKeys) => - olmSessions.containsKey(deviceKeys.curve25519Key)); - if (deviceKeysWithoutSession.isNotEmpty) { - await startOutgoingOlmSessions(deviceKeysWithoutSession); - } - sendToDeviceMessage = { - "algorithm": "m.olm.v1.curve25519-aes-sha2", - "sender_key": identityKey, - "ciphertext": Map(), - }; - for (DeviceKeys device in deviceKeys) { - List existingSessions = olmSessions[device.curve25519Key]; - if (existingSessions == null || existingSessions.isEmpty) continue; - existingSessions - .sort((a, b) => a.session_id().compareTo(b.session_id())); - - final Map payload = { - "type": type, - "content": message, - "sender": this.userID, - "keys": {"ed25519": fingerprintKey}, - "recipient": device.userId, - "recipient_keys": {"ed25519": device.ed25519Key}, - }; - final olm.EncryptResult encryptResult = - existingSessions.first.encrypt(json.encode(payload)); - storeOlmSession(device.curve25519Key, existingSessions.first); - sendToDeviceMessage["ciphertext"][device.curve25519Key] = { - "type": encryptResult.type, - "body": encryptResult.body, - }; - } - type = "m.room.encrypted"; - } - // Send with send-to-device messaging Map data = { "messages": Map(), @@ -1770,9 +1731,48 @@ class Client { if (!data["messages"].containsKey(device.userId)) { data["messages"][device.userId] = Map(); } + + if (encrypted) { + // Create new sessions with devices if there is no existing session yet. + List deviceKeysWithoutSession = + List.from(deviceKeys); + deviceKeysWithoutSession.removeWhere((DeviceKeys deviceKeys) => + olmSessions.containsKey(deviceKeys.curve25519Key)); + if (deviceKeysWithoutSession.isNotEmpty) { + await startOutgoingOlmSessions(deviceKeysWithoutSession); + } + List existingSessions = + olmSessions[device.curve25519Key]; + if (existingSessions == null || existingSessions.isEmpty) continue; + existingSessions + .sort((a, b) => a.session_id().compareTo(b.session_id())); + + final Map payload = { + "type": type, + "content": message, + "sender": this.userID, + "keys": {"ed25519": fingerprintKey}, + "recipient": device.userId, + "recipient_keys": {"ed25519": device.ed25519Key}, + }; + final olm.EncryptResult encryptResult = + existingSessions.first.encrypt(json.encode(payload)); + storeOlmSession(device.curve25519Key, existingSessions.first); + sendToDeviceMessage = { + "algorithm": "m.olm.v1.curve25519-aes-sha2", + "sender_key": identityKey, + "ciphertext": Map(), + }; + sendToDeviceMessage["ciphertext"][device.curve25519Key] = { + "type": encryptResult.type, + "body": encryptResult.body, + }; + } + data["messages"][device.userId][device.deviceId] = sendToDeviceMessage; } } + if (encrypted) type = "m.room.encrypted"; final String messageID = "msg${DateTime.now().millisecondsSinceEpoch}"; await jsonRequest( type: HTTPType.PUT,