From 77ca7523d0995ff885525d15003f8b882019cabf Mon Sep 17 00:00:00 2001 From: Jayesh Nirve Date: Thu, 23 Sep 2021 22:44:50 +0530 Subject: [PATCH] refactor: remove unused clientId --- lib/encryption/encryption.dart | 6 +- lib/encryption/key_manager.dart | 15 +-- lib/encryption/olm_manager.dart | 21 ++-- lib/encryption/ssss.dart | 16 +-- lib/encryption/utils/bootstrap.dart | 2 +- lib/src/client.dart | 72 +++++------ lib/src/database/database_api.dart | 93 +++++--------- lib/src/database/hive_database.dart | 173 ++++++++++++-------------- lib/src/event.dart | 2 +- lib/src/room.dart | 20 ++- lib/src/timeline.dart | 1 - lib/src/utils/device_keys_list.dart | 12 +- test/database_api_test.dart | 103 +++++++-------- test/encryption/olm_manager_test.dart | 4 - test/matrix_database_test.dart | 55 ++++---- 15 files changed, 255 insertions(+), 340 deletions(-) diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index fd3cd5fa..010544fb 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -220,10 +220,7 @@ class Encryption { // line *could* throw an error. As that is a future, though, and we call // it un-awaited here, nothing happens, which is exactly the result we want client.database?.updateInboundGroupSessionIndexes( - json.encode(inboundGroupSession.indexes), - client.id, - roomId, - sessionId); + json.encode(inboundGroupSession.indexes), roomId, sessionId); } decryptedPayload = json.decode(decryptResult.plaintext); } catch (exception) { @@ -301,7 +298,6 @@ class Encryption { event.room?.setState(event); } await client.database?.storeEventUpdate( - client.id, EventUpdate( content: event.toJson(), roomID: event.roomId, diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart index d61706fb..7d23f912 100644 --- a/lib/encryption/key_manager.dart +++ b/lib/encryption/key_manager.dart @@ -149,7 +149,6 @@ class KeyManager { } client.database ?.storeInboundGroupSession( - client.id, roomId, sessionId, inboundGroupSession.pickle(client.userID), @@ -164,8 +163,7 @@ class KeyManager { return; } if (uploaded) { - client.database - .markInboundGroupSessionAsUploaded(client.id, roomId, sessionId); + client.database.markInboundGroupSessionAsUploaded(roomId, sessionId); } else { _haveKeysToUpload = true; } @@ -238,8 +236,8 @@ class KeyManager { } return sess; // nothing to do } - final session = await client.database - ?.getInboundGroupSession(client.id, roomId, sessionId); + final session = + await client.database?.getInboundGroupSession(roomId, sessionId); if (session == null) { return null; } @@ -382,7 +380,6 @@ class KeyManager { if (client.database != null) { await client.database.updateInboundGroupSessionAllowedAtIndex( json.encode(inboundSess.allowedAtIndex), - client.id, room.id, sess.outboundGroupSession.session_id()); } @@ -401,7 +398,7 @@ class KeyManager { } sess.dispose(); _outboundGroupSessions.remove(roomId); - await client.database?.removeOutboundGroupSession(client.id, roomId); + await client.database?.removeOutboundGroupSession(roomId); return true; } @@ -412,7 +409,6 @@ class KeyManager { return; } await client.database?.storeOutboundGroupSession( - client.id, roomId, sess.outboundGroupSession.pickle(client.userID), json.encode(sess.devices), @@ -516,7 +512,6 @@ class KeyManager { } _loadedOutboundGroupSessions.add(roomId); final sess = await client.database.getOutboundGroupSession( - client.id, roomId, client.userID, ); @@ -753,7 +748,7 @@ class KeyManager { // no need to optimze this, as we only run it so seldomly and almost never with many keys at once for (final dbSession in dbSessions) { await client.database.markInboundGroupSessionAsUploaded( - client.id, dbSession.roomId, dbSession.sessionId); + dbSession.roomId, dbSession.sessionId); } } finally { decryption.free(); diff --git a/lib/encryption/olm_manager.dart b/lib/encryption/olm_manager.dart index 1e73559f..85a34db1 100644 --- a/lib/encryption/olm_manager.dart +++ b/lib/encryption/olm_manager.dart @@ -233,7 +233,9 @@ class OlmManager { // in case the app gets killed during upload or the upload fails due to bad network // we can still re-try later if (updateDatabase) { - await client.database?.updateClientKeys(pickledOlmAccount, client.id); + await client.database?.updateClientKeys( + pickledOlmAccount, + ); } final response = await client.uploadKeys( deviceKeys: uploadDeviceKeys @@ -245,7 +247,7 @@ class OlmManager { // mark the OTKs as published and save that to datbase _olmAccount.mark_keys_as_published(); if (updateDatabase) { - await client.database?.updateClientKeys(pickledOlmAccount, client.id); + await client.database?.updateClientKeys(pickledOlmAccount); } return (uploadedOneTimeKeysCount != null && response['signed_curve25519'] == uploadedOneTimeKeysCount) || @@ -309,7 +311,6 @@ class OlmManager { return; } await client.database.storeOlmSession( - client.id, session.identityKey, session.sessionId, session.pickledSession, @@ -346,7 +347,6 @@ class OlmManager { device.lastActive = DateTime.now(); await client.database?.setLastActiveUserDeviceKey( device.lastActive.millisecondsSinceEpoch, - client.id, device.userId, device.deviceId); } @@ -383,7 +383,9 @@ class OlmManager { try { newSession.create_inbound_from(_olmAccount, senderKey, body); _olmAccount.remove_one_time_keys(newSession); - client.database?.updateClientKeys(pickledOlmAccount, client.id); + client.database?.updateClientKeys( + pickledOlmAccount, + ); plaintext = newSession.decrypt(type, body); runInRoot(() => storeOlmSession(OlmSession( key: client.userID, @@ -424,8 +426,8 @@ class OlmManager { if (client.database == null) { return []; } - final olmSessions = await client.database - .getOlmSessions(client.id, senderKey, client.userID); + final olmSessions = + await client.database.getOlmSessions(senderKey, client.userID); return olmSessions.where((sess) => sess.isValid).toList(); } @@ -435,7 +437,6 @@ class OlmManager { return; } final rows = await client.database.getOlmSessionsForDevices( - client.id, senderKeys, client.userID, ); @@ -596,7 +597,6 @@ class OlmManager { 'type': type, 'content': payload, }), - client.id, device.userId, device.deviceId)); } @@ -660,8 +660,7 @@ class OlmManager { Logs().v( '[OlmManager] Device ${device.userId}:${device.deviceId} generated a new olm session, replaying last sent message...'); final lastSentMessageRes = await client.database - .getLastSentMessageUserDeviceKey( - client.id, device.userId, device.deviceId); + .getLastSentMessageUserDeviceKey(device.userId, device.deviceId); if (lastSentMessageRes.isEmpty || (lastSentMessageRes.first?.isEmpty ?? true)) { return; diff --git a/lib/encryption/ssss.dart b/lib/encryption/ssss.dart index 03cf66cd..2c6d3ece 100644 --- a/lib/encryption/ssss.dart +++ b/lib/encryption/ssss.dart @@ -63,7 +63,7 @@ class SSSS { // for testing Future clearCache() async { - await client.database?.clearSSSSCache(client.id); + await client.database?.clearSSSSCache(); _cache.clear(); } @@ -282,7 +282,7 @@ class SSSS { if (_cache.containsKey(type) && isValid(_cache[type])) { return _cache[type].content; } - final ret = await client.database.getSSSSCache(client.id, type); + final ret = await client.database.getSSSSCache(type); if (ret == null) { return null; } @@ -311,7 +311,7 @@ class SSSS { if (cacheTypes.contains(type) && client.database != null) { // cache the thing await client.database - .storeSSSSCache(client.id, type, keyId, enc['ciphertext'], decrypted); + .storeSSSSCache(type, keyId, enc['ciphertext'], decrypted); if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) { _cacheCallbacks[type](decrypted); } @@ -344,7 +344,7 @@ class SSSS { if (cacheTypes.contains(type) && client.database != null) { // cache the thing await client.database - .storeSSSSCache(client.id, type, keyId, encrypted.ciphertext, secret); + .storeSSSSCache(type, keyId, encrypted.ciphertext, secret); if (triggerCacheCallback) { _cacheCallbacks[type](secret); } @@ -369,8 +369,8 @@ class SSSS { await client.setAccountData(client.userID, type, content); if (cacheTypes.contains(type) && client.database != null) { // cache the thing - await client.database.storeSSSSCache(client.id, type, keyId, - content['encrypted'][keyId]['ciphertext'], secret); + await client.database.storeSSSSCache( + type, keyId, content['encrypted'][keyId]['ciphertext'], secret); } } @@ -532,8 +532,8 @@ class SSSS { if (keyId != null) { final ciphertext = client.accountData[request.type] .content['encrypted'][keyId]['ciphertext']; - await client.database.storeSSSSCache( - client.id, request.type, keyId, ciphertext, secret); + await client.database + .storeSSSSCache(request.type, keyId, ciphertext, secret); if (_cacheCallbacks.containsKey(request.type)) { _cacheCallbacks[request.type](secret); } diff --git a/lib/encryption/utils/bootstrap.dart b/lib/encryption/utils/bootstrap.dart index b3142146..8d8791c8 100644 --- a/lib/encryption/utils/bootstrap.dart +++ b/lib/encryption/utils/bootstrap.dart @@ -575,7 +575,7 @@ class Bootstrap { await newSsssKey.store(megolmKey, base64.encode(privKey)); Logs().v( 'And finally set all megolm keys as needing to be uploaded again...'); - await client.database?.markInboundGroupSessionsAsNeedingUpload(client.id); + await client.database?.markInboundGroupSessionsAsNeedingUpload(); } catch (e, s) { Logs().e('[Bootstrapping] Error setting up online key backup', e, s); state = BootstrapState.error; diff --git a/lib/src/client.dart b/lib/src/client.dart index 602a3ab7..c00c6fd0 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -980,7 +980,6 @@ class Client extends MatrixApi { _deviceName, prevBatch, encryption?.pickledOlmAccount, - id, ); } else { _id = await database.insertClient( @@ -997,7 +996,7 @@ class Client extends MatrixApi { _userDeviceKeys = await database.getUserDeviceKeys(this); _rooms = await database.getRoomList(this); _sortRooms(); - accountData = await database.getAccountData(id); + accountData = await database.getAccountData(); presences.clear(); } _initLock = false; @@ -1030,7 +1029,7 @@ class Client extends MatrixApi { Logs().outputEvents.clear(); try { await abortSync(); - await database?.clear(id); + await database?.clear(); _backgroundSync = true; } catch (e, s) { Logs().e('Unable to clear database', e, s); @@ -1093,7 +1092,7 @@ class Client extends MatrixApi { Future _checkSyncFilter() async { if (syncFilterId == null) { syncFilterId = await defineFilter(userID, syncFilter); - await database?.storeSyncFilterId(syncFilterId, id); + await database?.storeSyncFilterId(syncFilterId); } return; } @@ -1132,7 +1131,7 @@ class Client extends MatrixApi { _currentTransaction = database.transaction(() async { await _handleSync(syncResp); if (prevBatch != syncResp.nextBatch) { - await database.storePrevBatch(syncResp.nextBatch, id); + await database.storePrevBatch(syncResp.nextBatch); } }); await _currentTransaction; @@ -1218,7 +1217,6 @@ class Client extends MatrixApi { for (final newAccountData in sync.accountData) { if (database != null) { await database.storeAccountData( - id, newAccountData.type, jsonEncode(newAccountData.content), ); @@ -1243,7 +1241,7 @@ class Client extends MatrixApi { if (_userDeviceKeys.containsKey(userId)) { _userDeviceKeys[userId].outdated = true; if (database != null) { - await database.storeUserDeviceKeysInfo(id, userId, true); + await database.storeUserDeviceKeysInfo(userId, true); } } } @@ -1285,7 +1283,7 @@ class Client extends MatrixApi { if (database != null) { // TODO: This method seems to be rather slow for some updates // Perhaps don't dynamically build that one query? - await database.storeRoomUpdate(this.id, id, room, getRoomById(id)); + await database.storeRoomUpdate(id, room, getRoomById(id)); } _updateRoomsByRoomUpdate(id, room); @@ -1427,13 +1425,13 @@ class Client extends MatrixApi { database != null && room.getState(EventTypes.RoomMember, event['sender']) == null) { // In order to correctly render room list previews we need to fetch the member from the database - final user = await database.getUser(id, event['sender'], room); + final user = await database.getUser(event['sender'], room); if (user != null) { room.setState(user); } } if (type != EventUpdateType.ephemeral && database != null) { - await database.storeEventUpdate(id, update); + await database.storeEventUpdate(update); } _updateRoomsByEventUpdate(update); if (encryptionEnabled) { @@ -1708,7 +1706,7 @@ class Client extends MatrixApi { // Check if deviceId or deviceKeys are known if (!oldKeys.containsKey(deviceId)) { final oldPublicKeys = - await database.deviceIdSeen(id, userId, deviceId); + await database.deviceIdSeen(userId, deviceId); if (oldPublicKeys != null && oldPublicKeys != entry.curve25519Key + entry.ed25519Key) { Logs().w( @@ -1716,25 +1714,24 @@ class Client extends MatrixApi { continue; } final oldDeviceId = - await database.publicKeySeen(id, entry.ed25519Key); + await database.publicKeySeen(entry.ed25519Key); if (oldDeviceId != null && oldDeviceId != deviceId) { Logs().w( 'Already seen ED25519 has been added again. This might be an attack!'); continue; } final oldDeviceId2 = - await database.publicKeySeen(id, entry.curve25519Key); + await database.publicKeySeen(entry.curve25519Key); if (oldDeviceId2 != null && oldDeviceId2 != deviceId) { Logs().w( 'Already seen Curve25519 has been added again. This might be an attack!'); continue; } - await database.addSeenDeviceId(id, userId, deviceId, - entry.curve25519Key + entry.ed25519Key); + await database.addSeenDeviceId( + userId, deviceId, entry.curve25519Key + entry.ed25519Key); + await database.addSeenPublicKey(entry.ed25519Key, deviceId); await database.addSeenPublicKey( - id, entry.ed25519Key, deviceId); - await database.addSeenPublicKey( - id, entry.curve25519Key, deviceId); + entry.curve25519Key, deviceId); } // is this a new key or the same one as an old one? @@ -1756,7 +1753,6 @@ class Client extends MatrixApi { entry.setDirectVerified(true); } dbActions.add(() => database.storeUserDeviceKey( - id, userId, deviceId, json.encode(entry.toJson()), @@ -1780,14 +1776,14 @@ class Client extends MatrixApi { final deviceId = oldDeviceKeyEntry.key; if (!_userDeviceKeys[userId].deviceKeys.containsKey(deviceId)) { // we need to remove an old key - dbActions.add( - () => database.removeUserDeviceKey(id, userId, deviceId)); + dbActions + .add(() => database.removeUserDeviceKey(userId, deviceId)); } } _userDeviceKeys[userId].outdated = false; if (database != null) { - dbActions.add( - () => database.storeUserDeviceKeysInfo(id, userId, false)); + dbActions + .add(() => database.storeUserDeviceKeysInfo(userId, false)); } } } @@ -1819,8 +1815,8 @@ class Client extends MatrixApi { } else if (database != null) { // There is a previous cross-signing key with this usage, that we no // longer need/use. Clear it from the database. - dbActions.add(() => database.removeUserCrossSigningKey( - id, userId, oldEntry.key)); + dbActions.add(() => + database.removeUserCrossSigningKey(userId, oldEntry.key)); } } final entry = CrossSigningKey.fromMatrixCrossSigningKey( @@ -1845,7 +1841,6 @@ class Client extends MatrixApi { } if (database != null) { dbActions.add(() => database.storeUserCrossSigningKey( - id, userId, publicKey, json.encode(entry.toJson()), @@ -1856,8 +1851,8 @@ class Client extends MatrixApi { } _userDeviceKeys[userId].outdated = false; if (database != null) { - dbActions.add( - () => database.storeUserDeviceKeysInfo(id, userId, false)); + dbActions + .add(() => database.storeUserDeviceKeysInfo(userId, false)); } } } @@ -1892,7 +1887,7 @@ class Client extends MatrixApi { if (database == null || !_toDeviceQueueNeedsProcessing) { return; } - final entries = await database.getToDeviceEventQueue(id); + final entries = await database.getToDeviceEventQueue(); if (entries.isEmpty) { _toDeviceQueueNeedsProcessing = false; return; @@ -1907,7 +1902,7 @@ class Client extends MatrixApi { k, Map.from(v))))); await super.sendToDevice(entry.type, entry.txnId, data); - await database.deleteFromToDeviceQueue(id, entry.id); + await database.deleteFromToDeviceQueue(entry.id); } } @@ -1931,7 +1926,7 @@ class Client extends MatrixApi { if (database != null) { _toDeviceQueueNeedsProcessing = true; await database.insertIntoToDeviceQueue( - id, eventType, txnId, json.encode(messages)); + eventType, txnId, json.encode(messages)); } rethrow; } @@ -2140,7 +2135,7 @@ class Client extends MatrixApi { await abortSync(); prevBatch = null; rooms.clear(); - await database?.clearCache(id); + await database?.clearCache(); encryption?.keyManager?.clearOutboundGroupSessions(); onCacheCleared.add(true); // Restart the syncloop @@ -2246,11 +2241,10 @@ class Client extends MatrixApi { ); Logs().d('Migrate SSSSCache...'); for (final type in cacheTypes) { - final ssssCache = await legacyDatabase.getSSSSCache(_id, type); + final ssssCache = await legacyDatabase.getSSSSCache(type); if (ssssCache != null) { Logs().d('Migrate $type...'); await database.storeSSSSCache( - _id, type, ssssCache.keyId, ssssCache.ciphertext, @@ -2267,7 +2261,6 @@ class Client extends MatrixApi { Logs().d( 'Migrate cross signing key with usage ${crossSigningKey.usage} and verified ${crossSigningKey.directVerified}...'); await database.storeUserCrossSigningKey( - _id, userId, crossSigningKey.publicKey, jsonEncode(crossSigningKey.toJson()), @@ -2278,7 +2271,6 @@ class Client extends MatrixApi { for (final deviceKeys in deviceKeysList.deviceKeys.values) { Logs().d('Migrate device keys for ${deviceKeys.deviceId}...'); await database.storeUserDeviceKey( - _id, userId, deviceKeys.deviceId, jsonEncode(deviceKeys.toJson()), @@ -2288,17 +2280,15 @@ class Client extends MatrixApi { ); } Logs().d('Migrate user device keys info...'); - await database.storeUserDeviceKeysInfo( - _id, userId, deviceKeysList.outdated); + await database.storeUserDeviceKeysInfo(userId, deviceKeysList.outdated); } Logs().d('Migrate inbound group sessions...'); try { - final sessions = await legacyDatabase.getAllInboundGroupSessions(_id); + final sessions = await legacyDatabase.getAllInboundGroupSessions(); for (var i = 0; i < sessions.length; i++) { Logs().d('$i / ${sessions.length}'); final session = sessions[i]; await database.storeInboundGroupSession( - _id, session.roomId, session.sessionId, session.pickle, @@ -2313,7 +2303,7 @@ class Client extends MatrixApi { Logs().e('Unable to migrate inbound group sessions!', e, s); } - await legacyDatabase.clear(_id); + await legacyDatabase.clear(); await legacyDatabaseDestroyer?.call(this); } await legacyDatabase.close(); diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index f56ae03b..f96f3c37 100644 --- a/lib/src/database/database_api.dart +++ b/lib/src/database/database_api.dart @@ -39,7 +39,6 @@ abstract class DatabaseApi { String deviceName, String prevBatch, String olmAccount, - int clientId, ); Future insertClient( @@ -55,34 +54,32 @@ abstract class DatabaseApi { Future> getRoomList(Client client); - Future> getAccountData(int clientId); + Future> getAccountData(); /// Stores a RoomUpdate object in the database. Must be called inside of /// [transaction]. - Future storeRoomUpdate( - int clientId, String roomId, SyncRoomUpdate roomUpdate, + Future storeRoomUpdate(String roomId, SyncRoomUpdate roomUpdate, [Room oldRoom]); /// Stores an EventUpdate object in the database. Must be called inside of /// [transaction]. - Future storeEventUpdate(int clientId, EventUpdate eventUpdate); + Future storeEventUpdate(EventUpdate eventUpdate); - Future getEventById(int clientId, String eventId, Room room); + Future getEventById(String eventId, Room room); - bool eventIsKnown(int clientId, String eventId, String roomId); + bool eventIsKnown(String eventId, String roomId); - Future forgetRoom(int clientId, String roomId); + Future forgetRoom(String roomId); - Future clearCache(int clientId); + Future clearCache(); - Future clear(int clientId); + Future clear(); - Future getUser(int clientId, String userId, Room room); + Future getUser(String userId, Room room); - Future> getUsers(int clientId, Room room); + Future> getUsers(Room room); Future> getEventList( - int clientId, Room room, { int start = 0, int limit, @@ -92,39 +89,35 @@ abstract class DatabaseApi { Future storeFile(Uri mxcUri, Uint8List bytes, int time); - Future storeSyncFilterId(String syncFilterId, int clientId); + Future storeSyncFilterId( + String syncFilterId, + ); - Future storeAccountData(int clientId, String type, String content); + Future storeAccountData(String type, String content); Future> getUserDeviceKeys(Client client); - Future getSSSSCache(int clientId, String type); + Future getSSSSCache(String type); Future getOutboundGroupSession( - int clientId, String roomId, String userId, ); - Future> getAllInboundGroupSessions( - int clientId, - ); + Future> getAllInboundGroupSessions(); Future getInboundGroupSession( - int clientId, String roomId, String sessionId, ); Future updateInboundGroupSessionIndexes( String indexes, - int clientId, String roomId, String sessionId, ); Future storeInboundGroupSession( - int clientId, String roomId, String sessionId, String pickle, @@ -136,22 +129,19 @@ abstract class DatabaseApi { ); Future markInboundGroupSessionAsUploaded( - int clientId, String roomId, String sessionId, ); Future updateInboundGroupSessionAllowedAtIndex( String allowedAtIndex, - int clientId, String roomId, String sessionId, ); - Future removeOutboundGroupSession(int clientId, String roomId); + Future removeOutboundGroupSession(String roomId); Future storeOutboundGroupSession( - int clientId, String roomId, String pickle, String deviceIds, @@ -159,10 +149,11 @@ abstract class DatabaseApi { int sentMessages, ); - Future updateClientKeys(String olmAccount, int clientId); + Future updateClientKeys( + String olmAccount, + ); Future storeOlmSession( - int clientId, String identitiyKey, String sessionId, String pickle, @@ -171,42 +162,39 @@ abstract class DatabaseApi { Future setLastActiveUserDeviceKey( int lastActive, - int clientId, String userId, String deviceId, ); Future setLastSentMessageUserDeviceKey( String lastSentMessage, - int clientId, String userId, String deviceId, ); - Future clearSSSSCache(int clientId); + Future clearSSSSCache(); Future storeSSSSCache( - int clientId, String type, String keyId, String ciphertext, String content, ); - Future markInboundGroupSessionsAsNeedingUpload(int clientId); + Future markInboundGroupSessionsAsNeedingUpload(); - Future storePrevBatch(String prevBatch, int clientId); + Future storePrevBatch( + String prevBatch, + ); Future deleteOldFiles(int savedAt); Future storeUserDeviceKeysInfo( - int clientId, String userId, bool outdated, ); Future storeUserDeviceKey( - int clientId, String userId, String deviceId, String content, @@ -216,19 +204,16 @@ abstract class DatabaseApi { ); Future removeUserDeviceKey( - int clientId, String userId, String deviceId, ); Future removeUserCrossSigningKey( - int clientId, String userId, String publicKey, ); Future storeUserCrossSigningKey( - int clientId, String userId, String publicKey, String content, @@ -236,84 +221,73 @@ abstract class DatabaseApi { bool blocked, ); - Future deleteFromToDeviceQueue(int clientId, int id); + Future deleteFromToDeviceQueue(int id); - Future removeEvent(int clientId, String eventId, String roomId); + Future removeEvent(String eventId, String roomId); Future updateRoomSortOrder( double oldestSortOrder, double newestSortOrder, - int clientId, String roomId, ); Future setRoomPrevBatch( String prevBatch, - int clientId, String roomId, ); - Future resetNotificationCount(int clientId, String roomId); + Future resetNotificationCount(String roomId); Future setVerifiedUserCrossSigningKey( bool verified, - int clientId, String userId, String publicKey, ); Future setBlockedUserCrossSigningKey( bool blocked, - int clientId, String userId, String publicKey, ); Future setVerifiedUserDeviceKey( bool verified, - int clientId, String userId, String deviceId, ); Future setBlockedUserDeviceKey( bool blocked, - int clientId, String userId, String deviceId, ); Future> getUnimportantRoomEventStatesForRoom( - int clientId, List events, Room room, ); Future> getOlmSessions( - int clientId, String identityKey, String userId, ); Future> getOlmSessionsForDevices( - int clientId, List identityKeys, String userId, ); - Future> getToDeviceEventQueue(int clientId); + Future> getToDeviceEventQueue(); /// Please do `jsonEncode(content)` in your code to stay compatible with /// auto generated methods here. Future insertIntoToDeviceQueue( - int clientId, String type, String txnId, String content, ); Future> getLastSentMessageUserDeviceKey( - int clientId, String userId, String deviceId, ); @@ -321,14 +295,13 @@ abstract class DatabaseApi { Future> getInboundGroupSessionsToUpload(); Future addSeenDeviceId( - int clientId, String userId, String deviceId, String publicKeys); + String userId, String deviceId, String publicKeys); - Future addSeenPublicKey( - int clientId, String publicKey, String deviceId); + Future addSeenPublicKey(String publicKey, String deviceId); - Future deviceIdSeen(int clientId, userId, deviceId); + Future deviceIdSeen(userId, deviceId); - Future publicKeySeen(int clientId, String publicKey); + Future publicKeySeen(String publicKey); Future close(); diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index a117255a..325f2897 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -231,22 +231,21 @@ class FamedlySdkHiveDatabase extends DatabaseApi { convertToJson(raw), Client(''), ); - await addSeenDeviceId(0, deviceKeys.userId, deviceKeys.deviceId, + await addSeenDeviceId(deviceKeys.userId, deviceKeys.deviceId, deviceKeys.curve25519Key + deviceKeys.ed25519Key); - await addSeenPublicKey(0, deviceKeys.ed25519Key, deviceKeys.deviceId); - await addSeenPublicKey( - 0, deviceKeys.curve25519Key, deviceKeys.deviceId); + await addSeenPublicKey(deviceKeys.ed25519Key, deviceKeys.deviceId); + await addSeenPublicKey(deviceKeys.curve25519Key, deviceKeys.deviceId); } catch (e) { Logs().w('Can not migrate device $key', e); } } } - await clearCache(0); + await clearCache(); await _clientBox.put('version', version); } @override - Future clear(int clientId) async { + Future clear() async { Logs().i('Clear and close hive database...'); await _actionOnAllBoxes((box) async { try { @@ -261,7 +260,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future clearCache(int clientId) async { + Future clearCache() async { await _roomsBox.deleteAll(_roomsBox.keys); await _accountDataBox.deleteAll(_accountDataBox.keys); await _roomStateBox.deleteAll(_roomStateBox.keys); @@ -274,7 +273,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future clearSSSSCache(int clientId) async { + Future clearSSSSCache() async { await _ssssCacheBox.deleteAll(_ssssCacheBox.keys); } @@ -282,7 +281,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { Future close() => _actionOnAllBoxes((box) => box.close()); @override - Future deleteFromToDeviceQueue(int clientId, int id) async { + Future deleteFromToDeviceQueue(int id) async { await _toDeviceQueueBox.delete(id); return; } @@ -293,7 +292,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future forgetRoom(int clientId, String roomId) async { + Future forgetRoom(String roomId) async { await _timelineFragmentsBox.delete(MultiKey(roomId, '').toString()); for (final key in _eventsBox.keys) { final multiKey = MultiKey.fromString(key); @@ -319,7 +318,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future> getAccountData(int clientId) async { + Future> getAccountData() async { final accountData = {}; for (final key in _accountDataBox.keys) { final raw = await _accountDataBox.get(key); @@ -343,14 +342,14 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future getEventById(int clientId, String eventId, Room room) async { + Future getEventById(String eventId, Room room) async { final raw = await _eventsBox.get(MultiKey(room.id, eventId).toString()); if (raw == null) return null; return Event.fromJson(convertToJson(raw), room); } @override - bool eventIsKnown(int clientId, String eventId, String roomId) => + bool eventIsKnown(String eventId, String roomId) => _eventsBox.keys.contains(MultiKey(roomId, eventId).toString()); /// Loads a whole list of events at once from the store for a specific room @@ -368,7 +367,6 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future> getEventList( - int clientId, Room room, { int start = 0, int? limit, @@ -404,7 +402,6 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future getInboundGroupSession( - int clientId, String roomId, String sessionId, ) async { @@ -432,7 +429,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future> getLastSentMessageUserDeviceKey( - int clientId, String userId, String deviceId) async { + String userId, String deviceId) async { final raw = await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); if (raw == null) return []; @@ -440,8 +437,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storeOlmSession(int clientId, String identityKey, - String sessionId, String pickle, int lastReceived) async { + Future storeOlmSession(String identityKey, String sessionId, + String pickle, int lastReceived) async { final rawSessions = (await _olmSessionsBox.get(identityKey.toHiveKey) as Map?) ?? {}; rawSessions[sessionId] = { @@ -456,7 +453,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future> getOlmSessions( - int clientId, String identityKey, String userId) async { + String identityKey, String userId) async { final rawSessions = await _olmSessionsBox.get(identityKey.toHiveKey) as Map?; if (rawSessions == null || rawSessions.isEmpty) return []; @@ -467,15 +464,15 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future> getOlmSessionsForDevices( - int clientId, List identityKey, String userId) async { - final sessions = await Future.wait(identityKey - .map((identityKey) => getOlmSessions(clientId, identityKey, userId))); + List identityKey, String userId) async { + final sessions = await Future.wait( + identityKey.map((identityKey) => getOlmSessions(identityKey, userId))); return [for (final sublist in sessions) ...sublist]; } @override Future getOutboundGroupSession( - int clientId, String roomId, String userId) async { + String roomId, String userId) async { final raw = await _outboundGroupSessionsBox.get(roomId.toHiveKey); if (raw == null) return null; return OutboundGroupSession.fromJson(convertToJson(raw), userId); @@ -555,14 +552,14 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future getSSSSCache(int clientId, String type) async { + Future getSSSSCache(String type) async { final raw = await _ssssCacheBox.get(type); if (raw == null) return null; return SSSSCache.fromJson(convertToJson(raw)); } @override - Future> getToDeviceEventQueue(int clientId) async => + Future> getToDeviceEventQueue() async => await Future.wait(_toDeviceQueueBox.keys.map((i) async { final raw = await _toDeviceQueueBox.get(i); raw['id'] = i; @@ -571,7 +568,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future> getUnimportantRoomEventStatesForRoom( - int clientId, List events, Room room) async { + List events, Room room) async { final keys = _roomStateBox.keys.where((key) { final tuple = MultiKey.fromString(key); return tuple.parts.first == room.id && !events.contains(tuple.parts[1]); @@ -587,7 +584,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future getUser(int clientId, String userId, Room room) async { + Future getUser(String userId, Room room) async { final state = await _roomMembersBox.get(MultiKey(room.id, userId).toString()); if (state == null) return null; @@ -627,7 +624,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future> getUsers(int clientId, Room room) async { + Future> getUsers(Room room) async { final users = []; for (final key in _roomMembersBox.keys) { final statesKey = MultiKey.fromString(key); @@ -661,7 +658,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future insertIntoToDeviceQueue( - int clientId, String type, String txnId, String content) async { + String type, String txnId, String content) async { return await _toDeviceQueueBox.add({ 'type': type, 'txn_id': txnId, @@ -671,7 +668,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future markInboundGroupSessionAsUploaded( - int clientId, String roomId, String sessionId) async { + String roomId, String sessionId) async { final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey); if (raw == null) { Logs().w( @@ -684,7 +681,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future markInboundGroupSessionsAsNeedingUpload(int clientId) async { + Future markInboundGroupSessionsAsNeedingUpload() async { for (final sessionId in _inboundGroupSessionsBox.keys) { final raw = await _inboundGroupSessionsBox.get(sessionId); raw['uploaded'] = false; @@ -694,7 +691,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future removeEvent(int clientId, String eventId, String roomId) async { + Future removeEvent(String eventId, String roomId) async { await _eventsBox.delete(MultiKey(roomId, eventId).toString()); for (final key in _timelineFragmentsBox.keys) { final multiKey = MultiKey.fromString(key); @@ -710,28 +707,27 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future removeOutboundGroupSession(int clientId, String roomId) async { + Future removeOutboundGroupSession(String roomId) async { await _outboundGroupSessionsBox.delete(roomId.toHiveKey); return; } @override Future removeUserCrossSigningKey( - int clientId, String userId, String publicKey) async { + String userId, String publicKey) async { await _userCrossSigningKeysBox .delete(MultiKey(userId, publicKey).toString()); return; } @override - Future removeUserDeviceKey( - int clientId, String userId, String deviceId) async { + Future removeUserDeviceKey(String userId, String deviceId) async { await _userDeviceKeysBox.delete(MultiKey(userId, deviceId).toString()); return; } @override - Future resetNotificationCount(int clientId, String roomId) async { + Future resetNotificationCount(String roomId) async { final raw = await _roomsBox.get(roomId.toHiveKey); if (raw == null) return; raw['notification_count'] = raw['highlight_count'] = 0; @@ -741,7 +737,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future setBlockedUserCrossSigningKey( - bool blocked, int clientId, String userId, String publicKey) async { + bool blocked, String userId, String publicKey) async { final raw = await _userCrossSigningKeysBox .get(MultiKey(userId, publicKey).toString()); raw['blocked'] = blocked; @@ -754,7 +750,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future setBlockedUserDeviceKey( - bool blocked, int clientId, String userId, String deviceId) async { + bool blocked, String userId, String deviceId) async { final raw = await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); raw['blocked'] = blocked; @@ -767,7 +763,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future setLastActiveUserDeviceKey( - int lastActive, int clientId, String userId, String deviceId) async { + int lastActive, String userId, String deviceId) async { final raw = await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); raw['last_active'] = lastActive; @@ -778,8 +774,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future setLastSentMessageUserDeviceKey(String lastSentMessage, - int clientId, String userId, String deviceId) async { + Future setLastSentMessageUserDeviceKey( + String lastSentMessage, String userId, String deviceId) async { final raw = await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); raw['last_sent_message'] = lastSentMessage; @@ -790,8 +786,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future setRoomPrevBatch( - String prevBatch, int clientId, String roomId) async { + Future setRoomPrevBatch(String prevBatch, String roomId) async { final raw = await _roomsBox.get(roomId.toHiveKey); if (raw == null) return; final room = Room.fromJson(convertToJson(raw)); @@ -802,7 +797,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future setVerifiedUserCrossSigningKey( - bool verified, int clientId, String userId, String publicKey) async { + bool verified, String userId, String publicKey) async { final raw = (await _userCrossSigningKeysBox .get(MultiKey(userId, publicKey).toString()) as Map?) ?? {}; @@ -816,7 +811,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future setVerifiedUserDeviceKey( - bool verified, int clientId, String userId, String deviceId) async { + bool verified, String userId, String deviceId) async { final raw = await _userDeviceKeysBox.get(MultiKey(userId, deviceId).toString()); raw['verified'] = verified; @@ -828,23 +823,21 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storeAccountData( - int clientId, String type, String content) async { + Future storeAccountData(String type, String content) async { await _accountDataBox.put( type.toHiveKey, convertToJson(jsonDecode(content))); return; } @override - Future storeEventUpdate(int clientId, EventUpdate eventUpdate) async { + Future storeEventUpdate(EventUpdate eventUpdate) async { // Ephemerals should not be stored if (eventUpdate.type == EventUpdateType.ephemeral) return; // In case of this is a redaction event if (eventUpdate.content['type'] == EventTypes.Redaction) { final tmpRoom = Room(id: eventUpdate.roomID); - final event = - await getEventById(clientId, eventUpdate.content['redacts'], tmpRoom); + final event = await getEventById(eventUpdate.content['redacts'], tmpRoom); if (event != null) { event.setRedactionEvent(Event.fromJson(eventUpdate.content, tmpRoom)); await _eventsBox.put( @@ -922,7 +915,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { // Is there a transaction id? Then delete the event with this id. if (status != -1 && status != 0 && transactionId != null) { - await removeEvent(clientId, transactionId, eventUpdate.roomID); + await removeEvent(transactionId, eventUpdate.roomID); } } @@ -991,7 +984,6 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future storeInboundGroupSession( - int clientId, String roomId, String sessionId, String pickle, @@ -1003,7 +995,6 @@ class FamedlySdkHiveDatabase extends DatabaseApi { await _inboundGroupSessionsBox.put( sessionId.toHiveKey, StoredInboundGroupSession( - clientId: clientId, roomId: roomId, sessionId: sessionId, pickle: pickle, @@ -1018,13 +1009,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storeOutboundGroupSession( - int clientId, - String roomId, - String pickle, - String deviceIds, - int creationTime, - int sentMessages) async { + Future storeOutboundGroupSession(String roomId, String pickle, + String deviceIds, int creationTime, int sentMessages) async { await _outboundGroupSessionsBox.put(roomId.toHiveKey, { 'room_id': roomId, 'pickle': pickle, @@ -1036,19 +1022,20 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storePrevBatch(String prevBatch, int clientId) async { + Future storePrevBatch( + String prevBatch, + ) async { if (_clientBox.keys.isEmpty) return; await _clientBox.put('prev_batch', prevBatch); return; } @override - Future storeRoomUpdate( - int clientId, String roomId, SyncRoomUpdate roomUpdate, + Future storeRoomUpdate(String roomId, SyncRoomUpdate roomUpdate, [dynamic _]) async { // Leave room if membership is leave if (roomUpdate is LeftRoomUpdate) { - await forgetRoom(clientId, roomId); + await forgetRoom(roomId); return; } final membership = roomUpdate is LeftRoomUpdate @@ -1106,8 +1093,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storeSSSSCache(int clientId, String type, String keyId, - String ciphertext, String content) async { + Future storeSSSSCache( + String type, String keyId, String ciphertext, String content) async { await _ssssCacheBox.put( type, SSSSCache( @@ -1119,13 +1106,15 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storeSyncFilterId(String syncFilterId, int clientId) async { + Future storeSyncFilterId( + String syncFilterId, + ) async { await _clientBox.put('sync_filter_id', syncFilterId); } @override - Future storeUserCrossSigningKey(int clientId, String userId, - String publicKey, String content, bool verified, bool blocked) async { + Future storeUserCrossSigningKey(String userId, String publicKey, + String content, bool verified, bool blocked) async { await _userCrossSigningKeysBox.put( MultiKey(userId, publicKey).toString(), { @@ -1139,7 +1128,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storeUserDeviceKey(int clientId, String userId, String deviceId, + Future storeUserDeviceKey(String userId, String deviceId, String content, bool verified, bool blocked, int lastActive) async { await _userDeviceKeysBox.put(MultiKey(userId, deviceId).toString(), { 'user_id': userId, @@ -1154,8 +1143,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future storeUserDeviceKeysInfo( - int clientId, String userId, bool outdated) async { + Future storeUserDeviceKeysInfo(String userId, bool outdated) async { await _userDeviceKeysOutdatedBox.put(userId.toHiveKey, outdated); return; } @@ -1214,14 +1202,14 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future updateClient( - String homeserverUrl, - String token, - String userId, - String deviceId, - String deviceName, - String prevBatch, - String olmAccount, - int clientId) async { + String homeserverUrl, + String token, + String userId, + String deviceId, + String deviceName, + String prevBatch, + String olmAccount, + ) async { await _clientBox.put('homeserver_url', homeserverUrl); await _clientBox.put('token', token); await _clientBox.put('user_id', userId); @@ -1233,14 +1221,16 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future updateClientKeys(String olmAccount, int clientId) async { + Future updateClientKeys( + String olmAccount, + ) async { await _clientBox.put('olm_account', olmAccount); return; } @override - Future updateInboundGroupSessionAllowedAtIndex(String allowedAtIndex, - int clientId, String roomId, String sessionId) async { + Future updateInboundGroupSessionAllowedAtIndex( + String allowedAtIndex, String roomId, String sessionId) async { final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey); if (raw == null) { Logs().w( @@ -1254,7 +1244,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future updateInboundGroupSessionIndexes( - String indexes, int clientId, String roomId, String sessionId) async { + String indexes, String roomId, String sessionId) async { final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey); if (raw == null) { Logs().w( @@ -1267,8 +1257,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future updateRoomSortOrder(double oldestSortOrder, - double newestSortOrder, int clientId, String roomId) async { + Future updateRoomSortOrder( + double oldestSortOrder, double newestSortOrder, String roomId) async { final raw = await _roomsBox.get(roomId.toHiveKey); raw['oldest_sort_order'] = oldestSortOrder; raw['newest_sort_order'] = newestSortOrder; @@ -1277,8 +1267,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future> getAllInboundGroupSessions( - int clientId) async { + Future> getAllInboundGroupSessions() async { final rawSessions = await Future.wait(_inboundGroupSessionsBox.keys .map((key) => _inboundGroupSessionsBox.get(key))); return rawSessions @@ -1288,7 +1277,6 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future addSeenDeviceId( - int clientId, String userId, String deviceId, String publicKeysHash, @@ -1298,14 +1286,13 @@ class FamedlySdkHiveDatabase extends DatabaseApi { @override Future addSeenPublicKey( - int clientId, String publicKey, String deviceId, ) => _seenDeviceKeysBox.put(publicKey.toHiveKey, deviceId); @override - Future deviceIdSeen(int clientId, userId, deviceId) async { + Future deviceIdSeen(userId, deviceId) async { final raw = await _seenDeviceIdsBox.get(MultiKey(userId, deviceId).toString()); if (raw == null) return null; @@ -1313,7 +1300,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future publicKeySeen(int clientId, String publicKey) async { + Future publicKeySeen(String publicKey) async { final raw = await _seenDeviceKeysBox.get(publicKey.toHiveKey); if (raw == null) return null; return raw as String; diff --git a/lib/src/event.dart b/lib/src/event.dart index 20142ace..f964e570 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -305,7 +305,7 @@ class Event extends MatrixEvent { /// from the database and the timelines. Returns false if not removed. Future remove() async { if (status < 1) { - await room.client.database?.removeEvent(room.client.id, eventId, room.id); + await room.client.database?.removeEvent(eventId, room.id); room.client.onEvent.add(EventUpdate( roomID: room.id, diff --git a/lib/src/room.dart b/lib/src/room.dart index 8ee11f42..08bbcabb 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -286,7 +286,7 @@ class Room { } final allStates = await client.database .getUnimportantRoomEventStatesForRoom( - client.id, client.importantStateEvents.toList(), this); + client.importantStateEvents.toList(), this); for (final state in allStates) { setState(state); @@ -342,7 +342,7 @@ class Room { if (prevEvent != null && prevEvent.eventId != state.eventId && client.database != null && - client.database.eventIsKnown(client.id, state.eventId, state.roomId)) { + client.database.eventIsKnown(state.eventId, state.roomId)) { return; } @@ -1053,7 +1053,7 @@ class Room { /// Call the Matrix API to forget this room if you already left it. Future forget() async { - await client.database?.forgetRoom(client.id, id); + await client.database?.forgetRoom(id); await client.forgetRoom(id); return; } @@ -1135,7 +1135,7 @@ class Room { if (client.database != null) { await client.database.transaction(() async { - await client.database.setRoomPrevBatch(resp.end, client.id, id); + await client.database.setRoomPrevBatch(resp.end, id); await loadFn(); }); } else { @@ -1188,7 +1188,7 @@ class Room { Future setReadMarker(String eventId, {String mRead}) async { if (mRead != null) { notificationCount = 0; - await client.database?.resetNotificationCount(client.id, id); + await client.database?.resetNotificationCount(id); } await client.setReadMarker( id, @@ -1202,7 +1202,7 @@ class Room { /// specified. Future postReceipt(String eventId) async { notificationCount = 0; - await client.database?.resetNotificationCount(client.id, id); + await client.database?.resetNotificationCount(id); await client.postReceipt( id, ReceiptType.mRead, @@ -1216,7 +1216,7 @@ class Room { @Deprecated('Use sendReadMarker instead') Future sendReadReceipt(String eventID) async { notificationCount = 0; - await client.database?.resetNotificationCount(client.id, id); + await client.database?.resetNotificationCount(id); await client.setReadMarker( id, eventID, @@ -1232,7 +1232,6 @@ class Room { var events; if (client.database != null) { events = await client.database.getEventList( - client.id, this, limit: defaultHistoryCount, ); @@ -1286,7 +1285,7 @@ class Room { Future> requestParticipants() async { if (!participantListComplete && partial && client.database != null) { // we aren't fully loaded, maybe the users are in the database - final users = await client.database.getUsers(client.id, this); + final users = await client.database.getUsers(this); for (final user in users) { setState(user); } @@ -1362,7 +1361,7 @@ class Room { } if (client.database != null) { // it may be in the database - final user = await client.database.getUser(client.id, mxID, this); + final user = await client.database.getUser(mxID, this); if (user != null) { setState(user); onUpdate.add(id); @@ -1419,7 +1418,6 @@ class Room { 'state_key': mxID, }; await client.database.storeEventUpdate( - client.id, EventUpdate( content: content, roomID: id, diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 66817dc7..de0b6dae 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -80,7 +80,6 @@ class Timeline { try { // Look up for events in hive first final eventsFromStore = await room.client.database?.getEventList( - room.client.id, room, start: events.length, limit: Room.defaultHistoryCount, diff --git a/lib/src/utils/device_keys_list.dart b/lib/src/utils/device_keys_list.dart index 8a4d9231..86ad1992 100644 --- a/lib/src/utils/device_keys_list.dart +++ b/lib/src/utils/device_keys_list.dart @@ -358,15 +358,15 @@ class CrossSigningKey extends SignableKey { @override Future setVerified(bool newVerified, [bool sign = true]) async { await super.setVerified(newVerified, sign); - return client.database?.setVerifiedUserCrossSigningKey( - newVerified, client.id, userId, publicKey); + return client.database + ?.setVerifiedUserCrossSigningKey(newVerified, userId, publicKey); } @override Future setBlocked(bool newBlocked) { blocked = newBlocked; - return client.database?.setBlockedUserCrossSigningKey( - newBlocked, client.id, userId, publicKey); + return client.database + ?.setBlockedUserCrossSigningKey(newBlocked, userId, publicKey); } CrossSigningKey.fromMatrixCrossSigningKey(MatrixCrossSigningKey k, Client cl) @@ -433,14 +433,14 @@ class DeviceKeys extends SignableKey { Future setVerified(bool newVerified, [bool sign = true]) async { await super.setVerified(newVerified, sign); return client?.database - ?.setVerifiedUserDeviceKey(newVerified, client.id, userId, deviceId); + ?.setVerifiedUserDeviceKey(newVerified, userId, deviceId); } @override Future setBlocked(bool newBlocked) { blocked = newBlocked; return client?.database - ?.setBlockedUserDeviceKey(newBlocked, client.id, userId, deviceId); + ?.setBlockedUserDeviceKey(newBlocked, userId, deviceId); } DeviceKeys.fromMatrixDeviceKeys(MatrixDeviceKeys k, Client cl, diff --git a/test/database_api_test.dart b/test/database_api_test.dart index a06faa44..fe18752a 100644 --- a/test/database_api_test.dart +++ b/test/database_api_test.dart @@ -29,7 +29,9 @@ import 'fake_database.dart'; void main() { /// All Tests related to the ChatTime group('Hive Database Test', () { - testDatabase(getHiveDatabase(null), 0); + testDatabase( + getHiveDatabase(null), + ); }); } @@ -44,7 +46,9 @@ Future olmEnabled() async { return olmEnabled; } -void testDatabase(Future futureDatabase, int clientId) { +void testDatabase( + Future futureDatabase, +) { DatabaseApi database; int toDeviceQueueIndex; test('Open', () async { @@ -76,19 +80,18 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('insertIntoToDeviceQueue', () async { toDeviceQueueIndex = await database.insertIntoToDeviceQueue( - clientId, 'm.test', 'txnId', '{"foo":"bar"}', ); }); test('getToDeviceEventQueue', () async { - final toDeviceQueue = await database.getToDeviceEventQueue(clientId); + final toDeviceQueue = await database.getToDeviceEventQueue(); expect(toDeviceQueue.first.type, 'm.test'); }); test('deleteFromToDeviceQueue', () async { - await database.deleteFromToDeviceQueue(clientId, toDeviceQueueIndex); - final toDeviceQueue = await database.getToDeviceEventQueue(clientId); + await database.deleteFromToDeviceQueue(toDeviceQueueIndex); + final toDeviceQueue = await database.getToDeviceEventQueue(); expect(toDeviceQueue.isEmpty, true); }); test('storeFile', () async { @@ -112,7 +115,7 @@ void testDatabase(Future futureDatabase, int clientId) { 'limited_timeline': false, 'membership': Membership.join, }); - await database.storeRoomUpdate(clientId, '!testroom', roomUpdate); + await database.storeRoomUpdate('!testroom', roomUpdate); final rooms = await database.getRoomList(Client('testclient')); expect(rooms.single.id, '!testroom'); }); @@ -121,12 +124,12 @@ void testDatabase(Future futureDatabase, int clientId) { expect(list.single.id, '!testroom'); }); test('setRoomPrevBatch', () async { - await database.setRoomPrevBatch('1234', clientId, '!testroom'); + await database.setRoomPrevBatch('1234', '!testroom'); final rooms = await database.getRoomList(Client('testclient')); expect(rooms.single.prev_batch, '1234'); }); test('forgetRoom', () async { - await database.forgetRoom(clientId, '!testroom'); + await database.forgetRoom('!testroom'); final rooms = await database.getRoomList(Client('testclient')); expect(rooms.isEmpty, true); }); @@ -134,7 +137,7 @@ void testDatabase(Future futureDatabase, int clientId) { await database.getClient('name'); }); test('insertClient', () async { - clientId = await database.insertClient( + await database.insertClient( 'name', 'homeserverUrl', 'token', @@ -144,6 +147,7 @@ void testDatabase(Future futureDatabase, int clientId) { 'prevBatch', 'olmAccount', ); + final client = await database.getClient('name'); expect(client['token'], 'token'); }); @@ -156,36 +160,38 @@ void testDatabase(Future futureDatabase, int clientId) { 'deviceName', 'prevBatch', 'olmAccount', - clientId, ); final client = await database.getClient('name'); expect(client['token'], 'token_different'); }); test('updateClientKeys', () async { - await database.updateClientKeys('olmAccount2', clientId); + await database.updateClientKeys( + 'olmAccount2', + ); final client = await database.getClient('name'); expect(client['olm_account'], 'olmAccount2'); }); test('storeSyncFilterId', () async { - await database.storeSyncFilterId('1234', clientId); + await database.storeSyncFilterId( + '1234', + ); final client = await database.getClient('name'); expect(client['sync_filter_id'], '1234'); }); test('getAccountData', () async { - await database.getAccountData(clientId); + await database.getAccountData(); }); test('storeAccountData', () async { - await database.storeAccountData(clientId, 'm.test', '{"foo":"bar"}'); - final events = await database.getAccountData(clientId); + await database.storeAccountData('m.test', '{"foo":"bar"}'); + final events = await database.getAccountData(); expect(events.values.single.type, 'm.test'); - await database.storeAccountData(clientId, 'm.abc+de', '{"foo":"bar"}'); - final events2 = await database.getAccountData(clientId); + await database.storeAccountData('m.abc+de', '{"foo":"bar"}'); + final events2 = await database.getAccountData(); expect(events2.values.any((element) => element.type == 'm.abc+de'), true); }); test('storeEventUpdate', () async { await database.storeEventUpdate( - clientId, EventUpdate( roomID: '!testroom:example.com', type: EventUpdateType.timeline, @@ -211,45 +217,41 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('getEventById', () async { final event = await database.getEventById( - clientId, '\$event:example.com', Room(id: '!testroom:example.com')); + '\$event:example.com', Room(id: '!testroom:example.com')); expect(event.type, EventTypes.Message); }); test('getEventList', () async { - final events = await database.getEventList( - clientId, Room(id: '!testroom:example.com')); + final events = + await database.getEventList(Room(id: '!testroom:example.com')); expect(events.single.type, EventTypes.Message); }); test('getUser', () async { final user = await database.getUser( - clientId, '@bob:example.org', Room(id: '!testroom:example.com')); + '@bob:example.org', Room(id: '!testroom:example.com')); expect(user, null); }); test('getUsers', () async { - final users = - await database.getUsers(clientId, Room(id: '!testroom:example.com')); + final users = await database.getUsers(Room(id: '!testroom:example.com')); expect(users.isEmpty, true); }); test('removeEvent', () async { - await database.removeEvent( - clientId, '\$event:example.com', '!testroom:example.com'); + await database.removeEvent('\$event:example.com', '!testroom:example.com'); final event = await database.getEventById( - clientId, '\$event:example.com', Room(id: '!testroom:example.com')); + '\$event:example.com', Room(id: '!testroom:example.com')); expect(event, null); }); test('getAllInboundGroupSessions', () async { - final result = await database.getAllInboundGroupSessions(clientId); + final result = await database.getAllInboundGroupSessions(); expect(result.isEmpty, true); }); test('getInboundGroupSession', () async { - await database.getInboundGroupSession( - clientId, '!testroom:example.com', 'sessionId'); + await database.getInboundGroupSession('!testroom:example.com', 'sessionId'); }); test('getInboundGroupSessionsToUpload', () async { await database.getInboundGroupSessionsToUpload(); }); test('storeInboundGroupSession', () async { await database.storeInboundGroupSession( - clientId, '!testroom:example.com', 'sessionId', 'pickle', @@ -260,7 +262,6 @@ void testDatabase(Future futureDatabase, int clientId) { '{}', ); final session = await database.getInboundGroupSession( - clientId, '!testroom:example.com', 'sessionId', ); @@ -268,15 +269,14 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('markInboundGroupSessionAsUploaded', () async { await database.markInboundGroupSessionAsUploaded( - clientId, '!testroom:example.com', 'sessionId'); + '!testroom:example.com', 'sessionId'); }); test('markInboundGroupSessionsAsNeedingUpload', () async { - await database.markInboundGroupSessionsAsNeedingUpload(clientId); + await database.markInboundGroupSessionsAsNeedingUpload(); }); test('updateInboundGroupSessionAllowedAtIndex', () async { await database.updateInboundGroupSessionAllowedAtIndex( '{}', - clientId, '!testroom:example.com', 'sessionId', ); @@ -284,19 +284,17 @@ void testDatabase(Future futureDatabase, int clientId) { test('updateInboundGroupSessionIndexes', () async { await database.updateInboundGroupSessionIndexes( '{}', - clientId, '!testroom:example.com', 'sessionId', ); }); test('getSSSSCache', () async { - final cache = await database.getSSSSCache(clientId, 'type'); + final cache = await database.getSSSSCache('type'); expect(cache, null); }); test('storeSSSSCache', () async { - await database.storeSSSSCache( - clientId, 'type', 'keyId', 'ciphertext', '{}'); - final cache = await database.getSSSSCache(clientId, 'type'); + await database.storeSSSSCache('type', 'keyId', 'ciphertext', '{}'); + final cache = await database.getSSSSCache('type'); expect(cache.type, 'type'); expect(cache.keyId, 'keyId'); expect(cache.ciphertext, 'ciphertext'); @@ -304,7 +302,6 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('getOlmSessions', () async { final olm = await database.getOlmSessions( - clientId, 'identityKey', 'userId', ); @@ -312,7 +309,6 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('getOlmSessionsForDevices', () async { final olm = await database.getOlmSessionsForDevices( - clientId, ['identityKeys'], 'userId', ); @@ -321,14 +317,12 @@ void testDatabase(Future futureDatabase, int clientId) { test('storeOlmSession', () async { if (!(await olmEnabled())) return; await database.storeOlmSession( - clientId, 'identityKey', 'sessionId', 'pickle', 0, ); final olm = await database.getOlmSessions( - clientId, 'identityKey', 'userId', ); @@ -336,7 +330,6 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('getOutboundGroupSession', () async { final session = await database.getOutboundGroupSession( - clientId, '!testroom:example.com', '@alice:example.com', ); @@ -345,7 +338,6 @@ void testDatabase(Future futureDatabase, int clientId) { test('storeOutboundGroupSession', () async { if (!(await olmEnabled())) return; await database.storeOutboundGroupSession( - clientId, '!testroom:example.com', 'pickle', '{}', @@ -353,7 +345,6 @@ void testDatabase(Future futureDatabase, int clientId) { 0, ); final session = await database.getOutboundGroupSession( - clientId, '!testroom:example.com', '@alice:example.com', ); @@ -361,7 +352,6 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('getLastSentMessageUserDeviceKey', () async { final list = await database.getLastSentMessageUserDeviceKey( - clientId, 'userId', 'deviceId', ); @@ -369,7 +359,6 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('getUnimportantRoomEventStatesForRoom', () async { final events = await database.getUnimportantRoomEventStatesForRoom( - clientId, ['events'], Room(id: '!mep'), ); @@ -380,7 +369,6 @@ void testDatabase(Future futureDatabase, int clientId) { }); test('storeUserCrossSigningKey', () async { await database.storeUserCrossSigningKey( - clientId, '@alice:example.com', 'publicKey', '{}', @@ -391,7 +379,6 @@ void testDatabase(Future futureDatabase, int clientId) { test('setVerifiedUserCrossSigningKey', () async { await database.setVerifiedUserCrossSigningKey( true, - clientId, '@alice:example.com', 'publicKey', ); @@ -399,28 +386,24 @@ void testDatabase(Future futureDatabase, int clientId) { test('setBlockedUserCrossSigningKey', () async { await database.setBlockedUserCrossSigningKey( true, - clientId, '@alice:example.com', 'publicKey', ); }); test('removeUserCrossSigningKey', () async { await database.removeUserCrossSigningKey( - clientId, '@alice:example.com', 'publicKey', ); }); test('storeUserDeviceKeysInfo', () async { await database.storeUserDeviceKeysInfo( - clientId, '@alice:example.com', true, ); }); test('storeUserDeviceKey', () async { await database.storeUserDeviceKey( - clientId, '@alice:example.com', 'deviceId', '{}', @@ -432,7 +415,6 @@ void testDatabase(Future futureDatabase, int clientId) { test('setVerifiedUserDeviceKey', () async { await database.setVerifiedUserDeviceKey( true, - clientId, '@alice:example.com', 'deviceId', ); @@ -440,7 +422,6 @@ void testDatabase(Future futureDatabase, int clientId) { test('setBlockedUserDeviceKey', () async { await database.setBlockedUserDeviceKey( true, - clientId, '@alice:example.com', 'deviceId', ); @@ -448,13 +429,13 @@ void testDatabase(Future futureDatabase, int clientId) { // Clearing up from here test('clearSSSSCache', () async { - await database.clearSSSSCache(clientId); + await database.clearSSSSCache(); }); test('clearCache', () async { - await database.clearCache(clientId); + await database.clearCache(); }); test('clear', () async { - await database.clear(clientId); + await database.clear(); }); test('Close', () async { await database.close(); diff --git a/test/encryption/olm_manager_test.dart b/test/encryption/olm_manager_test.dart index 3b063bd8..e6e7b5f6 100644 --- a/test/encryption/olm_manager_test.dart +++ b/test/encryption/olm_manager_test.dart @@ -166,7 +166,6 @@ void main() { 'floof': 'foxhole', }, }), - client.id, userId, deviceId); var event = ToDeviceEvent( @@ -194,7 +193,6 @@ void main() { 'floof': 'foxhole', }, }), - client.id, userId, deviceId); event = ToDeviceEvent( @@ -218,7 +216,6 @@ void main() { 'floof': 'foxhole', }, }), - client.id, userId, deviceId); event = ToDeviceEvent( @@ -242,7 +239,6 @@ void main() { 'type': 'm.dummy', 'content': {}, }), - client.id, userId, deviceId); event = ToDeviceEvent( diff --git a/test/matrix_database_test.dart b/test/matrix_database_test.dart index e7aee6b5..aca1897c 100644 --- a/test/matrix_database_test.dart +++ b/test/matrix_database_test.dart @@ -26,20 +26,21 @@ import 'fake_database.dart'; void main() { group('Databse', () { Logs().level = Level.error; - var clientId = -1; final room = Room(id: '!room:blubb'); test('setupDatabase', () async { final database = await getDatabase(null); - clientId = await database.insertClient( - 'testclient', - 'https://example.org', - 'blubb', - '@test:example.org', - null, - null, - null, - null); + await database.insertClient( + 'testclient', + 'https://example.org', + 'blubb', + '@test:example.org', + null, + null, + null, + null, + ); }); + test('storeEventUpdate', () async { final database = await getDatabase(null); // store a simple update @@ -54,8 +55,8 @@ void main() { 'sender': '@blah:blubb', }, ); - await database.storeEventUpdate(clientId, update); - var event = await database.getEventById(clientId, '\$event-1', room); + await database.storeEventUpdate(update); + var event = await database.getEventById('\$event-1', room); expect(event.eventId, '\$event-1'); // insert a transaction id @@ -71,8 +72,8 @@ void main() { 'status': 0, }, ); - await database.storeEventUpdate(clientId, update); - event = await database.getEventById(clientId, 'transaction-1', room); + await database.storeEventUpdate(update); + event = await database.getEventById('transaction-1', room); expect(event.eventId, 'transaction-1'); update = EventUpdate( type: EventUpdateType.timeline, @@ -89,10 +90,10 @@ void main() { 'status': 1, }, ); - await database.storeEventUpdate(clientId, update); - event = await database.getEventById(clientId, 'transaction-1', room); + await database.storeEventUpdate(update); + event = await database.getEventById('transaction-1', room); expect(event, null); - event = await database.getEventById(clientId, '\$event-2', room); + event = await database.getEventById('\$event-2', room); // insert a transaction id if the event id for it already exists update = EventUpdate( @@ -107,8 +108,8 @@ void main() { 'status': 0, }, ); - await database.storeEventUpdate(clientId, update); - event = await database.getEventById(clientId, '\$event-3', room); + await database.storeEventUpdate(update); + event = await database.getEventById('\$event-3', room); expect(event.eventId, '\$event-3'); update = EventUpdate( type: EventUpdateType.timeline, @@ -125,11 +126,11 @@ void main() { }, }, ); - await database.storeEventUpdate(clientId, update); - event = await database.getEventById(clientId, '\$event-3', room); + await database.storeEventUpdate(update); + event = await database.getEventById('\$event-3', room); expect(event.eventId, '\$event-3'); expect(event.status, 1); - event = await database.getEventById(clientId, 'transaction-2', room); + event = await database.getEventById('transaction-2', room); expect(event, null); // insert transaction id and not update status @@ -145,8 +146,8 @@ void main() { 'status': 2, }, ); - await database.storeEventUpdate(clientId, update); - event = await database.getEventById(clientId, '\$event-4', room); + await database.storeEventUpdate(update); + event = await database.getEventById('\$event-4', room); expect(event.eventId, '\$event-4'); update = EventUpdate( type: EventUpdateType.timeline, @@ -163,11 +164,11 @@ void main() { }, }, ); - await database.storeEventUpdate(clientId, update); - event = await database.getEventById(clientId, '\$event-4', room); + await database.storeEventUpdate(update); + event = await database.getEventById('\$event-4', room); expect(event.eventId, '\$event-4'); expect(event.status, 2); - event = await database.getEventById(clientId, 'transaction-3', room); + event = await database.getEventById('transaction-3', room); expect(event, null); }); });