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,44 +1325,47 @@ 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);
final String sessionId = toDeviceEvent.content['session_id']; if (room == null && addToPendingIfNotFound) {
if (room != null) { _pendingToDeviceEvents.add(toDeviceEvent);
if (toDeviceEvent.type == 'm.room_key' && }
userDeviceKeys.containsKey(toDeviceEvent.sender) && final String sessionId = toDeviceEvent.content['session_id'];
userDeviceKeys[toDeviceEvent.sender].deviceKeys.containsKey( if (toDeviceEvent.type == 'm.room_key' &&
toDeviceEvent.content['requesting_device_id'])) { userDeviceKeys.containsKey(toDeviceEvent.sender) &&
toDeviceEvent.content['sender_claimed_ed25519_key'] = userDeviceKeys[toDeviceEvent.sender]
userDeviceKeys[toDeviceEvent.sender] .deviceKeys
.deviceKeys[ .containsKey(toDeviceEvent.content['requesting_device_id'])) {
toDeviceEvent.content['requesting_device_id']] toDeviceEvent.content['sender_claimed_ed25519_key'] =
.ed25519Key; userDeviceKeys[toDeviceEvent.sender]
} .deviceKeys[toDeviceEvent.content['requesting_device_id']]
room.setSessionKey( .ed25519Key;
sessionId, }
toDeviceEvent.content, room.setSessionKey(
forwarded: toDeviceEvent.type == 'm.forwarded_room_key', sessionId,
); toDeviceEvent.content,
if (toDeviceEvent.type == 'm.forwarded_room_key') { forwarded: toDeviceEvent.type == 'm.forwarded_room_key',
sendToDevice( );
[], if (toDeviceEvent.type == 'm.forwarded_room_key') {
'm.room_key_request', await sendToDevice(
{ [],
'action': 'request_cancellation', 'm.room_key_request',
'request_id': base64 {
.encode(utf8.encode(toDeviceEvent.content['room_id'])), 'action': 'request_cancellation',
'requesting_device_id': room.client.deviceID, 'request_id': base64
}, .encode(utf8.encode(toDeviceEvent.content['room_id'])),
encrypted: false, 'requesting_device_id': room.client.deviceID,
); },
} encrypted: false,
} );
} }
break; break;
case 'm.room_key_request': case 'm.room_key_request':
@ -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;