Skip to content

Commit e42b492

Browse files
committed
feat: webxdc sending contexts
1 parent 8292495 commit e42b492

4 files changed

Lines changed: 96 additions & 1 deletion

File tree

deltachat-ffi/deltachat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4234,6 +4234,10 @@ char* dc_msg_get_webxdc_blob (const dc_msg_t* msg, const char*
42344234
* true if the Webxdc should get internet access;
42354235
* this is the case i.e. for experimental maps integration.
42364236
* - self_addr: address to be used for `window.webxdc.selfAddr` in JS land.
4237+
* - app_sender_addr: address of the peer who initially shared the webxdc in the chat.
4238+
* Can be compared to self_addr to determine whether the app runs for the sender or a receiver.
4239+
* - can_only_send_updates_to_app_sender: true if updates sent by the local user
4240+
* will only be seen by the app sender.
42374241
* - send_update_interval: Milliseconds to wait before calling `sendUpdate()` again since the last call.
42384242
* Should be exposed to `webxdc.sendUpdateInterval` in JS land.
42394243
* - send_update_max_size: Maximum number of bytes accepted for a serialized update object.

deltachat-jsonrpc/src/api/types/webxdc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ pub struct WebxdcMessageInfo {
3737
internet_access: bool,
3838
/// Address to be used for `window.webxdc.selfAddr` in JS land.
3939
self_addr: String,
40+
/// Address of the peer who initially shared the webxdc in the chat.
41+
app_sender_addr: String,
42+
/// True if updates sent by the local user
43+
/// will only be seen by the app sender.
44+
can_only_send_updates_to_app_sender: bool,
4045
/// Milliseconds to wait before calling `sendUpdate()` again since the last call.
4146
/// Should be exposed to `window.sendUpdateInterval` in JS land.
4247
send_update_interval: usize,
@@ -60,6 +65,8 @@ impl WebxdcMessageInfo {
6065
request_integration: _,
6166
internet_access,
6267
self_addr,
68+
app_sender_addr,
69+
can_only_send_updates_to_app_sender,
6370
send_update_interval,
6471
send_update_max_size,
6572
} = message.get_webxdc_info(context).await?;
@@ -72,6 +79,8 @@ impl WebxdcMessageInfo {
7279
source_code_url: maybe_empty_string_to_option(source_code_url),
7380
internet_access,
7481
self_addr,
82+
app_sender_addr,
83+
can_only_send_updates_to_app_sender,
7584
send_update_interval,
7685
send_update_max_size,
7786
})

src/webxdc.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use tokio::{fs::File, io::BufReader};
3636

3737
use crate::chat::{self, Chat};
3838
use crate::constants::Chattype;
39-
use crate::contact::ContactId;
39+
use crate::contact::{Contact, ContactId};
4040
use crate::context::Context;
4141
use crate::events::EventType;
4242
use crate::key::self_fingerprint;
@@ -111,6 +111,15 @@ pub struct WebxdcInfo {
111111
/// Address to be used for `window.webxdc.selfAddr` in JS land.
112112
pub self_addr: String,
113113

114+
/// Address of the peer who initially shared the webxdc in the chat.
115+
/// Can be compared to `self_addr` to determine
116+
/// whether the app runs for the sender or a receiver.
117+
pub app_sender_addr: String,
118+
119+
/// `true` if updates sent by the local user
120+
/// will only be seen by the app sender.
121+
pub can_only_send_updates_to_app_sender: bool,
122+
114123
/// Milliseconds to wait before calling `sendUpdate()` again since the last call.
115124
/// Should be exposed to `window.sendUpdateInterval` in JS land.
116125
pub send_update_interval: usize,
@@ -923,6 +932,12 @@ impl Message {
923932
let internet_access = is_integrated;
924933

925934
let self_addr = self.get_webxdc_self_addr(context).await?;
935+
let app_sender_addr = self.get_webxdc_app_sender_addr(context).await?;
936+
937+
let chat = Chat::load_from_db(context, self.chat_id)
938+
.await
939+
.with_context(|| "Failed to load chat from the database")?;
940+
let can_only_send_updates_to_app_sender = chat.typ == Chattype::InBroadcast;
926941

927942
Ok(WebxdcInfo {
928943
name: if let Some(name) = manifest.name {
@@ -961,6 +976,8 @@ impl Message {
961976
request_integration,
962977
internet_access,
963978
self_addr,
979+
app_sender_addr,
980+
can_only_send_updates_to_app_sender,
964981
send_update_interval: context.ratelimit.read().await.update_interval(),
965982
send_update_max_size: RECOMMENDED_FILE_SIZE as usize,
966983
})
@@ -973,6 +990,29 @@ impl Message {
973990
Ok(format!("{hash:x}"))
974991
}
975992

993+
/// Computes the webxdc address of the message sender.
994+
async fn get_webxdc_app_sender_addr(&self, context: &Context) -> Result<String> {
995+
// UNDEFINED may be preset during drafts or tests, will be SELF on sending
996+
let fingerprint = if self.from_id == ContactId::SELF || self.from_id == ContactId::UNDEFINED
997+
{
998+
self_fingerprint(context).await?.to_string()
999+
} else {
1000+
let contact = Contact::get_by_id(context, self.from_id).await?;
1001+
contact
1002+
.fingerprint()
1003+
.with_context(|| {
1004+
format!(
1005+
"No fingerprint for contact {} (webxdc sender)",
1006+
self.from_id
1007+
)
1008+
})?
1009+
.hex()
1010+
};
1011+
let data = format!("{}-{}", fingerprint, self.rfc724_mid);
1012+
let hash = Sha256::digest(data.as_bytes());
1013+
Ok(format!("{hash:x}"))
1014+
}
1015+
9761016
/// Get link attached to an info message.
9771017
///
9781018
/// The info message needs to be of type SystemMessage::WebxdcInfoMessage.

src/webxdc/webxdc_tests.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::chatlist::Chatlist;
1212
use crate::config::Config;
1313
use crate::ephemeral;
1414
use crate::receive_imf::receive_imf;
15+
use crate::securejoin::get_securejoin_qr;
1516
use crate::test_utils::{E2EE_INFO_MSGS, TestContext, TestContextManager};
1617
use crate::tools::{self, SystemTime};
1718
use crate::{message, sql};
@@ -2195,3 +2196,44 @@ async fn test_self_addr_consistency() -> Result<()> {
21952196
assert_eq!(db_msg.get_webxdc_self_addr(alice).await?, self_addr);
21962197
Ok(())
21972198
}
2199+
2200+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2201+
async fn test_webxdc_info_fields() -> Result<()> {
2202+
let mut tcm = TestContextManager::new();
2203+
let alice = &tcm.alice().await;
2204+
let bob = &tcm.bob().await;
2205+
2206+
// Alice sends webxdc in a group chat
2207+
let alice_grp = alice.create_group_with_members("Group", &[&bob]).await;
2208+
let alice_instance = send_webxdc_instance(alice, alice_grp).await?;
2209+
let sent1 = alice.pop_sent_msg().await;
2210+
let alice_info = alice_instance.get_webxdc_info(alice).await?;
2211+
assert_eq!(alice_info.self_addr, alice_info.app_sender_addr);
2212+
assert!(!alice_info.can_only_send_updates_to_app_sender);
2213+
2214+
// Bob receives group webxdc
2215+
let bob_instance = bob.recv_msg(&sent1).await;
2216+
let bob_info = bob_instance.get_webxdc_info(bob).await?;
2217+
assert_ne!(bob_info.self_addr, bob_info.app_sender_addr);
2218+
assert_eq!(bob_info.app_sender_addr, alice_info.self_addr);
2219+
assert!(!bob_info.can_only_send_updates_to_app_sender);
2220+
2221+
// Alice sends webxdc to broadcast channel
2222+
let alice_chat_id = create_broadcast(alice, "Broadcast".to_string()).await?;
2223+
let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await.unwrap();
2224+
tcm.exec_securejoin_qr(bob, alice, &qr).await;
2225+
let alice_instance = send_webxdc_instance(&alice, alice_chat_id).await?;
2226+
let sent3 = alice.pop_sent_msg().await;
2227+
let alice_info = alice_instance.get_webxdc_info(&alice).await?;
2228+
assert_eq!(alice_info.self_addr, alice_info.app_sender_addr);
2229+
assert!(!alice_info.can_only_send_updates_to_app_sender);
2230+
2231+
// Bob receives broadcast webxdc
2232+
let bob_instance = bob.recv_msg(&sent3).await;
2233+
let bob_info = bob_instance.get_webxdc_info(bob).await?;
2234+
assert_ne!(bob_info.self_addr, bob_info.app_sender_addr);
2235+
assert_eq!(bob_info.app_sender_addr, alice_info.app_sender_addr);
2236+
assert!(bob_info.can_only_send_updates_to_app_sender);
2237+
2238+
Ok(())
2239+
}

0 commit comments

Comments
 (0)