refactor: less nested remove_contact_from_chat

This commit is contained in:
WofWca
2026-04-09 16:45:02 +04:00
parent f33e21ccb9
commit 5d5deedec3

View File

@@ -4131,61 +4131,56 @@ pub async fn remove_contact_from_chat(
delete_broadcast_secret(context, chat_id).await?; delete_broadcast_secret(context, chat_id).await?;
} }
if matches!( ensure!(
chat.typ, matches!(
Chattype::Group | Chattype::OutBroadcast | Chattype::InBroadcast chat.typ,
) { Chattype::Group | Chattype::OutBroadcast | Chattype::InBroadcast
if !chat.is_self_in_chat(context).await? { ),
let err_msg = format!( "Cannot remove members from non-group chats."
"Cannot remove contact {contact_id} from chat {chat_id}: self not in group." );
);
context.emit_event(EventType::ErrorSelfNotInGroup(err_msg.clone()));
bail!("{err_msg}");
} else {
let mut sync = Nosync;
if chat.is_promoted() && chat.typ != Chattype::OutBroadcast { if !chat.is_self_in_chat(context).await? {
remove_from_chat_contacts_table(context, chat_id, contact_id).await?; let err_msg =
} else { format!("Cannot remove contact {contact_id} from chat {chat_id}: self not in group.");
remove_from_chat_contacts_table_without_trace(context, chat_id, contact_id).await?; context.emit_event(EventType::ErrorSelfNotInGroup(err_msg.clone()));
} bail!("{err_msg}");
}
// We do not return an error if the contact does not exist in the database. let mut sync = Nosync;
// This allows to delete dangling references to deleted contacts
// in case of the database becoming inconsistent due to a bug.
if let Some(contact) = Contact::get_by_id_optional(context, contact_id).await? {
if chat.is_promoted() {
let addr = contact.get_addr();
let fingerprint = contact.fingerprint().map(|f| f.hex());
let res = send_member_removal_msg( if chat.is_promoted() && chat.typ != Chattype::OutBroadcast {
context, remove_from_chat_contacts_table(context, chat_id, contact_id).await?;
&chat, } else {
contact_id, remove_from_chat_contacts_table_without_trace(context, chat_id, contact_id).await?;
addr, }
fingerprint.as_deref(),
) // We do not return an error if the contact does not exist in the database.
// This allows to delete dangling references to deleted contacts
// in case of the database becoming inconsistent due to a bug.
if let Some(contact) = Contact::get_by_id_optional(context, contact_id).await? {
if chat.is_promoted() {
let addr = contact.get_addr();
let fingerprint = contact.fingerprint().map(|f| f.hex());
let res =
send_member_removal_msg(context, &chat, contact_id, addr, fingerprint.as_deref())
.await; .await;
if contact_id == ContactId::SELF { if contact_id == ContactId::SELF {
res?; res?;
} else if let Err(e) = res { } else if let Err(e) = res {
warn!( warn!(
context, context,
"remove_contact_from_chat({chat_id}, {contact_id}): send_msg() failed: {e:#}." "remove_contact_from_chat({chat_id}, {contact_id}): send_msg() failed: {e:#}."
); );
}
} else {
sync = Sync;
}
}
context.emit_event(EventType::ChatModified(chat_id));
if sync.into() {
chat.sync_contacts(context).await.log_err(context).ok();
} }
} else {
sync = Sync;
} }
} else { }
bail!("Cannot remove members from non-group chats."); context.emit_event(EventType::ChatModified(chat_id));
if sync.into() {
chat.sync_contacts(context).await.log_err(context).ok();
} }
Ok(()) Ok(())