Skip to content

Commit c6dd035

Browse files
committed
feat: add webxdc limits api
1 parent ff3efaf commit c6dd035

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

deltachat-ffi/deltachat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4203,6 +4203,10 @@ char* dc_msg_get_webxdc_blob (const dc_msg_t* msg, const char*
42034203
* currently, this is only true for encrypted Webxdc's in the self chat
42044204
* that have requested internet access in the manifest.
42054205
* - self_addr: address to be used for `window.webxdc.selfAddr` in JS land.
4206+
* - send_update_interval: Milliseconds to wait before calling `sendUpdate()` again since the last call.
4207+
* Should be exposed to `webxdc.sendUpdateInterval` in JS land.
4208+
* - send_update_max_size: Maximum number of bytes accepted for a serialized update object.
4209+
+ Should be exposed to `webxdc.sendUpdateMaxSize` in JS land.
42064210
*
42074211
* @memberof dc_msg_t
42084212
* @param msg The webxdc instance.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ pub struct WebxdcMessageInfo {
3737
internet_access: bool,
3838
/// Address to be used for `window.webxdc.selfAddr` in JS land.
3939
self_addr: String,
40+
/// Milliseconds to wait before calling `sendUpdate()` again since the last call.
41+
/// Should be exposed to `window.sendUpdateInterval` in JS land.
42+
send_update_interval: usize,
43+
/// Maximum number of bytes accepted for a serialized update object.
44+
/// Should be exposed to `window.sendUpdateMaxSize` in JS land.
45+
send_update_max_size: usize,
4046
}
4147

4248
impl WebxdcMessageInfo {
@@ -53,6 +59,8 @@ impl WebxdcMessageInfo {
5359
source_code_url,
5460
internet_access,
5561
self_addr,
62+
send_update_interval,
63+
send_update_max_size,
5664
} = message.get_webxdc_info(context).await?;
5765

5866
Ok(Self {
@@ -63,6 +71,8 @@ impl WebxdcMessageInfo {
6371
source_code_url: maybe_empty_string_to_option(source_code_url),
6472
internet_access,
6573
self_addr,
74+
send_update_interval,
75+
send_update_max_size,
6676
})
6777
}
6878
}

deltachat-ratelimit/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ impl Ratelimit {
9090
pub fn until_can_send(&self) -> Duration {
9191
self.until_can_send_at(SystemTime::now())
9292
}
93+
94+
/// Returns minimum possible update interval in milliseconds.
95+
pub fn update_interval(&self) -> usize {
96+
(self.window.as_millis() as f64 / self.quota) as usize
97+
}
9398
}
9499

95100
#[cfg(test)]
@@ -102,6 +107,7 @@ mod tests {
102107

103108
let mut ratelimit = Ratelimit::new_at(Duration::new(60, 0), 3.0, now);
104109
assert!(ratelimit.can_send_at(now));
110+
assert_eq!(ratelimit.update_interval(), 20_000);
105111

106112
// Send burst of 3 messages.
107113
ratelimit.send_at(now);

deltachat-rpc-client/tests/test_webxdc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def test_webxdc(acfactory) -> None:
2525
"sourceCodeUrl": None,
2626
"summary": None,
2727
"selfAddr": webxdc_info["selfAddr"],
28+
"sendUpdateInterval": 1000,
29+
"sendUpdateMaxSize": 18874368,
2830
}
2931

3032
status_updates = message.get_webxdc_status_updates()

src/webxdc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::events::EventType;
4040
use crate::key::{load_self_public_key, DcKey};
4141
use crate::message::{Message, MessageState, MsgId, Viewtype};
4242
use crate::mimefactory::wrapped_base64_encode;
43+
use crate::mimefactory::RECOMMENDED_FILE_SIZE;
4344
use crate::mimeparser::SystemMessage;
4445
use crate::param::Param;
4546
use crate::param::Params;
@@ -105,6 +106,14 @@ pub struct WebxdcInfo {
105106

106107
/// Address to be used for `window.webxdc.selfAddr` in JS land.
107108
pub self_addr: String,
109+
110+
/// Milliseconds to wait before calling `sendUpdate()` again since the last call.
111+
/// Should be exposed to `window.sendUpdateInterval` in JS land.
112+
pub send_update_interval: usize,
113+
114+
/// Maximum number of bytes accepted for a serialized update object.
115+
/// Should be exposed to `window.sendUpdateMaxSize` in JS land.
116+
pub send_update_max_size: usize,
108117
}
109118

110119
/// Status Update ID.
@@ -946,6 +955,8 @@ impl Message {
946955
},
947956
internet_access,
948957
self_addr,
958+
send_update_interval: context.ratelimit.read().await.update_interval(),
959+
send_update_max_size: RECOMMENDED_FILE_SIZE as usize,
949960
})
950961
}
951962

@@ -2258,6 +2269,8 @@ sth_for_the = "future""#
22582269
let info = instance.get_webxdc_info(&t).await?;
22592270
assert_eq!(info.name, "minimal.xdc");
22602271
assert_eq!(info.icon, WEBXDC_DEFAULT_ICON.to_string());
2272+
assert_eq!(info.send_update_interval, 10000);
2273+
assert_eq!(info.send_update_max_size, RECOMMENDED_FILE_SIZE as usize);
22612274

22622275
let mut instance = create_webxdc_instance(
22632276
&t,

0 commit comments

Comments
 (0)