refactor: factor create_adhoc_group() call out of create_group()

This commit is contained in:
link2xt
2024-05-30 17:54:21 +00:00
parent f48a047fe0
commit de57ef5ac7

View File

@@ -837,6 +837,7 @@ async fn add_parts(
if chat_id.is_none() && (allow_creation || test_normal_chat.is_some()) { if chat_id.is_none() && (allow_creation || test_normal_chat.is_some()) {
// try to create a group // try to create a group
if let Some(grpid) = mime_parser.get_chat_group_id().map(|s| s.to_string()) {
if let Some((new_chat_id, new_chat_id_blocked)) = create_group( if let Some((new_chat_id, new_chat_id_blocked)) = create_group(
context, context,
mime_parser, mime_parser,
@@ -845,12 +846,27 @@ async fn add_parts(
from_id, from_id,
to_ids, to_ids,
&verified_encryption, &verified_encryption,
&grpid,
) )
.await? .await?
{ {
chat_id = Some(new_chat_id); chat_id = Some(new_chat_id);
chat_id_blocked = new_chat_id_blocked; chat_id_blocked = new_chat_id_blocked;
} }
} else 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, // if the chat is somehow blocked but we want to create a non-blocked chat,
@@ -1099,6 +1115,7 @@ async fn add_parts(
} }
if chat_id.is_none() && allow_creation { if chat_id.is_none() && allow_creation {
if let Some(grpid) = mime_parser.get_chat_group_id().map(|s| s.to_string()) {
if let Some((new_chat_id, new_chat_id_blocked)) = create_group( if let Some((new_chat_id, new_chat_id_blocked)) = create_group(
context, context,
mime_parser, mime_parser,
@@ -1107,12 +1124,27 @@ async fn add_parts(
from_id, from_id,
to_ids, to_ids,
&verified_encryption, &verified_encryption,
&grpid,
) )
.await? .await?
{ {
chat_id = Some(new_chat_id); chat_id = Some(new_chat_id);
chat_id_blocked = new_chat_id_blocked; chat_id_blocked = new_chat_id_blocked;
} }
} else if let Some(new_chat_id) = create_adhoc_group(
context,
mime_parser,
Blocked::Not,
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 = Blocked::Not;
}
} }
if !to_ids.is_empty() { if !to_ids.is_empty() {
@@ -1822,6 +1854,7 @@ async fn is_probably_private_reply(
/// than two members, a new ad hoc group is created. /// than two members, a new ad hoc group is created.
/// ///
/// On success the function returns the created (chat_id, chat_blocked) tuple. /// On success the function returns the created (chat_id, chat_blocked) tuple.
#[allow(clippy::too_many_arguments)]
async fn create_group( async fn create_group(
context: &Context, context: &Context,
mime_parser: &mut MimeMessage, mime_parser: &mut MimeMessage,
@@ -1830,34 +1863,8 @@ async fn create_group(
from_id: ContactId, from_id: ContactId,
to_ids: &[ContactId], to_ids: &[ContactId],
verified_encryption: &VerifiedEncryption, verified_encryption: &VerifiedEncryption,
grpid: &str,
) -> Result<Option<(ChatId, Blocked)>> { ) -> Result<Option<(ChatId, Blocked)>> {
let Some(grpid) = mime_parser.get_chat_group_id().map(|s| s.to_string()) else {
if is_partial_download {
// Partial download may be an encrypted message with protected Subject header.
//
// We do not want to create a group with "..." or "Encrypted message" as a subject.
info!(
context,
"Ad-hoc group cannot be created from partial download."
);
return Ok(None);
}
let mut member_ids: Vec<ContactId> = to_ids.to_vec();
if !member_ids.contains(&(from_id)) {
member_ids.push(from_id);
}
if !member_ids.contains(&(ContactId::SELF)) {
member_ids.push(ContactId::SELF);
}
let res = create_adhoc_group(context, mime_parser, create_blocked, &member_ids)
.await
.context("could not create ad hoc group")?
.map(|chat_id| (chat_id, create_blocked));
return Ok(res);
};
let mut chat_id = None; let mut chat_id = None;
let mut chat_id_blocked = Default::default(); let mut chat_id_blocked = Default::default();
@@ -1900,7 +1907,7 @@ async fn create_group(
// otherwise, a pending "quit" message may pop up // otherwise, a pending "quit" message may pop up
&& mime_parser.get_header(HeaderDef::ChatGroupMemberRemoved).is_none() && mime_parser.get_header(HeaderDef::ChatGroupMemberRemoved).is_none()
// re-create explicitly left groups only if ourself is re-added // re-create explicitly left groups only if ourself is re-added
&& (!chat::is_group_explicitly_left(context, &grpid).await? && (!chat::is_group_explicitly_left(context, grpid).await?
|| self_explicitly_added(context, &mime_parser).await?) || self_explicitly_added(context, &mime_parser).await?)
{ {
// Group does not exist but should be created. // Group does not exist but should be created.
@@ -1913,7 +1920,7 @@ async fn create_group(
let new_chat_id = ChatId::create_multiuser_record( let new_chat_id = ChatId::create_multiuser_record(
context, context,
Chattype::Group, Chattype::Group,
&grpid, grpid,
grpname, grpname,
create_blocked, create_blocked,
create_protected, create_protected,
@@ -2471,8 +2478,29 @@ async fn create_adhoc_group(
context: &Context, context: &Context,
mime_parser: &MimeMessage, mime_parser: &MimeMessage,
create_blocked: Blocked, create_blocked: Blocked,
member_ids: &[ContactId], from_id: ContactId,
to_ids: &[ContactId],
is_partial_download: bool,
) -> Result<Option<ChatId>> { ) -> Result<Option<ChatId>> {
if is_partial_download {
// Partial download may be an encrypted message with protected Subject header.
//
// We do not want to create a group with "..." or "Encrypted message" as a subject.
info!(
context,
"Ad-hoc group cannot be created from partial download."
);
return Ok(None);
}
let mut member_ids: Vec<ContactId> = to_ids.to_vec();
if !member_ids.contains(&(from_id)) {
member_ids.push(from_id);
}
if !member_ids.contains(&(ContactId::SELF)) {
member_ids.push(ContactId::SELF);
}
if mime_parser.is_mailinglist_message() { if mime_parser.is_mailinglist_message() {
return Ok(None); return Ok(None);
} }
@@ -2518,7 +2546,7 @@ async fn create_adhoc_group(
context, context,
"Created ad-hoc group id={new_chat_id}, name={grpname:?}." "Created ad-hoc group id={new_chat_id}, name={grpname:?}."
); );
chat::add_to_chat_contacts_table(context, new_chat_id, member_ids).await?; chat::add_to_chat_contacts_table(context, new_chat_id, &member_ids).await?;
context.emit_event(EventType::ChatModified(new_chat_id)); context.emit_event(EventType::ChatModified(new_chat_id));
chatlist_events::emit_chatlist_changed(context); chatlist_events::emit_chatlist_changed(context);