Skip to content

Commit aed8d8c

Browse files
committed
feat: Send webxdc status updates in InBroadcast-s (#7679)
They are already applied by the broadcast owner and other subscriber's devices, no changes are needed on the receiver side.
1 parent dec6d7f commit aed8d8c

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

src/chat.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2693,7 +2693,9 @@ async fn prepare_send_msg(
26932693
.unwrap_or_default(),
26942694
_ => false,
26952695
};
2696-
if let Some(reason) = chat.why_cant_send_ex(context, &skip_fn).await? {
2696+
if msg.param.get_cmd() == SystemMessage::WebxdcStatusUpdate {
2697+
// Already checked in `send_webxdc_status_update_struct()`.
2698+
} else if let Some(reason) = chat.why_cant_send_ex(context, &skip_fn).await? {
26972699
bail!("Cannot send to {chat_id}: {reason}");
26982700
}
26992701

src/webxdc.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use serde_json::Value;
3434
use sha2::{Digest, Sha256};
3535
use tokio::{fs::File, io::BufReader};
3636

37-
use crate::chat::{self, Chat};
37+
use crate::chat::{self, CantSendReason, Chat};
3838
use crate::constants::Chattype;
3939
use crate::contact::ContactId;
4040
use crate::context::Context;
@@ -534,9 +534,14 @@ impl Context {
534534
let chat = Chat::load_from_db(self, chat_id)
535535
.await
536536
.with_context(|| format!("Failed to load chat {chat_id} from the database"))?;
537-
if let Some(reason) = chat.why_cant_send(self).await.with_context(|| {
538-
format!("Failed to check if webxdc update can be sent to chat {chat_id}")
539-
})? {
537+
let skip_fn = |reason: &CantSendReason| *reason == CantSendReason::InBroadcast;
538+
if let Some(reason) = chat
539+
.why_cant_send_ex(self, &skip_fn)
540+
.await
541+
.with_context(|| {
542+
format!("Failed to check if webxdc update can be sent to chat {chat_id}")
543+
})?
544+
{
540545
bail!("Cannot send to {chat_id}: {reason}.");
541546
}
542547

src/webxdc/webxdc_tests.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use crate::chat::{
1111
use crate::chatlist::Chatlist;
1212
use crate::config::Config;
1313
use crate::ephemeral;
14+
use crate::imex::{BackupProvider, get_backup};
1415
use crate::receive_imf::receive_imf;
16+
use crate::securejoin::get_securejoin_qr;
1517
use crate::test_utils::{E2EE_INFO_MSGS, TestContext, TestContextManager};
1618
use crate::tools::{self, SystemTime};
1719
use crate::{message, sql};
@@ -1588,6 +1590,55 @@ async fn test_webxdc_no_internet_access() -> Result<()> {
15881590
Ok(())
15891591
}
15901592

1593+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1594+
async fn test_in_broadcast_send_status_update() -> Result<()> {
1595+
let mut tcm = TestContextManager::new();
1596+
let alice = &tcm.alice().await;
1597+
let bob = &tcm.bob().await;
1598+
1599+
let alice_chat_id = create_broadcast(alice, "bc".to_string()).await?;
1600+
let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await?;
1601+
let bob_chat_id = tcm.exec_securejoin_qr(bob, alice, &qr).await;
1602+
1603+
let alice_instance = send_webxdc_instance(alice, alice_chat_id).await?;
1604+
let sent_msg = alice.pop_sent_msg().await;
1605+
let bob_instance = bob.recv_msg(&sent_msg).await;
1606+
assert_eq!(bob_instance.chat_id, bob_chat_id);
1607+
1608+
let provider = BackupProvider::prepare(bob).await?;
1609+
let bob1 = &tcm.unconfigured().await;
1610+
get_backup(bob1, provider.qr()).await?;
1611+
1612+
bob.send_webxdc_status_update(bob_instance.id, r#"{"payload":42}"#)
1613+
.await?;
1614+
bob.flush_status_updates().await?;
1615+
let sent_msg = bob.pop_sent_msg().await;
1616+
1617+
alice.recv_msg_trash(&sent_msg).await;
1618+
assert_eq!(
1619+
alice
1620+
.get_webxdc_status_updates(alice_instance.id, StatusUpdateSerial(0))
1621+
.await?,
1622+
r#"[{"payload":42,"serial":1,"max_serial":1}]"#
1623+
);
1624+
1625+
bob1.recv_msg_trash(&sent_msg).await;
1626+
assert_eq!(
1627+
bob1.get_webxdc_status_updates(bob_instance.id, StatusUpdateSerial(0))
1628+
.await?,
1629+
r#"[{"payload":42,"serial":1,"max_serial":1}]"#
1630+
);
1631+
1632+
// Non-subscriber's status updates are rejected.
1633+
let alice_bob_id = alice.add_or_lookup_contact_id(bob).await;
1634+
remove_contact_from_chat(alice, alice_chat_id, alice_bob_id).await?;
1635+
alice.pop_sent_msg().await;
1636+
let status =
1637+
helper_send_receive_status_update(bob, alice, &bob_instance, &alice_instance).await?;
1638+
assert_eq!(status, r#"[{"payload":42,"serial":1,"max_serial":1}]"#);
1639+
Ok(())
1640+
}
1641+
15911642
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
15921643
async fn test_webxdc_chatlist_summary() -> Result<()> {
15931644
let t = TestContext::new_alice().await;

0 commit comments

Comments
 (0)