Skip to content

Commit e98e87d

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

4 files changed

Lines changed: 131 additions & 3 deletions

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: 42 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,13 @@ 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 =
941+
chat.typ == Chattype::InBroadcast || chat.typ == Chattype::OutBroadcast;
926942

927943
Ok(WebxdcInfo {
928944
name: if let Some(name) = manifest.name {
@@ -961,6 +977,8 @@ impl Message {
961977
request_integration,
962978
internet_access,
963979
self_addr,
980+
app_sender_addr,
981+
can_only_send_updates_to_app_sender,
964982
send_update_interval: context.ratelimit.read().await.update_interval(),
965983
send_update_max_size: RECOMMENDED_FILE_SIZE as usize,
966984
})
@@ -973,6 +991,29 @@ impl Message {
973991
Ok(format!("{hash:x}"))
974992
}
975993

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

src/webxdc/webxdc_tests.rs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use serde_json::json;
55

66
use super::*;
77
use crate::chat::{
8-
ChatId, MuteDuration, add_contact_to_chat, create_broadcast, create_group, forward_msgs,
9-
remove_contact_from_chat, resend_msgs, send_msg, send_text_msg, set_muted,
8+
ChatId, MuteDuration, add_contact_to_chat, add_contact_to_chat_ex, create_broadcast,
9+
create_group, forward_msgs, remove_contact_from_chat, resend_msgs, send_msg, send_text_msg,
10+
set_muted,
1011
};
1112
use crate::chatlist::Chatlist;
1213
use crate::config::Config;
1314
use crate::ephemeral;
1415
use crate::receive_imf::receive_imf;
16+
use crate::sync::Sync;
1517
use crate::test_utils::{E2EE_INFO_MSGS, TestContext, TestContextManager};
1618
use crate::tools::{self, SystemTime};
1719
use crate::{message, sql};
@@ -2195,3 +2197,75 @@ async fn test_self_addr_consistency() -> Result<()> {
21952197
assert_eq!(db_msg.get_webxdc_self_addr(alice).await?, self_addr);
21962198
Ok(())
21972199
}
2200+
2201+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2202+
async fn test_webxdc_info_fields() -> Result<()> {
2203+
let mut tcm = TestContextManager::new();
2204+
let alice = tcm.alice().await;
2205+
let bob = tcm.bob().await;
2206+
2207+
alice.set_config(Config::Displayname, Some("alice")).await?;
2208+
bob.set_config(Config::Displayname, Some("bob")).await?;
2209+
2210+
// Alice sends webxdc in a group chat
2211+
let alice_grp = create_group(&alice, "Alice & Bob").await?;
2212+
add_contact_to_chat(
2213+
&alice,
2214+
alice_grp,
2215+
alice.add_or_lookup_contact_id(&bob).await,
2216+
)
2217+
.await?;
2218+
let alice_instance = send_webxdc_instance(&alice, alice_grp).await?;
2219+
let sent = alice.pop_sent_msg().await;
2220+
2221+
let alice_info = alice_instance.get_webxdc_info(&alice).await?;
2222+
assert_eq!(alice_info.self_addr, alice_info.app_sender_addr);
2223+
assert!(!alice_info.can_only_send_updates_to_app_sender);
2224+
2225+
// Bob receives it
2226+
let bob_instance = bob.recv_msg(&sent).await;
2227+
let bob_info = bob_instance.get_webxdc_info(&bob).await?;
2228+
2229+
assert_ne!(bob_info.self_addr, bob_info.app_sender_addr);
2230+
assert_eq!(bob_info.app_sender_addr, alice_info.app_sender_addr);
2231+
assert!(!bob_info.can_only_send_updates_to_app_sender);
2232+
2233+
// Now testing broadcast chat
2234+
let alice_broadcast = create_broadcast(&alice, "Broadcast".to_string()).await?;
2235+
add_contact_to_chat_ex(
2236+
&alice,
2237+
Sync::Sync,
2238+
alice_broadcast,
2239+
alice.add_or_lookup_contact_id(&bob).await,
2240+
true,
2241+
)
2242+
.await?;
2243+
let sent_join = alice.pop_sent_msg().await;
2244+
2245+
let alice_broadcast_instance = send_webxdc_instance(&alice, alice_broadcast).await?;
2246+
let sent_app_broadcast = alice.pop_sent_msg().await;
2247+
2248+
let alice_broadcast_info = alice_broadcast_instance.get_webxdc_info(&alice).await?;
2249+
assert_eq!(
2250+
alice_broadcast_info.self_addr,
2251+
alice_broadcast_info.app_sender_addr
2252+
);
2253+
assert!(alice_broadcast_info.can_only_send_updates_to_app_sender);
2254+
2255+
// Bob receives broadcast webxdc
2256+
bob.recv_msg(&sent_join).await;
2257+
let bob_broadcast_instance = bob.recv_msg(&sent_app_broadcast).await;
2258+
let bob_broadcast_info = bob_broadcast_instance.get_webxdc_info(&bob).await?;
2259+
2260+
assert_ne!(
2261+
bob_broadcast_info.self_addr,
2262+
bob_broadcast_info.app_sender_addr
2263+
);
2264+
assert_eq!(
2265+
bob_broadcast_info.app_sender_addr,
2266+
alice_broadcast_info.app_sender_addr
2267+
);
2268+
assert!(bob_broadcast_info.can_only_send_updates_to_app_sender);
2269+
2270+
Ok(())
2271+
}

0 commit comments

Comments
 (0)