fix(chat): quotes show the lines of the quoted message when it is loaded
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -8,8 +8,8 @@ use store::MessageRecord;
8
8
use teams_core::{
9
9
AdaptiveCard, CallEvent, CallPhase, CallType, Draft, FileCard, ImageRef, LinkPreview,
10
10
ReactionInfo, Span, adaptive_cards, call_event, card_texts, files, format_duration, images,
11
link_preview, linked_message_spans, meeting_link_in_html, message_spans, reactions,
12
translated_spans,
11
link_preview, linked_message_spans_with, meeting_link_in_html, message_spans, quotes,
12
reactions, readable_text, translated_spans,
13
13
};
14
14
15
15
use crate::card_state::CardOverride;
@@ -612,7 +612,38 @@ fn call_row(record: &MessageRecord, event: &CallEvent, context: &RowContext) ->
612
612
}
613
613
}
614
614
615
const QUOTE_LINES: usize = 5;
616
617
pub fn quoted_texts(records: &[MessageRecord]) -> HashMap<String, String> {
618
let wanted: std::collections::HashSet<String> = records
619
.iter()
620
.flat_map(quotes)
621
.map(|quote| quote.message_id)
622
.collect();
623
records
624
.iter()
625
.filter(|record| !record.deleted && wanted.contains(&record.message_id))
626
.map(|record| {
627
let text = readable_text(record);
628
let lines: Vec<&str> = text.lines().filter(|line| !line.trim().is_empty()).collect();
629
let mut shown = lines.iter().take(QUOTE_LINES).copied().collect::<Vec<_>>().join("\n");
630
if lines.len() > QUOTE_LINES {
631
shown.push_str(" …");
632
}
633
(record.message_id.clone(), shown)
634
})
635
.collect()
636
}
637
615
638
pub fn message_row(record: &MessageRecord, context: &RowContext) -> MessageRow {
639
message_row_with(record, context, &HashMap::new())
640
}
641
642
fn message_row_with(
643
record: &MessageRecord,
644
context: &RowContext,
645
quoted: &HashMap<String, String>,
646
) -> MessageRow {
616
647
if let Some(event) = call_event(record) {
617
648
return call_row(record, &event, context);
618
649
}
@@ -625,7 +656,7 @@ pub fn message_row(record: &MessageRecord, context: &RowContext) -> MessageRow {
625
656
} else if let Some(html) = &translation.translated_html {
626
657
layout_blocks(&translated_spans(record, html))
627
658
} else {
628
layout_blocks(&linked_message_spans(record))
659
layout_blocks(&linked_message_spans_with(record, &|id| quoted.get(id).cloned()))
629
660
};
630
661
let blocks = if images.is_empty() {
631
662
blocks
@@ -702,6 +733,7 @@ pub fn flat_rows(records: &[MessageRecord], context: &RowContext, has_older: boo
702
733
.is_some_and(|event| is_open_start(event, &finished))
703
734
});
704
735
let mut previous_day: Option<NaiveDate> = None;
736
let quoted = quoted_texts(records);
705
737
for (index, (record, event)) in records.iter().zip(&events).enumerate() {
706
738
let mut row = match event {
707
739
Some(event)
@@ -710,7 +742,7 @@ pub fn flat_rows(records: &[MessageRecord], context: &RowContext, has_older: boo
710
742
continue;
711
743
}
712
744
Some(event) => call_row(record, event, context),
713
None => message_row(record, context),
745
None => message_row_with(record, context, "ed),
714
746
};
715
747
if let Some(call) = row.call.as_mut() {
716
748
call.started &= newest_open_start == Some(index);
@@ -937,6 +969,26 @@ mod tests {
937
969
}
938
970
}
939
971
972
#[test]
973
fn a_quote_shows_the_lines_of_the_quoted_message_when_it_is_loaded() {
974
let mut quoted = record("q1", None, 8, 6);
975
quoted.body_html = "<p>first link</p><p>second link</p>".into();
976
let mut answer = record("a1", None, 9, 6);
977
answer.body_html = "<attachment id=\"q1\"></attachment><p>please again</p>".into();
978
answer.attachments_json = r#"[{"content_type":"messageReference","name":null,"url":null,"text":null,"id":"q1","quote":{"message_id":"q1","preview":"first link second link","sender_name":"Robin"}}]"#.into();
979
let rows = flat_rows(&[quoted, answer], &context(), false);
980
let reply = rows
981
.iter()
982
.find_map(|row| match row {
983
Row::Message(row) if row.key == "a1" => Some(row.blocks.clone()),
984
_ => None,
985
})
986
.unwrap();
987
let Some(Block::Reply(children)) = reply.first() else { panic!("{reply:?}") };
988
let text = format!("{children:?}");
989
assert!(text.contains(r"first link\nsecond link"), "{text}");
990
}
991
940
992
fn own_reaction(reaction_type: &str) -> ReactionInfo {
941
993
ReactionInfo {
942
994
reaction_type: reaction_type.into(),
@@ -1112,7 +1164,7 @@ mod tests {
1112
1164
));
1113
1165
assert_eq!(
1114
1166
row.blocks,
1115
layout_blocks(&linked_message_spans(&record("a", None, 8, 6)))
1167
layout_blocks(&teams_core::linked_message_spans(&record("a", None, 8, 6)))
1116
1168
);
1117
1169
}
1118
1170
@@ -8,8 +8,8 @@ use store::MessageRecord;8
use teams_core::{8
use teams_core::{9
AdaptiveCard, CallEvent, CallPhase, CallType, Draft, FileCard, ImageRef, LinkPreview,9
AdaptiveCard, CallEvent, CallPhase, CallType, Draft, FileCard, ImageRef, LinkPreview,10
ReactionInfo, Span, adaptive_cards, call_event, card_texts, files, format_duration, images,10
ReactionInfo, Span, adaptive_cards, call_event, card_texts, files, format_duration, images,11
link_preview, linked_message_spans, meeting_link_in_html, message_spans, reactions,11
link_preview, linked_message_spans_with, meeting_link_in_html, message_spans, quotes,12
translated_spans,12
reactions, readable_text, translated_spans,13
};13
};14
14
15
use crate::card_state::CardOverride;15
use crate::card_state::CardOverride;@@ -612,7 +612,38 @@ fn call_row(record: &MessageRecord, event: &CallEvent, context: &RowContext) ->612
}612
}613
}613
}614
614
615
const QUOTE_LINES: usize = 5;616
617
pub fn quoted_texts(records: &[MessageRecord]) -> HashMap<String, String> {618
let wanted: std::collections::HashSet<String> = records619
.iter()620
.flat_map(quotes)621
.map(|quote| quote.message_id)622
.collect();623
records624
.iter()625
.filter(|record| !record.deleted && wanted.contains(&record.message_id))626
.map(|record| {627
let text = readable_text(record);628
let lines: Vec<&str> = text.lines().filter(|line| !line.trim().is_empty()).collect();629
let mut shown = lines.iter().take(QUOTE_LINES).copied().collect::<Vec<_>>().join("\n");630
if lines.len() > QUOTE_LINES {631
shown.push_str(" …");632
}633
(record.message_id.clone(), shown)634
})635
.collect()636
}637
615
pub fn message_row(record: &MessageRecord, context: &RowContext) -> MessageRow {638
pub fn message_row(record: &MessageRecord, context: &RowContext) -> MessageRow {639
message_row_with(record, context, &HashMap::new())640
}641
642
fn message_row_with(643
record: &MessageRecord,644
context: &RowContext,645
quoted: &HashMap<String, String>,646
) -> MessageRow {616
if let Some(event) = call_event(record) {647
if let Some(event) = call_event(record) {617
return call_row(record, &event, context);648
return call_row(record, &event, context);618
}649
}@@ -625,7 +656,7 @@ pub fn message_row(record: &MessageRecord, context: &RowContext) -> MessageRow {625
} else if let Some(html) = &translation.translated_html {656
} else if let Some(html) = &translation.translated_html {626
layout_blocks(&translated_spans(record, html))657
layout_blocks(&translated_spans(record, html))627
} else {658
} else {628
layout_blocks(&linked_message_spans(record))659
layout_blocks(&linked_message_spans_with(record, &|id| quoted.get(id).cloned()))629
};660
};630
let blocks = if images.is_empty() {661
let blocks = if images.is_empty() {631
blocks662
blocks@@ -702,6 +733,7 @@ pub fn flat_rows(records: &[MessageRecord], context: &RowContext, has_older: boo702
.is_some_and(|event| is_open_start(event, &finished))733
.is_some_and(|event| is_open_start(event, &finished))703
});734
});704
let mut previous_day: Option<NaiveDate> = None;735
let mut previous_day: Option<NaiveDate> = None;736
let quoted = quoted_texts(records);705
for (index, (record, event)) in records.iter().zip(&events).enumerate() {737
for (index, (record, event)) in records.iter().zip(&events).enumerate() {706
let mut row = match event {738
let mut row = match event {707
Some(event)739
Some(event)@@ -710,7 +742,7 @@ pub fn flat_rows(records: &[MessageRecord], context: &RowContext, has_older: boo710
continue;742
continue;711
}743
}712
Some(event) => call_row(record, event, context),744
Some(event) => call_row(record, event, context),713
None => message_row(record, context),745
None => message_row_with(record, context, "ed),714
};746
};715
if let Some(call) = row.call.as_mut() {747
if let Some(call) = row.call.as_mut() {716
call.started &= newest_open_start == Some(index);748
call.started &= newest_open_start == Some(index);@@ -937,6 +969,26 @@ mod tests {937
}969
}938
}970
}939
971
972
#[test]973
fn a_quote_shows_the_lines_of_the_quoted_message_when_it_is_loaded() {974
let mut quoted = record("q1", None, 8, 6);975
quoted.body_html = "<p>first link</p><p>second link</p>".into();976
let mut answer = record("a1", None, 9, 6);977
answer.body_html = "<attachment id=\"q1\"></attachment><p>please again</p>".into();978
answer.attachments_json = r#"[{"content_type":"messageReference","name":null,"url":null,"text":null,"id":"q1","quote":{"message_id":"q1","preview":"first link second link","sender_name":"Robin"}}]"#.into();979
let rows = flat_rows(&[quoted, answer], &context(), false);980
let reply = rows981
.iter()982
.find_map(|row| match row {983
Row::Message(row) if row.key == "a1" => Some(row.blocks.clone()),984
_ => None,985
})986
.unwrap();987
let Some(Block::Reply(children)) = reply.first() else { panic!("{reply:?}") };988
let text = format!("{children:?}");989
assert!(text.contains(r"first link\nsecond link"), "{text}");990
}991
940
fn own_reaction(reaction_type: &str) -> ReactionInfo {992
fn own_reaction(reaction_type: &str) -> ReactionInfo {941
ReactionInfo {993
ReactionInfo {942
reaction_type: reaction_type.into(),994
reaction_type: reaction_type.into(),@@ -1112,7 +1164,7 @@ mod tests {1112
));1164
));1113
assert_eq!(1165
assert_eq!(1114
row.blocks,1166
row.blocks,1115
layout_blocks(&linked_message_spans(&record("a", None, 8, 6)))1167
layout_blocks(&teams_core::linked_message_spans(&record("a", None, 8, 6)))1116
);1168
);1117
}1169
}1118
1170
@@ -136,7 +136,7 @@ pub use store::{ConversationHit, HIGHLIGHT_END, HIGHLIGHT_START, SearchHit};
136
136
pub use stored::{
137
137
AttachmentInfo, FileCard, FileKind, ImageKind, ImageRef, MentionInfo, PROFILE_LINK_PREFIX,
138
138
QuoteInfo, ReactionInfo, ReactionSummary, adaptive_cards, attachments, can_delete, can_edit,
139
card_texts, copy_text, files, images, linked_message_spans, mentions, message_quote,
139
card_texts, copy_text, files, images, linked_message_spans, linked_message_spans_with, mentions, message_quote,
140
140
message_spans, quote_html, quotes, reaction_summaries, reactions, readable_text, translated_spans,
141
141
user_mention_inputs,
142
142
};
@@ -136,7 +136,7 @@ pub use store::{ConversationHit, HIGHLIGHT_END, HIGHLIGHT_START, SearchHit};136
pub use stored::{136
pub use stored::{137
AttachmentInfo, FileCard, FileKind, ImageKind, ImageRef, MentionInfo, PROFILE_LINK_PREFIX,137
AttachmentInfo, FileCard, FileKind, ImageKind, ImageRef, MentionInfo, PROFILE_LINK_PREFIX,138
QuoteInfo, ReactionInfo, ReactionSummary, adaptive_cards, attachments, can_delete, can_edit,138
QuoteInfo, ReactionInfo, ReactionSummary, adaptive_cards, attachments, can_delete, can_edit,139
card_texts, copy_text, files, images, linked_message_spans, mentions, message_quote,139
card_texts, copy_text, files, images, linked_message_spans, linked_message_spans_with, mentions, message_quote,140
message_spans, quote_html, quotes, reaction_summaries, reactions, readable_text, translated_spans,140
message_spans, quote_html, quotes, reaction_summaries, reactions, readable_text, translated_spans,141
user_mention_inputs,141
user_mention_inputs,142
};142
};@@ -558,8 +558,17 @@ pub const PROFILE_LINK_PREFIX: &str = "teams-profile:";
558
558
559
559
/// Like `message_spans`, with the id of each user mention replaced by a profile link.
560
560
pub fn linked_message_spans(record: &MessageRecord) -> Vec<Span> {
561
linked_message_spans_with(record, &|_| None)
562
}
563
564
/// Like `linked_message_spans`, but a quote shows `quoted_text(message_id)` when known instead of the one-line server preview.
565
pub fn linked_message_spans_with(
566
record: &MessageRecord,
567
quoted_text: &dyn Fn(&str) -> Option<String>,
568
) -> Vec<Span> {
561
569
let infos = mentions(record);
562
let mut spans = spans_with_mentions(record, &infos);
570
let mut spans = html_to_spans(&with_inline_quotes(record, quoted_text));
571
merge_split_mentions(&mut spans, &infos);
563
572
link_user_mentions(&mut spans, &infos);
564
573
spans
565
574
}
@@ -609,17 +618,20 @@ fn link_user_mentions(spans: &mut [Span], infos: &[MentionInfo]) {
609
618
}
610
619
611
620
fn spans_with_mentions(record: &MessageRecord, infos: &[MentionInfo]) -> Vec<Span> {
612
let mut spans = html_to_spans(&with_inline_quotes(record));
621
let mut spans = html_to_spans(&with_inline_quotes(record, &|_| None));
613
622
merge_split_mentions(&mut spans, infos);
614
623
spans
615
624
}
616
625
617
fn with_inline_quotes(record: &MessageRecord) -> String {
626
fn with_inline_quotes(record: &MessageRecord, quoted_text: &dyn Fn(&str) -> Option<String>) -> String {
618
627
let mut html = record.body_html.clone();
619
628
for attachment in attachments(record) {
620
let Some(quote) = attachment.quote else {
629
let Some(mut quote) = attachment.quote else {
621
630
continue;
622
631
};
632
if let Some(text) = quoted_text("e.message_id).filter(|text| !text.trim().is_empty()) {
633
quote.preview = text;
634
}
623
635
let id = attachment.id.unwrap_or_else(|| quote.message_id.clone());
624
636
let tag = format!("<attachment id=\"{id}\"></attachment>");
625
637
let block = quote_html("e);
@@ -558,8 +558,17 @@ pub const PROFILE_LINK_PREFIX: &str = "teams-profile:";558
558
559
/// Like `message_spans`, with the id of each user mention replaced by a profile link.559
/// Like `message_spans`, with the id of each user mention replaced by a profile link.560
pub fn linked_message_spans(record: &MessageRecord) -> Vec<Span> {560
pub fn linked_message_spans(record: &MessageRecord) -> Vec<Span> {561
linked_message_spans_with(record, &|_| None)562
}563
564
/// Like `linked_message_spans`, but a quote shows `quoted_text(message_id)` when known instead of the one-line server preview.565
pub fn linked_message_spans_with(566
record: &MessageRecord,567
quoted_text: &dyn Fn(&str) -> Option<String>,568
) -> Vec<Span> {561
let infos = mentions(record);569
let infos = mentions(record);562
let mut spans = spans_with_mentions(record, &infos);570
let mut spans = html_to_spans(&with_inline_quotes(record, quoted_text));571
merge_split_mentions(&mut spans, &infos);563
link_user_mentions(&mut spans, &infos);572
link_user_mentions(&mut spans, &infos);564
spans573
spans565
}574
}@@ -609,17 +618,20 @@ fn link_user_mentions(spans: &mut [Span], infos: &[MentionInfo]) {609
}618
}610
619
611
fn spans_with_mentions(record: &MessageRecord, infos: &[MentionInfo]) -> Vec<Span> {620
fn spans_with_mentions(record: &MessageRecord, infos: &[MentionInfo]) -> Vec<Span> {612
let mut spans = html_to_spans(&with_inline_quotes(record));621
let mut spans = html_to_spans(&with_inline_quotes(record, &|_| None));613
merge_split_mentions(&mut spans, infos);622
merge_split_mentions(&mut spans, infos);614
spans623
spans615
}624
}616
625
617
fn with_inline_quotes(record: &MessageRecord) -> String {626
fn with_inline_quotes(record: &MessageRecord, quoted_text: &dyn Fn(&str) -> Option<String>) -> String {618
let mut html = record.body_html.clone();627
let mut html = record.body_html.clone();619
for attachment in attachments(record) {628
for attachment in attachments(record) {620
let Some(quote) = attachment.quote else {629
let Some(mut quote) = attachment.quote else {621
continue;630
continue;622
};631
};632
if let Some(text) = quoted_text("e.message_id).filter(|text| !text.trim().is_empty()) {633
quote.preview = text;634
}623
let id = attachment.id.unwrap_or_else(|| quote.message_id.clone());635
let id = attachment.id.unwrap_or_else(|| quote.message_id.clone());624
let tag = format!("<attachment id=\"{id}\"></attachment>");636
let tag = format!("<attachment id=\"{id}\"></attachment>");625
let block = quote_html("e);637
let block = quote_html("e);