diff --git a/lib/src/client.dart b/lib/src/client.dart index 4ce9629e..22f922be 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -2398,6 +2398,24 @@ class Client extends MatrixApi { ); } } + Logs().d('Migrate OLM sessions...'); + try { + final olmSessions = await legacyDatabase.getAllOlmSessions(); + for (final identityKey in olmSessions.keys) { + final sessions = olmSessions[identityKey]!; + for (final sessionId in sessions.keys) { + final session = sessions[sessionId]!; + await database.storeOlmSession( + identityKey, + session['session_id'] as String, + session['pickle'] as String, + session['last_received'] as int, + ); + } + } + } catch (e, s) { + Logs().e('Unable to migrate OLM sessions!', e, s); + } Logs().d('Migrate Device Keys...'); final userDeviceKeys = await legacyDatabase.getUserDeviceKeys(this); for (final userId in userDeviceKeys.keys) { diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index 7bdc6872..2e3b754e 100644 --- a/lib/src/database/database_api.dart +++ b/lib/src/database/database_api.dart @@ -270,6 +270,8 @@ abstract class DatabaseApi { String userId, ); + Future> getAllOlmSessions(); + Future> getOlmSessionsForDevices( List identityKeys, String userId, diff --git a/lib/src/database/fluffybox_database.dart b/lib/src/database/fluffybox_database.dart index acfa3489..05c4eb5f 100644 --- a/lib/src/database/fluffybox_database.dart +++ b/lib/src/database/fluffybox_database.dart @@ -439,6 +439,10 @@ class FluffyBoxDatabase extends DatabaseApi { .toList(); } + @override + Future> getAllOlmSessions() => + _olmSessionsBox.getAllValues(); + @override Future> getOlmSessionsForDevices( List identityKey, String userId) async { diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index 884b2c53..8cdb0acf 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -485,6 +485,21 @@ class FamedlySdkHiveDatabase extends DatabaseApi { .toList(); } + @override + Future> getAllOlmSessions() async { + final backup = Map.fromEntries( + await Future.wait( + _olmSessionsBox.keys.map( + (key) async => MapEntry( + key, + await _olmSessionsBox.get(key), + ), + ), + ), + ); + return backup.cast(); + } + @override Future> getOlmSessionsForDevices( List identityKey, String userId) async { diff --git a/test/database_api_test.dart b/test/database_api_test.dart index 0517bcc2..0d809a65 100644 --- a/test/database_api_test.dart +++ b/test/database_api_test.dart @@ -313,6 +313,42 @@ void testDatabase( ); expect(olm.isEmpty, true); }); + test('getAllOlmSessions', () async { + var sessions = await database.getAllOlmSessions(); + expect(sessions.isEmpty, true); + await database.storeOlmSession( + 'identityKey', + 'sessionId', + 'pickle', + 0, + ); + await database.storeOlmSession( + 'identityKey', + 'sessionId2', + 'pickle', + 0, + ); + sessions = await database.getAllOlmSessions(); + expect( + sessions, + { + 'identityKey': { + 'sessionId': { + 'identity_key': 'identityKey', + 'pickle': 'pickle', + 'session_id': 'sessionId', + 'last_received': 0 + }, + 'sessionId2': { + 'identity_key': 'identityKey', + 'pickle': 'pickle', + 'session_id': 'sessionId2', + 'last_received': 0 + } + } + }, + ); + }); test('getOlmSessionsForDevices', () async { final olm = await database.getOlmSessionsForDevices( ['identityKeys'],