| 1 | use graph::{MentionTarget, OutgoingMention}; |
| 2 | |
| 3 | use crate::error::{Error, Result}; |
| 4 | use crate::markdown::escape_html; |
| 5 | |
| 6 | /// `text` is the display name exactly as it appears after the `@` in the message text. |
| 7 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 8 | pub struct MentionInput { |
| 9 | pub target: MentionTarget, |
| 10 | pub text: String, |
| 11 | } |
| 12 | |
| 13 | impl MentionInput { |
| 14 | pub fn user(user_id: &str, text: &str) -> Self { |
| 15 | MentionInput { |
| 16 | target: MentionTarget::User { |
| 17 | user_id: user_id.to_owned(), |
| 18 | }, |
| 19 | text: text.to_owned(), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub fn channel(channel_id: &str, text: &str) -> Self { |
| 24 | MentionInput { |
| 25 | target: MentionTarget::Channel { |
| 26 | channel_id: channel_id.to_owned(), |
| 27 | }, |
| 28 | text: text.to_owned(), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn team(team_id: &str, text: &str) -> Self { |
| 33 | MentionInput { |
| 34 | target: MentionTarget::Team { |
| 35 | team_id: team_id.to_owned(), |
| 36 | }, |
| 37 | text: text.to_owned(), |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// Mentions are matched in order, each at the first `@text` after the previous one. Unmatched mentions stay plain text. |
| 43 | pub(crate) fn apply_mentions( |
| 44 | html: &str, |
| 45 | inputs: &[MentionInput], |
| 46 | ) -> (String, Vec<OutgoingMention>) { |
| 47 | let mut output = String::with_capacity(html.len()); |
| 48 | let mut outgoing = Vec::new(); |
| 49 | let mut cursor = 0; |
| 50 | for input in inputs { |
| 51 | let escaped = escape_html(&input.text); |
| 52 | let needle = format!("@{escaped}"); |
| 53 | let Some(offset) = html[cursor..].find(&needle) else { |
| 54 | continue; |
| 55 | }; |
| 56 | let id = outgoing.len() as u32; |
| 57 | output.push_str(&html[cursor..cursor + offset]); |
| 58 | output.push_str(&format!("<at id=\"{id}\">{escaped}</at>")); |
| 59 | cursor += offset + needle.len(); |
| 60 | outgoing.push(OutgoingMention { |
| 61 | id, |
| 62 | text: input.text.clone(), |
| 63 | target: input.target.clone(), |
| 64 | }); |
| 65 | } |
| 66 | output.push_str(&html[cursor..]); |
| 67 | (output, outgoing) |
| 68 | } |
| 69 | |
| 70 | pub(crate) fn ensure_allowed_in_chat(inputs: &[MentionInput]) -> Result<()> { |
| 71 | if inputs |
| 72 | .iter() |
| 73 | .any(|input| !matches!(input.target, MentionTarget::User { .. })) |
| 74 | { |
| 75 | return Err(Error::Unsupported("a channel or team mention in a chat")); |
| 76 | } |
| 77 | Ok(()) |
| 78 | } |
| 79 | |
| 80 | #[cfg(test)] |
| 81 | mod tests { |
| 82 | use super::*; |
| 83 | |
| 84 | #[test] |
| 85 | fn mentions_become_at_tags_with_sequential_ids() { |
| 86 | let (html, mentions) = apply_mentions( |
| 87 | "hi @Ada Example and @Bo, see @General", |
| 88 | &[ |
| 89 | MentionInput::user("u1", "Ada Example"), |
| 90 | MentionInput::user("u2", "Bo"), |
| 91 | MentionInput::channel("19:c", "General"), |
| 92 | ], |
| 93 | ); |
| 94 | assert_eq!( |
| 95 | html, |
| 96 | "hi <at id=\"0\">Ada Example</at> and <at id=\"1\">Bo</at>, see <at id=\"2\">General</at>" |
| 97 | ); |
| 98 | assert_eq!( |
| 99 | mentions |
| 100 | .iter() |
| 101 | .map(|mention| mention.id) |
| 102 | .collect::<Vec<_>>(), |
| 103 | [0, 1, 2] |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn repeated_names_are_matched_left_to_right() { |
| 109 | let (html, mentions) = apply_mentions( |
| 110 | "@Ada then @Ada", |
| 111 | &[ |
| 112 | MentionInput::user("u1", "Ada"), |
| 113 | MentionInput::user("u2", "Ada"), |
| 114 | ], |
| 115 | ); |
| 116 | assert_eq!(html, "<at id=\"0\">Ada</at> then <at id=\"1\">Ada</at>"); |
| 117 | assert_eq!(mentions.len(), 2); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn unmatched_mentions_are_dropped_and_ids_stay_dense() { |
| 122 | let (html, mentions) = apply_mentions( |
| 123 | "@Bo only", |
| 124 | &[ |
| 125 | MentionInput::user("u1", "Ada"), |
| 126 | MentionInput::user("u2", "Bo"), |
| 127 | ], |
| 128 | ); |
| 129 | assert_eq!(html, "<at id=\"0\">Bo</at> only"); |
| 130 | assert_eq!(mentions.len(), 1); |
| 131 | assert_eq!(mentions[0].id, 0); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn names_are_html_escaped_like_the_body() { |
| 136 | let (html, mentions) = apply_mentions( |
| 137 | &crate::markdown_to_html("ping @Ada & Co <x>"), |
| 138 | &[MentionInput::user("u1", "Ada & Co")], |
| 139 | ); |
| 140 | assert_eq!(html, "ping <at id=\"0\">Ada & Co</at> <x>"); |
| 141 | assert_eq!(mentions[0].text, "Ada & Co"); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn mentions_inside_formatted_draft_html_become_at_tags() { |
| 146 | let (html, mentions) = apply_mentions( |
| 147 | &crate::Draft::from_markdown("**hi @Ada** and `@Bo`").to_html(), |
| 148 | &[MentionInput::user("u1", "Ada")], |
| 149 | ); |
| 150 | assert_eq!(html, "<b>hi <at id=\"0\">Ada</at></b> and <code>@Bo</code>"); |
| 151 | assert_eq!(mentions.len(), 1); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn chats_only_take_user_mentions() { |
| 156 | assert!(ensure_allowed_in_chat(&[MentionInput::user("u", "A")]).is_ok()); |
| 157 | assert!(ensure_allowed_in_chat(&[MentionInput::team("t", "Squad")]).is_err()); |
| 158 | } |
| 159 | } |