refactor(remove_contact_from_chat): remove one level of indentation

This commit is contained in:
link2xt
2024-10-19 22:25:29 +00:00
parent 1b824705fd
commit 95c252a5a6

View File

@@ -3920,63 +3920,62 @@ pub async fn remove_contact_from_chat(
let mut msg = Message::default(); let mut msg = Message::default();
let chat = Chat::load_from_db(context, chat_id).await?; let chat = Chat::load_from_db(context, chat_id).await?;
if chat.typ == Chattype::Group || chat.typ == Chattype::Broadcast { if chat.typ != Chattype::Group && chat.typ != Chattype::Broadcast {
if !chat.is_self_in_chat(context).await? { bail!("Cannot remove members from non-group chats.");
let err_msg = format!( }
"Cannot remove contact {contact_id} from chat {chat_id}: self not in group."
); if !chat.is_self_in_chat(context).await? {
context.emit_event(EventType::ErrorSelfNotInGroup(err_msg.clone())); let err_msg =
bail!("{}", err_msg); format!("Cannot remove contact {contact_id} from chat {chat_id}: self not in group.");
} else { context.emit_event(EventType::ErrorSelfNotInGroup(err_msg.clone()));
let mut sync = Nosync; bail!("{}", err_msg);
// We do not return an error if the contact does not exist in the database. } else {
// This allows to delete dangling references to deleted contacts let mut sync = Nosync;
// in case of the database becoming inconsistent due to a bug. // We do not return an error if the contact does not exist in the database.
if let Some(contact) = Contact::get_by_id_optional(context, contact_id).await? { // This allows to delete dangling references to deleted contacts
if chat.typ == Chattype::Group && chat.is_promoted() { // in case of the database becoming inconsistent due to a bug.
msg.viewtype = Viewtype::Text; if let Some(contact) = Contact::get_by_id_optional(context, contact_id).await? {
if contact_id == ContactId::SELF { if chat.typ == Chattype::Group && chat.is_promoted() {
msg.text = stock_str::msg_group_left_local(context, ContactId::SELF).await; msg.viewtype = Viewtype::Text;
} else { if contact_id == ContactId::SELF {
msg.text = stock_str::msg_del_member_local( msg.text = stock_str::msg_group_left_local(context, ContactId::SELF).await;
context,
contact.get_addr(),
ContactId::SELF,
)
.await;
}
msg.param.set_cmd(SystemMessage::MemberRemovedFromGroup);
msg.param.set(Param::Arg, contact.get_addr().to_lowercase());
let res = send_msg(context, chat_id, &mut msg).await;
if contact_id == ContactId::SELF {
res?;
set_group_explicitly_left(context, &chat.grpid).await?;
} else if let Err(e) = res {
warn!(context, "remove_contact_from_chat({chat_id}, {contact_id}): send_msg() failed: {e:#}.");
}
} else { } else {
sync = Sync; msg.text = stock_str::msg_del_member_local(
context,
contact.get_addr(),
ContactId::SELF,
)
.await;
} }
} msg.param.set_cmd(SystemMessage::MemberRemovedFromGroup);
// we remove the member from the chat after constructing the msg.param.set(Param::Arg, contact.get_addr().to_lowercase());
// to-be-send message. If between send_msg() and here the let res = send_msg(context, chat_id, &mut msg).await;
// process dies, the user will be able to redo the action. It's better than the other if contact_id == ContactId::SELF {
// way round: you removed someone from DB but no peer or device gets to know about it res?;
// and group membership is thus different on different devices. But if send_msg() set_group_explicitly_left(context, &chat.grpid).await?;
// failed, we still remove the member locally, otherwise it would be impossible to } else if let Err(e) = res {
// remove a member with missing key from a protected group. warn!(context, "remove_contact_from_chat({chat_id}, {contact_id}): send_msg() failed: {e:#}.");
// Note also that sending a message needs all recipients }
// in order to correctly determine encryption so if we } else {
// removed it first, it would complicate the sync = Sync;
// check/encryption logic.
remove_from_chat_contacts_table(context, chat_id, contact_id).await?;
context.emit_event(EventType::ChatModified(chat_id));
if sync.into() {
chat.sync_contacts(context).await.log_err(context).ok();
} }
} }
} else { // we remove the member from the chat after constructing the
bail!("Cannot remove members from non-group chats."); // to-be-send message. If between send_msg() and here the
// process dies, the user will be able to redo the action. It's better than the other
// way round: you removed someone from DB but no peer or device gets to know about it
// and group membership is thus different on different devices. But if send_msg()
// failed, we still remove the member locally, otherwise it would be impossible to
// remove a member with missing key from a protected group.
// Note also that sending a message needs all recipients
// in order to correctly determine encryption so if we
// removed it first, it would complicate the
// check/encryption logic.
remove_from_chat_contacts_table(context, chat_id, contact_id).await?;
context.emit_event(EventType::ChatModified(chat_id));
if sync.into() {
chat.sync_contacts(context).await.log_err(context).ok();
}
} }
Ok(()) Ok(())