use graph::{MentionTarget, OutgoingMention}; use serde_json::{Value, json}; use store::MessageRecord; use crate::markdown::escape_html; use crate::preview::preview_text; const REPLY_ITEMTYPE: &str = "http://schema.skype.com/Reply"; const MENTION_ITEMTYPE: &str = "http://schema.skype.com/Mention"; const ORGID_PREFIX: &str = "8:orgid:"; const BOT_PREFIX: &str = "28:"; const FORMAT_VARIANT: &str = "TEAMS"; #[derive(Debug, Clone, PartialEq)] pub(crate) struct TeamsQuote { pub html: String, pub quoted_message: Value, } /// Teams' `createBlockquoteTag`: no quote without sender, name or preview. pub(crate) fn teams_quote(record: &MessageRecord) -> Option { let sender_mri = sender_mri(record)?; let sender_name = record .sender_name .as_deref() .filter(|name| !name.is_empty())?; let preview = preview_text(&record.body_html).filter(|preview| !preview.is_empty())?; let time = record.created_at.timestamp_millis(); let html = format!( "
{}

{}

", escape_html(&record.message_id), escape_html(&sender_mri), escape_html(sender_name), escape_html(&preview), ); let quoted_message = json!({ "messageId": record.message_id, "sender": sender_mri, "time": time, }); Some(TeamsQuote { html, quoted_message, }) } /// The `properties` Teams' `sendMessageInConv` writes; `None` when a mention has no chatsvc shape here. pub(crate) fn message_properties( quote: Option<&TeamsQuote>, mentions: &[OutgoingMention], importance: Option<&str>, files: &Value, ) -> Option { let mention_entries = mention_entries(mentions)?; let quoted: Vec = quote .map(|quote| quote.quoted_message.clone()) .into_iter() .collect(); Some(json!({ "importance": importance.unwrap_or_default(), "links": "[]", "mentions": Value::Array(mention_entries).to_string(), "onbehalfof": null, "files": files.to_string(), "qtdMsgs": Value::Array(quoted).to_string(), "formatVariant": FORMAT_VARIANT, })) } /// The chatsvc `mentions` entries; `None` for a team or tag mention (their chatsvc ids are not at hand). pub(crate) fn mention_entries(mentions: &[OutgoingMention]) -> Option> { mentions.iter().map(mention_entry).collect() } fn mention_entry(mention: &OutgoingMention) -> Option { let (mri, mention_type) = match &mention.target { MentionTarget::User { user_id } => (user_mri(user_id), "person"), MentionTarget::Everyone { chat_id } => (chat_id.clone(), "everyone"), MentionTarget::Channel { channel_id } => (channel_id.clone(), "channel"), MentionTarget::Team { .. } | MentionTarget::Tag { .. } => return None, }; Some(json!({ "@type": MENTION_ITEMTYPE, "itemid": mention.id.to_string(), "mri": mri, "mentionType": mention_type, "displayName": mention.text, })) } pub(crate) fn chatsvc_mention_html(html: &str, mentions: &[OutgoingMention]) -> String { let mut converted = html.to_owned(); for mention in mentions { converted = converted.replace( &format!("", mention.id), &format!( "", mention.id ), ); } converted.replace("", "") } fn sender_mri(record: &MessageRecord) -> Option { match (&record.sender_id, &record.sender_application_id) { (Some(user_id), _) => Some(user_mri(user_id)), (None, Some(application_id)) if application_id.contains(':') => { Some(application_id.clone()) } (None, Some(application_id)) => Some(format!("{BOT_PREFIX}{application_id}")), (None, None) => None, } } fn user_mri(user_id: &str) -> String { if user_id.contains(':') { user_id.to_owned() } else { format!("{ORGID_PREFIX}{user_id}") } } #[cfg(test)] mod tests { use super::*; fn mention(id: u32, target: MentionTarget) -> OutgoingMention { OutgoingMention { id, text: "Ada".into(), target, } } fn quote() -> TeamsQuote { TeamsQuote { html: String::new(), quoted_message: json!({"messageId": "1"}), } } #[test] fn a_quote_reply_gets_the_teams_property_set() { let properties = message_properties(Some("e()), &[], None, &json!([])).unwrap(); assert_eq!( properties, json!({ "importance": "", "links": "[]", "mentions": "[]", "onbehalfof": null, "files": "[]", "qtdMsgs": "[{\"messageId\":\"1\"}]", "formatVariant": "TEAMS", }) ); } #[test] fn person_mentions_become_teams_spans_and_a_mentions_property() { let mentions = [mention( 0, MentionTarget::User { user_id: "user-ada".into(), }, )]; let html = chatsvc_mention_html("hi Ada", &mentions); assert_eq!( html, "hi Ada" ); let properties = message_properties(Some("e()), &mentions, Some("high"), &json!([])).unwrap(); assert_eq!(properties["importance"], "high"); assert_eq!(properties["qtdMsgs"], "[{\"messageId\":\"1\"}]"); let entries: Value = serde_json::from_str(properties["mentions"].as_str().unwrap()).unwrap(); assert_eq!( entries, json!([{ "@type": "http://schema.skype.com/Mention", "itemid": "0", "mri": "8:orgid:user-ada", "mentionType": "person", "displayName": "Ada", }]) ); } #[test] fn an_everyone_mention_names_the_chat_itself() { let everyone = mention( 0, MentionTarget::Everyone { chat_id: "19:g@thread.v2".into(), }, ); let entries = mention_entries(&[everyone]).unwrap(); assert_eq!(entries[0]["mri"], "19:g@thread.v2"); assert_eq!(entries[0]["mentionType"], "everyone"); } #[test] fn a_channel_mention_names_the_channel_thread() { let channel = mention( 1, MentionTarget::Channel { channel_id: "19:c@thread.tacv2".into(), }, ); let entries = mention_entries(&[channel]).unwrap(); assert_eq!(entries[0]["mri"], "19:c@thread.tacv2"); assert_eq!(entries[0]["mentionType"], "channel"); assert_eq!(entries[0]["itemid"], "1"); } #[test] fn team_and_tag_mentions_have_no_chatsvc_shape() { for target in [ MentionTarget::Team { team_id: "team-1".into(), }, MentionTarget::Tag { tag_id: "tag-1".into(), }, ] { let mentions = [mention(0, target)]; assert!(message_properties(Some("e()), &mentions, None, &json!([])).is_none()); } } #[test] fn a_bot_sender_is_quoted_with_its_28_mri() { let record = MessageRecord { message_id: "5".into(), sender_name: Some("Echo".into()), sender_application_id: Some("app-1".into()), body_html: "

pong

".into(), ..MessageRecord::default() }; let quote = teams_quote(&record).unwrap(); assert_eq!(quote.quoted_message["sender"], "28:app-1"); assert!(quote.html.contains("itemid=\"28:app-1\">Echo")); } }