fix(chat): show quoted replies sent in the new Teams format
Replies from new Teams and Graph replyWithQuote carry the quote as a messageReference attachment at an <attachment id> tag. The renderer skipped that tag, so only the reply text showed. The quote now renders as a reply block at the tag's position, like the classic blockquote format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -4,10 +4,12 @@ use serde::{Deserialize, Serialize};
4
4
use store::MessageRecord;
5
5
6
6
use crate::adaptive_card::{AdaptiveCard, card_content_text};
7
use crate::markdown::escape_html;
7
8
use crate::mentions::MentionInput;
8
9
use crate::spans::{Span, html_to_spans};
9
10
10
11
const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";
12
const REPLY_ITEMTYPE: &str = "schema.skype.com/Reply";
11
13
12
14
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13
15
pub struct AttachmentInfo {
@@ -432,11 +434,42 @@ pub fn message_spans(record: &MessageRecord) -> Vec<Span> {
432
434
}
433
435
434
436
fn spans_with_mentions(record: &MessageRecord, infos: &[MentionInfo]) -> Vec<Span> {
435
let mut spans = html_to_spans(&record.body_html);
437
let mut spans = html_to_spans(&with_inline_quotes(record));
436
438
merge_split_mentions(&mut spans, infos);
437
439
spans
438
440
}
439
441
442
fn with_inline_quotes(record: &MessageRecord) -> String {
443
let mut html = record.body_html.clone();
444
for attachment in attachments(record) {
445
let Some(quote) = attachment.quote else {
446
continue;
447
};
448
let id = attachment.id.unwrap_or_else(|| quote.message_id.clone());
449
let tag = format!("<attachment id=\"{id}\"></attachment>");
450
let block = quote_html("e);
451
if html.contains(&tag) {
452
html = html.replacen(&tag, &block, 1);
453
} else {
454
html.insert_str(0, &block);
455
}
456
}
457
html
458
}
459
460
fn quote_html(quote: &QuoteInfo) -> String {
461
let sender = quote
462
.sender_name
463
.as_deref()
464
.map(|name| format!("<strong itemprop=\"mri\">{}</strong>", escape_html(name)))
465
.unwrap_or_default();
466
format!(
467
"<blockquote itemscope=\"\" itemtype=\"http://{REPLY_ITEMTYPE}\" itemid=\"{}\">{sender}<p itemprop=\"preview\">{}</p></blockquote>",
468
escape_html("e.message_id),
469
escape_html("e.preview),
470
)
471
}
472
440
473
pub fn user_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {
441
474
let infos = mentions(record);
442
475
let mut inputs = Vec::new();
@@ -741,6 +774,30 @@ mod tests {
741
774
assert_eq!(quotes(&stored).len(), 1);
742
775
}
743
776
777
#[test]
778
fn a_message_reference_renders_as_a_reply_quote_where_its_tag_sits() {
779
let json = r#"[{"content_type":"messageReference","name":null,"url":null,"text":null,"id":"Q1","quote":{"message_id":"Q1","preview":"a < b","sender_name":"Ada"}}]"#;
780
let stored = record("<p></p><attachment id=\"Q1\"></attachment>\ntest<p></p>", json);
781
let spans = message_spans(&stored);
782
let Some(Span::Quote(children)) = spans.first() else {
783
panic!("expected a reply quote first, got {spans:?}");
784
};
785
let mut quoted = String::new();
786
push_plain(&mut quoted, children);
787
assert!(quoted.contains("Ada"));
788
assert!(quoted.contains("a < b"));
789
let mut own = String::new();
790
push_plain(&mut own, &spans[1..]);
791
assert_eq!(own.trim(), "test");
792
}
793
794
#[test]
795
fn a_message_reference_without_its_tag_still_shows_the_quote() {
796
let json = r#"[{"content_type":"messageReference","name":null,"url":null,"text":null,"quote":{"message_id":"Q1","preview":"hi","sender_name":null}}]"#;
797
let spans = message_spans(&record("<p>answer</p>", json));
798
assert!(matches!(spans.first(), Some(Span::Quote(_))));
799
}
800
744
801
fn with_mentions(body_html: &str, mentions_json: &str) -> MessageRecord {
745
802
let mut stored = record(body_html, "[]");
746
803
stored.mentions_json = mentions_json.to_owned();
@@ -4,10 +4,12 @@ use serde::{Deserialize, Serialize};4
use store::MessageRecord;4
use store::MessageRecord;5
5
6
use crate::adaptive_card::{AdaptiveCard, card_content_text};6
use crate::adaptive_card::{AdaptiveCard, card_content_text};7
use crate::markdown::escape_html;7
use crate::mentions::MentionInput;8
use crate::mentions::MentionInput;8
use crate::spans::{Span, html_to_spans};9
use crate::spans::{Span, html_to_spans};9
10
10
const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";11
const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";12
const REPLY_ITEMTYPE: &str = "schema.skype.com/Reply";11
13
12
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]14
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]13
pub struct AttachmentInfo {15
pub struct AttachmentInfo {@@ -432,11 +434,42 @@ pub fn message_spans(record: &MessageRecord) -> Vec<Span> {432
}434
}433
435
434
fn spans_with_mentions(record: &MessageRecord, infos: &[MentionInfo]) -> Vec<Span> {436
fn spans_with_mentions(record: &MessageRecord, infos: &[MentionInfo]) -> Vec<Span> {435
let mut spans = html_to_spans(&record.body_html);437
let mut spans = html_to_spans(&with_inline_quotes(record));436
merge_split_mentions(&mut spans, infos);438
merge_split_mentions(&mut spans, infos);437
spans439
spans438
}440
}439
441
442
fn with_inline_quotes(record: &MessageRecord) -> String {443
let mut html = record.body_html.clone();444
for attachment in attachments(record) {445
let Some(quote) = attachment.quote else {446
continue;447
};448
let id = attachment.id.unwrap_or_else(|| quote.message_id.clone());449
let tag = format!("<attachment id=\"{id}\"></attachment>");450
let block = quote_html("e);451
if html.contains(&tag) {452
html = html.replacen(&tag, &block, 1);453
} else {454
html.insert_str(0, &block);455
}456
}457
html458
}459
460
fn quote_html(quote: &QuoteInfo) -> String {461
let sender = quote462
.sender_name463
.as_deref()464
.map(|name| format!("<strong itemprop=\"mri\">{}</strong>", escape_html(name)))465
.unwrap_or_default();466
format!(467
"<blockquote itemscope=\"\" itemtype=\"http://{REPLY_ITEMTYPE}\" itemid=\"{}\">{sender}<p itemprop=\"preview\">{}</p></blockquote>",468
escape_html("e.message_id),469
escape_html("e.preview),470
)471
}472
440
pub fn user_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {473
pub fn user_mention_inputs(record: &MessageRecord) -> Vec<MentionInput> {441
let infos = mentions(record);474
let infos = mentions(record);442
let mut inputs = Vec::new();475
let mut inputs = Vec::new();@@ -741,6 +774,30 @@ mod tests {741
assert_eq!(quotes(&stored).len(), 1);774
assert_eq!(quotes(&stored).len(), 1);742
}775
}743
776
777
#[test]778
fn a_message_reference_renders_as_a_reply_quote_where_its_tag_sits() {779
let json = r#"[{"content_type":"messageReference","name":null,"url":null,"text":null,"id":"Q1","quote":{"message_id":"Q1","preview":"a < b","sender_name":"Ada"}}]"#;780
let stored = record("<p></p><attachment id=\"Q1\"></attachment>\ntest<p></p>", json);781
let spans = message_spans(&stored);782
let Some(Span::Quote(children)) = spans.first() else {783
panic!("expected a reply quote first, got {spans:?}");784
};785
let mut quoted = String::new();786
push_plain(&mut quoted, children);787
assert!(quoted.contains("Ada"));788
assert!(quoted.contains("a < b"));789
let mut own = String::new();790
push_plain(&mut own, &spans[1..]);791
assert_eq!(own.trim(), "test");792
}793
794
#[test]795
fn a_message_reference_without_its_tag_still_shows_the_quote() {796
let json = r#"[{"content_type":"messageReference","name":null,"url":null,"text":null,"quote":{"message_id":"Q1","preview":"hi","sender_name":null}}]"#;797
let spans = message_spans(&record("<p>answer</p>", json));798
assert!(matches!(spans.first(), Some(Span::Quote(_))));799
}800
744
fn with_mentions(body_html: &str, mentions_json: &str) -> MessageRecord {801
fn with_mentions(body_html: &str, mentions_json: &str) -> MessageRecord {745
let mut stored = record(body_html, "[]");802
let mut stored = record(body_html, "[]");746
stored.mentions_json = mentions_json.to_owned();803
stored.mentions_json = mentions_json.to_owned();