refactor: Trigger upload keys on sync and not in background job and upload them before logout
This commit is contained in:
parent
31a52cb463
commit
5c3c85ba3d
|
|
@ -77,9 +77,8 @@ class Encryption {
|
||||||
olmAccount: olmAccount,
|
olmAccount: olmAccount,
|
||||||
deviceId: isDehydratedDevice ? deviceId : ourDeviceId,
|
deviceId: isDehydratedDevice ? deviceId : ourDeviceId,
|
||||||
pickleKey: pickleKey);
|
pickleKey: pickleKey);
|
||||||
_backgroundTasksRunning = ourDeviceId ==
|
|
||||||
client.deviceID; // Don't run tasks for dehydrated devices
|
if (!isDehydratedDevice) keyManager.startAutoUploadKeys();
|
||||||
_backgroundTasks(); // start the background tasks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isMinOlmVersion(int major, int minor, int patch) {
|
bool isMinOlmVersion(int major, int minor, int patch) {
|
||||||
|
|
@ -420,24 +419,7 @@ class Encryption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// this method is responsible for all background tasks, such as uploading online key backups
|
|
||||||
bool _backgroundTasksRunning = true;
|
|
||||||
void _backgroundTasks() {
|
|
||||||
if (!_backgroundTasksRunning || !client.isLogged()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
keyManager.backgroundTasks();
|
|
||||||
|
|
||||||
// autovalidateMasterOwnKey();
|
|
||||||
|
|
||||||
if (_backgroundTasksRunning) {
|
|
||||||
Timer(Duration(seconds: 10), _backgroundTasks);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> dispose() async {
|
Future<void> dispose() async {
|
||||||
_backgroundTasksRunning = false;
|
|
||||||
keyManager.dispose();
|
keyManager.dispose();
|
||||||
await olmManager.dispose();
|
await olmManager.dispose();
|
||||||
keyVerificationManager.dispose();
|
keyVerificationManager.dispose();
|
||||||
|
|
|
||||||
|
|
@ -747,15 +747,34 @@ class KeyManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _isUploadingKeys = false;
|
Future<void>? _uploadingFuture;
|
||||||
|
|
||||||
Future<void> backgroundTasks() async {
|
void startAutoUploadKeys() {
|
||||||
|
_uploadKeysOnSync = encryption.client.onSync.stream
|
||||||
|
.listen((_) => uploadInboundGroupSessions(skipIfInProgress: true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This task should be performed after sync processing but should not block
|
||||||
|
/// the sync. To make sure that it never gets executed multiple times, it is
|
||||||
|
/// skipped when an upload task is already in progress. Set `skipIfInProgress`
|
||||||
|
/// to `false` to await the pending upload task instead.
|
||||||
|
Future<void> uploadInboundGroupSessions(
|
||||||
|
{bool skipIfInProgress = false}) async {
|
||||||
final database = client.database;
|
final database = client.database;
|
||||||
final userID = client.userID;
|
final userID = client.userID;
|
||||||
if (_isUploadingKeys || database == null || userID == null) {
|
if (database == null || userID == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_isUploadingKeys = true;
|
|
||||||
|
// Make sure to not run in parallel
|
||||||
|
if (_uploadingFuture != null) {
|
||||||
|
if (skipIfInProgress) return;
|
||||||
|
await _uploadingFuture;
|
||||||
|
}
|
||||||
|
final completer = Completer<void>();
|
||||||
|
_uploadingFuture = completer.future;
|
||||||
|
|
||||||
|
await client.userDeviceKeysLoading;
|
||||||
try {
|
try {
|
||||||
if (!(await isCached())) {
|
if (!(await isCached())) {
|
||||||
return; // we can't backup anyways
|
return; // we can't backup anyways
|
||||||
|
|
@ -817,7 +836,7 @@ class KeyManager {
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logs().e('[Key Manager] Error uploading room keys', e, s);
|
Logs().e('[Key Manager] Error uploading room keys', e, s);
|
||||||
} finally {
|
} finally {
|
||||||
_isUploadingKeys = false;
|
completer.complete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1017,7 +1036,10 @@ class KeyManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StreamSubscription<SyncUpdate>? _uploadKeysOnSync;
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
_uploadKeysOnSync?.cancel();
|
||||||
for (final sess in _outboundGroupSessions.values) {
|
for (final sess in _outboundGroupSessions.values) {
|
||||||
sess.dispose();
|
sess.dispose();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -594,7 +594,7 @@ class Bootstrap {
|
||||||
'And finally set all megolm keys as needing to be uploaded again...');
|
'And finally set all megolm keys as needing to be uploaded again...');
|
||||||
await client.database?.markInboundGroupSessionsAsNeedingUpload();
|
await client.database?.markInboundGroupSessionsAsNeedingUpload();
|
||||||
Logs().v('And uploading keys...');
|
Logs().v('And uploading keys...');
|
||||||
await client.encryption?.keyManager.backgroundTasks();
|
await client.encryption?.keyManager.uploadInboundGroupSessions();
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logs().e('[Bootstrapping] Error setting up online key backup', e, s);
|
Logs().e('[Bootstrapping] Error setting up online key backup', e, s);
|
||||||
state = BootstrapState.error;
|
state = BootstrapState.error;
|
||||||
|
|
|
||||||
|
|
@ -553,6 +553,9 @@ class Client extends MatrixApi {
|
||||||
/// including all persistent data from the store.
|
/// including all persistent data from the store.
|
||||||
@override
|
@override
|
||||||
Future<void> logout() async {
|
Future<void> logout() async {
|
||||||
|
// Upload keys to make sure all are cached on the next login.
|
||||||
|
await encryption?.keyManager.uploadInboundGroupSessions();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await super.logout();
|
await super.logout();
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
|
|
@ -567,6 +570,9 @@ class Client extends MatrixApi {
|
||||||
/// including all persistent data from the store.
|
/// including all persistent data from the store.
|
||||||
@override
|
@override
|
||||||
Future<void> logoutAll() async {
|
Future<void> logoutAll() async {
|
||||||
|
// Upload keys to make sure all are cached on the next login.
|
||||||
|
await encryption?.keyManager.uploadInboundGroupSessions();
|
||||||
|
|
||||||
final futures = <Future>[];
|
final futures = <Future>[];
|
||||||
futures.add(super.logoutAll());
|
futures.add(super.logoutAll());
|
||||||
futures.add(clear());
|
futures.add(clear());
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ void main() {
|
||||||
forwarded: true);
|
forwarded: true);
|
||||||
var dbSessions = await client.database!.getInboundGroupSessionsToUpload();
|
var dbSessions = await client.database!.getInboundGroupSessionsToUpload();
|
||||||
expect(dbSessions.isNotEmpty, true);
|
expect(dbSessions.isNotEmpty, true);
|
||||||
await client.encryption!.keyManager.backgroundTasks();
|
await client.encryption!.keyManager.uploadInboundGroupSessions();
|
||||||
await FakeMatrixApi.firstWhereValue(
|
await FakeMatrixApi.firstWhereValue(
|
||||||
'/client/v3/room_keys/keys?version=5');
|
'/client/v3/room_keys/keys?version=5');
|
||||||
final payload = FakeMatrixApi
|
final payload = FakeMatrixApi
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue