BlueGit

ict rusty-teams

Files

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

desktop_payload.rs

desktop_payload.rs 11.0 KiB Download Raw
1//! Pure desktop-notification payloads (no D-Bus). Linux maps these to
2//! `org.freedesktop.Notifications`; Windows keeps the GPUI card.
3
4use super::stack::{
5 MENTION_DURATION, MESSAGE_DURATION, ToastModel,
6};
7use super::text::describe;
8use crate::call::reminder::MeetingToast;
9use crate::call::ReminderPhase;
10
11/// Action ids used on the bus and when dispatching back into the notification center.
12pub mod action {
13 pub const DEFAULT: &str = "default";
14 pub const MARK_READ: &str = "mark-read";
15 pub const REPLY: &str = "inline-reply";
16 pub const DECLINE: &str = "decline";
17 pub const ACCEPT_AUDIO: &str = "accept-audio";
18 pub const ACCEPT_VIDEO: &str = "accept-video";
19 pub const JOIN: &str = "join";
20 pub const SNOOZE: &str = "snooze";
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum Urgency {
25 Low = 0,
26 Normal = 1,
27 Critical = 2,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Hash)]
31pub enum DesktopKey {
32 Message(u64),
33 Ring(u64),
34 Meeting(u64),
35}
36
37impl DesktopKey {
38 pub fn replace_token(&self) -> String {
39 match self {
40 DesktopKey::Message(id) => format!("message:{id}"),
41 DesktopKey::Ring(id) => format!("ring:{id}"),
42 DesktopKey::Meeting(id) => format!("meeting:{id}"),
43 }
44 }
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct DesktopAction {
49 pub id: &'static str,
50 pub label: String,
51}
52
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct DesktopPayload {
55 pub key: DesktopKey,
56 /// Stable replace key across updates (conversation, ring id, meeting event).
57 pub replace_key: String,
58 pub summary: String,
59 pub body: String,
60 pub actions: Vec<DesktopAction>,
61 /// Milliseconds. `0` means do not expire.
62 pub timeout_ms: i32,
63 pub urgency: Urgency,
64 pub category: &'static str,
65}
66
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct MessageLabels {
69 pub mark_read: String,
70 pub reply: String,
71}
72
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct RingLabels {
75 pub decline: String,
76 pub accept: String,
77 pub audio: String,
78 pub video: String,
79}
80
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct MeetingLabels {
83 pub join: String,
84 pub snooze: String,
85}
86
87pub fn message_payload(
88 model: &ToastModel,
89 preview_on: bool,
90 labels: &MessageLabels,
91) -> DesktopPayload {
92 let text = describe(model, preview_on);
93 let body = if model.agent.is_some() {
94 if text.preview.is_empty() {
95 model
96 .agent
97 .as_ref()
98 .map(|agent| agent.body.clone())
99 .unwrap_or_default()
100 } else {
101 text.preview.clone()
102 }
103 } else {
104 text.preview.clone()
105 };
106 let summary = if let Some(agent) = &model.agent {
107 if agent.title.is_empty() {
108 text.title.clone()
109 } else {
110 agent.title.clone()
111 }
112 } else {
113 text.title.clone()
114 };
115 let mention = model.mentions_me;
116 let timeout_ms = if mention {
117 MENTION_DURATION.as_millis() as i32
118 } else {
119 MESSAGE_DURATION.as_millis() as i32
120 };
121 let urgency = if model.urgent {
122 Urgency::Critical
123 } else {
124 Urgency::Normal
125 };
126 let mut actions = vec![DesktopAction {
127 id: action::MARK_READ,
128 label: labels.mark_read.clone(),
129 }];
130 if model.agent.is_none() && !model.status_change {
131 actions.push(DesktopAction {
132 id: action::REPLY,
133 label: labels.reply.clone(),
134 });
135 }
136 DesktopPayload {
137 key: DesktopKey::Message(model.id),
138 replace_key: format!("conversation:{}", model.conversation_id),
139 summary,
140 body,
141 actions,
142 timeout_ms,
143 urgency,
144 category: if model.agent.is_some() {
145 "im"
146 } else {
147 "im.received"
148 },
149 }
150}
151
152pub fn ring_payload(
153 ring_id: u64,
154 caller_name: &str,
155 subtitle: &str,
156 video: bool,
157 labels: &RingLabels,
158) -> DesktopPayload {
159 let accept_label = if video {
160 labels.audio.clone()
161 } else {
162 labels.accept.clone()
163 };
164 let mut actions = vec![
165 DesktopAction {
166 id: action::DECLINE,
167 label: labels.decline.clone(),
168 },
169 DesktopAction {
170 id: action::ACCEPT_AUDIO,
171 label: accept_label,
172 },
173 ];
174 if video {
175 actions.push(DesktopAction {
176 id: action::ACCEPT_VIDEO,
177 label: labels.video.clone(),
178 });
179 }
180 DesktopPayload {
181 key: DesktopKey::Ring(ring_id),
182 replace_key: format!("ring:{ring_id}"),
183 summary: caller_name.to_owned(),
184 body: subtitle.to_owned(),
185 actions,
186 timeout_ms: 0,
187 urgency: Urgency::Critical,
188 category: "x-teams.call.incoming",
189 }
190}
191
192pub fn meeting_payload(toast: &MeetingToast, body: &str, labels: &MeetingLabels) -> DesktopPayload {
193 let mut actions = Vec::new();
194 if toast.phase == ReminderPhase::Soon {
195 actions.push(DesktopAction {
196 id: action::SNOOZE,
197 label: labels.snooze.clone(),
198 });
199 }
200 actions.push(DesktopAction {
201 id: action::JOIN,
202 label: labels.join.clone(),
203 });
204 DesktopPayload {
205 key: DesktopKey::Meeting(toast.id),
206 replace_key: format!("meeting:{}", toast.event_key),
207 summary: toast.subject.clone(),
208 body: body.to_owned(),
209 actions,
210 timeout_ms: 0,
211 urgency: Urgency::Normal,
212 category: "x-teams.meeting.reminder",
213 }
214}
215
216#[cfg(test)]
217mod tests {
218 use std::time::Instant;
219
220 use super::*;
221 use crate::notify::rules::{ChatKind, Preview};
222 use crate::notify::stack::{Fade, ReplyState, ToastTimer};
223
224 fn message_labels() -> MessageLabels {
225 MessageLabels {
226 mark_read: "Mark as read".into(),
227 reply: "Reply".into(),
228 }
229 }
230
231 fn ring_labels() -> RingLabels {
232 RingLabels {
233 decline: "Decline".into(),
234 accept: "Accept".into(),
235 audio: "Audio".into(),
236 video: "Video".into(),
237 }
238 }
239
240 fn meeting_labels() -> MeetingLabels {
241 MeetingLabels {
242 join: "Join".into(),
243 snooze: "Snooze".into(),
244 }
245 }
246
247 fn toast(mention: bool, urgent: bool, preview: Preview) -> ToastModel {
248 ToastModel {
249 id: 7,
250 conversation_id: "chat-a".into(),
251 message_id: "m1".into(),
252 kind: ChatKind::Direct,
253 chat_title: String::new(),
254 sender_id: Some("u1".into()),
255 sender_name: "Priya Nair".into(),
256 preview,
257 mentions_me: mention,
258 count: 1,
259 timer: ToastTimer::new(Instant::now(), mention),
260 reply: ReplyState::Closed,
261 reply_text: String::new(),
262 hovered: false,
263 time: String::new(),
264 queued: false,
265 dismissed: false,
266 fade: Fade::shown(Instant::now()),
267 agent: None,
268 status_change: false,
269 urgent,
270 }
271 }
272
273 #[test]
274 fn preview_off_hides_message_text() {
275 let model = toast(false, false, Preview::Text("secret".into()));
276 let payload = message_payload(&model, false, &message_labels());
277 assert_eq!(payload.body, "New message");
278 assert_eq!(payload.replace_key, "conversation:chat-a");
279 assert_eq!(payload.timeout_ms, MESSAGE_DURATION.as_millis() as i32);
280 }
281
282 #[test]
283 fn mention_uses_longer_timeout() {
284 let model = toast(true, false, Preview::Text("hi".into()));
285 let payload = message_payload(&model, true, &message_labels());
286 assert_eq!(payload.timeout_ms, MENTION_DURATION.as_millis() as i32);
287 }
288
289 #[test]
290 fn urgent_sets_critical_urgency() {
291 let model = toast(false, true, Preview::Text("down".into()));
292 let payload = message_payload(&model, true, &message_labels());
293 assert_eq!(payload.urgency, Urgency::Critical);
294 }
295
296 #[test]
297 fn message_offers_mark_read_and_reply() {
298 let model = toast(false, false, Preview::Text("hi".into()));
299 let payload = message_payload(&model, true, &message_labels());
300 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
301 assert_eq!(ids, vec![action::MARK_READ, action::REPLY]);
302 }
303
304 #[test]
305 fn bundling_shares_conversation_replace_key() {
306 let first = toast(false, false, Preview::Text("one".into()));
307 let mut second = toast(false, false, Preview::Text("two".into()));
308 second.id = 99;
309 let a = message_payload(&first, true, &message_labels());
310 let b = message_payload(&second, true, &message_labels());
311 assert_eq!(a.replace_key, b.replace_key);
312 assert_ne!(a.key, b.key);
313 }
314
315 #[test]
316 fn audio_ring_has_no_timeout_and_no_video_action() {
317 let payload = ring_payload(3, "Alex", "Incoming audio call", false, &ring_labels());
318 assert_eq!(payload.timeout_ms, 0);
319 assert_eq!(payload.urgency, Urgency::Critical);
320 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
321 assert_eq!(ids, vec![action::DECLINE, action::ACCEPT_AUDIO]);
322 assert_eq!(payload.actions[1].label, "Accept");
323 }
324
325 #[test]
326 fn video_ring_offers_audio_and_video() {
327 let payload = ring_payload(3, "Alex", "Incoming video call", true, &ring_labels());
328 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
329 assert_eq!(
330 ids,
331 vec![action::DECLINE, action::ACCEPT_AUDIO, action::ACCEPT_VIDEO]
332 );
333 assert_eq!(payload.actions[1].label, "Audio");
334 }
335
336 #[test]
337 fn meeting_soon_offers_snooze_and_join() {
338 let toast = MeetingToast {
339 id: 1,
340 event_key: "e@1".into(),
341 event_id: "e".into(),
342 phase: ReminderPhase::Soon,
343 subject: "Standup".into(),
344 start: chrono::Utc::now(),
345 join_url: "https://teams.microsoft.com/l/meetup-join/x".into(),
346 };
347 let payload = meeting_payload(&toast, "in 5 minutes", &meeting_labels());
348 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
349 assert_eq!(ids, vec![action::SNOOZE, action::JOIN]);
350 assert_eq!(payload.timeout_ms, 0);
351 }
352
353 #[test]
354 fn meeting_live_drops_snooze_same_replace_key() {
355 let soon = MeetingToast {
356 id: 1,
357 event_key: "e@1".into(),
358 event_id: "e".into(),
359 phase: ReminderPhase::Soon,
360 subject: "Standup".into(),
361 start: chrono::Utc::now(),
362 join_url: "https://example".into(),
363 };
364 let live = MeetingToast {
365 id: 2,
366 phase: ReminderPhase::Live,
367 ..soon.clone()
368 };
369 let a = meeting_payload(&soon, "soon", &meeting_labels());
370 let b = meeting_payload(&live, "live now", &meeting_labels());
371 assert_eq!(a.replace_key, b.replace_key);
372 assert_eq!(
373 b.actions.iter().map(|a| a.id).collect::<Vec<_>>(),
374 vec![action::JOIN]
375 );
376 }
377}