BlueGit

ict rusty-teams

Files

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

desktop_payload.rs

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