Skip to content

Commit

Permalink
TW-1262: notification-for-multiple-accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Jan 4, 2024
1 parent 11f036d commit 0ef73e4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/src/database/hive_collections_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class HiveCollectionsDatabase extends DatabaseApi {

late CollectionBox<String> _seenDeviceKeysBox;

late CollectionBox<String> _roomIdClientBox;

String get _clientBoxName => 'box_client';

String get _accountDataBoxName => 'box_account_data';
Expand Down Expand Up @@ -128,6 +130,8 @@ class HiveCollectionsDatabase extends DatabaseApi {

String get _seenDeviceKeysBoxName => 'box_seen_device_keys';

String get _roomIdClientBoxName => 'box_room_id_client';

HiveCollectionsDatabase(
this.name,
this.path,{
Expand Down Expand Up @@ -162,6 +166,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
_eventsBoxName,
_seenDeviceIdsBoxName,
_seenDeviceKeysBoxName,
_roomIdClientBoxName,
},
key: key,
path: path,
Expand Down Expand Up @@ -229,6 +234,10 @@ class HiveCollectionsDatabase extends DatabaseApi {
_seenDeviceKeysBoxName,
);

_roomIdClientBox = await _collection.openBox(
_roomIdClientBoxName,
);

// Check version and check if we need a migration
final currentVersion = int.tryParse(await _clientBox.get('version') ?? '');
if (currentVersion == null) {
Expand Down Expand Up @@ -276,6 +285,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
await _eventsBox.clear();
await _seenDeviceIdsBox.clear();
await _seenDeviceKeysBox.clear();
await _roomIdClientBox.clear();
if (supportDeleteCollections) {
await _collection.deleteFromDisk();
}
Expand Down Expand Up @@ -338,6 +348,9 @@ class HiveCollectionsDatabase extends DatabaseApi {
if (multiKey.parts.first != roomId) continue;
await _roomAccountDataBox.delete(key);
}
for (final key in await _roomIdClientBox.getAllKeys()) {
await _roomIdClientBox.delete(key);
}
await _roomsBox.delete(roomId);
});

Expand Down Expand Up @@ -604,7 +617,13 @@ class HiveCollectionsDatabase extends DatabaseApi {

// Add to the list and continue.
rooms[room.id] = room;

}


await Future.wait(rooms.values.map((room) {
return _roomIdClientBox.put(room.id, client.clientName);
}));

for (final room in rooms.values) {
// Add states to the room
Expand Down Expand Up @@ -1532,6 +1551,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
_eventsBoxName: await _eventsBox.getAllValues(),
_seenDeviceIdsBoxName: await _seenDeviceIdsBox.getAllValues(),
_seenDeviceKeysBoxName: await _seenDeviceKeysBox.getAllValues(),
_roomIdClientBoxName: await _roomIdClientBox.getAllValues(),
};
final json = jsonEncode(dataMap);
await clear(supportDeleteCollections: supportDeleteCollections);
Expand Down Expand Up @@ -1606,6 +1626,9 @@ class HiveCollectionsDatabase extends DatabaseApi {
for (final key in json[_seenDeviceKeysBoxName]!.keys) {
await _seenDeviceKeysBox.put(key, json[_seenDeviceKeysBoxName]![key]);
}
for (final key in json[_roomIdClientBoxName]!.keys) {
await _roomIdClientBox.put(key, json[_roomIdClientBoxName]![key]);
}
return true;
} catch (e, s) {
Logs().e('Database import error: ', e, s);
Expand Down

0 comments on commit 0ef73e4

Please sign in to comment.