fix(chat): system events no longer mark a chat unread
- A transcript or recording event after the read marker made the Chats badge count a chat that Teams and the activity bell treat as read. - A system event keeps the unread state the chat already had. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -38,6 +38,7 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> Option<ChatRecord> {
38
38
.last_message_preview
39
39
.as_ref()
40
40
.map(OwnedPreview::from_graph);
41
let last_event_system = preview.as_ref().is_some_and(|preview| preview.system);
41
42
Some(ChatRecord {
42
43
id: chat.id.clone(),
43
44
kind: chat.chat_type.clone(),
@@ -49,7 +50,8 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> Option<ChatRecord> {
49
50
.join(", "),
50
51
last_message_at,
51
52
last_read_at,
52
unread: is_unread(chat.last_message_time(), last_read_at, last_from_me),
53
unread: !last_event_system
54
&& is_unread(chat.last_message_time(), last_read_at, last_from_me),
53
55
members: chat
54
56
.members
55
57
.iter()
@@ -66,6 +68,7 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> Option<ChatRecord> {
66
68
last_message_sender_name: preview
67
69
.as_ref()
68
70
.and_then(|preview| preview.sender_name.clone()),
71
last_event_system,
69
72
})
70
73
}
71
74
@@ -284,6 +287,42 @@ mod tests {
284
287
285
288
use super::*;
286
289
290
fn chat_with_preview(preview: serde_json::Value) -> Chat {
291
serde_json::from_value(json!({
292
"id": "19:meeting@thread.v2",
293
"chatType": "meeting",
294
"viewpoint": {"lastMessageReadDateTime": "2026-10-06T09:00:00Z"},
295
"lastMessagePreview": preview,
296
}))
297
.unwrap()
298
}
299
300
#[test]
301
fn system_event_after_the_read_marker_is_not_unread() {
302
let chat = chat_with_preview(json!({
303
"id": "1",
304
"messageType": "unknownFutureValue",
305
"createdDateTime": "2026-10-06T10:00:00Z",
306
"from": null,
307
}));
308
let record = chat_record(&chat, "me").unwrap();
309
assert!(!record.unread);
310
assert!(record.last_event_system);
311
}
312
313
#[test]
314
fn regular_message_after_the_read_marker_is_unread() {
315
let chat = chat_with_preview(json!({
316
"id": "1",
317
"messageType": "message",
318
"createdDateTime": "2026-10-06T10:00:00Z",
319
"from": {"user": {"id": "other"}},
320
}));
321
let record = chat_record(&chat, "me").unwrap();
322
assert!(record.unread);
323
assert!(!record.last_event_system);
324
}
325
287
326
fn mentions_of(mentioned: serde_json::Value) -> Vec<MentionInfo> {
288
327
let message: Message = serde_json::from_value(json!({
289
328
"id": "1",
@@ -38,6 +38,7 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> Option<ChatRecord> {38
.last_message_preview38
.last_message_preview39
.as_ref()39
.as_ref()40
.map(OwnedPreview::from_graph);40
.map(OwnedPreview::from_graph);41
let last_event_system = preview.as_ref().is_some_and(|preview| preview.system);41
Some(ChatRecord {42
Some(ChatRecord {42
id: chat.id.clone(),43
id: chat.id.clone(),43
kind: chat.chat_type.clone(),44
kind: chat.chat_type.clone(),@@ -49,7 +50,8 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> Option<ChatRecord> {49
.join(", "),50
.join(", "),50
last_message_at,51
last_message_at,51
last_read_at,52
last_read_at,52
unread: is_unread(chat.last_message_time(), last_read_at, last_from_me),53
unread: !last_event_system54
&& is_unread(chat.last_message_time(), last_read_at, last_from_me),53
members: chat55
members: chat54
.members56
.members55
.iter()57
.iter()@@ -66,6 +68,7 @@ pub fn chat_record(chat: &Chat, my_user_id: &str) -> Option<ChatRecord> {66
last_message_sender_name: preview68
last_message_sender_name: preview67
.as_ref()69
.as_ref()68
.and_then(|preview| preview.sender_name.clone()),70
.and_then(|preview| preview.sender_name.clone()),71
last_event_system,69
})72
})70
}73
}71
74
@@ -284,6 +287,42 @@ mod tests {284
287
285
use super::*;288
use super::*;286
289
290
fn chat_with_preview(preview: serde_json::Value) -> Chat {291
serde_json::from_value(json!({292
"id": "19:meeting@thread.v2",293
"chatType": "meeting",294
"viewpoint": {"lastMessageReadDateTime": "2026-10-06T09:00:00Z"},295
"lastMessagePreview": preview,296
}))297
.unwrap()298
}299
300
#[test]301
fn system_event_after_the_read_marker_is_not_unread() {302
let chat = chat_with_preview(json!({303
"id": "1",304
"messageType": "unknownFutureValue",305
"createdDateTime": "2026-10-06T10:00:00Z",306
"from": null,307
}));308
let record = chat_record(&chat, "me").unwrap();309
assert!(!record.unread);310
assert!(record.last_event_system);311
}312
313
#[test]314
fn regular_message_after_the_read_marker_is_unread() {315
let chat = chat_with_preview(json!({316
"id": "1",317
"messageType": "message",318
"createdDateTime": "2026-10-06T10:00:00Z",319
"from": {"user": {"id": "other"}},320
}));321
let record = chat_record(&chat, "me").unwrap();322
assert!(record.unread);323
assert!(!record.last_event_system);324
}325
287
fn mentions_of(mentioned: serde_json::Value) -> Vec<MentionInfo> {326
fn mentions_of(mentioned: serde_json::Value) -> Vec<MentionInfo> {288
let message: Message = serde_json::from_value(json!({327
let message: Message = serde_json::from_value(json!({289
"id": "1",328
"id": "1",@@ -23,6 +23,7 @@ pub(crate) struct OwnedPreview {
23
23
pub sender_id: Option<String>,
24
24
pub sender_name: Option<String>,
25
25
pub deleted: bool,
26
pub system: bool,
26
27
}
27
28
28
29
impl OwnedPreview {
@@ -48,6 +49,7 @@ impl OwnedPreview {
48
49
.and_then(|sender| sender.display_name())
49
50
.map(str::to_owned),
50
51
deleted,
52
system: !regular,
51
53
}
52
54
}
53
55
@@ -61,6 +63,7 @@ impl OwnedPreview {
61
63
sender_id: record.sender_id.clone(),
62
64
sender_name: record.sender_name.clone(),
63
65
deleted: record.deleted,
66
system: false,
64
67
}
65
68
}
66
69
@@ -23,6 +23,7 @@ pub(crate) struct OwnedPreview {23
pub sender_id: Option<String>,23
pub sender_id: Option<String>,24
pub sender_name: Option<String>,24
pub sender_name: Option<String>,25
pub deleted: bool,25
pub deleted: bool,26
pub system: bool,26
}27
}27
28
28
impl OwnedPreview {29
impl OwnedPreview {@@ -48,6 +49,7 @@ impl OwnedPreview {48
.and_then(|sender| sender.display_name())49
.and_then(|sender| sender.display_name())49
.map(str::to_owned),50
.map(str::to_owned),50
deleted,51
deleted,52
system: !regular,51
}53
}52
}54
}53
55
@@ -61,6 +63,7 @@ impl OwnedPreview {61
sender_id: record.sender_id.clone(),63
sender_id: record.sender_id.clone(),62
sender_name: record.sender_name.clone(),64
sender_name: record.sender_name.clone(),63
deleted: record.deleted,65
deleted: record.deleted,66
system: false,64
}67
}65
}68
}66
69
@@ -27,11 +27,11 @@ impl Store {
27
27
member_summary = excluded.member_summary,
28
28
last_message_at = excluded.last_message_at,
29
29
last_read_at = COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at),
30
unread = excluded.unread AND (
30
unread = CASE WHEN ?12 THEN chats.unread ELSE excluded.unread AND (
31
31
excluded.last_message_at IS NULL
32
32
OR COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at) IS NULL
33
33
OR excluded.last_message_at > COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at)
34
), last_message_preview = excluded.last_message_preview,
34
) END, last_message_preview = excluded.last_message_preview,
35
35
last_message_sender_id = excluded.last_message_sender_id,
36
36
last_message_sender_name = excluded.last_message_sender_name,
37
37
last_message_deleted = excluded.last_message_deleted",
@@ -54,6 +54,7 @@ impl Store {
54
54
chat.last_message_sender_id,
55
55
chat.last_message_sender_name,
56
56
chat.last_message_deleted,
57
chat.last_event_system,
57
58
])?;
58
59
clear_members.execute([&chat.id])?;
59
60
for (position, member) in chat.members.iter().enumerate() {
@@ -179,6 +180,7 @@ fn chat_from_row(row: &Row<'_>) -> rusqlite::Result<ChatRecord> {
179
180
last_message_sender_id: row.get(8)?,
180
181
last_message_sender_name: row.get(9)?,
181
182
last_message_deleted: row.get(10)?,
183
last_event_system: false,
182
184
})
183
185
}
184
186
@@ -27,11 +27,11 @@ impl Store {27
member_summary = excluded.member_summary,27
member_summary = excluded.member_summary,28
last_message_at = excluded.last_message_at,28
last_message_at = excluded.last_message_at,29
last_read_at = COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at),29
last_read_at = COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at),30
unread = excluded.unread AND (30
unread = CASE WHEN ?12 THEN chats.unread ELSE excluded.unread AND (31
excluded.last_message_at IS NULL31
excluded.last_message_at IS NULL32
OR COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at) IS NULL32
OR COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at) IS NULL33
OR excluded.last_message_at > COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at)33
OR excluded.last_message_at > COALESCE(MAX(chats.last_read_at, excluded.last_read_at), chats.last_read_at, excluded.last_read_at)34
), last_message_preview = excluded.last_message_preview,34
) END, last_message_preview = excluded.last_message_preview,35
last_message_sender_id = excluded.last_message_sender_id,35
last_message_sender_id = excluded.last_message_sender_id,36
last_message_sender_name = excluded.last_message_sender_name,36
last_message_sender_name = excluded.last_message_sender_name,37
last_message_deleted = excluded.last_message_deleted",37
last_message_deleted = excluded.last_message_deleted",@@ -54,6 +54,7 @@ impl Store {54
chat.last_message_sender_id,54
chat.last_message_sender_id,55
chat.last_message_sender_name,55
chat.last_message_sender_name,56
chat.last_message_deleted,56
chat.last_message_deleted,57
chat.last_event_system,57
])?;58
])?;58
clear_members.execute([&chat.id])?;59
clear_members.execute([&chat.id])?;59
for (position, member) in chat.members.iter().enumerate() {60
for (position, member) in chat.members.iter().enumerate() {@@ -179,6 +180,7 @@ fn chat_from_row(row: &Row<'_>) -> rusqlite::Result<ChatRecord> {179
last_message_sender_id: row.get(8)?,180
last_message_sender_id: row.get(8)?,180
last_message_sender_name: row.get(9)?,181
last_message_sender_name: row.get(9)?,181
last_message_deleted: row.get(10)?,182
last_message_deleted: row.get(10)?,183
last_event_system: false,182
})184
})183
}185
}184
186
@@ -20,6 +20,7 @@ pub struct ChatRecord {
20
20
pub last_message_sender_id: Option<String>,
21
21
pub last_message_sender_name: Option<String>,
22
22
pub last_message_deleted: bool,
23
pub last_event_system: bool,
23
24
}
24
25
25
26
#[derive(Debug, Clone, PartialEq)]
@@ -20,6 +20,7 @@ pub struct ChatRecord {20
pub last_message_sender_id: Option<String>,20
pub last_message_sender_id: Option<String>,21
pub last_message_sender_name: Option<String>,21
pub last_message_sender_name: Option<String>,22
pub last_message_deleted: bool,22
pub last_message_deleted: bool,23
pub last_event_system: bool,23
}24
}24
25
25
#[derive(Debug, Clone, PartialEq)]26
#[derive(Debug, Clone, PartialEq)]@@ -5,6 +5,23 @@ use store::{
5
5
MessageRecord, Store, SyncState, TeamRecord,
6
6
};
7
7
8
#[test]
9
fn a_system_event_keeps_the_previous_unread_state() {
10
let store = Store::open_in_memory().unwrap();
11
for (previous, expected) in [(true, true), (false, false)] {
12
let id = format!("chat-{previous}");
13
let mut seeded = chat(&id, "Planning", Some(at(5)));
14
seeded.unread = previous;
15
store.upsert_chats(&[seeded]).unwrap();
16
17
let mut event = chat(&id, "Planning", Some(at(12)));
18
event.unread = false;
19
event.last_event_system = true;
20
store.upsert_chats(&[event]).unwrap();
21
assert_eq!(store.chat(&id).unwrap().unwrap().unread, expected);
22
}
23
}
24
8
25
fn at(minute: u32) -> DateTime<Utc> {
9
26
Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap()
10
27
}
@@ -57,7 +74,9 @@ fn sender_application_round_trips() {
57
74
sender_application_id: Some("bot-guid".to_owned()),
58
75
..message("c1", "m1", 1)
59
76
};
60
store.upsert_messages(std::slice::from_ref(&bot_message)).unwrap();
77
store
78
.upsert_messages(std::slice::from_ref(&bot_message))
79
.unwrap();
61
80
let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap();
62
81
assert_eq!(found["m1"], bot_message);
63
82
}
@@ -843,11 +862,15 @@ fn activity(id: i64, minute: u32) -> store::ActivityRecord {
843
862
#[test]
844
863
fn activity_upsert_inserts_then_updates_in_place() {
845
864
let store = Store::open_in_memory().unwrap();
846
store.upsert_activity(&[activity(1, 5), activity(2, 9)]).unwrap();
865
store
866
.upsert_activity(&[activity(1, 5), activity(2, 9)])
867
.unwrap();
847
868
let mut changed = activity(1, 12);
848
869
changed.count = 3;
849
870
changed.read = true;
850
store.upsert_activity(std::slice::from_ref(&changed)).unwrap();
871
store
872
.upsert_activity(std::slice::from_ref(&changed))
873
.unwrap();
851
874
let records = store.activity().unwrap();
852
875
assert_eq!(records.len(), 2);
853
876
assert_eq!(records[0], changed);
@@ -861,6 +884,11 @@ fn activity_prune_drops_only_older_rows() {
861
884
.upsert_activity(&[activity(1, 5), activity(2, 9), activity(3, 20)])
862
885
.unwrap();
863
886
assert_eq!(store.prune_activity(at(9)).unwrap(), 1);
864
let ids: Vec<i64> = store.activity().unwrap().iter().map(|record| record.id).collect();
887
let ids: Vec<i64> = store
888
.activity()
889
.unwrap()
890
.iter()
891
.map(|record| record.id)
892
.collect();
865
893
assert_eq!(ids, vec![3, 2]);
866
894
}
@@ -5,6 +5,23 @@ use store::{5
MessageRecord, Store, SyncState, TeamRecord,5
MessageRecord, Store, SyncState, TeamRecord,6
};6
};7
7
8
#[test]9
fn a_system_event_keeps_the_previous_unread_state() {10
let store = Store::open_in_memory().unwrap();11
for (previous, expected) in [(true, true), (false, false)] {12
let id = format!("chat-{previous}");13
let mut seeded = chat(&id, "Planning", Some(at(5)));14
seeded.unread = previous;15
store.upsert_chats(&[seeded]).unwrap();16
17
let mut event = chat(&id, "Planning", Some(at(12)));18
event.unread = false;19
event.last_event_system = true;20
store.upsert_chats(&[event]).unwrap();21
assert_eq!(store.chat(&id).unwrap().unwrap().unread, expected);22
}23
}24
8
fn at(minute: u32) -> DateTime<Utc> {25
fn at(minute: u32) -> DateTime<Utc> {9
Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap()26
Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap()10
}27
}@@ -57,7 +74,9 @@ fn sender_application_round_trips() {57
sender_application_id: Some("bot-guid".to_owned()),74
sender_application_id: Some("bot-guid".to_owned()),58
..message("c1", "m1", 1)75
..message("c1", "m1", 1)59
};76
};60
store.upsert_messages(std::slice::from_ref(&bot_message)).unwrap();77
store78
.upsert_messages(std::slice::from_ref(&bot_message))79
.unwrap();61
let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap();80
let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap();62
assert_eq!(found["m1"], bot_message);81
assert_eq!(found["m1"], bot_message);63
}82
}@@ -843,11 +862,15 @@ fn activity(id: i64, minute: u32) -> store::ActivityRecord {843
#[test]862
#[test]844
fn activity_upsert_inserts_then_updates_in_place() {863
fn activity_upsert_inserts_then_updates_in_place() {845
let store = Store::open_in_memory().unwrap();864
let store = Store::open_in_memory().unwrap();846
store.upsert_activity(&[activity(1, 5), activity(2, 9)]).unwrap();865
store866
.upsert_activity(&[activity(1, 5), activity(2, 9)])867
.unwrap();847
let mut changed = activity(1, 12);868
let mut changed = activity(1, 12);848
changed.count = 3;869
changed.count = 3;849
changed.read = true;870
changed.read = true;850
store.upsert_activity(std::slice::from_ref(&changed)).unwrap();871
store872
.upsert_activity(std::slice::from_ref(&changed))873
.unwrap();851
let records = store.activity().unwrap();874
let records = store.activity().unwrap();852
assert_eq!(records.len(), 2);875
assert_eq!(records.len(), 2);853
assert_eq!(records[0], changed);876
assert_eq!(records[0], changed);@@ -861,6 +884,11 @@ fn activity_prune_drops_only_older_rows() {861
.upsert_activity(&[activity(1, 5), activity(2, 9), activity(3, 20)])884
.upsert_activity(&[activity(1, 5), activity(2, 9), activity(3, 20)])862
.unwrap();885
.unwrap();863
assert_eq!(store.prune_activity(at(9)).unwrap(), 1);886
assert_eq!(store.prune_activity(at(9)).unwrap(), 1);864
let ids: Vec<i64> = store.activity().unwrap().iter().map(|record| record.id).collect();887
let ids: Vec<i64> = store888
.activity()889
.unwrap()890
.iter()891
.map(|record| record.id)892
.collect();865
assert_eq!(ids, vec![3, 2]);893
assert_eq!(ids, vec![3, 2]);866
}894
}