From a1e0b3516a2e85be7e1707d8b9936bc9dacc03bf Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 13 Jan 2025 15:31:39 +0100 Subject: [PATCH 1/5] feat: Set BccSelf to true when receiving a sync message --- src/sync.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/sync.rs b/src/sync.rs index d2671e22bc..7fbd2e1192 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -268,6 +268,17 @@ impl Context { .log_err(self) .ok(); } + + // Since there was a sync message, we know that there is a second device. + // Set BccSelf to true if it isn't already. + if !items.items.is_empty() && !self.get_config_bool(Config::BccSelf).await.unwrap_or(false) + { + self.set_config_ex(Sync::Nosync, Config::BccSelf, Some("1")) + .await + .log_err(self) + .ok(); + } + // TODO check that a test fails if we do this unconditionally } async fn add_qr_token(&self, token: &QrTokenData) -> Result<()> { @@ -578,6 +589,43 @@ mod tests { Ok(()) } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_send_sync_msg_enables_bccself() -> Result<()> { + let alice1 = TestContext::new_alice().await; + let alice2 = TestContext::new_alice().await; + + alice1.set_config_bool(Config::IsChatmail, true).await?; + alice2.set_config_bool(Config::IsChatmail, true).await?; + + // SyncMsgs defaults to true on real devices, but in tests it defaults to false, + // so we need to enable it + alice1.set_config_bool(Config::SyncMsgs, true).await?; + alice2.set_config_bool(Config::SyncMsgs, true).await?; + + alice1.set_config_bool(Config::BccSelf, true).await?; + + alice1 + .add_sync_item(SyncData::AddQrToken(QrTokenData { + invitenumber: "in".to_string(), + auth: "testtoken".to_string(), + grpid: None, + })) + .await?; + alice1.send_sync_msg().await?.unwrap(); + let sent_msg = alice1.pop_sent_sync_msg().await; + + // On chatmail accounts, BccSelf defaults to false. + // When receiving a sync message from another device, + // there obviously is a multi-device-setup, and BccSelf + // should be enabled. + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); + + alice2.recv_msg_trash(&sent_msg).await; + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true); + + Ok(()) + } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_bot_no_sync_msgs() -> Result<()> { let mut tcm = TestContextManager::new(); From a5877558aef1ab16f4b337b6f716cf60aaca5d7c Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 13 Jan 2025 16:40:51 +0100 Subject: [PATCH 2/5] Always do it when we receive a message from a second device, not just sync messages --- src/receive_imf.rs | 15 +++++++++++++++ src/sync.rs | 11 ----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index 16f5dfbea1..3885b86841 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -489,6 +489,21 @@ pub(crate) async fn receive_imf_inner( } } + if from_id == ContactId::SELF + && !context + .get_config_bool(Config::BccSelf) + .await + .unwrap_or(false) + { + // Since this is a message from another device, we know that there is another device. + // Set BccSelf to true. + context + .set_config_ex(Nosync, Config::BccSelf, Some("1")) + .await + .log_err(context) + .ok(); + } + if let Some(ref status_update) = mime_parser.webxdc_status_update { let can_info_msg; let instance = if mime_parser diff --git a/src/sync.rs b/src/sync.rs index 7fbd2e1192..0e55458c4f 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -268,17 +268,6 @@ impl Context { .log_err(self) .ok(); } - - // Since there was a sync message, we know that there is a second device. - // Set BccSelf to true if it isn't already. - if !items.items.is_empty() && !self.get_config_bool(Config::BccSelf).await.unwrap_or(false) - { - self.set_config_ex(Sync::Nosync, Config::BccSelf, Some("1")) - .await - .log_err(self) - .ok(); - } - // TODO check that a test fails if we do this unconditionally } async fn add_qr_token(&self, token: &QrTokenData) -> Result<()> { From 468f926baa42fbfaf8be96f6f68812e3ed9d6abf Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 13 Jan 2025 17:07:43 +0100 Subject: [PATCH 3/5] Improve test --- src/receive_imf/tests.rs | 62 ++++++++++++++++++++++++++++++++++++++++ src/sync.rs | 37 ------------------------ 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/receive_imf/tests.rs b/src/receive_imf/tests.rs index 60bbeb6801..6d8aa0847e 100644 --- a/src/receive_imf/tests.rs +++ b/src/receive_imf/tests.rs @@ -3323,6 +3323,68 @@ async fn test_accept_outgoing() -> Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_selfsent_msg_enables_bccself() -> Result<()> { + let mut tcm = TestContextManager::new(); + let alice1 = &tcm.alice().await; + let alice2 = &tcm.alice().await; + let bob = &tcm.bob().await; + + alice1.set_config_bool(Config::IsChatmail, true).await?; + alice2.set_config_bool(Config::IsChatmail, true).await?; + + // SyncMsgs defaults to true on real devices, but in tests it defaults to false, + // so we need to enable it + alice1.set_config_bool(Config::SyncMsgs, true).await?; + alice2.set_config_bool(Config::SyncMsgs, true).await?; + + alice1.set_config_bool(Config::BccSelf, true).await?; + + let chat = alice1.create_chat(&bob).await; + let sent_msg = alice1.send_text(chat.id, "Hello!").await; + + // On chatmail accounts, BccSelf defaults to false. + // When receiving a message from another device, + // there obviously is a multi-device-setup, and BccSelf + // should be enabled. + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); + alice2.recv_msg(&sent_msg).await; + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true); + + // Suppose that some bug disables BccSelf once more + alice2.set_config_bool(Config::BccSelf, false).await?; + + alice1 + .add_sync_item(crate::sync::SyncData::AddQrToken( + crate::sync::QrTokenData { + invitenumber: "in".to_string(), + auth: "testtoken".to_string(), + grpid: None, + }, + )) + .await?; + alice1.send_sync_msg().await?.unwrap(); + let sent_msg = alice1.pop_sent_sync_msg().await; + + // Again, BccSelf should be enabled when receiving the message. + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); + alice2.recv_msg_trash(&sent_msg).await; + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true); + + // However, merely receiving a message sent by the same device + // or by a contact must not enable BccSelf: + alice2.set_config_bool(Config::BccSelf, false).await?; + + let sent_msg = alice2.send_text(chat.id, "Hello!").await; + alice2.recv_msg_opt(&sent_msg).await; + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); + + tcm.send_recv(bob, alice1, "Hello back!").await; + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); + + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_outgoing_private_reply_multidevice() -> Result<()> { let mut tcm = TestContextManager::new(); diff --git a/src/sync.rs b/src/sync.rs index 0e55458c4f..d2671e22bc 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -578,43 +578,6 @@ mod tests { Ok(()) } - #[tokio::test(flavor = "multi_thread", worker_threads = 2)] - async fn test_send_sync_msg_enables_bccself() -> Result<()> { - let alice1 = TestContext::new_alice().await; - let alice2 = TestContext::new_alice().await; - - alice1.set_config_bool(Config::IsChatmail, true).await?; - alice2.set_config_bool(Config::IsChatmail, true).await?; - - // SyncMsgs defaults to true on real devices, but in tests it defaults to false, - // so we need to enable it - alice1.set_config_bool(Config::SyncMsgs, true).await?; - alice2.set_config_bool(Config::SyncMsgs, true).await?; - - alice1.set_config_bool(Config::BccSelf, true).await?; - - alice1 - .add_sync_item(SyncData::AddQrToken(QrTokenData { - invitenumber: "in".to_string(), - auth: "testtoken".to_string(), - grpid: None, - })) - .await?; - alice1.send_sync_msg().await?.unwrap(); - let sent_msg = alice1.pop_sent_sync_msg().await; - - // On chatmail accounts, BccSelf defaults to false. - // When receiving a sync message from another device, - // there obviously is a multi-device-setup, and BccSelf - // should be enabled. - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); - - alice2.recv_msg_trash(&sent_msg).await; - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true); - - Ok(()) - } - #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_bot_no_sync_msgs() -> Result<()> { let mut tcm = TestContextManager::new(); From c1e8cfa6f2c24b40906161a6daec07785281f320 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 13 Jan 2025 17:26:16 +0100 Subject: [PATCH 4/5] Revert "Improve test" This reverts commit 468f926baa42fbfaf8be96f6f68812e3ed9d6abf. --- src/receive_imf/tests.rs | 62 ---------------------------------------- src/sync.rs | 37 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 62 deletions(-) diff --git a/src/receive_imf/tests.rs b/src/receive_imf/tests.rs index 6d8aa0847e..60bbeb6801 100644 --- a/src/receive_imf/tests.rs +++ b/src/receive_imf/tests.rs @@ -3323,68 +3323,6 @@ async fn test_accept_outgoing() -> Result<()> { Ok(()) } -#[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn test_selfsent_msg_enables_bccself() -> Result<()> { - let mut tcm = TestContextManager::new(); - let alice1 = &tcm.alice().await; - let alice2 = &tcm.alice().await; - let bob = &tcm.bob().await; - - alice1.set_config_bool(Config::IsChatmail, true).await?; - alice2.set_config_bool(Config::IsChatmail, true).await?; - - // SyncMsgs defaults to true on real devices, but in tests it defaults to false, - // so we need to enable it - alice1.set_config_bool(Config::SyncMsgs, true).await?; - alice2.set_config_bool(Config::SyncMsgs, true).await?; - - alice1.set_config_bool(Config::BccSelf, true).await?; - - let chat = alice1.create_chat(&bob).await; - let sent_msg = alice1.send_text(chat.id, "Hello!").await; - - // On chatmail accounts, BccSelf defaults to false. - // When receiving a message from another device, - // there obviously is a multi-device-setup, and BccSelf - // should be enabled. - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); - alice2.recv_msg(&sent_msg).await; - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true); - - // Suppose that some bug disables BccSelf once more - alice2.set_config_bool(Config::BccSelf, false).await?; - - alice1 - .add_sync_item(crate::sync::SyncData::AddQrToken( - crate::sync::QrTokenData { - invitenumber: "in".to_string(), - auth: "testtoken".to_string(), - grpid: None, - }, - )) - .await?; - alice1.send_sync_msg().await?.unwrap(); - let sent_msg = alice1.pop_sent_sync_msg().await; - - // Again, BccSelf should be enabled when receiving the message. - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); - alice2.recv_msg_trash(&sent_msg).await; - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true); - - // However, merely receiving a message sent by the same device - // or by a contact must not enable BccSelf: - alice2.set_config_bool(Config::BccSelf, false).await?; - - let sent_msg = alice2.send_text(chat.id, "Hello!").await; - alice2.recv_msg_opt(&sent_msg).await; - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); - - tcm.send_recv(bob, alice1, "Hello back!").await; - assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); - - Ok(()) -} - #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_outgoing_private_reply_multidevice() -> Result<()> { let mut tcm = TestContextManager::new(); diff --git a/src/sync.rs b/src/sync.rs index d2671e22bc..0e55458c4f 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -578,6 +578,43 @@ mod tests { Ok(()) } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_send_sync_msg_enables_bccself() -> Result<()> { + let alice1 = TestContext::new_alice().await; + let alice2 = TestContext::new_alice().await; + + alice1.set_config_bool(Config::IsChatmail, true).await?; + alice2.set_config_bool(Config::IsChatmail, true).await?; + + // SyncMsgs defaults to true on real devices, but in tests it defaults to false, + // so we need to enable it + alice1.set_config_bool(Config::SyncMsgs, true).await?; + alice2.set_config_bool(Config::SyncMsgs, true).await?; + + alice1.set_config_bool(Config::BccSelf, true).await?; + + alice1 + .add_sync_item(SyncData::AddQrToken(QrTokenData { + invitenumber: "in".to_string(), + auth: "testtoken".to_string(), + grpid: None, + })) + .await?; + alice1.send_sync_msg().await?.unwrap(); + let sent_msg = alice1.pop_sent_sync_msg().await; + + // On chatmail accounts, BccSelf defaults to false. + // When receiving a sync message from another device, + // there obviously is a multi-device-setup, and BccSelf + // should be enabled. + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, false); + + alice2.recv_msg_trash(&sent_msg).await; + assert_eq!(alice2.get_config_bool(Config::BccSelf).await?, true); + + Ok(()) + } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_bot_no_sync_msgs() -> Result<()> { let mut tcm = TestContextManager::new(); From 51b9c21e8bcaeb65a90d3734a233313ea1dd00d8 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 13 Jan 2025 17:26:25 +0100 Subject: [PATCH 5/5] Revert "Always do it when we receive a message from a second device, not just sync messages" This reverts commit a5877558aef1ab16f4b337b6f716cf60aaca5d7c. --- src/receive_imf.rs | 15 --------------- src/sync.rs | 11 +++++++++++ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index 3885b86841..16f5dfbea1 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -489,21 +489,6 @@ pub(crate) async fn receive_imf_inner( } } - if from_id == ContactId::SELF - && !context - .get_config_bool(Config::BccSelf) - .await - .unwrap_or(false) - { - // Since this is a message from another device, we know that there is another device. - // Set BccSelf to true. - context - .set_config_ex(Nosync, Config::BccSelf, Some("1")) - .await - .log_err(context) - .ok(); - } - if let Some(ref status_update) = mime_parser.webxdc_status_update { let can_info_msg; let instance = if mime_parser diff --git a/src/sync.rs b/src/sync.rs index 0e55458c4f..7fbd2e1192 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -268,6 +268,17 @@ impl Context { .log_err(self) .ok(); } + + // Since there was a sync message, we know that there is a second device. + // Set BccSelf to true if it isn't already. + if !items.items.is_empty() && !self.get_config_bool(Config::BccSelf).await.unwrap_or(false) + { + self.set_config_ex(Sync::Nosync, Config::BccSelf, Some("1")) + .await + .log_err(self) + .ok(); + } + // TODO check that a test fails if we do this unconditionally } async fn add_qr_token(&self, token: &QrTokenData) -> Result<()> {