| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
use crate::{CatchUp, Registry, config::Settings}; |
| 39 |
use std::collections::{HashMap, HashSet}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 48 |
pub struct Occurrence { |
| 49 |
|
| 50 |
pub kind: &'static str, |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub token: String, |
| 60 |
|
| 61 |
pub title: String, |
| 62 |
|
| 63 |
pub body: String, |
| 64 |
} |
| 65 |
|
| 66 |
impl Occurrence { |
| 67 |
|
| 68 |
pub fn new( |
| 69 |
kind: &'static str, |
| 70 |
token: impl Into<String>, |
| 71 |
title: impl Into<String>, |
| 72 |
body: impl Into<String>, |
| 73 |
) -> Self { |
| 74 |
Self { |
| 75 |
kind, |
| 76 |
token: token.into(), |
| 77 |
title: title.into(), |
| 78 |
body: body.into(), |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub trait Deliver { |
| 90 |
|
| 91 |
fn deliver(&mut self, note: &Occurrence); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 100 |
pub enum Sent { |
| 101 |
|
| 102 |
Delivered, |
| 103 |
|
| 104 |
Off, |
| 105 |
|
| 106 |
Duplicate, |
| 107 |
|
| 108 |
|
| 109 |
Late, |
| 110 |
|
| 111 |
Unknown, |
| 112 |
} |
| 113 |
|
| 114 |
impl Sent { |
| 115 |
|
| 116 |
#[must_use] |
| 117 |
pub const fn delivered(self) -> bool { |
| 118 |
matches!(self, Self::Delivered) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#[derive(Debug)] |
| 129 |
pub struct Outbox<D> { |
| 130 |
registry: Registry, |
| 131 |
adapter: D, |
| 132 |
|
| 133 |
seen: HashMap<&'static str, HashSet<String>>, |
| 134 |
|
| 135 |
swept: HashSet<&'static str>, |
| 136 |
remember: usize, |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
pub const REMEMBER: usize = 10_000; |
| 144 |
|
| 145 |
impl<D: Deliver> Outbox<D> { |
| 146 |
|
| 147 |
pub fn new(registry: Registry, adapter: D) -> Self { |
| 148 |
Self { |
| 149 |
registry, |
| 150 |
adapter, |
| 151 |
seen: HashMap::new(), |
| 152 |
swept: HashSet::new(), |
| 153 |
remember: REMEMBER, |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
#[must_use] |
| 159 |
pub const fn remembering(mut self, tokens: usize) -> Self { |
| 160 |
self.remember = tokens; |
| 161 |
self |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
pub const fn adapter(&mut self) -> &mut D { |
| 166 |
&mut self.adapter |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
pub fn offer(&mut self, note: &Occurrence, settings: &impl Settings) -> Sent { |
| 177 |
let sent = self.consider(note, settings); |
| 178 |
self.swept.insert(note.kind); |
| 179 |
sent |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
pub fn sweep<'a>(&'a mut self, kind: &'static str) -> Sweep<'a, D> { |
| 189 |
Sweep { outbox: self, kind } |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
#[must_use] |
| 194 |
pub fn swept(&self, kind: &str) -> bool { |
| 195 |
self.swept.contains(kind) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
#[must_use] |
| 200 |
pub fn seen(&self, kind: &str, token: &str) -> bool { |
| 201 |
self.seen.get(kind).is_some_and(|set| set.contains(token)) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
fn consider(&mut self, note: &Occurrence, settings: &impl Settings) -> Sent { |
| 206 |
let Some(kind) = self.registry.kind(note.kind) else { |
| 207 |
return Sent::Unknown; |
| 208 |
}; |
| 209 |
let restart = kind.restart; |
| 210 |
|
| 211 |
if !self.registry.is_on(note.kind, settings) { |
| 212 |
return Sent::Off; |
| 213 |
} |
| 214 |
if self.seen(note.kind, ¬e.token) { |
| 215 |
return Sent::Duplicate; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
self.note(note.kind, note.token.clone()); |
| 221 |
|
| 222 |
if restart == CatchUp::Skip && !self.swept(note.kind) { |
| 223 |
return Sent::Late; |
| 224 |
} |
| 225 |
|
| 226 |
self.adapter.deliver(note); |
| 227 |
Sent::Delivered |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
fn note(&mut self, kind: &'static str, token: String) { |
| 233 |
let set = self.seen.entry(kind).or_default(); |
| 234 |
if set.len() >= self.remember { |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
set.clear(); |
| 241 |
self.swept.remove(kind); |
| 242 |
} |
| 243 |
self.seen.entry(kind).or_default().insert(token); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
#[derive(Debug)] |
| 253 |
pub struct Sweep<'a, D> { |
| 254 |
outbox: &'a mut Outbox<D>, |
| 255 |
kind: &'static str, |
| 256 |
} |
| 257 |
|
| 258 |
impl<D: Deliver> Sweep<'_, D> { |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
pub fn offer(&mut self, note: &Occurrence, settings: &impl Settings) -> Sent { |
| 265 |
self.outbox.consider(note, settings) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
#[must_use] |
| 270 |
pub const fn kind(&self) -> &'static str { |
| 271 |
self.kind |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
impl<D> Drop for Sweep<'_, D> { |
| 276 |
fn drop(&mut self) { |
| 277 |
self.outbox.swept.insert(self.kind); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
#[cfg(test)] |
| 282 |
mod tests { |
| 283 |
use super::*; |
| 284 |
use crate::{Kind, Registry, config::Unset}; |
| 285 |
use std::collections::HashMap; |
| 286 |
|
| 287 |
static KINDS: &[Kind] = &[ |
| 288 |
Kind::new( |
| 289 |
"snooze-expiry", |
| 290 |
"Snoozed items resurface", |
| 291 |
"When something you snoozed comes back.", |
| 292 |
"Reminders", |
| 293 |
) |
| 294 |
.shipping_on(), |
| 295 |
Kind::new( |
| 296 |
"event-reminder", |
| 297 |
"Event reminders", |
| 298 |
"Before an event starts.", |
| 299 |
"Reminders", |
| 300 |
) |
| 301 |
.shipping_on() |
| 302 |
.quiet_after_restart(), |
| 303 |
Kind::new("digest", "Daily digest", "One summary.", "Summaries"), |
| 304 |
]; |
| 305 |
|
| 306 |
static NOTIFS: Registry = Registry::new(KINDS); |
| 307 |
|
| 308 |
|
| 309 |
#[derive(Debug, Default)] |
| 310 |
struct Spy(Vec<String>); |
| 311 |
|
| 312 |
impl Deliver for Spy { |
| 313 |
fn deliver(&mut self, note: &Occurrence) { |
| 314 |
self.0.push(format!("{}:{}", note.kind, note.token)); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
fn outbox() -> Outbox<Spy> { |
| 319 |
Outbox::new(NOTIFS, Spy::default()) |
| 320 |
} |
| 321 |
|
| 322 |
fn stored(pairs: &[(&str, &str)]) -> impl Settings { |
| 323 |
let map: HashMap<String, String> = pairs |
| 324 |
.iter() |
| 325 |
.map(|(k, v)| ((*k).to_string(), (*v).to_string())) |
| 326 |
.collect(); |
| 327 |
move |key: &str| map.get(key).cloned() |
| 328 |
} |
| 329 |
|
| 330 |
fn snooze(token: &str) -> Occurrence { |
| 331 |
Occurrence::new("snooze-expiry", token, "Task resurfaced", "Write the thing") |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn an_off_kind_produces_no_call_at_all() { |
| 336 |
|
| 337 |
|
| 338 |
let mut outbox = outbox(); |
| 339 |
let off = stored(&[("snooze-expiry.enabled", "false")]); |
| 340 |
assert_eq!(outbox.offer(&snooze("t1"), &off), Sent::Off); |
| 341 |
assert!(outbox.adapter().0.is_empty()); |
| 342 |
} |
| 343 |
|
| 344 |
#[test] |
| 345 |
fn a_kind_that_ships_on_fires_for_a_reader_who_has_touched_nothing() { |
| 346 |
let mut outbox = outbox(); |
| 347 |
assert_eq!(outbox.offer(&snooze("t1"), &Unset), Sent::Delivered); |
| 348 |
assert_eq!(outbox.adapter().0, vec!["snooze-expiry:t1"]); |
| 349 |
} |
| 350 |
|
| 351 |
#[test] |
| 352 |
fn a_kind_that_ships_off_stays_quiet_until_it_is_turned_on() { |
| 353 |
let mut outbox = outbox(); |
| 354 |
let note = Occurrence::new("digest", "2026-08-17", "Today", "Six things"); |
| 355 |
assert_eq!(outbox.offer(¬e, &Unset), Sent::Off); |
| 356 |
let on = stored(&[("digest.enabled", "true")]); |
| 357 |
assert_eq!(outbox.offer(¬e, &on), Sent::Delivered); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn the_same_token_is_delivered_once_however_many_ticks_ask() { |
| 362 |
|
| 363 |
|
| 364 |
let mut outbox = outbox(); |
| 365 |
assert_eq!(outbox.offer(&snooze("t1"), &Unset), Sent::Delivered); |
| 366 |
for _ in 0..5 { |
| 367 |
assert_eq!(outbox.offer(&snooze("t1"), &Unset), Sent::Duplicate); |
| 368 |
} |
| 369 |
assert_eq!(outbox.offer(&snooze("t2"), &Unset), Sent::Delivered); |
| 370 |
assert_eq!( |
| 371 |
outbox.adapter().0, |
| 372 |
vec!["snooze-expiry:t1", "snooze-expiry:t2"] |
| 373 |
); |
| 374 |
} |
| 375 |
|
| 376 |
#[test] |
| 377 |
fn a_restart_does_not_spam_the_kind_that_says_the_moment_has_passed() { |
| 378 |
let mut outbox = outbox(); |
| 379 |
let late = |n: &'static str| Occurrence::new("event-reminder", n, "Standup", "In 15 min"); |
| 380 |
|
| 381 |
|
| 382 |
{ |
| 383 |
let mut pass = outbox.sweep("event-reminder"); |
| 384 |
assert_eq!(pass.offer(&late("e1"), &Unset), Sent::Late); |
| 385 |
assert_eq!(pass.offer(&late("e2"), &Unset), Sent::Late); |
| 386 |
} |
| 387 |
assert!(outbox.adapter().0.is_empty()); |
| 388 |
|
| 389 |
|
| 390 |
{ |
| 391 |
let mut pass = outbox.sweep("event-reminder"); |
| 392 |
assert_eq!(pass.offer(&late("e1"), &Unset), Sent::Duplicate); |
| 393 |
assert_eq!(pass.offer(&late("e3"), &Unset), Sent::Delivered); |
| 394 |
} |
| 395 |
assert_eq!(outbox.adapter().0, vec!["event-reminder:e3"]); |
| 396 |
} |
| 397 |
|
| 398 |
#[test] |
| 399 |
fn a_kind_that_keeps_its_worth_when_late_fires_on_the_first_pass() { |
| 400 |
|
| 401 |
|
| 402 |
let mut outbox = outbox(); |
| 403 |
let mut pass = outbox.sweep("snooze-expiry"); |
| 404 |
assert_eq!(pass.offer(&snooze("t1"), &Unset), Sent::Delivered); |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn one_kinds_pass_does_not_end_anothers() { |
| 409 |
let mut outbox = outbox(); |
| 410 |
drop(outbox.sweep("snooze-expiry")); |
| 411 |
assert!(outbox.swept("snooze-expiry")); |
| 412 |
assert!(!outbox.swept("event-reminder")); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
fn forgetting_a_kinds_tokens_puts_it_back_through_its_bootstrap() { |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
let mut outbox = outbox().remembering(2); |
| 421 |
let note = |n: &'static str| Occurrence::new("event-reminder", n, "Standup", "Soon"); |
| 422 |
|
| 423 |
drop(outbox.sweep("event-reminder")); |
| 424 |
{ |
| 425 |
let mut pass = outbox.sweep("event-reminder"); |
| 426 |
assert_eq!(pass.offer(¬e("e1"), &Unset), Sent::Delivered); |
| 427 |
assert_eq!(pass.offer(¬e("e2"), &Unset), Sent::Delivered); |
| 428 |
|
| 429 |
|
| 430 |
assert_eq!(pass.offer(¬e("e3"), &Unset), Sent::Late); |
| 431 |
} |
| 432 |
assert_eq!( |
| 433 |
outbox.adapter().0, |
| 434 |
vec!["event-reminder:e1", "event-reminder:e2"] |
| 435 |
); |
| 436 |
assert!(!outbox.seen("event-reminder", "e1")); |
| 437 |
} |
| 438 |
|
| 439 |
#[test] |
| 440 |
fn an_undeclared_kind_never_reaches_the_host() { |
| 441 |
let mut outbox = outbox(); |
| 442 |
let note = Occurrence::new("invented", "x", "Hello", "There"); |
| 443 |
assert_eq!(outbox.offer(¬e, &Unset), Sent::Unknown); |
| 444 |
assert!(outbox.adapter().0.is_empty()); |
| 445 |
|
| 446 |
|
| 447 |
assert!(!outbox.seen("invented", "x")); |
| 448 |
} |
| 449 |
|
| 450 |
#[test] |
| 451 |
fn what_was_suppressed_is_countable_rather_than_only_logged() { |
| 452 |
let mut outbox = outbox(); |
| 453 |
let off = stored(&[("snooze-expiry.enabled", "false")]); |
| 454 |
assert!(!outbox.offer(&snooze("t1"), &off).delivered()); |
| 455 |
assert!(outbox.offer(&snooze("t1"), &Unset).delivered()); |
| 456 |
} |
| 457 |
} |
| 458 |
|