feat: Migrate olm sessions on database migration

This adds a getAllOlmSessions
endpoint to the database API and
implements them in both
implementations. This also
adds it to the database
migration.
This commit is contained in:
Krille Fear 2021-11-26 08:17:43 +01:00
parent bdb4ad4594
commit ac06864627
5 changed files with 75 additions and 0 deletions

View File

@ -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...'); Logs().d('Migrate Device Keys...');
final userDeviceKeys = await legacyDatabase.getUserDeviceKeys(this); final userDeviceKeys = await legacyDatabase.getUserDeviceKeys(this);
for (final userId in userDeviceKeys.keys) { for (final userId in userDeviceKeys.keys) {

View File

@ -270,6 +270,8 @@ abstract class DatabaseApi {
String userId, String userId,
); );
Future<Map<String, Map>> getAllOlmSessions();
Future<List<OlmSession>> getOlmSessionsForDevices( Future<List<OlmSession>> getOlmSessionsForDevices(
List<String> identityKeys, List<String> identityKeys,
String userId, String userId,

View File

@ -439,6 +439,10 @@ class FluffyBoxDatabase extends DatabaseApi {
.toList(); .toList();
} }
@override
Future<Map<String, Map>> getAllOlmSessions() =>
_olmSessionsBox.getAllValues();
@override @override
Future<List<OlmSession>> getOlmSessionsForDevices( Future<List<OlmSession>> getOlmSessionsForDevices(
List<String> identityKey, String userId) async { List<String> identityKey, String userId) async {

View File

@ -485,6 +485,21 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
.toList(); .toList();
} }
@override
Future<Map<String, Map>> getAllOlmSessions() async {
final backup = Map.fromEntries(
await Future.wait(
_olmSessionsBox.keys.map(
(key) async => MapEntry(
key,
await _olmSessionsBox.get(key),
),
),
),
);
return backup.cast<String, Map>();
}
@override @override
Future<List<OlmSession>> getOlmSessionsForDevices( Future<List<OlmSession>> getOlmSessionsForDevices(
List<String> identityKey, String userId) async { List<String> identityKey, String userId) async {

View File

@ -313,6 +313,42 @@ void testDatabase(
); );
expect(olm.isEmpty, true); 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 { test('getOlmSessionsForDevices', () async {
final olm = await database.getOlmSessionsForDevices( final olm = await database.getOlmSessionsForDevices(
['identityKeys'], ['identityKeys'],