Merge branch 'room-fix-restore-session' into 'master'

[Room] Fix restore session

See merge request famedly/famedlysdk!256
This commit is contained in:
Christian Pauly 2020-04-02 08:39:00 +00:00
commit 63a5d5dba3
2 changed files with 50 additions and 40 deletions

View file

@ -952,6 +952,10 @@ class Client {
/// Use this method only for testing utilities! /// Use this method only for testing utilities!
void handleSync(dynamic sync) { void handleSync(dynamic sync) {
if (sync['to_device'] is Map<String, dynamic> &&
sync['to_device']['events'] is List<dynamic>) {
_handleToDeviceEvents(sync['to_device']['events']);
}
if (sync['rooms'] is Map<String, dynamic>) { if (sync['rooms'] is Map<String, dynamic>) {
if (sync['rooms']['join'] is Map<String, dynamic>) { if (sync['rooms']['join'] is Map<String, dynamic>) {
_handleRooms(sync['rooms']['join'], Membership.join); _handleRooms(sync['rooms']['join'], Membership.join);
@ -971,16 +975,18 @@ class Client {
sync['account_data']['events'] is List<dynamic>) { sync['account_data']['events'] is List<dynamic>) {
_handleGlobalEvents(sync['account_data']['events'], 'account_data'); _handleGlobalEvents(sync['account_data']['events'], 'account_data');
} }
if (sync['to_device'] is Map<String, dynamic> &&
sync['to_device']['events'] is List<dynamic>) {
_handleToDeviceEvents(sync['to_device']['events']);
}
if (sync['device_lists'] is Map<String, dynamic>) { if (sync['device_lists'] is Map<String, dynamic>) {
_handleDeviceListsEvents(sync['device_lists']); _handleDeviceListsEvents(sync['device_lists']);
} }
if (sync['device_one_time_keys_count'] is Map<String, dynamic>) { if (sync['device_one_time_keys_count'] is Map<String, dynamic>) {
_handleDeviceOneTimeKeysCount(sync['device_one_time_keys_count']); _handleDeviceOneTimeKeysCount(sync['device_one_time_keys_count']);
} }
while (_pendingToDeviceEvents.isNotEmpty) {
_updateRoomsByToDeviceEvent(
_pendingToDeviceEvents.removeLast(),
addToPendingIfNotFound: false,
);
}
onSync.add(sync); onSync.add(sync);
} }
@ -1319,23 +1325,28 @@ class Client {
if (eventUpdate.type == 'timeline') _sortRooms(); if (eventUpdate.type == 'timeline') _sortRooms();
} }
void _updateRoomsByToDeviceEvent(ToDeviceEvent toDeviceEvent) { final List<ToDeviceEvent> _pendingToDeviceEvents = [];
void _updateRoomsByToDeviceEvent(ToDeviceEvent toDeviceEvent,
{addToPendingIfNotFound = true}) async {
try { try {
switch (toDeviceEvent.type) { switch (toDeviceEvent.type) {
case 'm.room_key': case 'm.room_key':
case 'm.forwarded_room_key': case 'm.forwarded_room_key':
var room = getRoomById(toDeviceEvent.content['room_id']); final roomId = toDeviceEvent.content['room_id'];
if (room != null) { var room = getRoomById(roomId);
if (room == null && addToPendingIfNotFound) {
_pendingToDeviceEvents.add(toDeviceEvent);
}
final String sessionId = toDeviceEvent.content['session_id']; final String sessionId = toDeviceEvent.content['session_id'];
if (room != null) {
if (toDeviceEvent.type == 'm.room_key' && if (toDeviceEvent.type == 'm.room_key' &&
userDeviceKeys.containsKey(toDeviceEvent.sender) && userDeviceKeys.containsKey(toDeviceEvent.sender) &&
userDeviceKeys[toDeviceEvent.sender].deviceKeys.containsKey( userDeviceKeys[toDeviceEvent.sender]
toDeviceEvent.content['requesting_device_id'])) { .deviceKeys
.containsKey(toDeviceEvent.content['requesting_device_id'])) {
toDeviceEvent.content['sender_claimed_ed25519_key'] = toDeviceEvent.content['sender_claimed_ed25519_key'] =
userDeviceKeys[toDeviceEvent.sender] userDeviceKeys[toDeviceEvent.sender]
.deviceKeys[ .deviceKeys[toDeviceEvent.content['requesting_device_id']]
toDeviceEvent.content['requesting_device_id']]
.ed25519Key; .ed25519Key;
} }
room.setSessionKey( room.setSessionKey(
@ -1344,7 +1355,7 @@ class Client {
forwarded: toDeviceEvent.type == 'm.forwarded_room_key', forwarded: toDeviceEvent.type == 'm.forwarded_room_key',
); );
if (toDeviceEvent.type == 'm.forwarded_room_key') { if (toDeviceEvent.type == 'm.forwarded_room_key') {
sendToDevice( await sendToDevice(
[], [],
'm.room_key_request', 'm.room_key_request',
{ {
@ -1356,8 +1367,6 @@ class Client {
encrypted: false, encrypted: false,
); );
} }
}
}
break; break;
case 'm.room_key_request': case 'm.room_key_request':
if (!toDeviceEvent.content.containsKey('body')) break; if (!toDeviceEvent.content.containsKey('body')) break;
@ -1376,7 +1385,7 @@ class Client {
if (deviceKeys.userId == userID && if (deviceKeys.userId == userID &&
deviceKeys.verified && deviceKeys.verified &&
!deviceKeys.blocked) { !deviceKeys.blocked) {
roomKeyRequest.forwardKey(); await roomKeyRequest.forwardKey();
} else { } else {
onRoomKeyRequest.add(roomKeyRequest); onRoomKeyRequest.add(roomKeyRequest);
} }

View file

@ -926,7 +926,7 @@ class Room {
return; return;
} }
void restoreGroupSessionKeys() async { Future<void> restoreGroupSessionKeys() async {
// Restore the inbound and outbound session keys // Restore the inbound and outbound session keys
if (client.encryptionEnabled && client.storeAPI != null) { if (client.encryptionEnabled && client.storeAPI != null) {
final String outboundGroupSessionPickle = await client.storeAPI.getItem( final String outboundGroupSessionPickle = await client.storeAPI.getItem(
@ -970,6 +970,7 @@ class Room {
json.encode(sessionKeys)); json.encode(sessionKeys));
_tryAgainDecryptLastMessage(); _tryAgainDecryptLastMessage();
_fullyRestored = true; _fullyRestored = true;
return;
} }
bool _fullyRestored = false; bool _fullyRestored = false;