From 22a7cfe9c3504cb994cd891164764c5fa16c90c4 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Tue, 21 Jan 2025 17:38:35 +0100 Subject: [PATCH] refactor: Extract `group_changes_msgs()` function (#6460) --- src/receive_imf.rs | 68 +++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index 8b7f4c7fb..51cb475a4 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -2227,7 +2227,6 @@ async fn apply_group_changes( let mut send_event_chat_modified = false; let (mut removed_id, mut added_id) = (None, None); let mut better_msg = None; - let mut group_changes_msgs = Vec::new(); // True if a Delta Chat client has explicitly added our current primary address. let self_added = @@ -2409,33 +2408,7 @@ async fn apply_group_changes( if let Some(removed_id) = removed_id { removed_ids.remove(&removed_id); } - if !added_ids.is_empty() { - warn!( - context, - "Implicit addition of {added_ids:?} to chat {chat_id}." - ); - } - if !removed_ids.is_empty() { - warn!( - context, - "Implicit removal of {removed_ids:?} from chat {chat_id}." - ); - } - group_changes_msgs.reserve(added_ids.len() + removed_ids.len()); - for contact_id in added_ids { - let contact = Contact::get_by_id(context, contact_id).await?; - group_changes_msgs.push( - stock_str::msg_add_member_local(context, contact.get_addr(), ContactId::UNDEFINED) - .await, - ); - } - for contact_id in removed_ids { - let contact = Contact::get_by_id(context, contact_id).await?; - group_changes_msgs.push( - stock_str::msg_del_member_local(context, contact.get_addr(), ContactId::UNDEFINED) - .await, - ); - } + let group_changes_msgs = group_changes_msgs(context, &added_ids, &removed_ids, chat_id).await?; if let Some(avatar_action) = &mime_parser.group_avatar { if !new_chat_contacts.contains(&ContactId::SELF) { @@ -2475,6 +2448,45 @@ async fn apply_group_changes( Ok((group_changes_msgs, better_msg)) } +/// Returns a list of strings that should be shown as info messages, informing about group membership changes. +async fn group_changes_msgs( + context: &Context, + added_ids: &HashSet, + removed_ids: &HashSet, + chat_id: ChatId, +) -> Result> { + let mut group_changes_msgs = Vec::new(); + if !added_ids.is_empty() { + warn!( + context, + "Implicit addition of {added_ids:?} to chat {chat_id}." + ); + } + if !removed_ids.is_empty() { + warn!( + context, + "Implicit removal of {removed_ids:?} from chat {chat_id}." + ); + } + group_changes_msgs.reserve(added_ids.len() + removed_ids.len()); + for contact_id in added_ids { + let contact = Contact::get_by_id(context, *contact_id).await?; + group_changes_msgs.push( + stock_str::msg_add_member_local(context, contact.get_addr(), ContactId::UNDEFINED) + .await, + ); + } + for contact_id in removed_ids { + let contact = Contact::get_by_id(context, *contact_id).await?; + group_changes_msgs.push( + stock_str::msg_del_member_local(context, contact.get_addr(), ContactId::UNDEFINED) + .await, + ); + } + + Ok(group_changes_msgs) +} + static LIST_ID_REGEX: Lazy = Lazy::new(|| Regex::new(r"^(.+)<(.+)>$").unwrap()); fn mailinglist_header_listid(list_id_header: &str) -> Result {