feat: DeviceListModifiedEvent now contains a delta
This commit is contained in:
@@ -59,3 +59,27 @@ OmemoPublicKey? decodeKeyIfNotNull(
|
||||
int getTimestamp() {
|
||||
return DateTime.now().millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
/// Describes the differences between two lists in terms of its items.
|
||||
class ListDiff<T> {
|
||||
ListDiff(this.added, this.removed);
|
||||
|
||||
/// The items that were added.
|
||||
final List<T> added;
|
||||
|
||||
/// The items that were removed.
|
||||
final List<T> removed;
|
||||
}
|
||||
|
||||
extension BeforeAfterListDiff<T> on List<T> {
|
||||
/// Compute the set-based changes between this list and [newList].
|
||||
ListDiff<T> diff(List<T> newList) {
|
||||
final oldSet = Set<T>.from(this);
|
||||
final newSet = Set<T>.from(newList);
|
||||
|
||||
return ListDiff(
|
||||
newSet.difference(oldSet).toList(),
|
||||
oldSet.difference(newSet).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,13 +39,16 @@ class RatchetRemovedEvent extends OmemoEvent {
|
||||
|
||||
/// Triggered when the device map has been modified
|
||||
class DeviceListModifiedEvent extends OmemoEvent {
|
||||
DeviceListModifiedEvent(this.jid, this.devices);
|
||||
DeviceListModifiedEvent(this.jid, this.added, this.removed);
|
||||
|
||||
/// The JID of the user.
|
||||
final String jid;
|
||||
|
||||
/// The list of devices for [jid].
|
||||
final List<int> devices;
|
||||
/// The list of added devices for [jid].
|
||||
final List<int> added;
|
||||
|
||||
/// The list of removed devices for [jid].
|
||||
final List<int> removed;
|
||||
}
|
||||
|
||||
/// Triggered by the OmemoSessionManager when our own device bundle was modified
|
||||
|
||||
@@ -154,7 +154,7 @@ class OmemoManager {
|
||||
_deviceListRequested[jid] = true;
|
||||
|
||||
_eventStreamController.add(
|
||||
DeviceListModifiedEvent(jid, newDeviceList),
|
||||
DeviceListModifiedEvent(jid, newDeviceList, []),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -709,9 +709,10 @@ class OmemoManager {
|
||||
}
|
||||
|
||||
// Clear the device list
|
||||
_eventStreamController
|
||||
.add(DeviceListModifiedEvent(jid, [], _deviceList[jid]!));
|
||||
_deviceList.remove(jid);
|
||||
_deviceListRequested.remove(jid);
|
||||
_eventStreamController.add(DeviceListModifiedEvent(jid, []));
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -719,13 +720,26 @@ class OmemoManager {
|
||||
/// To be called when a update to the device list of [jid] is returned.
|
||||
/// [devices] is the list of device identifiers contained in the update.
|
||||
Future<void> onDeviceListUpdate(String jid, List<int> devices) async {
|
||||
// Update our state
|
||||
_deviceList[jid] = devices;
|
||||
_deviceListRequested[jid] = true;
|
||||
await _ratchetQueue.synchronized(
|
||||
[jid],
|
||||
() async {
|
||||
// Compute the delta
|
||||
ListDiff<int> delta;
|
||||
if (_deviceList.containsKey(jid)) {
|
||||
delta = _deviceList[jid]!.diff(devices);
|
||||
} else {
|
||||
delta = ListDiff(devices, []);
|
||||
}
|
||||
|
||||
// Commit the device list
|
||||
_eventStreamController.add(
|
||||
DeviceListModifiedEvent(jid, devices),
|
||||
// Update our state
|
||||
_deviceList[jid] = devices;
|
||||
_deviceListRequested[jid] = true;
|
||||
|
||||
// Commit the device list
|
||||
_eventStreamController.add(
|
||||
DeviceListModifiedEvent(jid, delta.added, delta.removed),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user