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