Skip to content

Commit

Permalink
feat: add webxdc limits api
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Nov 26, 2024
1 parent ff3efaf commit c6dd035
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions deltachat-ffi/deltachat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions deltachat-jsonrpc/src/api/types/webxdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
})
}
}
6 changes: 6 additions & 0 deletions deltachat-ratelimit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions deltachat-rpc-client/tests/test_webxdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions src/webxdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c6dd035

Please sign in to comment.