Sendtodevice enhance performance

This commit is contained in:
Christian Pauly 2020-03-06 12:04:27 +00:00
parent 5eda493ba2
commit 39717f5917

View file

@ -814,7 +814,7 @@ class Client {
if (this.debug) { if (this.debug) {
print( print(
"[REQUEST ${type.toString().split('.').last}] Action: $action, Data: $data"); "[REQUEST ${type.toString().split('.').last}] Action: $action, Data: ${jsonEncode(data)}");
} }
http.Response resp; http.Response resp;
@ -1711,45 +1711,6 @@ class Client {
Map<String, dynamic> sendToDeviceMessage = message; Map<String, dynamic> sendToDeviceMessage = message;
if (encrypted) {
// Create new sessions with devices if there is no existing session yet.
List<DeviceKeys> deviceKeysWithoutSession =
List<DeviceKeys>.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<String, dynamic>(),
};
for (DeviceKeys device in deviceKeys) {
List<olm.Session> existingSessions = olmSessions[device.curve25519Key];
if (existingSessions == null || existingSessions.isEmpty) continue;
existingSessions
.sort((a, b) => a.session_id().compareTo(b.session_id()));
final Map<String, dynamic> 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 // Send with send-to-device messaging
Map<String, dynamic> data = { Map<String, dynamic> data = {
"messages": Map<String, dynamic>(), "messages": Map<String, dynamic>(),
@ -1770,9 +1731,48 @@ class Client {
if (!data["messages"].containsKey(device.userId)) { if (!data["messages"].containsKey(device.userId)) {
data["messages"][device.userId] = Map<String, dynamic>(); data["messages"][device.userId] = Map<String, dynamic>();
} }
if (encrypted) {
// Create new sessions with devices if there is no existing session yet.
List<DeviceKeys> deviceKeysWithoutSession =
List<DeviceKeys>.from(deviceKeys);
deviceKeysWithoutSession.removeWhere((DeviceKeys deviceKeys) =>
olmSessions.containsKey(deviceKeys.curve25519Key));
if (deviceKeysWithoutSession.isNotEmpty) {
await startOutgoingOlmSessions(deviceKeysWithoutSession);
}
List<olm.Session> existingSessions =
olmSessions[device.curve25519Key];
if (existingSessions == null || existingSessions.isEmpty) continue;
existingSessions
.sort((a, b) => a.session_id().compareTo(b.session_id()));
final Map<String, dynamic> 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<String, dynamic>(),
};
sendToDeviceMessage["ciphertext"][device.curve25519Key] = {
"type": encryptResult.type,
"body": encryptResult.body,
};
}
data["messages"][device.userId][device.deviceId] = sendToDeviceMessage; data["messages"][device.userId][device.deviceId] = sendToDeviceMessage;
} }
} }
if (encrypted) type = "m.room.encrypted";
final String messageID = "msg${DateTime.now().millisecondsSinceEpoch}"; final String messageID = "msg${DateTime.now().millisecondsSinceEpoch}";
await jsonRequest( await jsonRequest(
type: HTTPType.PUT, type: HTTPType.PUT,