From bbc843232affc4b04fb7dbd45a925ddde4a7e878 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 | 14 +++++++++---- lib/src/database/database_api.dart | 11 +++++++--- .../database/hive_collections_database.dart | 20 +++++++++++++------ lib/src/database/hive_database.dart | 11 +++++++--- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 65567719..18ec1f2d 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -109,6 +109,8 @@ class Client extends MatrixApi { final Duration sendTimelineEventTimeout; + bool _supportDeleteCollections = 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(supportDeleteCollections: _supportDeleteCollections); 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, supportDeleteCollections: _supportDeleteCollections); 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(supportDeleteCollections: _supportDeleteCollections); _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(supportDeleteCollections: _supportDeleteCollections); } await legacyDatabase?.close(); _initLock = false; @@ -3064,6 +3066,10 @@ class Client extends MatrixApi { ); } } + + set isSupportDeleteCollections(bool supportDeleteCollections) { + _supportDeleteCollections = supportDeleteCollections; + } } class SdkError { diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index 589e7102..54926559 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 supportDeleteCollections = false, + }); Future getUser(String userId, Room room); @@ -309,7 +311,10 @@ abstract class DatabaseApi { Future transaction(Future Function() action); - Future exportDump(); + Future exportDump({bool supportDeleteCollections = false}); - Future importDump(String export); + Future importDump( + String export, { + bool supportDeleteCollections = false, + }); } diff --git a/lib/src/database/hive_collections_database.dart b/lib/src/database/hive_collections_database.dart index 425d7afb..9fdbf0b5 100644 --- a/lib/src/database/hive_collections_database.dart +++ b/lib/src/database/hive_collections_database.dart @@ -242,7 +242,10 @@ class HiveCollectionsDatabase extends DatabaseApi { } @override - Future clear() => transaction(() async { + Future clear({ + bool supportDeleteCollections = false, + }) => + transaction(() async { await _clientBox.clear(); await _accountDataBox.clear(); await _roomsBox.clear(); @@ -262,7 +265,9 @@ class HiveCollectionsDatabase extends DatabaseApi { await _eventsBox.clear(); await _seenDeviceIdsBox.clear(); await _seenDeviceKeysBox.clear(); - await _collection.deleteFromDisk(); + if (supportDeleteCollections) { + await _collection.deleteFromDisk(); + } }); @override @@ -1486,7 +1491,7 @@ class HiveCollectionsDatabase extends DatabaseApi { } @override - Future exportDump() async { + Future exportDump({bool supportDeleteCollections = false}) async { final dataMap = { _clientBoxName: await _clientBox.getAllValues(), _accountDataBoxName: await _accountDataBox.getAllValues(), @@ -1513,14 +1518,17 @@ class HiveCollectionsDatabase extends DatabaseApi { _seenDeviceKeysBoxName: await _seenDeviceKeysBox.getAllValues(), }; final json = jsonEncode(dataMap); - await clear(); + await clear(supportDeleteCollections: supportDeleteCollections); return json; } @override - Future importDump(String export) async { + Future importDump( + String export, { + bool supportDeleteCollections = false, + }) async { try { - await clear(); + await clear(supportDeleteCollections: supportDeleteCollections); 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 f3bfa9c7..388633d6 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 supportDeleteCollections = 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 supportDeleteCollections = false}) { // see no need to implement this in a deprecated part throw UnimplementedError(); } @override - Future importDump(String export) { + Future importDump( + String export, { + bool supportDeleteCollections = false, + }) { // see no need to implement this in a deprecated part throw UnimplementedError(); }