BlueGit

ict rusty-teams

Files

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

text.rs

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