wayBetter for X
This commit is contained in:
commit
2f1e35ba20
720 changed files with 147465 additions and 0 deletions
145
apps/desktop/src-tauri/src/alerts.rs
Normal file
145
apps/desktop/src-tauri/src/alerts.rs
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
//! Local-only notifications: X's keyword hits on the wayBetter host's keyword alerts (settings,
|
||||
//! matching, snippets, rate limiting: `waybetter_host::alerts`). Nothing here touches the network;
|
||||
//! matches come from responses X's own UI already fetched.
|
||||
|
||||
use serde::Deserialize;
|
||||
pub use waybetter_host::alerts::*;
|
||||
|
||||
/// A match reported by an X tab about a tweet it already received.
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KeywordHit {
|
||||
pub keyword: String,
|
||||
pub tweet_id: String,
|
||||
pub author: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl KeywordMatch for KeywordHit {
|
||||
fn item_id(&self) -> &str {
|
||||
&self.tweet_id
|
||||
}
|
||||
fn keyword(&self) -> &str {
|
||||
&self.keyword
|
||||
}
|
||||
}
|
||||
|
||||
/// A verified keyword hit, ready for a notification in the owner's language.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AlertMessage {
|
||||
pub keyword: String,
|
||||
/// The post's author handle (without @), empty when unknown.
|
||||
pub author: String,
|
||||
pub snippet: String,
|
||||
}
|
||||
|
||||
/// Validates a page-reported hit against the owner's settings. The notification text is built from
|
||||
/// the result by the caller, in the owner's language (desktop_i18n.rs).
|
||||
pub fn check_hit(settings: &AlertSettings, hit: &KeywordHit) -> Result<AlertMessage, String> {
|
||||
let keyword = settings.configured(&hit.keyword)?;
|
||||
if hit.tweet_id.is_empty() || hit.tweet_id.len() > 25 || !hit.tweet_id.bytes().all(|b| b.is_ascii_digit()) {
|
||||
return Err("VALIDATION: invalid tweet id".into());
|
||||
}
|
||||
if hit.author.len() > 32 || !hit.author.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') {
|
||||
return Err("VALIDATION: invalid author".into());
|
||||
}
|
||||
let snippet = snippet(&hit.text, &keyword)?;
|
||||
Ok(AlertMessage { keyword, author: hit.author.clone(), snippet })
|
||||
}
|
||||
|
||||
/// Only storage-level capture failures (the text was not saved) are worth a desktop notification;
|
||||
/// validation, conflicts and quota are expected page behavior.
|
||||
pub fn capture_failure_notifies(error: &str) -> bool {
|
||||
error.starts_with("DATABASE:") || error.starts_with("STORAGE:")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn settings(words: &[&str]) -> AlertSettings {
|
||||
AlertSettings { enabled: true, keywords: words.iter().map(|w| w.to_string()).collect() }
|
||||
.validated()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn hit(keyword: &str, id: &str, text: &str) -> KeywordHit {
|
||||
KeywordHit { keyword: keyword.into(), tweet_id: id.into(), author: "jack".into(), text: text.into() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_normalize_and_dedupe() {
|
||||
let s = settings(&[" Rust Lang ", "rust lang", "", "Tauri"]);
|
||||
assert_eq!(s.keywords, vec!["rust lang".to_string(), "tauri".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_reject_bad_keywords() {
|
||||
assert!(AlertSettings { enabled: true, keywords: vec!["a".into()] }.validated().is_err());
|
||||
assert!(AlertSettings { enabled: true, keywords: vec!["x".repeat(65)] }.validated().is_err());
|
||||
let many = AlertSettings { enabled: true, keywords: (0..33).map(|i| format!("kw{i}")).collect() };
|
||||
assert!(many.validated().is_err());
|
||||
assert!(AlertSettings { enabled: true, keywords: vec!["a\u{7}b".into()] }.validated().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disabled_settings_watch_nothing() {
|
||||
let mut s = settings(&["tauri"]);
|
||||
s.enabled = false;
|
||||
assert!(s.active_keywords().is_empty());
|
||||
assert!(check_hit(&s, &hit("tauri", "1", "tauri rocks")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hit_must_match_configured_keyword_and_text() {
|
||||
let s = settings(&["tauri"]);
|
||||
let message = check_hit(&s, &hit("TAURI", "123", "I love\nTauri 2")).unwrap();
|
||||
assert_eq!(message.keyword, "tauri");
|
||||
assert_eq!(message.author, "jack");
|
||||
assert_eq!(message.snippet, "I love Tauri 2");
|
||||
assert!(check_hit(&s, &hit("electron", "123", "electron")).is_err());
|
||||
assert!(check_hit(&s, &hit("tauri", "123", "nothing here")).is_err());
|
||||
assert!(check_hit(&s, &hit("tauri", "12a", "tauri")).is_err());
|
||||
let mut bad = hit("tauri", "1", "tauri");
|
||||
bad.author = "<script>".into();
|
||||
assert!(check_hit(&s, &bad).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snippet_is_truncated() {
|
||||
let s = settings(&["tauri"]);
|
||||
let text = format!("tauri {}", "a".repeat(500));
|
||||
let message = check_hit(&s, &hit("tauri", "1", &text)).unwrap();
|
||||
assert_eq!(message.snippet.chars().count(), MAX_SNIPPET_CHARS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn throttle_limits_per_key_and_globally() {
|
||||
let mut t = Throttle::new(Duration::from_secs(60), Duration::from_secs(600), 3);
|
||||
let now = Instant::now();
|
||||
assert!(t.allow("a", now));
|
||||
assert!(!t.allow("a", now + Duration::from_secs(30)));
|
||||
assert!(t.allow("a", now + Duration::from_secs(61)));
|
||||
assert!(t.allow("b", now + Duration::from_secs(62)));
|
||||
assert!(!t.allow("c", now + Duration::from_secs(63)));
|
||||
assert!(t.allow("c", now + Duration::from_secs(601)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gate_notifies_each_tweet_once() {
|
||||
let mut gate = AlertGate::default();
|
||||
let now = Instant::now();
|
||||
assert!(gate.allow(&hit("tauri", "1", "tauri"), now));
|
||||
assert!(!gate.allow(&hit("tauri", "1", "tauri"), now + Duration::from_secs(120)));
|
||||
assert!(gate.allow(&hit("tauri", "2", "tauri"), now + Duration::from_secs(120)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capture_failures_that_notify() {
|
||||
assert!(capture_failure_notifies("DATABASE: disk full"));
|
||||
assert!(!capture_failure_notifies("VALIDATION: invalid capture payload"));
|
||||
assert!(!capture_failure_notifies("CONFLICT: stale capture sequence"));
|
||||
assert!(!capture_failure_notifies("QUOTA: too many writes"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue