Skip to content

Commit 0b9746b

Browse files
committed
refactor: make memberlist update logic easier to follow
1 parent fa016b3 commit 0b9746b

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

src/receive_imf.rs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,7 @@ async fn apply_group_changes(
22372237
false
22382238
};
22392239

2240-
let mut chat_contacts =
2240+
let chat_contacts =
22412241
HashSet::<ContactId>::from_iter(chat::get_chat_contacts(context, chat_id).await?);
22422242
let is_from_in_chat =
22432243
!chat_contacts.contains(&ContactId::SELF) || chat_contacts.contains(&from_id);
@@ -2328,11 +2328,6 @@ async fn apply_group_changes(
23282328
}
23292329
}
23302330

2331-
// These are for adding info messages about implicit membership changes, so they are only
2332-
// filled when such messages are needed.
2333-
let mut added_ids = HashSet::<ContactId>::new();
2334-
let mut removed_ids = HashSet::<ContactId>::new();
2335-
23362331
if is_from_in_chat {
23372332
if let Some(ref chat_group_member_timestamps) = mime_parser.chat_group_member_timestamps() {
23382333
send_event_chat_modified |= update_chats_contacts_timestamps(
@@ -2344,41 +2339,31 @@ async fn apply_group_changes(
23442339
chat_group_member_timestamps,
23452340
)
23462341
.await?;
2347-
let new_chat_contacts = HashSet::<ContactId>::from_iter(
2348-
chat::get_chat_contacts(context, chat_id)
2349-
.await?
2350-
.iter()
2351-
.copied(),
2352-
);
2353-
added_ids = new_chat_contacts
2354-
.difference(&chat_contacts)
2355-
.copied()
2356-
.collect();
2357-
removed_ids = chat_contacts
2358-
.difference(&new_chat_contacts)
2359-
.copied()
2360-
.collect();
23612342
} else {
2362-
let mut new_members = HashSet::from_iter(to_ids.iter().copied());
2363-
new_members.insert(ContactId::SELF);
2364-
if !from_id.is_special() {
2365-
new_members.insert(from_id);
2366-
}
2367-
2368-
if !self_added {
2369-
if mime_parser.get_header(HeaderDef::ChatVersion).is_none() {
2370-
// Allow non-Delta Chat MUAs to add members.
2371-
added_ids = new_members.difference(&chat_contacts).copied().collect();
2343+
let mut new_members;
2344+
if self_added {
2345+
new_members = HashSet::from_iter(to_ids.iter().copied());
2346+
new_members.insert(ContactId::SELF);
2347+
if !from_id.is_special() {
2348+
new_members.insert(from_id);
23722349
}
2350+
} else {
2351+
new_members = chat_contacts.clone();
2352+
}
23732353

2374-
if let Some(added_id) = added_id {
2375-
added_ids.insert(added_id);
2376-
}
2377-
new_members.clone_from(&chat_contacts);
2354+
// Allow non-Delta Chat MUAs to add members.
2355+
if mime_parser.get_header(HeaderDef::ChatVersion).is_none() {
23782356
// Don't delete any members locally, but instead add absent ones to provide group
23792357
// membership consistency for all members:
2380-
new_members.extend(added_ids.clone());
2358+
new_members.extend(to_ids.iter());
23812359
}
2360+
2361+
// Apply explicit addition if any.
2362+
if let Some(added_id) = added_id {
2363+
new_members.insert(added_id);
2364+
}
2365+
2366+
// Apply explicit removal if any.
23822367
if let Some(removed_id) = removed_id {
23832368
new_members.remove(&removed_id);
23842369
}
@@ -2391,12 +2376,28 @@ async fn apply_group_changes(
23912376
&new_members,
23922377
)
23932378
.await?;
2394-
chat_contacts = new_members;
23952379
send_event_chat_modified = true;
23962380
}
23972381
}
23982382
}
23992383

2384+
let new_chat_contacts = HashSet::<ContactId>::from_iter(
2385+
chat::get_chat_contacts(context, chat_id)
2386+
.await?
2387+
.iter()
2388+
.copied(),
2389+
);
2390+
2391+
// These are for adding info messages about implicit membership changes.
2392+
let mut added_ids: HashSet<ContactId> = new_chat_contacts
2393+
.difference(&chat_contacts)
2394+
.copied()
2395+
.collect();
2396+
let mut removed_ids: HashSet<ContactId> = chat_contacts
2397+
.difference(&new_chat_contacts)
2398+
.copied()
2399+
.collect();
2400+
24002401
if let Some(added_id) = added_id {
24012402
if !added_ids.remove(&added_id) && !self_added {
24022403
// No-op "Member added" message.
@@ -2437,12 +2438,12 @@ async fn apply_group_changes(
24372438
}
24382439

24392440
if let Some(avatar_action) = &mime_parser.group_avatar {
2440-
if !chat_contacts.contains(&ContactId::SELF) {
2441+
if !new_chat_contacts.contains(&ContactId::SELF) {
24412442
warn!(
24422443
context,
24432444
"Received group avatar update for group chat {chat_id} we are not a member of."
24442445
);
2445-
} else if !chat_contacts.contains(&from_id) {
2446+
} else if !new_chat_contacts.contains(&from_id) {
24462447
warn!(
24472448
context,
24482449
"Contact {from_id} attempts to modify group chat {chat_id} avatar without being a member.",

0 commit comments

Comments
 (0)