diff --git a/lib/encryption/olm_manager.dart b/lib/encryption/olm_manager.dart index 75071e3c..a91599af 100644 --- a/lib/encryption/olm_manager.dart +++ b/lib/encryption/olm_manager.dart @@ -274,6 +274,14 @@ class OlmManager { unusedFallbackKey = false; } + // fixup accidental too many uploads. We delete only one of them so that the server has time to update the counts and because we will get rate limited anyway. + if (keyCount > _olmAccount.max_number_of_one_time_keys()) { + final requestingKeysFrom = { + client.userID: {client.deviceID: 'signed_curve25519'} + }; + client.claimKeys(requestingKeysFrom, timeout: 10000); + } + // Only upload keys if they are less than half of the max or we have no unused fallback key if (keyCount < (_olmAccount.max_number_of_one_time_keys() / 2) || !unusedFallbackKey) { diff --git a/lib/src/client.dart b/lib/src/client.dart index ac039bed..066242ef 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1091,7 +1091,7 @@ class Client extends MatrixApi { } if (database != null) { _currentTransaction = database.transaction(() async { - await handleSync(syncResp); + await _handleSync(syncResp); if (prevBatch != syncResp.nextBatch) { await database.storePrevBatch(syncResp.nextBatch, id); } @@ -1099,7 +1099,7 @@ class Client extends MatrixApi { await _currentTransaction; onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp)); } else { - await handleSync(syncResp); + await _handleSync(syncResp); } if (_disposed || _aborted) return; if (prevBatch == null) { @@ -1145,6 +1145,12 @@ class Client extends MatrixApi { /// Use this method only for testing utilities! Future handleSync(SyncUpdate sync, {bool sortAtTheEnd = false}) async { + // ensure we don't upload keys because someone forgot to set a key count + sync.deviceOneTimeKeysCount ??= {'signed_curve25519': 100}; + await _handleSync(sync, sortAtTheEnd: sortAtTheEnd); + } + + Future _handleSync(SyncUpdate sync, {bool sortAtTheEnd = false}) async { if (sync.toDevice != null) { await _handleToDeviceEvents(sync.toDevice); }