From 8365aefb82d572385fd1b50093229ce62fb92c3c Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Mon, 2 Oct 2023 11:22:02 +0700 Subject: [PATCH] Fix clear DB when logout in web --- lib/src/client.dart | 10 ++- lib/src/database/database_api.dart | 11 ++- .../database/hive_collections_database.dart | 81 ++++++++++--------- lib/src/database/hive_database.dart | 11 ++- 4 files changed, 66 insertions(+), 47 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 65567719f..0cb0f0d09 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -109,6 +109,8 @@ class Client extends MatrixApi { final Duration sendTimelineEventTimeout; + bool kIsWeb = false; + Future Function( MatrixImageFileResizeArguments)? customImageResizer; @@ -1033,7 +1035,7 @@ class Client extends MatrixApi { await abortSync(); await dispose(closeDatabase: false); - final export = await database!.exportDump(); + final export = await database!.exportDump(isWeb: kIsWeb); await clear(); return export; @@ -1054,7 +1056,7 @@ class Client extends MatrixApi { _database ??= await databaseBuilder!.call(this); - final success = await database!.importDump(export); + final success = await database!.importDump(export, isWeb: kIsWeb); if (success) { // closing including DB @@ -1588,7 +1590,7 @@ class Client extends MatrixApi { Logs().outputEvents.clear(); try { await abortSync(); - await database?.clear(); + await database?.clear(isWeb: kIsWeb); _backgroundSync = true; } catch (e, s) { Logs().e('Unable to clear database', e, s); @@ -3053,7 +3055,7 @@ class Client extends MatrixApi { Logs().e('Unable to migrate inbound group sessions!', e, s); } - await legacyDatabase.clear(); + await legacyDatabase.clear(isWeb: kIsWeb); } await legacyDatabase?.close(); _initLock = false; diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index 589e71022..ed8138ce7 100644 --- a/lib/src/database/database_api.dart +++ b/lib/src/database/database_api.dart @@ -73,7 +73,9 @@ abstract class DatabaseApi { Future clearCache(); - Future clear(); + Future clear({ + bool isWeb = false, + }); Future getUser(String userId, Room room); @@ -309,7 +311,10 @@ abstract class DatabaseApi { Future transaction(Future Function() action); - Future exportDump(); + Future exportDump({bool isWeb = false}); - Future importDump(String export); + Future importDump( + String export, { + bool isWeb = false, + }); } diff --git a/lib/src/database/hive_collections_database.dart b/lib/src/database/hive_collections_database.dart index 425d7afbf..0df83f924 100644 --- a/lib/src/database/hive_collections_database.dart +++ b/lib/src/database/hive_collections_database.dart @@ -242,41 +242,45 @@ class HiveCollectionsDatabase extends DatabaseApi { } @override - Future clear() => transaction(() async { - await _clientBox.clear(); - await _accountDataBox.clear(); - await _roomsBox.clear(); - await _roomStateBox.clear(); - await _roomMembersBox.clear(); - await _toDeviceQueueBox.clear(); - await _roomAccountDataBox.clear(); - await _inboundGroupSessionsBox.clear(); - await _outboundGroupSessionsBox.clear(); - await _olmSessionsBox.clear(); - await _userDeviceKeysBox.clear(); - await _userDeviceKeysOutdatedBox.clear(); - await _userCrossSigningKeysBox.clear(); - await _ssssCacheBox.clear(); - await _presencesBox.clear(); - await _timelineFragmentsBox.clear(); - await _eventsBox.clear(); - await _seenDeviceIdsBox.clear(); - await _seenDeviceKeysBox.clear(); - await _collection.deleteFromDisk(); - }); + Future clear({ + bool isWeb = false, + }) async { + await _clientBox.clear(); + await _accountDataBox.clear(); + await _roomsBox.clear(); + await _roomStateBox.clear(); + await _roomMembersBox.clear(); + await _toDeviceQueueBox.clear(); + await _roomAccountDataBox.clear(); + await _inboundGroupSessionsBox.clear(); + await _outboundGroupSessionsBox.clear(); + await _olmSessionsBox.clear(); + await _userDeviceKeysBox.clear(); + await _userDeviceKeysOutdatedBox.clear(); + await _userCrossSigningKeysBox.clear(); + await _ssssCacheBox.clear(); + await _presencesBox.clear(); + await _timelineFragmentsBox.clear(); + await _eventsBox.clear(); + await _seenDeviceIdsBox.clear(); + await _seenDeviceKeysBox.clear(); + if (!isWeb) { + await _collection.deleteFromDisk(); + } + } @override - Future clearCache() => transaction(() async { - await _roomsBox.clear(); - await _accountDataBox.clear(); - await _roomStateBox.clear(); - await _roomMembersBox.clear(); - await _eventsBox.clear(); - await _timelineFragmentsBox.clear(); - await _outboundGroupSessionsBox.clear(); - await _presencesBox.clear(); - await _clientBox.delete('prev_batch'); - }); + Future clearCache() async { + await _roomsBox.clear(); + await _accountDataBox.clear(); + await _roomStateBox.clear(); + await _roomMembersBox.clear(); + await _eventsBox.clear(); + await _timelineFragmentsBox.clear(); + await _outboundGroupSessionsBox.clear(); + await _presencesBox.clear(); + await _clientBox.delete('prev_batch'); + } @override Future clearSSSSCache() => _ssssCacheBox.clear(); @@ -1486,7 +1490,7 @@ class HiveCollectionsDatabase extends DatabaseApi { } @override - Future exportDump() async { + Future exportDump({bool isWeb = false}) async { final dataMap = { _clientBoxName: await _clientBox.getAllValues(), _accountDataBoxName: await _accountDataBox.getAllValues(), @@ -1513,14 +1517,17 @@ class HiveCollectionsDatabase extends DatabaseApi { _seenDeviceKeysBoxName: await _seenDeviceKeysBox.getAllValues(), }; final json = jsonEncode(dataMap); - await clear(); + await clear(isWeb: isWeb); return json; } @override - Future importDump(String export) async { + Future importDump( + String export, { + bool isWeb = false, + }) async { try { - await clear(); + await clear(isWeb: isWeb); await open(); final json = Map.from(jsonDecode(export)).cast(); for (final key in json[_clientBoxName]!.keys) { diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index f3bfa9c75..cc31f868b 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -267,7 +267,9 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future clear() async { + Future clear({ + bool isWeb = false, + }) async { Logs().i('Clear and close hive database...'); await _actionOnAllBoxes((box) async { try { @@ -1451,13 +1453,16 @@ class FamedlySdkHiveDatabase extends DatabaseApi { } @override - Future exportDump() { + Future exportDump({bool isWeb = false}) { // see no need to implement this in a deprecated part throw UnimplementedError(); } @override - Future importDump(String export) { + Future importDump( + String export, { + bool isWeb = false, + }) { // see no need to implement this in a deprecated part throw UnimplementedError(); }