BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / notify / pause.rs

pause.rs

pause.rs 2.0 KiB Download Raw
1use chrono::{DateTime, Local, Utc};
2use gpui_kit::Context;
3use store::Store;
4
5use crate::app_state::AppState;
6use crate::own_status::until_label;
7
8const META_KEY: &str = "notify_paused_until";
9pub const PAUSE_LABEL: &str = "Pause notifications";
10
11pub fn load(store: &Store) -> Option<DateTime<Utc>> {
12 store
13 .meta(META_KEY)
14 .ok()
15 .flatten()
16 .and_then(|text| DateTime::parse_from_rfc3339(&text).ok())
17 .map(|until| until.with_timezone(&Utc))
18}
19
20pub fn is_paused(until: Option<DateTime<Utc>>, now: DateTime<Utc>) -> bool {
21 until.is_some_and(|until| now < until)
22}
23
24pub fn menu_label(until: Option<DateTime<Utc>>, now: DateTime<Local>) -> String {
25 match until.filter(|until| now.with_timezone(&Utc) < *until) {
26 Some(until) => format!("Paused {}", until_label(until, &now)),
27 None => PAUSE_LABEL.to_owned(),
28 }
29}
30
31impl AppState {
32 pub fn notifications_paused(&self, now: DateTime<Utc>) -> bool {
33 is_paused(self.notifications_paused_until, now)
34 }
35
36 pub fn pause_notifications(&mut self, until: DateTime<Utc>, cx: &mut Context<Self>) {
37 let _ = self.store.set_meta(META_KEY, &until.to_rfc3339());
38 self.notifications_paused_until = Some(until);
39 cx.notify();
40 }
41
42 pub fn resume_notifications(&mut self, cx: &mut Context<Self>) {
43 let _ = self.store.set_meta(META_KEY, "");
44 self.notifications_paused_until = None;
45 cx.notify();
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use chrono::TimeZone;
52
53 use super::*;
54
55 #[test]
56 fn a_pause_ends_at_its_time() {
57 let until = Utc.with_ymd_and_hms(2026, 11, 8, 15, 0, 0).unwrap();
58 let before = Utc.with_ymd_and_hms(2026, 11, 8, 14, 59, 0).unwrap();
59 assert!(is_paused(Some(until), before));
60 assert!(!is_paused(Some(until), until));
61 assert!(!is_paused(None, before));
62 let later = Local.from_utc_datetime(&until.naive_utc());
63 assert_eq!(menu_label(Some(until), later), PAUSE_LABEL);
64 }
65}