diff --git a/src/receive_imf.rs b/src/receive_imf.rs index e1f7ce283..6f84de655 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -769,6 +769,18 @@ async fn add_parts( info!(context, "Message is an MDN (TRASH).",); } + // Try to assign to a chat based on Chat-Group-ID. + if chat_id.is_none() { + if let Some(grpid) = mime_parser.get_chat_group_id() { + if let Some((id, _protected, blocked)) = + chat::get_chat_id_by_grpid(context, grpid).await? + { + chat_id = Some(id); + chat_id_blocked = blocked; + } + } + } + if chat_id.is_none() { // try to assign to a chat based on In-Reply-To/References: @@ -1035,6 +1047,18 @@ async fn add_parts( chat_id = Some(DC_CHAT_ID_TRASH); } + // Try to assign to a chat based on Chat-Group-ID. + if chat_id.is_none() { + if let Some(grpid) = mime_parser.get_chat_group_id() { + if let Some((id, _protected, blocked)) = + chat::get_chat_id_by_grpid(context, grpid).await? + { + chat_id = Some(id); + chat_id_blocked = blocked; + } + } + } + if chat_id.is_none() { // try to assign to a chat based on In-Reply-To/References: diff --git a/src/receive_imf/tests.rs b/src/receive_imf/tests.rs index 8e16a29cd..216a7ed94 100644 --- a/src/receive_imf/tests.rs +++ b/src/receive_imf/tests.rs @@ -2693,6 +2693,103 @@ Second thread."#; Ok(()) } +/// Test that `Chat-Group-ID` is preferred over `In-Reply-To` and `References`. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_chat_assignment_chat_group_id_preference() -> Result<()> { + let t = &TestContext::new_alice().await; + + receive_imf( + t, + br#"Subject: Hello +Chat-Group-ID: eJ_llQIXf0K +Chat-Group-Name: Group name +Chat-Version: 1.0 +Message-ID: +References: +Date: Fri, 28 May 2021 10:15:05 +0000 +From: Alice +To: Bob , +Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no +Content-Transfer-Encoding: quoted-printable + +Hello, I've just created a group for us."#, + false, + ) + .await?; + let group_msg = t.get_last_msg().await; + + receive_imf( + t, + br#"Subject: Hello +Chat-Version: 1.0 +Message-ID: +References: +Date: Fri, 28 May 2021 10:15:05 +0000 +From: Bob +To: Alice +Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no +Content-Transfer-Encoding: quoted-printable + +Hello from Bob in 1:1 chat."#, + false, + ) + .await?; + + // References and In-Reply-To point to a message + // already assigned to 1:1 chat, but Chat-Group-ID is + // a stronger signal to assign message to a group. + receive_imf( + t, + br#"Subject: Hello +Chat-Group-ID: eJ_llQIXf0K +Chat-Group-Name: Group name +Chat-Version: 1.0 +Message-ID: +In-Reply-To: +References: +Date: Fri, 28 May 2021 10:15:05 +0000 +From: Bob +To: Alice , +Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no +Content-Transfer-Encoding: quoted-printable + +Hello from Bob in a group."#, + false, + ) + .await?; + + let msg = t.get_last_msg().await; + assert_eq!(msg.text, "Hello from Bob in a group."); + assert_eq!(msg.chat_id, group_msg.chat_id); + + // Test outgoing message as well. + receive_imf( + t, + br#"Subject: Hello +Chat-Group-ID: eJ_llQIXf0K +Chat-Group-Name: Group name +Chat-Version: 1.0 +Message-ID: +In-Reply-To: +References: +Date: Fri, 28 May 2021 10:15:05 +0000 +From: Alice +To: Bob , +Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no +Content-Transfer-Encoding: quoted-printable + +Hello from Alice in a group."#, + false, + ) + .await?; + + let msg_outgoing = t.get_last_msg().await; + assert_eq!(msg_outgoing.text, "Hello from Alice in a group."); + assert_eq!(msg_outgoing.chat_id, group_msg.chat_id); + + Ok(()) +} + /// Test that read receipts don't create chats. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_read_receipts_dont_create_chats() -> Result<()> {