BlueGit

ict rusty-teams

Files

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

text.rs

text.rs 4.8 KiB Download Raw
1use super::rules::{ChatKind, Preview};
2use super::stack::ToastModel;
3use crate::format;
4
5const PREVIEW_OFF: &str = "New message";
6const IMAGE_LABEL: &str = "Image";
7const UNKNOWN_SENDER: &str = "Unknown";
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ToastText {
11 pub title: String,
12 pub subtitle: String,
13 pub preview: String,
14 pub image_only: bool,
15}
16
17impl ToastText {
18 pub fn narration(&self) -> String {
19 format!("{}, {}, {}", self.title, self.subtitle, self.preview)
20 }
21}
22
23pub fn describe(model: &ToastModel, preview_on: bool) -> ToastText {
24 let sender = if model.sender_name.trim().is_empty() {
25 UNKNOWN_SENDER
26 } else {
27 model.sender_name.as_str()
28 };
29 let (title, subtitle) = match &model.kind {
30 ChatKind::Direct => (sender.to_owned(), "Direct".to_owned()),
31 ChatKind::Group { member_count } => (
32 model.chat_title.clone(),
33 format!("Group, {member_count} people"),
34 ),
35 ChatKind::Channel { team, channel } => (sender.to_owned(), format!("{team} > {channel}")),
36 };
37 let body = match (&model.preview, preview_on) {
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
56/// Byte range of the first word after each `@`, for highlighting mentions.
57pub fn mention_ranges(text: &str) -> Vec<std::ops::Range<usize>> {
58 let mut ranges = Vec::new();
59 let mut search_from = 0;
60 while let Some(offset) = text[search_from..].find('@') {
61 let start = search_from + offset;
62 let end = text[start + 1..]
63 .char_indices()
64 .find(|(_, character)| !character.is_alphanumeric())
65 .map_or(text.len(), |(index, _)| start + 1 + index);
66 if end > start + 1 {
67 ranges.push(start..end);
68 }
69 search_from = end.max(start + 1);
70 }
71 ranges
72}
73
74#[cfg(test)]
75mod tests {
76 use std::time::Instant;
77
78 use super::*;
79 use crate::notify::stack::{Fade, ReplyState, ToastTimer};
80
81 fn model(kind: ChatKind, preview: Preview) -> ToastModel {
82 ToastModel {
83 id: 1,
84 conversation_id: "c".into(),
85 message_id: "m".into(),
86 kind,
87 chat_title: "Retro-Team".into(),
88 sender_id: None,
89 sender_name: "Priya Nair".into(),
90 preview,
91 mentions_me: false,
92 count: 1,
93 timer: ToastTimer::new(Instant::now(), false),
94 reply: ReplyState::Closed,
95 reply_text: String::new(),
96 hovered: false,
97 time: "14:30".into(),
98 queued: false,
99 dismissed: false,
100 fade: Fade::shown(Instant::now()),
101 }
102 }
103
104 #[test]
105 fn direct_shows_sender_and_plain_preview() {
106 let text = describe(&model(ChatKind::Direct, Preview::Text("Hello".into())), true);
107 assert_eq!(text.title, "Priya Nair");
108 assert_eq!(text.subtitle, "Direct");
109 assert_eq!(text.preview, "Hello");
110 }
111
112 #[test]
113 fn group_shows_chat_title_and_prefixes_sender() {
114 let text = describe(
115 &model(ChatKind::Group { member_count: 5 }, Preview::Text("Hi".into())),
116 true,
117 );
118 assert_eq!(text.title, "Retro-Team");
119 assert_eq!(text.subtitle, "Group, 5 people");
120 assert_eq!(text.preview, "Priya: Hi");
121 }
122
123 #[test]
124 fn channel_shows_path_and_prefixes_sender() {
125 let kind = ChatKind::Channel {
126 team: "Platform".into(),
127 channel: "Releases".into(),
128 };
129 let text = describe(&model(kind, Preview::Text("Go".into())), true);
130 assert_eq!(text.title, "Priya Nair");
131 assert_eq!(text.subtitle, "Platform > Releases");
132 assert_eq!(text.preview, "Priya: Go");
133 }
134
135 #[test]
136 fn preview_off_hides_content_but_keeps_name() {
137 let text = describe(&model(ChatKind::Direct, Preview::Text("geheim".into())), false);
138 assert_eq!(text.preview, "New message");
139 assert!(!text.narration().contains("geheim"));
140 }
141
142 #[test]
143 fn image_only_is_flagged() {
144 let text = describe(&model(ChatKind::Direct, Preview::Image), true);
145 assert_eq!(text.preview, "Image");
146 assert!(text.image_only);
147 }
148
149 #[test]
150 fn mention_ranges_cover_first_word_after_at() {
151 let text = "Ping @Jonas can you ask @Mara @";
152 let found: Vec<&str> = mention_ranges(text).into_iter().map(|range| &text[range]).collect();
153 assert_eq!(found, ["@Jonas", "@Mara"]);
154 }
155}