BlueGit

ict rusty-teams

Files

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

desktop_payload.rs

desktop_payload.rs 11.2 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::new();
120 if model.org.is_none() {
121 actions.push(DesktopAction {
122 id: action::MARK_READ,
123 label: labels.mark_read.clone(),
124 });
125 }
126 if model.agent.is_none() && !model.status_change && model.org.is_none() {
127 actions.push(DesktopAction {
128 id: action::REPLY,
129 label: labels.reply.clone(),
130 });
131 }
132 DesktopPayload {
133 key: DesktopKey::Message(model.id),
134 replace_key: format!("conversation:{}", model.conversation_id),
135 summary,
136 body,
137 actions,
138 timeout_ms,
139 urgency,
140 category: if model.agent.is_some() {
141 "im"
142 } else {
143 "im.received"
144 },
145 }
146}
147
148pub fn ring_payload(
149 ring_id: u64,
150 caller_name: &str,
151 subtitle: &str,
152 video: bool,
153 labels: &RingLabels,
154) -> DesktopPayload {
155 let accept_label = if video {
156 labels.audio.clone()
157 } else {
158 labels.accept.clone()
159 };
160 let mut actions = vec![
161 DesktopAction {
162 id: action::DECLINE,
163 label: labels.decline.clone(),
164 },
165 DesktopAction {
166 id: action::ACCEPT_AUDIO,
167 label: accept_label,
168 },
169 ];
170 if video {
171 actions.push(DesktopAction {
172 id: action::ACCEPT_VIDEO,
173 label: labels.video.clone(),
174 });
175 }
176 DesktopPayload {
177 key: DesktopKey::Ring(ring_id),
178 replace_key: format!("ring:{ring_id}"),
179 summary: caller_name.to_owned(),
180 body: subtitle.to_owned(),
181 actions,
182 timeout_ms: 0,
183 urgency: Urgency::Critical,
184 category: "x-teams.call.incoming",
185 }
186}
187
188pub fn meeting_payload(toast: &MeetingToast, body: &str, labels: &MeetingLabels) -> DesktopPayload {
189 let mut actions = Vec::new();
190 if toast.phase == ReminderPhase::Soon {
191 actions.push(DesktopAction {
192 id: action::SNOOZE,
193 label: labels.snooze.clone(),
194 });
195 }
196 actions.push(DesktopAction {
197 id: action::JOIN,
198 label: labels.join.clone(),
199 });
200 DesktopPayload {
201 key: DesktopKey::Meeting(toast.id),
202 replace_key: format!("meeting:{}", toast.event_key),
203 summary: toast.subject.clone(),
204 body: body.to_owned(),
205 actions,
206 timeout_ms: 0,
207 urgency: Urgency::Normal,
208 category: "x-teams.meeting.reminder",
209 }
210}
211
212#[cfg(test)]
213mod tests {
214 use std::time::Instant;
215
216 use super::*;
217 use crate::notify::rules::{ChatKind, Preview};
218 use crate::notify::stack::{Fade, ReplyState, ToastTimer};
219
220 fn message_labels() -> MessageLabels {
221 MessageLabels {
222 mark_read: "Mark as read".into(),
223 reply: "Reply".into(),
224 }
225 }
226
227 fn ring_labels() -> RingLabels {
228 RingLabels {
229 decline: "Decline".into(),
230 accept: "Accept".into(),
231 audio: "Audio".into(),
232 video: "Video".into(),
233 }
234 }
235
236 fn meeting_labels() -> MeetingLabels {
237 MeetingLabels {
238 join: "Join".into(),
239 snooze: "Snooze".into(),
240 }
241 }
242
243 fn toast(mention: bool, urgent: bool, preview: Preview) -> ToastModel {
244 ToastModel {
245 id: 7,
246 conversation_id: "chat-a".into(),
247 message_id: "m1".into(),
248 kind: ChatKind::Direct,
249 chat_title: String::new(),
250 sender_id: Some("u1".into()),
251 sender_name: "Priya Nair".into(),
252 preview,
253 mentions_me: mention,
254 count: 1,
255 timer: ToastTimer::new(Instant::now(), mention),
256 reply: ReplyState::Closed,
257 reply_text: String::new(),
258 hovered: false,
259 time: String::new(),
260 queued: false,
261 dismissed: false,
262 fade: Fade::shown(Instant::now()),
263 agent: None,
264 status_change: false,
265 urgent,
266 org: None,
267 }
268 }
269
270 #[test]
271 fn preview_off_hides_message_text() {
272 let model = toast(false, false, Preview::Text("secret".into()));
273 let payload = message_payload(&model, false, &message_labels());
274 assert_eq!(payload.body, "New message");
275 assert_eq!(payload.replace_key, "conversation:chat-a");
276 assert_eq!(payload.timeout_ms, MESSAGE_DURATION.as_millis() as i32);
277 }
278
279 #[test]
280 fn mention_uses_longer_timeout() {
281 let model = toast(true, false, Preview::Text("hi".into()));
282 let payload = message_payload(&model, true, &message_labels());
283 assert_eq!(payload.timeout_ms, MENTION_DURATION.as_millis() as i32);
284 }
285
286 #[test]
287 fn urgent_sets_critical_urgency() {
288 let model = toast(false, true, Preview::Text("down".into()));
289 let payload = message_payload(&model, true, &message_labels());
290 assert_eq!(payload.urgency, Urgency::Critical);
291 }
292
293 #[test]
294 fn a_message_from_another_org_offers_no_actions() {
295 let mut model = toast(false, false, Preview::Text("hi".into()));
296 model.org = Some(app_base::orgs::Org {
297 id: "guest".into(),
298 name: "Guest Org".into(),
299 guest: true,
300 });
301 assert!(
302 message_payload(&model, true, &message_labels())
303 .actions
304 .is_empty()
305 );
306 }
307
308 #[test]
309 fn message_offers_mark_read_and_reply() {
310 let model = toast(false, false, Preview::Text("hi".into()));
311 let payload = message_payload(&model, true, &message_labels());
312 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
313 assert_eq!(ids, vec![action::MARK_READ, action::REPLY]);
314 }
315
316 #[test]
317 fn bundling_shares_conversation_replace_key() {
318 let first = toast(false, false, Preview::Text("one".into()));
319 let mut second = toast(false, false, Preview::Text("two".into()));
320 second.id = 99;
321 let a = message_payload(&first, true, &message_labels());
322 let b = message_payload(&second, true, &message_labels());
323 assert_eq!(a.replace_key, b.replace_key);
324 assert_ne!(a.key, b.key);
325 }
326
327 #[test]
328 fn audio_ring_has_no_timeout_and_no_video_action() {
329 let payload = ring_payload(3, "Alex", "Incoming audio call", false, &ring_labels());
330 assert_eq!(payload.timeout_ms, 0);
331 assert_eq!(payload.urgency, Urgency::Critical);
332 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
333 assert_eq!(ids, vec![action::DECLINE, action::ACCEPT_AUDIO]);
334 assert_eq!(payload.actions[1].label, "Accept");
335 }
336
337 #[test]
338 fn video_ring_offers_audio_and_video() {
339 let payload = ring_payload(3, "Alex", "Incoming video call", true, &ring_labels());
340 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
341 assert_eq!(
342 ids,
343 vec![action::DECLINE, action::ACCEPT_AUDIO, action::ACCEPT_VIDEO]
344 );
345 assert_eq!(payload.actions[1].label, "Audio");
346 }
347
348 #[test]
349 fn meeting_soon_offers_snooze_and_join() {
350 let toast = MeetingToast {
351 id: 1,
352 event_key: "e@1".into(),
353 event_id: "e".into(),
354 phase: ReminderPhase::Soon,
355 subject: "Standup".into(),
356 start: chrono::Utc::now(),
357 join_url: "https://teams.microsoft.com/l/meetup-join/x".into(),
358 };
359 let payload = meeting_payload(&toast, "in 5 minutes", &meeting_labels());
360 let ids: Vec<_> = payload.actions.iter().map(|a| a.id).collect();
361 assert_eq!(ids, vec![action::SNOOZE, action::JOIN]);
362 assert_eq!(payload.timeout_ms, 0);
363 }
364
365 #[test]
366 fn meeting_live_drops_snooze_same_replace_key() {
367 let soon = MeetingToast {
368 id: 1,
369 event_key: "e@1".into(),
370 event_id: "e".into(),
371 phase: ReminderPhase::Soon,
372 subject: "Standup".into(),
373 start: chrono::Utc::now(),
374 join_url: "https://example".into(),
375 };
376 let live = MeetingToast {
377 id: 2,
378 phase: ReminderPhase::Live,
379 ..soon.clone()
380 };
381 let a = meeting_payload(&soon, "soon", &meeting_labels());
382 let b = meeting_payload(&live, "live now", &meeting_labels());
383 assert_eq!(a.replace_key, b.replace_key);
384 assert_eq!(
385 b.actions.iter().map(|a| a.id).collect::<Vec<_>>(),
386 vec![action::JOIN]
387 );
388 }
389}