From a4037b82784fa5abc36b98f42ef9b2d7d34acc94 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 3 Jun 2024 14:40:48 +0000 Subject: [PATCH] refactor: put duplicate code into `lookup_chat_or_create_adhoc_group` --- src/receive_imf.rs | 97 +++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index ddff89833..f5584b32f 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -840,34 +840,23 @@ async fn add_parts( } if chat_id.is_none() { - // try to assign to a chat based on In-Reply-To/References: - - if let Some((new_chat_id, new_chat_id_blocked)) = - lookup_chat_by_reply(context, mime_parser, &parent, to_ids, from_id).await? + if let Some((new_chat_id, new_chat_id_blocked)) = lookup_chat_or_create_adhoc_group( + context, + mime_parser, + &parent, + to_ids, + from_id, + allow_creation || test_normal_chat.is_some(), + create_blocked, + is_partial_download.is_some(), + ) + .await? { chat_id = Some(new_chat_id); chat_id_blocked = new_chat_id_blocked; } } - if chat_id.is_none() && (allow_creation || test_normal_chat.is_some()) { - // Try to create an ad hoc group. - if let Some(new_chat_id) = create_adhoc_group( - context, - mime_parser, - create_blocked, - from_id, - to_ids, - is_partial_download.is_some(), - ) - .await - .context("Could not create ad hoc group")? - { - chat_id = Some(new_chat_id); - chat_id_blocked = create_blocked; - } - } - // if the chat is somehow blocked but we want to create a non-blocked chat, // unblock the chat if chat_id_blocked != Blocked::Not && create_blocked != Blocked::Yes { @@ -1087,17 +1076,6 @@ async fn add_parts( } } - if chat_id.is_none() { - // try to assign to a chat based on In-Reply-To/References: - - if let Some((new_chat_id, new_chat_id_blocked)) = - lookup_chat_by_reply(context, mime_parser, &parent, to_ids, from_id).await? - { - chat_id = Some(new_chat_id); - chat_id_blocked = new_chat_id_blocked; - } - } - if mime_parser.decrypting_failed && !fetching_existing_messages { if chat_id.is_none() { chat_id = Some(DC_CHAT_ID_TRASH); @@ -1129,20 +1107,21 @@ async fn add_parts( } } - if chat_id.is_none() && allow_creation { - if let Some(new_chat_id) = create_adhoc_group( + if chat_id.is_none() { + if let Some((new_chat_id, new_chat_id_blocked)) = lookup_chat_or_create_adhoc_group( context, mime_parser, - Blocked::Not, - from_id, + &parent, to_ids, + from_id, + allow_creation, + Blocked::Not, is_partial_download.is_some(), ) - .await - .context("Could not create ad hoc group")? + .await? { chat_id = Some(new_chat_id); - chat_id_blocked = Blocked::Not; + chat_id_blocked = new_chat_id_blocked; } } @@ -1811,6 +1790,44 @@ async fn lookup_chat_by_reply( Ok(Some((parent_chat.id, parent_chat.blocked))) } +#[allow(clippy::too_many_arguments)] +async fn lookup_chat_or_create_adhoc_group( + context: &Context, + mime_parser: &MimeMessage, + parent: &Option, + to_ids: &[ContactId], + from_id: ContactId, + allow_creation: bool, + create_blocked: Blocked, + is_partial_download: bool, +) -> Result> { + if let Some((new_chat_id, new_chat_id_blocked)) = + // Try to assign to a chat based on In-Reply-To/References. + lookup_chat_by_reply(context, mime_parser, parent, to_ids, from_id).await? + { + Ok(Some((new_chat_id, new_chat_id_blocked))) + } else if allow_creation { + // Try to create an ad hoc group. + if let Some(new_chat_id) = create_adhoc_group( + context, + mime_parser, + create_blocked, + from_id, + to_ids, + is_partial_download, + ) + .await + .context("Could not create ad hoc group")? + { + Ok(Some((new_chat_id, create_blocked))) + } else { + Ok(None) + } + } else { + Ok(None) + } +} + /// If this method returns true, the message shall be assigned to the 1:1 chat with the sender. /// If it returns false, it shall be assigned to the parent chat. async fn is_probably_private_reply(