BlueGit

ict rusty-teams

Files

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

text.rs

feat(presence): notify when available
metis Daniel Däschle · pushed by metis · 3 weeks ago
19beb28e History
text.rs 5.8 KiB Download Raw
1use super::rules::{ChatKind, Preview};
2use super::stack::ToastModel;
3use crate::format;
4use crate::importance::urgent_toast_title;
5
6const PREVIEW_OFF: &str = "New message";
7const IMAGE_LABEL: &str = "Image";
8const UNKNOWN_SENDER: &str = "Unknown";
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct ToastText {
12 pub title: String,
13 pub subtitle: String,
14 pub preview: String,
15 pub image_only: bool,
16}
17
18impl ToastText {
19 pub fn narration(&self) -> String {
20 format!("{}, {}, {}", self.title, self.subtitle, self.preview)
21 }
22}
23
24pub fn describe(model: &ToastModel, preview_on: bool) -> ToastText {
25 let sender = if model.sender_name.trim().is_empty() {
26 UNKNOWN_SENDER
27 } else {
28 model.sender_name.as_str()
29 };
30 let title = match &model.kind {
31 _ if model.urgent => urgent_toast_title(sender),
32 ChatKind::Group { .. } => model.chat_title.clone(),
33 ChatKind::Direct | ChatKind::Channel { .. } => sender.to_owned(),
34 };
35 let subtitle = subtitle_of(&model.kind);
36 let body = match (&model.preview, preview_on) {
37 (Preview::Text(text), false) if model.status_change => text.clone(),
38 (_, false) => PREVIEW_OFF.to_owned(),
39 (Preview::Text(text), true) => text.clone(),
40 (Preview::Image, true) => IMAGE_LABEL.to_owned(),
41 };
42 let prefixed = preview_on && model.kind != ChatKind::Direct;
43 let preview = if prefixed {
44 format!("{}: {body}", format::first_name(sender))
45 } else {
46 body
47 };
48 ToastText {
49 title,
50 subtitle,
51 preview,
52 image_only: preview_on && model.preview == Preview::Image,
53 }
54}
55
56fn subtitle_of(kind: &ChatKind) -> String {
57 match kind {
58 ChatKind::Direct => "Direct".to_owned(),
59 ChatKind::Group { member_count } => format!("Group, {member_count} people"),
60 ChatKind::Channel { team, channel } => format!("{team} > {channel}"),
61 }
62}
63
64/// Byte range of the first word after each `@`, for highlighting mentions.
65pub fn mention_ranges(text: &str) -> Vec<std::ops::Range<usize>> {
66 let mut ranges = Vec::new();
67 let mut search_from = 0;
68 while let Some(offset) = text[search_from..].find('@') {
69 let start = search_from + offset;
70 let end = text[start + 1..]
71 .char_indices()
72 .find(|(_, character)| !character.is_alphanumeric())
73 .map_or(text.len(), |(index, _)| start + 1 + index);
74 if end > start + 1 {
75 ranges.push(start..end);
76 }
77 search_from = end.max(start + 1);
78 }
79 ranges
80}
81
82#[cfg(test)]
83mod tests {
84 use std::time::Instant;
85
86 use super::*;
87 use crate::notify::stack::{Fade, ReplyState, ToastTimer};
88
89 fn model(kind: ChatKind, preview: Preview) -> ToastModel {
90 ToastModel {
91 id: 1,
92 conversation_id: "c".into(),
93 message_id: "m".into(),
94 kind,
95 chat_title: "Retro-Team".into(),
96 sender_id: None,
97 sender_name: "Priya Nair".into(),
98 preview,
99 mentions_me: false,
100 count: 1,
101 timer: ToastTimer::new(Instant::now(), false),
102 reply: ReplyState::Closed,
103 reply_text: String::new(),
104 hovered: false,
105 time: "14:30".into(),
106 queued: false,
107 dismissed: false,
108 fade: Fade::shown(Instant::now()),
109 agent: None,
110 status_change: false,
111 urgent: false,
112 }
113 }
114
115 #[test]
116 fn direct_shows_sender_and_plain_preview() {
117 let text = describe(
118 &model(ChatKind::Direct, Preview::Text("Hello".into())),
119 true,
120 );
121 assert_eq!(text.title, "Priya Nair");
122 assert_eq!(text.subtitle, "Direct");
123 assert_eq!(text.preview, "Hello");
124 }
125
126 #[test]
127 fn urgent_names_the_sender_like_teams() {
128 let mut urgent = model(
129 ChatKind::Group { member_count: 5 },
130 Preview::Text("Server down".into()),
131 );
132 urgent.urgent = true;
133 let text = describe(&urgent, true);
134 assert_eq!(text.title, "Priya Nair sent an urgent message");
135 assert_eq!(text.subtitle, "Group, 5 people");
136 assert_eq!(text.preview, "Priya: Server down");
137 }
138
139 #[test]
140 fn group_shows_chat_title_and_prefixes_sender() {
141 let text = describe(
142 &model(
143 ChatKind::Group { member_count: 5 },
144 Preview::Text("Hi".into()),
145 ),
146 true,
147 );
148 assert_eq!(text.title, "Retro-Team");
149 assert_eq!(text.subtitle, "Group, 5 people");
150 assert_eq!(text.preview, "Priya: Hi");
151 }
152
153 #[test]
154 fn channel_shows_path_and_prefixes_sender() {
155 let kind = ChatKind::Channel {
156 team: "Platform".into(),
157 channel: "Releases".into(),
158 };
159 let text = describe(&model(kind, Preview::Text("Go".into())), true);
160 assert_eq!(text.title, "Priya Nair");
161 assert_eq!(text.subtitle, "Platform > Releases");
162 assert_eq!(text.preview, "Priya: Go");
163 }
164
165 #[test]
166 fn preview_off_hides_content_but_keeps_name() {
167 let text = describe(
168 &model(ChatKind::Direct, Preview::Text("geheim".into())),
169 false,
170 );
171 assert_eq!(text.preview, "New message");
172 assert!(!text.narration().contains("geheim"));
173 }
174
175 #[test]
176 fn image_only_is_flagged() {
177 let text = describe(&model(ChatKind::Direct, Preview::Image), true);
178 assert_eq!(text.preview, "Image");
179 assert!(text.image_only);
180 }
181
182 #[test]
183 fn mention_ranges_cover_first_word_after_at() {
184 let text = "Ping @Jonas can you ask @Mara @";
185 let found: Vec<&str> = mention_ranges(text)
186 .into_iter()
187 .map(|range| &text[range])
188 .collect();
189 assert_eq!(found, ["@Jonas", "@Mara"]);
190 }
191}