fix(chats): bot card chats show the card text in the chat list
- card messages preview their card text, "Sent a card" only without text - Graph chat refresh keeps the stored line when the last body is only attachments - ChatRecord last_event_call renamed to keeps_stored_preview Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -163,7 +163,7 @@ mod tests {
163
163
last_message_sender_name: None,
164
164
last_message_deleted: false,
165
165
last_event_system: false,
166
last_event_call: false,
166
keeps_stored_preview: false,
167
167
});
168
168
state
169
169
}
@@ -163,7 +163,7 @@ mod tests {163
last_message_sender_name: None,163
last_message_sender_name: None,164
last_message_deleted: false,164
last_message_deleted: false,165
last_event_system: false,165
last_event_system: false,166
last_event_call: false,166
keeps_stored_preview: false,167
});167
});168
state168
state169
}169
}@@ -43,7 +43,10 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> ChatRecord {
43
43
.map(OwnedPreview::from_graph);
44
44
let last_event_system = preview.as_ref().is_some_and(|preview| preview.system);
45
45
// Teams leaves calls out of the chat list line: the line of the message before the call stays.
46
let last_event_call = preview.as_ref().is_some_and(|preview| preview.call);
46
// Graph also leaves out the attachments, so a card line only comes from the synced message.
47
let keeps_stored_preview = preview
48
.as_ref()
49
.is_some_and(|preview| preview.call || preview.attachments_only);
47
50
ChatRecord {
48
51
id: chat.id.clone(),
49
52
kind: chat.chat_type.clone(),
@@ -75,7 +78,7 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> ChatRecord {
75
78
.as_ref()
76
79
.and_then(|preview| preview.sender_name.clone()),
77
80
last_event_system,
78
last_event_call,
81
keeps_stored_preview,
79
82
}
80
83
}
81
84
@@ -43,7 +43,10 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> ChatRecord {43
.map(OwnedPreview::from_graph);43
.map(OwnedPreview::from_graph);44
let last_event_system = preview.as_ref().is_some_and(|preview| preview.system);44
let last_event_system = preview.as_ref().is_some_and(|preview| preview.system);45
// Teams leaves calls out of the chat list line: the line of the message before the call stays.45
// Teams leaves calls out of the chat list line: the line of the message before the call stays.46
let last_event_call = preview.as_ref().is_some_and(|preview| preview.call);46
// Graph also leaves out the attachments, so a card line only comes from the synced message.47
let keeps_stored_preview = preview48
.as_ref()49
.is_some_and(|preview| preview.call || preview.attachments_only);47
ChatRecord {50
ChatRecord {48
id: chat.id.clone(),51
id: chat.id.clone(),49
kind: chat.chat_type.clone(),52
kind: chat.chat_type.clone(),@@ -75,7 +78,7 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> ChatRecord {75
.as_ref()78
.as_ref()76
.and_then(|preview| preview.sender_name.clone()),79
.and_then(|preview| preview.sender_name.clone()),77
last_event_system,80
last_event_system,78
last_event_call,81
keeps_stored_preview,79
}82
}80
}83
}81
84
@@ -75,10 +75,20 @@ pub fn message_preview(html: &str, attachments: &[AttachmentInfo]) -> Option<Str
75
75
if parts.images > 0 {
76
76
return Some(IMAGE_PREVIEW.to_owned());
77
77
}
78
attachment_label(attachments).map(str::to_owned)
78
attachment_label(attachments)
79
79
}
80
80
81
fn attachment_label(attachments: &[AttachmentInfo]) -> Option<&'static str> {
81
fn attachment_label(attachments: &[AttachmentInfo]) -> Option<String> {
82
fixed_label(attachments).map(str::to_owned).or_else(|| {
83
let mut cards = attachments.iter().filter(|attachment| attachment.is_card());
84
cards
85
.clone()
86
.find_map(|card| collapse(&card.card_text()?))
87
.or_else(|| cards.next().map(|_| CARD_PREVIEW.to_owned()))
88
})
89
}
90
91
fn fixed_label(attachments: &[AttachmentInfo]) -> Option<&'static str> {
82
92
let has_type = |wanted: &str| {
83
93
attachments
84
94
.iter()
@@ -92,8 +102,6 @@ fn attachment_label(attachments: &[AttachmentInfo]) -> Option<&'static str> {
92
102
Some(AUDIO_PREVIEW)
93
103
} else if has_type(LOOP_CARD_TYPE) {
94
104
Some(LOOP_PREVIEW)
95
} else if attachments.iter().any(AttachmentInfo::is_card) {
96
Some(CARD_PREVIEW)
97
105
} else {
98
106
None
99
107
}
@@ -214,6 +222,7 @@ pub(crate) struct OwnedPreview {
214
222
pub deleted: bool,
215
223
pub system: bool,
216
224
pub call: bool,
225
pub attachments_only: bool,
217
226
}
218
227
219
228
impl OwnedPreview {
@@ -227,17 +236,20 @@ impl OwnedPreview {
227
236
let call = detail
228
237
.and_then(CallEvent::from_graph)
229
238
.is_some_and(|event| event.phase.is_call());
239
let html = message
240
.body
241
.as_ref()
242
.and_then(|body| body.content.as_deref())
243
.filter(|_| regular && !deleted);
230
244
let text = if deleted {
231
245
None
232
246
} else if regular {
233
message
234
.body
235
.as_ref()
236
.and_then(|body| body.content.as_deref())
237
.and_then(|html| message_preview(html, &[]))
247
html.and_then(|html| message_preview(html, &[]))
238
248
} else {
239
249
detail.and_then(system_event_preview).map(str::to_owned)
240
250
};
251
let attachments_only =
252
text.is_none() && html.is_some_and(|html| html.contains("<attachment"));
241
253
let sender = message.from.as_ref().filter(|_| regular);
242
254
OwnedPreview {
243
255
text,
@@ -250,6 +262,7 @@ impl OwnedPreview {
250
262
deleted,
251
263
system: !regular,
252
264
call,
265
attachments_only,
253
266
}
254
267
}
255
268
@@ -273,6 +286,7 @@ impl OwnedPreview {
273
286
deleted: record.deleted,
274
287
system: !sender,
275
288
call: event.is_some_and(|event| event.phase.is_call()),
289
attachments_only: false,
276
290
}
277
291
}
278
292
@@ -395,6 +409,41 @@ mod tests {
395
409
assert_eq!(message_preview(body, &[]), None);
396
410
}
397
411
412
#[test]
413
fn a_card_with_text_shows_the_card_text() {
414
let body = r#"<attachment id="1792149081221"></attachment>"#;
415
let mut card = attachment("application/vnd.microsoft.card.adaptive");
416
card.content = Some(
417
serde_json::json!({
418
"type": "AdaptiveCard",
419
"body": [
420
{"type": "TextBlock", "text": "Ada Lovelace edited Engine Notes"},
421
{"type": "TextBlock", "text": "Page updated"},
422
],
423
})
424
.to_string(),
425
);
426
assert_eq!(
427
message_preview(body, &[card]).as_deref(),
428
Some("Ada Lovelace edited Engine Notes Page updated")
429
);
430
}
431
432
#[test]
433
fn graph_attachment_only_bodies_keep_the_stored_line() {
434
let preview = |content: &str| {
435
OwnedPreview::from_graph(&graph_message(serde_json::json!({
436
"id": "1",
437
"messageType": "message",
438
"body": {"contentType": "html", "content": content},
439
})))
440
};
441
let card = preview(r#"<attachment id="1792149081221"></attachment>"#);
442
assert!(card.attachments_only);
443
assert_eq!(card.text, None);
444
assert!(!preview("<p>hi</p>").attachments_only);
445
}
446
398
447
#[test]
399
448
fn a_file_with_text_shows_the_text() {
400
449
let body = r#"Hi Philipp,<br>Kann's dir zeigen.<attachment id="0cf57a46"></attachment>"#;
@@ -75,10 +75,20 @@ pub fn message_preview(html: &str, attachments: &[AttachmentInfo]) -> Option<Str75
if parts.images > 0 {75
if parts.images > 0 {76
return Some(IMAGE_PREVIEW.to_owned());76
return Some(IMAGE_PREVIEW.to_owned());77
}77
}78
attachment_label(attachments).map(str::to_owned)78
attachment_label(attachments)79
}79
}80
80
81
fn attachment_label(attachments: &[AttachmentInfo]) -> Option<&'static str> {81
fn attachment_label(attachments: &[AttachmentInfo]) -> Option<String> {82
fixed_label(attachments).map(str::to_owned).or_else(|| {83
let mut cards = attachments.iter().filter(|attachment| attachment.is_card());84
cards85
.clone()86
.find_map(|card| collapse(&card.card_text()?))87
.or_else(|| cards.next().map(|_| CARD_PREVIEW.to_owned()))88
})89
}90
91
fn fixed_label(attachments: &[AttachmentInfo]) -> Option<&'static str> {82
let has_type = |wanted: &str| {92
let has_type = |wanted: &str| {83
attachments93
attachments84
.iter()94
.iter()@@ -92,8 +102,6 @@ fn attachment_label(attachments: &[AttachmentInfo]) -> Option<&'static str> {92
Some(AUDIO_PREVIEW)102
Some(AUDIO_PREVIEW)93
} else if has_type(LOOP_CARD_TYPE) {103
} else if has_type(LOOP_CARD_TYPE) {94
Some(LOOP_PREVIEW)104
Some(LOOP_PREVIEW)95
} else if attachments.iter().any(AttachmentInfo::is_card) {96
Some(CARD_PREVIEW)97
} else {105
} else {98
None106
None99
}107
}@@ -214,6 +222,7 @@ pub(crate) struct OwnedPreview {214
pub deleted: bool,222
pub deleted: bool,215
pub system: bool,223
pub system: bool,216
pub call: bool,224
pub call: bool,225
pub attachments_only: bool,217
}226
}218
227
219
impl OwnedPreview {228
impl OwnedPreview {@@ -227,17 +236,20 @@ impl OwnedPreview {227
let call = detail236
let call = detail228
.and_then(CallEvent::from_graph)237
.and_then(CallEvent::from_graph)229
.is_some_and(|event| event.phase.is_call());238
.is_some_and(|event| event.phase.is_call());239
let html = message240
.body241
.as_ref()242
.and_then(|body| body.content.as_deref())243
.filter(|_| regular && !deleted);230
let text = if deleted {244
let text = if deleted {231
None245
None232
} else if regular {246
} else if regular {233
message247
html.and_then(|html| message_preview(html, &[]))234
.body235
.as_ref()236
.and_then(|body| body.content.as_deref())237
.and_then(|html| message_preview(html, &[]))238
} else {248
} else {239
detail.and_then(system_event_preview).map(str::to_owned)249
detail.and_then(system_event_preview).map(str::to_owned)240
};250
};251
let attachments_only =252
text.is_none() && html.is_some_and(|html| html.contains("<attachment"));241
let sender = message.from.as_ref().filter(|_| regular);253
let sender = message.from.as_ref().filter(|_| regular);242
OwnedPreview {254
OwnedPreview {243
text,255
text,@@ -250,6 +262,7 @@ impl OwnedPreview {250
deleted,262
deleted,251
system: !regular,263
system: !regular,252
call,264
call,265
attachments_only,253
}266
}254
}267
}255
268
@@ -273,6 +286,7 @@ impl OwnedPreview {273
deleted: record.deleted,286
deleted: record.deleted,274
system: !sender,287
system: !sender,275
call: event.is_some_and(|event| event.phase.is_call()),288
call: event.is_some_and(|event| event.phase.is_call()),289
attachments_only: false,276
}290
}277
}291
}278
292
@@ -395,6 +409,41 @@ mod tests {395
assert_eq!(message_preview(body, &[]), None);409
assert_eq!(message_preview(body, &[]), None);396
}410
}397
411
412
#[test]413
fn a_card_with_text_shows_the_card_text() {414
let body = r#"<attachment id="1792149081221"></attachment>"#;415
let mut card = attachment("application/vnd.microsoft.card.adaptive");416
card.content = Some(417
serde_json::json!({418
"type": "AdaptiveCard",419
"body": [420
{"type": "TextBlock", "text": "Ada Lovelace edited Engine Notes"},421
{"type": "TextBlock", "text": "Page updated"},422
],423
})424
.to_string(),425
);426
assert_eq!(427
message_preview(body, &[card]).as_deref(),428
Some("Ada Lovelace edited Engine Notes Page updated")429
);430
}431
432
#[test]433
fn graph_attachment_only_bodies_keep_the_stored_line() {434
let preview = |content: &str| {435
OwnedPreview::from_graph(&graph_message(serde_json::json!({436
"id": "1",437
"messageType": "message",438
"body": {"contentType": "html", "content": content},439
})))440
};441
let card = preview(r#"<attachment id="1792149081221"></attachment>"#);442
assert!(card.attachments_only);443
assert_eq!(card.text, None);444
assert!(!preview("<p>hi</p>").attachments_only);445
}446
398
#[test]447
#[test]399
fn a_file_with_text_shows_the_text() {448
fn a_file_with_text_shows_the_text() {400
let body = r#"Hi Philipp,<br>Kann's dir zeigen.<attachment id="0cf57a46"></attachment>"#;449
let body = r#"Hi Philipp,<br>Kann's dir zeigen.<attachment id="0cf57a46"></attachment>"#;@@ -56,7 +56,7 @@ impl Store {
56
56
chat.last_message_sender_name,
57
57
chat.last_message_deleted,
58
58
chat.last_event_system,
59
chat.last_event_call,
59
chat.keeps_stored_preview,
60
60
])?;
61
61
clear_members.execute([&chat.id])?;
62
62
for (position, member) in chat.members.iter().enumerate() {
@@ -226,7 +226,7 @@ fn chat_from_row(row: &Row<'_>) -> rusqlite::Result<ChatRecord> {
226
226
last_message_deleted: row.get(10)?,
227
227
muted: row.get(11)?,
228
228
last_event_system: false,
229
last_event_call: false,
229
keeps_stored_preview: false,
230
230
})
231
231
}
232
232
@@ -56,7 +56,7 @@ impl Store {56
chat.last_message_sender_name,56
chat.last_message_sender_name,57
chat.last_message_deleted,57
chat.last_message_deleted,58
chat.last_event_system,58
chat.last_event_system,59
chat.last_event_call,59
chat.keeps_stored_preview,60
])?;60
])?;61
clear_members.execute([&chat.id])?;61
clear_members.execute([&chat.id])?;62
for (position, member) in chat.members.iter().enumerate() {62
for (position, member) in chat.members.iter().enumerate() {@@ -226,7 +226,7 @@ fn chat_from_row(row: &Row<'_>) -> rusqlite::Result<ChatRecord> {226
last_message_deleted: row.get(10)?,226
last_message_deleted: row.get(10)?,227
muted: row.get(11)?,227
muted: row.get(11)?,228
last_event_system: false,228
last_event_system: false,229
last_event_call: false,229
keeps_stored_preview: false,230
})230
})231
}231
}232
232
@@ -37,7 +37,7 @@ pub struct ChatRecord {
37
37
pub last_message_sender_name: Option<String>,
38
38
pub last_message_deleted: bool,
39
39
pub last_event_system: bool,
40
pub last_event_call: bool,
40
pub keeps_stored_preview: bool,
41
41
}
42
42
43
43
impl ChatRecord {
@@ -37,7 +37,7 @@ pub struct ChatRecord {37
pub last_message_sender_name: Option<String>,37
pub last_message_sender_name: Option<String>,38
pub last_message_deleted: bool,38
pub last_message_deleted: bool,39
pub last_event_system: bool,39
pub last_event_system: bool,40
pub last_event_call: bool,40
pub keeps_stored_preview: bool,41
}41
}42
42
43
impl ChatRecord {43
impl ChatRecord {@@ -35,7 +35,7 @@ fn a_call_event_keeps_the_previous_preview() {
35
35
36
36
let mut call = chat("chat-call", "Planning", Some(at(12)));
37
37
call.last_event_system = true;
38
call.last_event_call = true;
38
call.keeps_stored_preview = true;
39
39
store.upsert_chats(&[call]).unwrap();
40
40
let stored = store.chat("chat-call").unwrap().unwrap();
41
41
assert_eq!(
@@ -35,7 +35,7 @@ fn a_call_event_keeps_the_previous_preview() {35
35
36
let mut call = chat("chat-call", "Planning", Some(at(12)));36
let mut call = chat("chat-call", "Planning", Some(at(12)));37
call.last_event_system = true;37
call.last_event_system = true;38
call.last_event_call = true;38
call.keeps_stored_preview = true;39
store.upsert_chats(&[call]).unwrap();39
store.upsert_chats(&[call]).unwrap();40
let stored = store.chat("chat-call").unwrap().unwrap();40
let stored = store.chat("chat-call").unwrap().unwrap();41
assert_eq!(41
assert_eq!(