From c6dd03590c990c4352c0cf691222175d7b282345 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 25 Nov 2024 22:11:11 +0100 Subject: [PATCH] feat: add webxdc limits api --- deltachat-ffi/deltachat.h | 4 ++++ deltachat-jsonrpc/src/api/types/webxdc.rs | 10 ++++++++++ deltachat-ratelimit/src/lib.rs | 6 ++++++ deltachat-rpc-client/tests/test_webxdc.py | 2 ++ src/webxdc.rs | 13 +++++++++++++ 5 files changed, 35 insertions(+) diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 3f863ab5bb..55dd895bd5 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -4203,6 +4203,10 @@ char* dc_msg_get_webxdc_blob (const dc_msg_t* msg, const char* * currently, this is only true for encrypted Webxdc's in the self chat * that have requested internet access in the manifest. * - self_addr: address to be used for `window.webxdc.selfAddr` in JS land. + * - send_update_interval: Milliseconds to wait before calling `sendUpdate()` again since the last call. + * Should be exposed to `webxdc.sendUpdateInterval` in JS land. + * - send_update_max_size: Maximum number of bytes accepted for a serialized update object. ++ Should be exposed to `webxdc.sendUpdateMaxSize` in JS land. * * @memberof dc_msg_t * @param msg The webxdc instance. diff --git a/deltachat-jsonrpc/src/api/types/webxdc.rs b/deltachat-jsonrpc/src/api/types/webxdc.rs index 28f0f384b3..e942d7be2f 100644 --- a/deltachat-jsonrpc/src/api/types/webxdc.rs +++ b/deltachat-jsonrpc/src/api/types/webxdc.rs @@ -37,6 +37,12 @@ pub struct WebxdcMessageInfo { internet_access: bool, /// Address to be used for `window.webxdc.selfAddr` in JS land. self_addr: String, + /// Milliseconds to wait before calling `sendUpdate()` again since the last call. + /// Should be exposed to `window.sendUpdateInterval` in JS land. + send_update_interval: usize, + /// Maximum number of bytes accepted for a serialized update object. + /// Should be exposed to `window.sendUpdateMaxSize` in JS land. + send_update_max_size: usize, } impl WebxdcMessageInfo { @@ -53,6 +59,8 @@ impl WebxdcMessageInfo { source_code_url, internet_access, self_addr, + send_update_interval, + send_update_max_size, } = message.get_webxdc_info(context).await?; Ok(Self { @@ -63,6 +71,8 @@ impl WebxdcMessageInfo { source_code_url: maybe_empty_string_to_option(source_code_url), internet_access, self_addr, + send_update_interval, + send_update_max_size, }) } } diff --git a/deltachat-ratelimit/src/lib.rs b/deltachat-ratelimit/src/lib.rs index 351746adc9..497cc83bb2 100644 --- a/deltachat-ratelimit/src/lib.rs +++ b/deltachat-ratelimit/src/lib.rs @@ -90,6 +90,11 @@ impl Ratelimit { pub fn until_can_send(&self) -> Duration { self.until_can_send_at(SystemTime::now()) } + + /// Returns minimum possible update interval in milliseconds. + pub fn update_interval(&self) -> usize { + (self.window.as_millis() as f64 / self.quota) as usize + } } #[cfg(test)] @@ -102,6 +107,7 @@ mod tests { let mut ratelimit = Ratelimit::new_at(Duration::new(60, 0), 3.0, now); assert!(ratelimit.can_send_at(now)); + assert_eq!(ratelimit.update_interval(), 20_000); // Send burst of 3 messages. ratelimit.send_at(now); diff --git a/deltachat-rpc-client/tests/test_webxdc.py b/deltachat-rpc-client/tests/test_webxdc.py index e189251df8..e40d169716 100644 --- a/deltachat-rpc-client/tests/test_webxdc.py +++ b/deltachat-rpc-client/tests/test_webxdc.py @@ -25,6 +25,8 @@ def test_webxdc(acfactory) -> None: "sourceCodeUrl": None, "summary": None, "selfAddr": webxdc_info["selfAddr"], + "sendUpdateInterval": 1000, + "sendUpdateMaxSize": 18874368, } status_updates = message.get_webxdc_status_updates() diff --git a/src/webxdc.rs b/src/webxdc.rs index 4945408fa7..ad66bb1b18 100644 --- a/src/webxdc.rs +++ b/src/webxdc.rs @@ -40,6 +40,7 @@ use crate::events::EventType; use crate::key::{load_self_public_key, DcKey}; use crate::message::{Message, MessageState, MsgId, Viewtype}; use crate::mimefactory::wrapped_base64_encode; +use crate::mimefactory::RECOMMENDED_FILE_SIZE; use crate::mimeparser::SystemMessage; use crate::param::Param; use crate::param::Params; @@ -105,6 +106,14 @@ pub struct WebxdcInfo { /// Address to be used for `window.webxdc.selfAddr` in JS land. pub self_addr: String, + + /// Milliseconds to wait before calling `sendUpdate()` again since the last call. + /// Should be exposed to `window.sendUpdateInterval` in JS land. + pub send_update_interval: usize, + + /// Maximum number of bytes accepted for a serialized update object. + /// Should be exposed to `window.sendUpdateMaxSize` in JS land. + pub send_update_max_size: usize, } /// Status Update ID. @@ -946,6 +955,8 @@ impl Message { }, internet_access, self_addr, + send_update_interval: context.ratelimit.read().await.update_interval(), + send_update_max_size: RECOMMENDED_FILE_SIZE as usize, }) } @@ -2258,6 +2269,8 @@ sth_for_the = "future""# let info = instance.get_webxdc_info(&t).await?; assert_eq!(info.name, "minimal.xdc"); assert_eq!(info.icon, WEBXDC_DEFAULT_ICON.to_string()); + assert_eq!(info.send_update_interval, 10000); + assert_eq!(info.send_update_max_size, RECOMMENDED_FILE_SIZE as usize); let mut instance = create_webxdc_instance( &t,