diff --git a/src/receive_imf.rs b/src/receive_imf.rs index dc1e19f71..390f6e327 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -1,6 +1,6 @@ //! Internet Message Format reception pipeline. -use std::collections::{BTreeMap, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::iter; use std::sync::LazyLock; @@ -2471,11 +2471,8 @@ async fn lookup_or_create_adhoc_group( .unwrap_or_else(|| "👥📧".to_string()) }); let to_ids: Vec = to_ids.iter().filter_map(|x| *x).collect(); - let mut contact_ids = Vec::with_capacity(to_ids.len() + 1); - contact_ids.extend(&to_ids); - if !contact_ids.contains(&from_id) { - contact_ids.push(from_id); - } + let mut contact_ids = BTreeSet::::from_iter(to_ids.iter().copied()); + contact_ids.insert(from_id); let trans_fn = |t: &mut rusqlite::Transaction| { t.pragma_update(None, "query_only", "0")?; t.execute( diff --git a/src/receive_imf/receive_imf_tests.rs b/src/receive_imf/receive_imf_tests.rs index 5af832714..c7317a30a 100644 --- a/src/receive_imf/receive_imf_tests.rs +++ b/src/receive_imf/receive_imf_tests.rs @@ -1970,18 +1970,23 @@ Message content", async fn test_unencrypted_doesnt_goto_self_chat() -> Result<()> { let mut tcm = TestContextManager::new(); let t = &tcm.alice().await; + let mut chat_id = None; - for to in [ + for (i, to) in [ + "", "", "alice@example.org, alice@example.org", "hidden-recipients:;", - ] { + ] + .iter() + .enumerate() + { receive_imf( t, format!( "Subject: s Chat-Version: 1.0 -Message-ID: +Message-ID: To: {to} From: @@ -1996,9 +2001,14 @@ Your server is hacked. Have a nice day!" assert_ne!(msg.chat_id, t.get_self_chat().await.id); assert_eq!(msg.from_id, ContactId::SELF); assert_eq!(msg.to_id, ContactId::SELF); - let chat = Chat::load_from_db(t, msg.chat_id).await?; - assert_eq!(chat.typ, Chattype::Group); - assert!(!chat.is_encrypted(t).await?); + if let Some(chat_id) = chat_id { + assert_eq!(msg.chat_id, chat_id); + } else { + chat_id = Some(msg.chat_id); + let chat = Chat::load_from_db(t, msg.chat_id).await?; + assert_eq!(chat.typ, Chattype::Group); + assert!(!chat.is_encrypted(t).await?); + } } Ok(()) }