| 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 quote_reply_properties( |
| 49 | quote: &TeamsQuote, |
| 50 | mentions: &[OutgoingMention], |
| 51 | importance: Option<&str>, |
| 52 | ) -> Option<Value> { |
| 53 | let mention_entries = mentions |
| 54 | .iter() |
| 55 | .map(mention_entry) |
| 56 | .collect::<Option<Vec<_>>>()?; |
| 57 | Some(json!({ |
| 58 | "importance": importance.unwrap_or_default(), |
| 59 | "links": "[]", |
| 60 | "mentions": Value::Array(mention_entries).to_string(), |
| 61 | "onbehalfof": null, |
| 62 | "files": "[]", |
| 63 | "qtdMsgs": Value::Array(vec![quote.quoted_message.clone()]).to_string(), |
| 64 | "formatVariant": FORMAT_VARIANT, |
| 65 | })) |
| 66 | } |
| 67 | |
| 68 | fn mention_entry(mention: &OutgoingMention) -> Option<Value> { |
| 69 | let MentionTarget::User { user_id } = &mention.target else { |
| 70 | return None; |
| 71 | }; |
| 72 | Some(json!({ |
| 73 | "@type": MENTION_ITEMTYPE, |
| 74 | "itemid": mention.id.to_string(), |
| 75 | "mri": user_mri(user_id), |
| 76 | "mentionType": "person", |
| 77 | "displayName": mention.text, |
| 78 | })) |
| 79 | } |
| 80 | |
| 81 | pub(crate) fn chatsvc_mention_html(html: &str, mentions: &[OutgoingMention]) -> String { |
| 82 | let mut converted = html.to_owned(); |
| 83 | for mention in mentions { |
| 84 | converted = converted.replace( |
| 85 | &format!("<at id=\"{}\">", mention.id), |
| 86 | &format!( |
| 87 | "<span itemscope=\"\" itemtype=\"{MENTION_ITEMTYPE}\" itemid=\"{}\">", |
| 88 | mention.id |
| 89 | ), |
| 90 | ); |
| 91 | } |
| 92 | converted.replace("</at>", "</span>") |
| 93 | } |
| 94 | |
| 95 | fn sender_mri(record: &MessageRecord) -> Option<String> { |
| 96 | match (&record.sender_id, &record.sender_application_id) { |
| 97 | (Some(user_id), _) => Some(user_mri(user_id)), |
| 98 | (None, Some(application_id)) if application_id.contains(':') => { |
| 99 | Some(application_id.clone()) |
| 100 | } |
| 101 | (None, Some(application_id)) => Some(format!("{BOT_PREFIX}{application_id}")), |
| 102 | (None, None) => None, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn user_mri(user_id: &str) -> String { |
| 107 | if user_id.contains(':') { |
| 108 | user_id.to_owned() |
| 109 | } else { |
| 110 | format!("{ORGID_PREFIX}{user_id}") |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[cfg(test)] |
| 115 | mod tests { |
| 116 | use super::*; |
| 117 | |
| 118 | fn mention(id: u32, target: MentionTarget) -> OutgoingMention { |
| 119 | OutgoingMention { |
| 120 | id, |
| 121 | text: "Ada".into(), |
| 122 | target, |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | fn quote() -> TeamsQuote { |
| 127 | TeamsQuote { |
| 128 | html: String::new(), |
| 129 | quoted_message: json!({"messageId": "1"}), |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn a_quote_reply_gets_the_teams_property_set() { |
| 135 | let properties = quote_reply_properties("e(), &[], None).unwrap(); |
| 136 | assert_eq!( |
| 137 | properties, |
| 138 | json!({ |
| 139 | "importance": "", |
| 140 | "links": "[]", |
| 141 | "mentions": "[]", |
| 142 | "onbehalfof": null, |
| 143 | "files": "[]", |
| 144 | "qtdMsgs": "[{\"messageId\":\"1\"}]", |
| 145 | "formatVariant": "TEAMS", |
| 146 | }) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn person_mentions_become_teams_spans_and_a_mentions_property() { |
| 152 | let mentions = [mention( |
| 153 | 0, |
| 154 | MentionTarget::User { |
| 155 | user_id: "user-ada".into(), |
| 156 | }, |
| 157 | )]; |
| 158 | let html = chatsvc_mention_html("hi <at id=\"0\">Ada</at>", &mentions); |
| 159 | assert_eq!( |
| 160 | html, |
| 161 | "hi <span itemscope=\"\" itemtype=\"http://schema.skype.com/Mention\" itemid=\"0\">Ada</span>" |
| 162 | ); |
| 163 | let properties = quote_reply_properties("e(), &mentions, Some("high")).unwrap(); |
| 164 | assert_eq!(properties["importance"], "high"); |
| 165 | assert_eq!(properties["qtdMsgs"], "[{\"messageId\":\"1\"}]"); |
| 166 | let entries: Value = |
| 167 | serde_json::from_str(properties["mentions"].as_str().unwrap()).unwrap(); |
| 168 | assert_eq!( |
| 169 | entries, |
| 170 | json!([{ |
| 171 | "@type": "http://schema.skype.com/Mention", |
| 172 | "itemid": "0", |
| 173 | "mri": "8:orgid:user-ada", |
| 174 | "mentionType": "person", |
| 175 | "displayName": "Ada", |
| 176 | }]) |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn group_mentions_have_no_chatsvc_shape() { |
| 182 | for target in [ |
| 183 | MentionTarget::Channel { |
| 184 | channel_id: "19:c@thread.tacv2".into(), |
| 185 | }, |
| 186 | MentionTarget::Team { |
| 187 | team_id: "team-1".into(), |
| 188 | }, |
| 189 | MentionTarget::Everyone { |
| 190 | chat_id: "19:g@thread.v2".into(), |
| 191 | }, |
| 192 | MentionTarget::Tag { |
| 193 | tag_id: "tag-1".into(), |
| 194 | }, |
| 195 | ] { |
| 196 | let mentions = [mention(0, target)]; |
| 197 | assert!(quote_reply_properties("e(), &mentions, None).is_none()); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn a_bot_sender_is_quoted_with_its_28_mri() { |
| 203 | let record = MessageRecord { |
| 204 | message_id: "5".into(), |
| 205 | sender_name: Some("Echo".into()), |
| 206 | sender_application_id: Some("app-1".into()), |
| 207 | body_html: "<p>pong</p>".into(), |
| 208 | ..MessageRecord::default() |
| 209 | }; |
| 210 | let quote = teams_quote(&record).unwrap(); |
| 211 | assert_eq!(quote.quoted_message["sender"], "28:app-1"); |
| 212 | assert!(quote.html.contains("itemid=\"28:app-1\">Echo</strong>")); |
| 213 | } |
| 214 | } |