Skip to content

Commit 22b9308

Browse files
authored
feat: update.href api (#6248)
add `update.href` property option to update objects send via `Context::send_webxdc_status_update()`. when set together with `update.info`, UI can implement the info message as a link that is passed to the webxdc via `window.location.href`. for that purpose, UI will read the link back from `Message::get_webxdc_href()`. Practically, this allows e.g. an calendar.xdc to emits clickable update messages opening the calendar at the correct date. closes #6219 documentation at webxdc/website#90
1 parent 1f0a12a commit 22b9308

File tree

5 files changed

+103
-11
lines changed

5 files changed

+103
-11
lines changed

deltachat-ffi/deltachat.h

+18
Original file line numberDiff line numberDiff line change
@@ -4518,6 +4518,24 @@ int dc_msg_get_info_type (const dc_msg_t* msg);
45184518
#define DC_INFO_INVALID_UNENCRYPTED_MAIL 13
45194519
#define DC_INFO_WEBXDC_INFO_MESSAGE 32
45204520

4521+
4522+
/**
4523+
* Get link attached to an webxdc info message.
4524+
* The info message needs to be of type DC_INFO_WEBXDC_INFO_MESSAGE.
4525+
*
4526+
* Typically, this is used to set `document.location.href` in JS land.
4527+
*
4528+
* Webxdc apps can define the link by setting `update.href` when sending and update,
4529+
* see dc_send_webxdc_status_update().
4530+
*
4531+
* @memberof dc_msg_t
4532+
* @param msg The info message object.
4533+
* Not: the webxdc instance.
4534+
* @return The link to be set to `document.location.href` in JS land.
4535+
* Returns NULL if there is no link attached to the info message and on errors.
4536+
*/
4537+
char* dc_msg_get_webxdc_href (const dc_msg_t* msg);
4538+
45214539
/**
45224540
* Check if a message is still in creation. A message is in creation between
45234541
* the calls to dc_prepare_msg() and dc_send_msg().

deltachat-ffi/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3687,6 +3687,17 @@ pub unsafe extern "C" fn dc_msg_get_info_type(msg: *mut dc_msg_t) -> libc::c_int
36873687
ffi_msg.message.get_info_type() as libc::c_int
36883688
}
36893689

3690+
#[no_mangle]
3691+
pub unsafe extern "C" fn dc_msg_get_webxdc_href(msg: *mut dc_msg_t) -> *mut libc::c_char {
3692+
if msg.is_null() {
3693+
eprintln!("ignoring careless call to dc_msg_get_webxdc_href()");
3694+
return "".strdup();
3695+
}
3696+
3697+
let ffi_msg = &*msg;
3698+
ffi_msg.message.get_webxdc_href().strdup()
3699+
}
3700+
36903701
#[no_mangle]
36913702
pub unsafe extern "C" fn dc_msg_is_increation(msg: *mut dc_msg_t) -> libc::c_int {
36923703
if msg.is_null() {

src/debug_logging.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub async fn debug_logging_loop(context: &Context, events: Receiver<DebugEventLo
6060
"time": time,
6161
}),
6262
info: None,
63+
href: None,
6364
summary: None,
6465
document: None,
6566
uid: None,

src/webxdc.rs

+72-11
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ pub struct StatusUpdateItem {
165165
#[serde(skip_serializing_if = "Option::is_none")]
166166
pub info: Option<String>,
167167

168+
/// Optional link the info message will point to.
169+
/// Used to set `window.location.href` in JS land.
170+
#[serde(skip_serializing_if = "Option::is_none")]
171+
pub href: Option<String>,
172+
168173
/// The new name of the editing document.
169174
/// This is not needed if the webxdc doesn't edit documents.
170175
#[serde(skip_serializing_if = "Option::is_none")]
@@ -353,19 +358,22 @@ impl Context {
353358

354359
if can_info_msg {
355360
if let Some(ref info) = status_update_item.info {
356-
if let Some(info_msg_id) = self
361+
let info_msg_id = self
357362
.get_overwritable_info_msg_id(&instance, from_id)
358-
.await?
359-
{
360-
chat::update_msg_text_and_timestamp(
361-
self,
362-
instance.chat_id,
363-
info_msg_id,
364-
info.as_str(),
365-
timestamp,
366-
)
367363
.await?;
368-
notify_msg_id = info_msg_id;
364+
365+
if info_msg_id.is_some() && status_update_item.href.is_none() {
366+
if let Some(info_msg_id) = info_msg_id {
367+
chat::update_msg_text_and_timestamp(
368+
self,
369+
instance.chat_id,
370+
info_msg_id,
371+
info.as_str(),
372+
timestamp,
373+
)
374+
.await?;
375+
notify_msg_id = info_msg_id;
376+
}
369377
} else {
370378
notify_msg_id = chat::add_info_msg_with_cmd(
371379
self,
@@ -380,6 +388,12 @@ impl Context {
380388
.await?;
381389
}
382390
notify_text = info.to_string();
391+
392+
if let Some(href) = status_update_item.href {
393+
let mut notify_msg = Message::load_from_db(self, notify_msg_id).await?;
394+
notify_msg.param.set(Param::Arg, href);
395+
notify_msg.update_param(self).await?;
396+
}
383397
}
384398
}
385399

@@ -944,6 +958,15 @@ impl Message {
944958
let hash = Sha256::digest(data.as_bytes());
945959
Ok(format!("{:x}", hash))
946960
}
961+
962+
/// Get link attached to an info message.
963+
///
964+
/// The info message needs to be of type SystemMessage::WebxdcInfoMessage.
965+
/// Typically, this is used to start the corresponding webxdc app
966+
/// with `window.location.href` set in JS land.
967+
pub fn get_webxdc_href(&self) -> Option<String> {
968+
self.param.get(Param::Arg).map(|href| href.to_string())
969+
}
947970
}
948971

949972
#[cfg(test)]
@@ -1457,6 +1480,7 @@ mod tests {
14571480
StatusUpdateItem {
14581481
payload: json!({"foo": "bar"}),
14591482
info: None,
1483+
href: None,
14601484
document: None,
14611485
summary: None,
14621486
uid: Some("iecie2Ze".to_string()),
@@ -1482,6 +1506,7 @@ mod tests {
14821506
StatusUpdateItem {
14831507
payload: json!({"nothing": "this should be ignored"}),
14841508
info: None,
1509+
href: None,
14851510
document: None,
14861511
summary: None,
14871512
uid: Some("iecie2Ze".to_string()),
@@ -1516,6 +1541,7 @@ mod tests {
15161541
StatusUpdateItem {
15171542
payload: json!({"foo2": "bar2"}),
15181543
info: None,
1544+
href: None,
15191545
document: None,
15201546
summary: None,
15211547
uid: None,
@@ -1536,6 +1562,7 @@ mod tests {
15361562
StatusUpdateItem {
15371563
payload: Value::Bool(true),
15381564
info: None,
1565+
href: None,
15391566
document: None,
15401567
summary: None,
15411568
uid: None,
@@ -3110,4 +3137,38 @@ sth_for_the = "future""#
31103137

31113138
Ok(())
31123139
}
3140+
3141+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3142+
async fn test_webxdc_href() -> Result<()> {
3143+
let mut tcm = TestContextManager::new();
3144+
let alice = tcm.alice().await;
3145+
let bob = tcm.bob().await;
3146+
3147+
let grp_id = alice
3148+
.create_group_with_members(ProtectionStatus::Unprotected, "grp", &[&bob])
3149+
.await;
3150+
let instance = send_webxdc_instance(&alice, grp_id).await?;
3151+
let sent1 = alice.pop_sent_msg().await;
3152+
3153+
alice
3154+
.send_webxdc_status_update(
3155+
instance.id,
3156+
r##"{"payload": "my deeplink data", "info": "my move!", "href": "#foobar"}"##,
3157+
"d",
3158+
)
3159+
.await?;
3160+
alice.flush_status_updates().await?;
3161+
let sent2 = alice.pop_sent_msg().await;
3162+
let info_msg = alice.get_last_msg().await;
3163+
assert!(info_msg.is_info());
3164+
assert_eq!(info_msg.get_webxdc_href(), Some("#foobar".to_string()));
3165+
3166+
bob.recv_msg(&sent1).await;
3167+
bob.recv_msg_trash(&sent2).await;
3168+
let info_msg = bob.get_last_msg().await;
3169+
assert!(info_msg.is_info());
3170+
assert_eq!(info_msg.get_webxdc_href(), Some("#foobar".to_string()));
3171+
3172+
Ok(())
3173+
}
31133174
}

src/webxdc/maps_integration.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub(crate) async fn intercept_get_updates(
146146
item: StatusUpdateItem {
147147
payload: serde_json::to_value(location_item)?,
148148
info: None,
149+
href: None,
149150
document: None,
150151
summary: None,
151152
uid: None,

0 commit comments

Comments
 (0)