From 4b65ab2ead7ce0521ee5acd829b4ef4ef7f75956 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Tue, 5 Nov 2024 20:41:22 +0100 Subject: [PATCH] fix: Clear draft when leaving a group or when Self is removed. Fix #6175. This is not a very bad bug (even WhatsApp has the same bug, and WhatsApp rarely has any bugs at all!), so I think it's fine not to add regression tests. I did test that it works on an Android phone. Clearing the draft when a different device leaves the group or when Self is removed from the group works. What doesn't work is clearing the draft when leaving a group from this device, because after leaving, the UI still has the draft in the now-hidden input bar, and calls set_draft() when the user leaves the activity, re-adding the draft. We could fix this by loading the chat in do_set_draft(), calling, `is_self_in_chat()`, and not setting the draft if self isn't in the chat. This is two extra database calls everytime the user leaves a group and goes back to the chat list - not a big deal, but I'm nonetheless not sure it's worth it for this super arcane bug. What do you think? --- src/chat.rs | 10 ++++++++++ src/receive_imf.rs | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/chat.rs b/src/chat.rs index 9d36f30d8..883ba39b0 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -3921,6 +3921,11 @@ pub async fn remove_contact_from_chat( let mut msg = Message::default(); let chat = Chat::load_from_db(context, chat_id).await?; + + if chat.typ == Chattype::Group && contact_id == ContactId::SELF { + chat_id.set_draft(context, None).await?; // Clear draft since the user left the group. + } + if chat.typ == Chattype::Group || chat.typ == Chattype::Broadcast { if !chat.is_self_in_chat(context).await? { let err_msg = format!( @@ -4611,6 +4616,11 @@ async fn set_contacts_by_addrs(context: &Context, id: ChatId, addrs: &[String]) if contacts == contacts_old { return Ok(()); } + + if chat.typ == Chattype::Group && !contacts.contains(&ContactId::SELF) { + id.set_draft(context, None).await?; // Clear draft since the user left the group. + } + update_chat_contacts_table(context, id, &contacts).await?; context.emit_event(EventType::ChatModified(id)); Ok(()) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index f3d513a60..3774b5f65 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -2317,6 +2317,9 @@ async fn apply_group_changes( } if new_members != chat_contacts { + if !new_members.contains(&ContactId::SELF) { + chat_id.set_draft(context, None).await?; // Clear draft since Self was removed from the group. + } chat::update_chat_contacts_table(context, chat_id, &new_members).await?; chat_contacts = new_members; send_event_chat_modified = true;