| 1 | use super::rules::{ChatKind, Preview}; |
| 2 | use super::stack::ToastModel; |
| 3 | use crate::format; |
| 4 | |
| 5 | const PREVIEW_OFF: &str = "New message"; |
| 6 | const IMAGE_LABEL: &str = "Image"; |
| 7 | const UNKNOWN_SENDER: &str = "Unknown"; |
| 8 | |
| 9 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 10 | pub struct ToastText { |
| 11 | pub title: String, |
| 12 | pub subtitle: String, |
| 13 | pub preview: String, |
| 14 | pub image_only: bool, |
| 15 | } |
| 16 | |
| 17 | impl ToastText { |
| 18 | pub fn narration(&self) -> String { |
| 19 | format!("{}, {}, {}", self.title, self.subtitle, self.preview) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub 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. |
| 57 | pub 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)] |
| 75 | mod 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 | agent: None, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn direct_shows_sender_and_plain_preview() { |
| 107 | let text = describe( |
| 108 | &model(ChatKind::Direct, Preview::Text("Hello".into())), |
| 109 | true, |
| 110 | ); |
| 111 | assert_eq!(text.title, "Priya Nair"); |
| 112 | assert_eq!(text.subtitle, "Direct"); |
| 113 | assert_eq!(text.preview, "Hello"); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn group_shows_chat_title_and_prefixes_sender() { |
| 118 | let text = describe( |
| 119 | &model( |
| 120 | ChatKind::Group { member_count: 5 }, |
| 121 | Preview::Text("Hi".into()), |
| 122 | ), |
| 123 | true, |
| 124 | ); |
| 125 | assert_eq!(text.title, "Retro-Team"); |
| 126 | assert_eq!(text.subtitle, "Group, 5 people"); |
| 127 | assert_eq!(text.preview, "Priya: Hi"); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn channel_shows_path_and_prefixes_sender() { |
| 132 | let kind = ChatKind::Channel { |
| 133 | team: "Platform".into(), |
| 134 | channel: "Releases".into(), |
| 135 | }; |
| 136 | let text = describe(&model(kind, Preview::Text("Go".into())), true); |
| 137 | assert_eq!(text.title, "Priya Nair"); |
| 138 | assert_eq!(text.subtitle, "Platform > Releases"); |
| 139 | assert_eq!(text.preview, "Priya: Go"); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn preview_off_hides_content_but_keeps_name() { |
| 144 | let text = describe( |
| 145 | &model(ChatKind::Direct, Preview::Text("geheim".into())), |
| 146 | false, |
| 147 | ); |
| 148 | assert_eq!(text.preview, "New message"); |
| 149 | assert!(!text.narration().contains("geheim")); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn image_only_is_flagged() { |
| 154 | let text = describe(&model(ChatKind::Direct, Preview::Image), true); |
| 155 | assert_eq!(text.preview, "Image"); |
| 156 | assert!(text.image_only); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn mention_ranges_cover_first_word_after_at() { |
| 161 | let text = "Ping @Jonas can you ask @Mara @"; |
| 162 | let found: Vec<&str> = mention_ranges(text) |
| 163 | .into_iter() |
| 164 | .map(|range| &text[range]) |
| 165 | .collect(); |
| 166 | assert_eq!(found, ["@Jonas", "@Mara"]); |
| 167 | } |
| 168 | } |