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 committed Oct 2, 2023
1 parent 5f75670 commit 8365aef
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 47 deletions.
10 changes: 6 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 kIsWeb = 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(isWeb: kIsWeb);

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, isWeb: kIsWeb);

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(isWeb: kIsWeb);
_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(isWeb: kIsWeb);
}
await legacyDatabase?.close();
_initLock = false;
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 isWeb = 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 isWeb = false});

Future<bool> importDump(String export);
Future<bool> importDump(
String export, {
bool isWeb = false,
});
}
81 changes: 44 additions & 37 deletions lib/src/database/hive_collections_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,41 +242,45 @@ class HiveCollectionsDatabase extends DatabaseApi {
}

@override
Future<void> 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<void> 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<void> 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<void> 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<void> clearSSSSCache() => _ssssCacheBox.clear();
Expand Down Expand Up @@ -1486,7 +1490,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
}

@override
Future<String> exportDump() async {
Future<String> exportDump({bool isWeb = false}) async {
final dataMap = {
_clientBoxName: await _clientBox.getAllValues(),
_accountDataBoxName: await _accountDataBox.getAllValues(),
Expand All @@ -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<bool> importDump(String export) async {
Future<bool> importDump(
String export, {
bool isWeb = false,
}) async {
try {
await clear();
await clear(isWeb: isWeb);
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 isWeb = 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 isWeb = 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 isWeb = false,
}) {
// see no need to implement this in a deprecated part
throw UnimplementedError();
}
Expand Down

0 comments on commit 8365aef

Please sign in to comment.