| 1 | use graph::{MentionTarget, OutgoingMention}; |
| 2 | use serde_json::{Value, json}; |
| 3 | use store::MessageRecord; |
| 4 | |
| 5 | use crate::markdown::escape_html; |
| 6 | use crate::preview::preview_text; |
| 7 | |
| 8 | const REPLY_ITEMTYPE: &str = "http://schema.skype.com/Reply"; |
| 9 | const MENTION_ITEMTYPE: &str = "http://schema.skype.com/Mention"; |
| 10 | const ORGID_PREFIX: &str = "8:orgid:"; |
| 11 | const BOT_PREFIX: &str = "28:"; |
| 12 | const FORMAT_VARIANT: &str = "TEAMS"; |
| 13 | |
| 14 | #[derive(Debug, Clone, PartialEq)] |
| 15 | pub(crate) struct TeamsQuote { |
| 16 | pub html: String, |
| 17 | pub quoted_message: Value, |
| 18 | } |
| 19 | |
| 20 | /// Teams' `createBlockquoteTag`: no quote without sender, name or preview. |
| 21 | pub(crate) fn teams_quote(record: &MessageRecord) -> Option<TeamsQuote> { |
| 22 | let sender_mri = sender_mri(record)?; |
| 23 | let sender_name = record |
| 24 | .sender_name |
| 25 | .as_deref() |
| 26 | .filter(|name| !name.is_empty())?; |
| 27 | let preview = preview_text(&record.body_html).filter(|preview| !preview.is_empty())?; |
| 28 | let time = record.created_at.timestamp_millis(); |
| 29 | let html = format!( |
| 30 | "<blockquote itemscope=\"\" itemtype=\"{REPLY_ITEMTYPE}\" itemid=\"{}\"><strong itemprop=\"mri\" itemid=\"{}\">{}</strong><span itemprop=\"time\" itemid=\"{time}\"></span><p itemprop=\"preview\">{}</p></blockquote>", |
| 31 | escape_html(&record.message_id), |
| 32 | escape_html(&sender_mri), |
| 33 | escape_html(sender_name), |
| 34 | escape_html(&preview), |
| 35 | ); |
| 36 | let quoted_message = json!({ |
| 37 | "messageId": record.message_id, |
| 38 | "sender": sender_mri, |
| 39 | "time": time, |
| 40 | }); |
| 41 | Some(TeamsQuote { |
| 42 | html, |
| 43 | quoted_message, |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | /// The `properties` Teams' `sendMessageInConv` writes; `None` when a mention has no chatsvc shape here. |
| 48 | pub(crate) fn message_properties( |
| 49 | quote: Option<&TeamsQuote>, |
| 50 | mentions: &[OutgoingMention], |
| 51 | importance: Option<&str>, |
| 52 | files: &Value, |
| 53 | ) -> Option<Value> { |
| 54 | let mention_entries = mention_entries(mentions)?; |
| 55 | let quoted: Vec<Value> = quote |
| 56 | .map(|quote| quote.quoted_message.clone()) |
| 57 | .into_iter() |
| 58 | .collect(); |
| 59 | Some(json!({ |
| 60 | "importance": importance.unwrap_or_default(), |
| 61 | "links": "[]", |
| 62 | "mentions": Value::Array(mention_entries).to_string(), |
| 63 | "onbehalfof": null, |
| 64 | "files": files.to_string(), |
| 65 | "qtdMsgs": Value::Array(quoted).to_string(), |
| 66 | "formatVariant": FORMAT_VARIANT, |
| 67 | })) |
| 68 | } |
| 69 | |
| 70 | /// The chatsvc `mentions` entries; `None` for a team or tag mention (their chatsvc ids are not at hand). |
| 71 | pub(crate) fn mention_entries(mentions: &[OutgoingMention]) -> Option<Vec<Value>> { |
| 72 | mentions.iter().map(mention_entry).collect() |
| 73 | } |
| 74 | |
| 75 | fn mention_entry(mention: &OutgoingMention) -> Option<Value> { |
| 76 | let (mri, mention_type) = match &mention.target { |
| 77 | MentionTarget::User { user_id } => (user_mri(user_id), "person"), |
| 78 | MentionTarget::Everyone { chat_id } => (chat_id.clone(), "everyone"), |
| 79 | MentionTarget::Channel { channel_id } => (channel_id.clone(), "channel"), |
| 80 | MentionTarget::Team { .. } | MentionTarget::Tag { .. } => return None, |
| 81 | }; |
| 82 | Some(json!({ |
| 83 | "@type": MENTION_ITEMTYPE, |
| 84 | "itemid": mention.id.to_string(), |
| 85 | "mri": mri, |
| 86 | "mentionType": mention_type, |
| 87 | "displayName": mention.text, |
| 88 | })) |
| 89 | } |
| 90 | |
| 91 | pub(crate) fn chatsvc_mention_html(html: &str, mentions: &[OutgoingMention]) -> String { |
| 92 | let mut converted = html.to_owned(); |
| 93 | for mention in mentions { |
| 94 | converted = converted.replace( |
| 95 | &format!("<at id=\"{}\">", mention.id), |
| 96 | &format!( |
| 97 | "<span itemscope=\"\" itemtype=\"{MENTION_ITEMTYPE}\" itemid=\"{}\">", |
| 98 | mention.id |
| 99 | ), |
| 100 | ); |
| 101 | } |
| 102 | converted.replace("</at>", "</span>") |
| 103 | } |
| 104 | |
| 105 | fn sender_mri(record: &MessageRecord) -> Option<String> { |
| 106 | match (&record.sender_id, &record.sender_application_id) { |
| 107 | (Some(user_id), _) => Some(user_mri(user_id)), |
| 108 | (None, Some(application_id)) if application_id.contains(':') => { |
| 109 | Some(application_id.clone()) |
| 110 | } |
| 111 | (None, Some(application_id)) => Some(format!("{BOT_PREFIX}{application_id}")), |
| 112 | (None, None) => None, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn user_mri(user_id: &str) -> String { |
| 117 | if user_id.contains(':') { |
| 118 | user_id.to_owned() |
| 119 | } else { |
| 120 | format!("{ORGID_PREFIX}{user_id}") |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #[cfg(test)] |
| 125 | mod tests { |
| 126 | use super::*; |
| 127 | |
| 128 | fn mention(id: u32, target: MentionTarget) -> OutgoingMention { |
| 129 | OutgoingMention { |
| 130 | id, |
| 131 | text: "Ada".into(), |
| 132 | target, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn quote() -> TeamsQuote { |
| 137 | TeamsQuote { |
| 138 | html: String::new(), |
| 139 | quoted_message: json!({"messageId": "1"}), |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn a_quote_reply_gets_the_teams_property_set() { |
| 145 | let properties = message_properties(Some("e()), &[], None, &json!([])).unwrap(); |
| 146 | assert_eq!( |
| 147 | properties, |
| 148 | json!({ |
| 149 | "importance": "", |
| 150 | "links": "[]", |
| 151 | "mentions": "[]", |
| 152 | "onbehalfof": null, |
| 153 | "files": "[]", |
| 154 | "qtdMsgs": "[{\"messageId\":\"1\"}]", |
| 155 | "formatVariant": "TEAMS", |
| 156 | }) |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn person_mentions_become_teams_spans_and_a_mentions_property() { |
| 162 | let mentions = [mention( |
| 163 | 0, |
| 164 | MentionTarget::User { |
| 165 | user_id: "user-ada".into(), |
| 166 | }, |
| 167 | )]; |
| 168 | let html = chatsvc_mention_html("hi <at id=\"0\">Ada</at>", &mentions); |
| 169 | assert_eq!( |
| 170 | html, |
| 171 | "hi <span itemscope=\"\" itemtype=\"http://schema.skype.com/Mention\" itemid=\"0\">Ada</span>" |
| 172 | ); |
| 173 | let properties = |
| 174 | message_properties(Some("e()), &mentions, Some("high"), &json!([])).unwrap(); |
| 175 | assert_eq!(properties["importance"], "high"); |
| 176 | assert_eq!(properties["qtdMsgs"], "[{\"messageId\":\"1\"}]"); |
| 177 | let entries: Value = |
| 178 | serde_json::from_str(properties["mentions"].as_str().unwrap()).unwrap(); |
| 179 | assert_eq!( |
| 180 | entries, |
| 181 | json!([{ |
| 182 | "@type": "http://schema.skype.com/Mention", |
| 183 | "itemid": "0", |
| 184 | "mri": "8:orgid:user-ada", |
| 185 | "mentionType": "person", |
| 186 | "displayName": "Ada", |
| 187 | }]) |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn an_everyone_mention_names_the_chat_itself() { |
| 193 | let everyone = mention( |
| 194 | 0, |
| 195 | MentionTarget::Everyone { |
| 196 | chat_id: "19:g@thread.v2".into(), |
| 197 | }, |
| 198 | ); |
| 199 | let entries = mention_entries(&[everyone]).unwrap(); |
| 200 | assert_eq!(entries[0]["mri"], "19:g@thread.v2"); |
| 201 | assert_eq!(entries[0]["mentionType"], "everyone"); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn a_channel_mention_names_the_channel_thread() { |
| 206 | let channel = mention( |
| 207 | 1, |
| 208 | MentionTarget::Channel { |
| 209 | channel_id: "19:c@thread.tacv2".into(), |
| 210 | }, |
| 211 | ); |
| 212 | let entries = mention_entries(&[channel]).unwrap(); |
| 213 | assert_eq!(entries[0]["mri"], "19:c@thread.tacv2"); |
| 214 | assert_eq!(entries[0]["mentionType"], "channel"); |
| 215 | assert_eq!(entries[0]["itemid"], "1"); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn team_and_tag_mentions_have_no_chatsvc_shape() { |
| 220 | for target in [ |
| 221 | MentionTarget::Team { |
| 222 | team_id: "team-1".into(), |
| 223 | }, |
| 224 | MentionTarget::Tag { |
| 225 | tag_id: "tag-1".into(), |
| 226 | }, |
| 227 | ] { |
| 228 | let mentions = [mention(0, target)]; |
| 229 | assert!(message_properties(Some("e()), &mentions, None, &json!([])).is_none()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn a_bot_sender_is_quoted_with_its_28_mri() { |
| 235 | let record = MessageRecord { |
| 236 | message_id: "5".into(), |
| 237 | sender_name: Some("Echo".into()), |
| 238 | sender_application_id: Some("app-1".into()), |
| 239 | body_html: "<p>pong</p>".into(), |
| 240 | ..MessageRecord::default() |
| 241 | }; |
| 242 | let quote = teams_quote(&record).unwrap(); |
| 243 | assert_eq!(quote.quoted_message["sender"], "28:app-1"); |
| 244 | assert!(quote.html.contains("itemid=\"28:app-1\">Echo</strong>")); |
| 245 | } |
| 246 | } |