Skip to content

Commit

Permalink
Fix clear DB when logout in web
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored and hoangdat committed Oct 2, 2023
1 parent 5f75670 commit bbc8432
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
14 changes: 10 additions & 4 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Client extends MatrixApi {

final Duration sendTimelineEventTimeout;

bool _supportDeleteCollections = false;

Future<MatrixImageFileResizedResponse?> Function(
MatrixImageFileResizeArguments)? customImageResizer;

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -3064,6 +3066,10 @@ class Client extends MatrixApi {
);
}
}

set isSupportDeleteCollections(bool supportDeleteCollections) {
_supportDeleteCollections = supportDeleteCollections;
}
}

class SdkError {
Expand Down
11 changes: 8 additions & 3 deletions lib/src/database/database_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ abstract class DatabaseApi {

Future<void> clearCache();

Future<void> clear();
Future<void> clear({
bool supportDeleteCollections = false,
});

Future<User?> getUser(String userId, Room room);

Expand Down Expand Up @@ -309,7 +311,10 @@ abstract class DatabaseApi {

Future<void> transaction(Future<void> Function() action);

Future<String> exportDump();
Future<String> exportDump({bool supportDeleteCollections = false});

Future<bool> importDump(String export);
Future<bool> importDump(
String export, {
bool supportDeleteCollections = false,
});
}
20 changes: 14 additions & 6 deletions lib/src/database/hive_collections_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ class HiveCollectionsDatabase extends DatabaseApi {
}

@override
Future<void> clear() => transaction(() async {
Future<void> clear({
bool supportDeleteCollections = false,
}) =>
transaction(() async {
await _clientBox.clear();
await _accountDataBox.clear();
await _roomsBox.clear();
Expand All @@ -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
Expand Down Expand Up @@ -1486,7 +1491,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
}

@override
Future<String> exportDump() async {
Future<String> exportDump({bool supportDeleteCollections = false}) async {
final dataMap = {
_clientBoxName: await _clientBox.getAllValues(),
_accountDataBoxName: await _accountDataBox.getAllValues(),
Expand All @@ -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<bool> importDump(String export) async {
Future<bool> importDump(
String export, {
bool supportDeleteCollections = false,
}) async {
try {
await clear();
await clear(supportDeleteCollections: supportDeleteCollections);
await open();
final json = Map.from(jsonDecode(export)).cast<String, Map>();
for (final key in json[_clientBoxName]!.keys) {
Expand Down
11 changes: 8 additions & 3 deletions lib/src/database/hive_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
}

@override
Future<void> clear() async {
Future<void> clear({
bool supportDeleteCollections = false,
}) async {
Logs().i('Clear and close hive database...');
await _actionOnAllBoxes((box) async {
try {
Expand Down Expand Up @@ -1451,13 +1453,16 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
}

@override
Future<String> exportDump() {
Future<String> exportDump({bool supportDeleteCollections = false}) {
// see no need to implement this in a deprecated part
throw UnimplementedError();
}

@override
Future<bool> importDump(String export) {
Future<bool> importDump(
String export, {
bool supportDeleteCollections = false,
}) {
// see no need to implement this in a deprecated part
throw UnimplementedError();
}
Expand Down

0 comments on commit bbc8432

Please sign in to comment.