More refactoring of the group session sharing code for clarity.

master
Denis Kasak 2021-03-02 10:27:19 +01:00
parent df8c489304
commit 2b5e1744ee
1 changed files with 23 additions and 22 deletions

View File

@ -255,20 +255,19 @@ impl GroupSessionManager {
for user_id in users { for user_id in users {
let user_devices = self.store.get_user_devices(&user_id).await?; let user_devices = self.store.get_user_devices(&user_id).await?;
let non_blacklisted_devices: Vec<Device> = user_devices
.devices()
.filter(|d| !d.is_blacklisted())
.collect();
// If we haven't already concluded that the session should be rotated, check whether // If we haven't already concluded that the session should be rotated for other
// any of the devices in the session got deleted or blacklisted in the meantime. If so, // reasons, we also need to check whether any of the devices in the session got deleted
// we should also rotate the session. // or blacklisted in the meantime. If so, we should also rotate the session.
if !should_rotate { if !should_rotate {
// Devices that should receive this session // Device IDs that should receive this session
let device_ids: HashSet<&DeviceId> = user_devices let non_blacklisted_device_ids: HashSet<&DeviceId> = non_blacklisted_devices
.keys() .iter()
.filter(|d| { .map(|d| d.device_id())
!user_devices
.get(d)
.map_or(false, |d| d.is_blacklisted())
})
.map(|d| d.as_ref())
.collect(); .collect();
if let Some(shared) = outbound.shared_with_set.get(user_id) { if let Some(shared) = outbound.shared_with_set.get(user_id) {
@ -278,16 +277,18 @@ impl GroupSessionManager {
shared.iter().map(|d| d.key().clone()).collect(); shared.iter().map(|d| d.key().clone()).collect();
let shared: HashSet<&DeviceId> = shared.iter().map(|d| d.as_ref()).collect(); let shared: HashSet<&DeviceId> = shared.iter().map(|d| d.as_ref()).collect();
// The difference between the devices that received the // The set difference between
// session and devices that should receive the session are
// our deleted or newly blacklisted devices
// //
// If the set isn't empty, a device got blacklisted or // 1. Devices that had previously received the session, and
// deleted, so remember it. // 2. Devices that would now receive the session
if !shared //
.difference(&device_ids) // represents newly deleted or blacklisted devices. If this set is non-empty,
.collect::<HashSet<_>>() // we must rotate.
.is_empty() let newly_deleted_or_blacklisted = shared
.difference(&non_blacklisted_device_ids)
.collect::<HashSet<_>>();
if !newly_deleted_or_blacklisted.is_empty()
{ {
should_rotate = true; should_rotate = true;
} }
@ -297,7 +298,7 @@ impl GroupSessionManager {
devices devices
.entry(user_id.clone()) .entry(user_id.clone())
.or_insert_with(Vec::new) .or_insert_with(Vec::new)
.extend(user_devices.devices().filter(|d| !d.is_blacklisted())); .extend(non_blacklisted_devices);
} }
Ok((should_rotate, devices)) Ok((should_rotate, devices))