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:
parent
bdb4ad4594
commit
ac06864627
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -270,6 +270,8 @@ abstract class DatabaseApi {
|
|||
String userId,
|
||||
);
|
||||
|
||||
Future<Map<String, Map>> getAllOlmSessions();
|
||||
|
||||
Future<List<OlmSession>> getOlmSessionsForDevices(
|
||||
List<String> identityKeys,
|
||||
String userId,
|
||||
|
|
|
|||
|
|
@ -439,6 +439,10 @@ class FluffyBoxDatabase extends DatabaseApi {
|
|||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, Map>> getAllOlmSessions() =>
|
||||
_olmSessionsBox.getAllValues();
|
||||
|
||||
@override
|
||||
Future<List<OlmSession>> getOlmSessionsForDevices(
|
||||
List<String> identityKey, String userId) async {
|
||||
|
|
|
|||
|
|
@ -485,6 +485,21 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
|
|||
.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
|
||||
Future<List<OlmSession>> getOlmSessionsForDevices(
|
||||
List<String> identityKey, String userId) async {
|
||||
|
|
|
|||
|
|
@ -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'],
|
||||
|
|
|
|||
Loading…
Reference in New Issue