BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / teams_quote.rs

teams_quote.rs

teams_quote.rs 4.8 KiB Download Raw
1use graph::{MentionTarget, OutgoingMention};
2use serde_json::{Value, json};
3use store::MessageRecord;
4
5use crate::markdown::escape_html;
6use crate::preview::preview_text;
7
8const REPLY_ITEMTYPE: &str = "http://schema.skype.com/Reply";
9const MENTION_ITEMTYPE: &str = "http://schema.skype.com/Mention";
10const ORGID_PREFIX: &str = "8:orgid:";
11
12#[derive(Debug, Clone, PartialEq)]
13pub(crate) struct TeamsQuote {
14 pub html: String,
15 pub quoted_message: Value,
16}
17
18/// Teams' `createBlockquoteTag`: no quote without sender, name or preview.
19pub(crate) fn teams_quote(record: &MessageRecord) -> Option<TeamsQuote> {
20 let sender_mri = user_mri(record.sender_id.as_deref()?);
21 let sender_name = record.sender_name.as_deref().filter(|name| !name.is_empty())?;
22 let preview = preview_text(&record.body_html).filter(|preview| !preview.is_empty())?;
23 let time = record.created_at.timestamp_millis();
24 let html = format!(
25 "<blockquote itemscope=\"\" itemtype=\"{REPLY_ITEMTYPE}\" itemid=\"{}\"><strong itemprop=\"mri\" itemid=\"{}\">{}</strong><span itemprop=\"time\" itemid=\"{time}\"></span><p itemprop=\"preview\">{}</p></blockquote>",
26 escape_html(&record.message_id),
27 escape_html(&sender_mri),
28 escape_html(sender_name),
29 escape_html(&preview),
30 );
31 let quoted_message = json!({
32 "messageId": record.message_id,
33 "sender": sender_mri,
34 "time": time,
35 });
36 Some(TeamsQuote {
37 html,
38 quoted_message,
39 })
40}
41
42pub(crate) fn quote_reply_properties(
43 quote: &TeamsQuote,
44 mentions: &[OutgoingMention],
45 importance: Option<&str>,
46) -> Option<Value> {
47 let mut properties = json!({
48 "importance": importance.unwrap_or_default(),
49 "subject": "",
50 "qtdMsgs": Value::Array(vec![quote.quoted_message.clone()]).to_string(),
51 });
52 if !mentions.is_empty() {
53 let entries = mentions
54 .iter()
55 .map(|mention| match &mention.target {
56 MentionTarget::User { user_id } => Some(json!({
57 "@type": MENTION_ITEMTYPE,
58 "itemid": mention.id,
59 "mri": user_mri(user_id),
60 "mentionType": "person",
61 "displayName": mention.text,
62 })),
63 MentionTarget::Channel { .. } | MentionTarget::Team { .. } => None,
64 })
65 .collect::<Option<Vec<_>>>()?;
66 properties["mentions"] = json!(Value::Array(entries).to_string());
67 }
68 Some(properties)
69}
70
71pub(crate) fn chatsvc_mention_html(html: &str, mentions: &[OutgoingMention]) -> String {
72 let mut converted = html.to_owned();
73 for mention in mentions {
74 converted = converted.replace(
75 &format!("<at id=\"{}\">", mention.id),
76 &format!(
77 "<span itemscope=\"\" itemtype=\"{MENTION_ITEMTYPE}\" itemid=\"{}\">",
78 mention.id
79 ),
80 );
81 }
82 converted.replace("</at>", "</span>")
83}
84
85fn user_mri(user_id: &str) -> String {
86 if user_id.starts_with("8:") {
87 user_id.to_owned()
88 } else {
89 format!("{ORGID_PREFIX}{user_id}")
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 fn mention(id: u32, target: MentionTarget) -> OutgoingMention {
98 OutgoingMention {
99 id,
100 text: "Ada".into(),
101 target,
102 }
103 }
104
105 #[test]
106 fn person_mentions_become_teams_spans_and_a_mentions_property() {
107 let quote = TeamsQuote {
108 html: String::new(),
109 quoted_message: json!({"messageId": "1"}),
110 };
111 let mentions = [mention(
112 0,
113 MentionTarget::User {
114 user_id: "user-ada".into(),
115 },
116 )];
117 let html = chatsvc_mention_html("hi <at id=\"0\">Ada</at>", &mentions);
118 assert_eq!(
119 html,
120 "hi <span itemscope=\"\" itemtype=\"http://schema.skype.com/Mention\" itemid=\"0\">Ada</span>"
121 );
122 let properties = quote_reply_properties(&quote, &mentions, Some("high")).unwrap();
123 assert_eq!(properties["importance"], "high");
124 assert_eq!(properties["qtdMsgs"], "[{\"messageId\":\"1\"}]");
125 let entries: Value =
126 serde_json::from_str(properties["mentions"].as_str().unwrap()).unwrap();
127 assert_eq!(entries[0]["mri"], "8:orgid:user-ada");
128 assert_eq!(entries[0]["mentionType"], "person");
129 }
130
131 #[test]
132 fn a_channel_mention_has_no_chatsvc_shape() {
133 let quote = TeamsQuote {
134 html: String::new(),
135 quoted_message: Value::Null,
136 };
137 let mentions = [mention(
138 0,
139 MentionTarget::Channel {
140 channel_id: "19:c".into(),
141 },
142 )];
143 assert!(quote_reply_properties(&quote, &mentions, None).is_none());
144 }
145}