Skip to content

Commit

Permalink
refactor: put duplicate code into lookup_chat_or_create_adhoc_group
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Jun 3, 2024
1 parent 3040505 commit a4037b8
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,31 +840,20 @@ 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 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(
if let Some((new_chat_id, new_chat_id_blocked)) = lookup_chat_or_create_adhoc_group(
context,
mime_parser,
create_blocked,
from_id,
&parent,
to_ids,
from_id,
allow_creation || test_normal_chat.is_some(),
create_blocked,
is_partial_download.is_some(),
)
.await
.context("Could not create ad hoc group")?
.await?
{
chat_id = Some(new_chat_id);
chat_id_blocked = create_blocked;
chat_id_blocked = new_chat_id_blocked;
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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<Message>,
to_ids: &[ContactId],
from_id: ContactId,
allow_creation: bool,
create_blocked: Blocked,
is_partial_download: bool,
) -> Result<Option<(ChatId, Blocked)>> {
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(
Expand Down

0 comments on commit a4037b8

Please sign in to comment.