use std::collections::HashMap; pub(crate) use chatsvc::graph_events::{attribute, unescape}; const MENTION_ITEMTYPE: &str = "schema.skype.com/Mention"; const INPUT_EXTENSION_ITEMTYPE: &str = "schema.skype.com/InputExtension"; const FORWARD_ITEMTYPE: &str = "schema.skype.com/Forward"; const EMOTICON_CLASS: &str = "animated-emoticon"; const REPLY_ITEMTYPE: &str = "schema.skype.com/Reply"; /// A chat service body in the shape Graph serves it: `` mentions and `` markers. #[derive(Debug, Default, PartialEq, Eq)] pub(crate) struct GraphBody { pub html: String, pub mention_texts: HashMap, pub forwards: Vec, pub quotes: Vec, } /// A Teams reply quote, which Graph serves as a `messageReference` attachment. #[derive(Debug, PartialEq, Eq)] pub(crate) struct QuotedMessage { pub message_id: String, pub sender_mri: Option, pub sender_name: Option, pub preview: String, } /// `forward_ids` name the `` that replaces each forward quote, in order. pub(crate) fn graph_body(content: &str, forward_ids: &[String]) -> GraphBody { let mut body = GraphBody::default(); let mut rest = content; while let Some(start) = rest.find('<') { body.html.push_str(&rest[..start]); rest = &rest[start..]; let Some(element) = Element::open_at(rest) else { body.html.push('<'); rest = &rest[1..]; continue; }; let itemtype = attribute(element.tag, "itemtype").unwrap_or_default(); let class = attribute(element.tag, "class").unwrap_or_default(); let Some(rewrite) = Rewrite::of(element.name, itemtype, class) else { body.html.push_str(element.tag); rest = &rest[element.tag.len()..]; continue; }; let Some((inner, after)) = element.inner_and_rest(rest) else { body.html.push_str(element.tag); rest = &rest[element.tag.len()..]; continue; }; let item_id = attribute(element.tag, "itemid").unwrap_or_default(); match rewrite { Rewrite::Mention => { let text = plain_text(inner); body.mention_texts.insert(item_id.to_owned(), text); body.html .push_str(&format!("{inner}")); } Rewrite::Emoticon => match emoji_tag(inner) { Some(tag) => body.html.push_str(&tag), None => body.html.push_str(&rest[..rest.len() - after.len()]), }, Rewrite::Reply if !item_id.is_empty() => { body.html .push_str(&format!("")); body.quotes.push(quoted_message(item_id, inner)); } Rewrite::Reply => body.html.push_str(&rest[..rest.len() - after.len()]), Rewrite::Card => body .html .push_str(&format!("")), Rewrite::Forward => { match forward_ids.get(body.forwards.len()) { Some(id) => body .html .push_str(&format!("")), None => body.html.push_str(&rest[..rest.len() - after.len()]), } body.forwards.push(inner.to_owned()); } } rest = after; } body.html.push_str(rest); body } #[derive(Clone, Copy)] enum Rewrite { Mention, Emoticon, Reply, Card, Forward, } impl Rewrite { fn of(name: &str, itemtype: &str, class: &str) -> Option { match name { "span" if itemtype.contains(MENTION_ITEMTYPE) => Some(Rewrite::Mention), "span" if class.contains(EMOTICON_CLASS) => Some(Rewrite::Emoticon), "span" if itemtype.contains(INPUT_EXTENSION_ITEMTYPE) => Some(Rewrite::Card), "blockquote" if itemtype.contains(FORWARD_ITEMTYPE) => Some(Rewrite::Forward), "blockquote" if itemtype.contains(REPLY_ITEMTYPE) => Some(Rewrite::Reply), _ => None, } } } struct Element<'a> { name: &'a str, tag: &'a str, } impl<'a> Element<'a> { fn open_at(text: &'a str) -> Option> { let end = text.find('>')? + 1; let tag = &text[..end]; let name_end = tag[1..].find(|character: char| { character.is_whitespace() || character == '>' || character == '/' })? + 1; let name = &tag[1..name_end]; (!name.is_empty() && name .chars() .all(|character| character.is_ascii_alphanumeric())) .then_some(Element { name, tag }) } /// The text between this element's tags and what follows its closing tag, nested elements of the same name included. fn inner_and_rest(&self, text: &'a str) -> Option<(&'a str, &'a str)> { let open = format!("<{}", self.name); let close = format!("", self.name); let inner_start = self.tag.len(); let mut depth = 1; let mut position = inner_start; while depth > 0 { let next_close = text[position..].find(&close)? + position; match text[position..].find(&open).map(|found| found + position) { Some(next_open) if next_open < next_close && opens_element(text, next_open, &open) => { depth += 1; position = next_open + open.len(); } _ => { depth -= 1; if depth == 0 { return Some(( &text[inner_start..next_close], &text[next_close + close.len()..], )); } position = next_close + close.len(); } } } None } } fn quoted_message(message_id: &str, inner: &str) -> QuotedMessage { let property = |name: &str| -> Option<(&str, &str)> { let marker = format!("itemprop=\"{name}\""); let at = inner.find(&marker)?; let tag_start = inner[..at].rfind('<')?; let element = Element::open_at(&inner[tag_start..])?; let (content, _) = element.inner_and_rest(&inner[tag_start..])?; Some((element.tag, content)) }; let sender = property("mri"); QuotedMessage { message_id: message_id.to_owned(), sender_mri: sender .and_then(|(tag, _)| attribute(tag, "itemid")) .filter(|mri| !mri.is_empty()) .map(str::to_owned), sender_name: sender .map(|(_, content)| plain_text(content)) .filter(|name| !name.is_empty()), preview: property("preview") .map(|(_, content)| plain_text(content)) .unwrap_or_default(), } } /// Graph's `` for a Teams emoticon span, read from the emoji image inside it. fn emoji_tag(inner: &str) -> Option { let start = inner.find("')? + start; let image = &inner[start..end]; let id = attribute(image, "itemid")?; let alt = attribute(image, "alt").unwrap_or_default(); let title = attribute(image, "title").unwrap_or_default(); Some(format!( "" )) } fn opens_element(text: &str, at: usize, open: &str) -> bool { text[at + open.len()..] .chars() .next() .is_some_and(|next| next.is_whitespace() || next == '>') } pub(crate) fn plain_text(html: &str) -> String { let mut text = String::new(); let mut in_tag = false; for character in html.chars() { match character { '<' => in_tag = true, '>' => in_tag = false, _ if !in_tag => text.push(character), _ => {} } } unescape(text.trim()) } #[cfg(test)] mod tests { use super::*; #[test] fn mention_spans_become_at_tags() { let body = graph_body( r#"

hi Ada & Co!

"#, &[], ); assert_eq!(body.html, r#"

hi Ada & Co!

"#); assert_eq!(body.mention_texts["0"], "Ada & Co"); } #[test] fn emoticon_spans_stay_and_nested_spans_close_right() { let html = r#"

Bo 🙂

"#; let body = graph_body(html, &[]); assert!( body.html .starts_with(r#"

Bo ok🙂

"#; assert_eq!( graph_body(html, &[]).html, r#"

ok

"# ); } #[test] fn reply_quotes_become_message_references() { let html = r#"
Ann

old & true

new

"#; let body = graph_body(html, &[]); assert_eq!( body.html, r#"

new

"# ); assert_eq!( body.quotes, vec![QuotedMessage { message_id: "1751975663649".into(), sender_mri: Some("8:orgid:ann".into()), sender_name: Some("Ann".into()), preview: "old & true".into(), }] ); } #[test] fn card_placeholders_and_forwards_become_attachment_markers() { let html = r#"

see

old

"#; let body = graph_body(html, &["m7".to_owned()]); assert_eq!( body.html, r#"

see

"# ); assert_eq!(body.forwards, vec!["

old

".to_owned()]); } #[test] fn a_forward_without_context_keeps_its_quote() { let html = r#"

old

"#; assert_eq!(graph_body(html, &[]).html, html); } #[test] fn broken_markup_is_kept() { assert_eq!( graph_body("a < b ", &[]).html, "a < b " ); } }