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 a637aae
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/src/database/hive_collections_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class HiveCollectionsDatabase extends DatabaseApi {
/// Key is a tuple as TupleKey(roomId, userId)
late CollectionBox<Map> _roomMembersBox;

late CollectionBox<Map> _roomMultiAccountBox;

/// Key is a tuple as TupleKey(roomId, type)
late CollectionBox<Map> _roomAccountDataBox;
late CollectionBox<Map> _inboundGroupSessionsBox;
Expand Down Expand Up @@ -128,6 +130,8 @@ class HiveCollectionsDatabase extends DatabaseApi {

String get _seenDeviceKeysBoxName => 'box_seen_device_keys';

String get _roomMultiAccountBoxName => 'box_room_multi_account';

HiveCollectionsDatabase(
this.name,
this.path,{
Expand Down Expand Up @@ -184,6 +188,9 @@ class HiveCollectionsDatabase extends DatabaseApi {
_roomMembersBox = await _collection.openBox(
_roomMembersBoxName,
);
_roomMultiAccountBox = await _collection.openBox(
_roomMultiAccountBoxName,
);
_toDeviceQueueBox = await _collection.openBox(
_toDeviceQueueBoxName,
preload: true,
Expand Down Expand Up @@ -228,6 +235,9 @@ class HiveCollectionsDatabase extends DatabaseApi {
_seenDeviceKeysBox = await _collection.openBox(
_seenDeviceKeysBoxName,
);
_roomMultiAccountBox = await _collection.openBox(
_roomMultiAccountBoxName,
);

// Check version and check if we need a migration
final currentVersion = int.tryParse(await _clientBox.get('version') ?? '');
Expand Down Expand Up @@ -276,6 +286,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
await _eventsBox.clear();
await _seenDeviceIdsBox.clear();
await _seenDeviceKeysBox.clear();
await _roomMultiAccountBox.clear();
if (supportDeleteCollections) {
await _collection.deleteFromDisk();
}
Expand Down Expand Up @@ -332,6 +343,11 @@ class HiveCollectionsDatabase extends DatabaseApi {
if (multiKey.parts.first != roomId) continue;
await _roomMembersBox.delete(key);
}
final roomMultiAccountKeys = await _roomMultiAccountBox.getAllKeys();
for (final key in roomMultiAccountKeys) {
if (key != roomId) continue;
await _roomMultiAccountBox.delete(key);
}
final roomAccountDataBoxKeys = await _roomAccountDataBox.getAllKeys();
for (final key in roomAccountDataBoxKeys) {
final multiKey = TupleKey.fromString(key);
Expand Down Expand Up @@ -1154,6 +1170,11 @@ class HiveCollectionsDatabase extends DatabaseApi {
eventUpdate.content['state_key'],
).toString(),
eventUpdate.content);

await _roomMultiAccountBox.put(
eventUpdate.roomID,
eventUpdate.content['state_key'],
);
} else {
final key = TupleKey(
eventUpdate.roomID,
Expand Down Expand Up @@ -1514,6 +1535,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
_roomsBoxName: await _roomsBox.getAllValues(),
_roomStateBoxName: await _roomStateBox.getAllValues(),
_roomMembersBoxName: await _roomMembersBox.getAllValues(),
_roomMultiAccountBoxName: await _roomMultiAccountBox.getAllValues(),
_toDeviceQueueBoxName: await _toDeviceQueueBox.getAllValues(),
_roomAccountDataBoxName: await _roomAccountDataBox.getAllValues(),
_inboundGroupSessionsBoxName:
Expand Down Expand Up @@ -1562,6 +1584,9 @@ class HiveCollectionsDatabase extends DatabaseApi {
for (final key in json[_roomMembersBoxName]!.keys) {
await _roomMembersBox.put(key, json[_roomMembersBoxName]![key]);
}
for (final key in json[_roomMultiAccountBoxName]!.keys) {
await _roomMultiAccountBox.put(key, json[_roomMultiAccountBoxName]![key]);
}
for (final key in json[_toDeviceQueueBoxName]!.keys) {
await _toDeviceQueueBox.put(key, json[_toDeviceQueueBoxName]![key]);
}
Expand Down

0 comments on commit a637aae

Please sign in to comment.