use std::collections::HashMap;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
use super::bridge::Notification;
use super::notify::Level;
pub fn desktop_argv(os: &str, n: &Notification) -> Option<Vec<String>> {
let r = &n.record;
let body = match &r.link {
Some(link) => format!("{}\n{link}", r.msg),
None => r.msg.clone(),
};
match os {
"linux" => {
let urgency = match r.level {
Level::Info | Level::Warn => "normal",
Level::Error => "critical",
};
Some(vec![
"notify-send".into(),
"--app-name=devsandbox".into(),
"-u".into(),
urgency.into(),
"--".into(),
format!("devsandbox: {}", n.instance),
body,
])
}
"macos" => {
let script = format!(
"display notification {} with title \"devsandbox\" subtitle {}",
applescript_string(&body),
applescript_string(&n.instance)
);
Some(vec!["osascript".into(), "-e".into(), script])
}
_ => None,
}
}
fn applescript_string(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('"');
for c in s.chars() {
if matches!(c, '\\' | '"') {
out.push('\\');
}
out.push(c);
}
out.push('"');
out
}
pub fn notify_desktop(n: &Notification) {
let Some(argv) = desktop_argv(std::env::consts::OS, n) else { return };
let spawned = Command::new(&argv[0])
.args(&argv[1..])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn();
if let Ok(mut child) = spawned {
std::thread::spawn(move || {
let _ = child.wait();
});
}
}
const RATE_BURST: u32 = 3;
const RATE_REFILL: Duration = Duration::from_secs(10);
const COALESCE_WINDOW: Duration = Duration::from_secs(60);
#[derive(Default)]
pub struct RateLimit {
buckets: HashMap<String, (u32, Instant)>,
shown: HashMap<(String, String), Instant>,
}
impl RateLimit {
pub fn allow(&mut self, n: &Notification, now: Instant) -> bool {
self.shown.retain(|_, at| now.saturating_duration_since(*at) < COALESCE_WINDOW);
let key = n.record.key.as_ref().map(|k| (n.instance.clone(), k.clone()));
if key.as_ref().is_some_and(|k| self.shown.contains_key(k)) {
return false;
}
let (tokens, since) = self.buckets.entry(n.instance.clone()).or_insert((RATE_BURST, now));
let earned = (now.saturating_duration_since(*since).as_nanos() / RATE_REFILL.as_nanos()) as u32;
if earned > 0 {
*tokens = (*tokens).saturating_add(earned).min(RATE_BURST);
*since += RATE_REFILL * earned;
}
if *tokens == 0 {
return false;
}
*tokens -= 1;
if let Some(k) = key {
self.shown.insert(k, now);
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::devsbd::notify::Record;
fn from(instance: &str, key: Option<&str>) -> Notification {
Notification {
instance: instance.into(),
record: Record { level: Level::Info, key: key.map(Into::into), link: None, msg: "m".into(), at: 0 },
}
}
#[test]
fn rate_limit_bursts_then_refills_per_instance() {
let mut rl = RateLimit::default();
let t0 = Instant::now();
let a = from("a", None);
for _ in 0..RATE_BURST {
assert!(rl.allow(&a, t0));
}
assert!(!rl.allow(&a, t0), "burst spent");
assert!(rl.allow(&from("b", None), t0));
assert!(!rl.allow(&a, t0 + RATE_REFILL - Duration::from_millis(1)));
assert!(rl.allow(&a, t0 + RATE_REFILL), "one token refilled");
assert!(!rl.allow(&a, t0 + RATE_REFILL));
let later = t0 + RATE_REFILL * 100;
for _ in 0..RATE_BURST {
assert!(rl.allow(&a, later));
}
assert!(!rl.allow(&a, later));
}
#[test]
fn rate_limit_coalesces_repeated_keys() {
let mut rl = RateLimit::default();
let t0 = Instant::now();
assert!(rl.allow(&from("a", Some("pr-1")), t0));
assert!(!rl.allow(&from("a", Some("pr-1")), t0 + Duration::from_secs(1)), "repeat coalesced");
assert!(rl.allow(&from("a", Some("pr-2")), t0 + Duration::from_secs(1)));
assert!(rl.allow(&from("b", Some("pr-1")), t0 + Duration::from_secs(1)));
assert!(rl.allow(&from("a", None), t0 + Duration::from_secs(1)));
assert!(rl.allow(&from("a", Some("pr-1")), t0 + COALESCE_WINDOW + Duration::from_secs(1)));
}
fn n(level: Level, msg: &str, link: Option<&str>) -> Notification {
Notification {
instance: "web-pr-1".into(),
record: Record { level, key: None, link: link.map(Into::into), msg: msg.into(), at: 0 },
}
}
fn strs(v: &[&str]) -> Vec<String> {
v.iter().map(|s| s.to_string()).collect()
}
#[test]
fn linux_uses_notify_send_with_urgency() {
let argv = desktop_argv("linux", &n(Level::Info, "PR 1 needs you", None)).unwrap();
assert_eq!(
argv,
strs(&["notify-send", "--app-name=devsandbox", "-u", "normal", "--", "devsandbox: web-pr-1", "PR 1 needs you"])
);
let warn = desktop_argv("linux", &n(Level::Warn, "m", None)).unwrap();
assert_eq!(warn[3], "normal");
let error = desktop_argv("linux", &n(Level::Error, "-m", Some("https://x/1"))).unwrap();
assert_eq!(error[3], "critical");
assert_eq!(error.last().unwrap(), "-m\nhttps://x/1");
}
#[test]
fn macos_uses_osascript_with_escaped_strings() {
let argv = desktop_argv("macos", &n(Level::Warn, r#"say "hi" \ bye"#, Some("https://x/1"))).unwrap();
assert_eq!(
argv,
strs(&[
"osascript",
"-e",
"display notification \"say \\\"hi\\\" \\\\ bye\nhttps://x/1\" with title \"devsandbox\" subtitle \"web-pr-1\"",
])
);
assert_eq!(applescript_string(""), "\"\"");
assert_eq!(applescript_string(r#"a"\"#), r#""a\"\\""#);
}
#[test]
fn other_os_has_no_notifier() {
assert_eq!(desktop_argv("windows", &n(Level::Info, "m", None)), None);
assert_eq!(desktop_argv("freebsd", &n(Level::Info, "m", None)), None);
}
}