fix(chats): a call moves the chat list time and keeps the older line
- realtime call events no longer wait for the next sync to update the time Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -552,20 +552,25 @@ impl<R: Remote> SyncEngine<R> {
552
552
Ok(delta)
553
553
}
554
554
555
/// A call keeps the older line but moves the chat time to the call, like Teams.
555
556
pub(crate) fn update_chat_preview(&self, records: &[MessageRecord]) -> Result<()> {
556
let Some(newest) = records
557
let (calls, lines): (Vec<&MessageRecord>, Vec<&MessageRecord>) = records
557
558
.iter()
558
.filter(|record| !crate::preview::keeps_previous_line(record))
559
.max_by_key(|record| record.created_at)
560
else {
561
return Ok(());
562
};
563
let preview = OwnedPreview::from_record(newest);
564
let changed = self.store.update_chat_preview(
565
&newest.conversation_id,
566
newest.created_at,
567
&preview.as_store(),
568
)?;
559
.partition(|record| crate::preview::keeps_previous_line(record));
560
let mut changed = false;
561
if let Some(newest) = lines.into_iter().max_by_key(|record| record.created_at) {
562
let preview = OwnedPreview::from_record(newest);
563
changed |= self.store.update_chat_preview(
564
&newest.conversation_id,
565
newest.created_at,
566
&preview.as_store(),
567
)?;
568
}
569
if let Some(call) = calls.into_iter().max_by_key(|record| record.created_at) {
570
changed |= self
571
.store
572
.touch_chat_time(&call.conversation_id, call.created_at)?;
573
}
569
574
if changed {
570
575
self.preview_changed.store(true, Ordering::SeqCst);
571
576
}
@@ -552,20 +552,25 @@ impl<R: Remote> SyncEngine<R> {552
Ok(delta)552
Ok(delta)553
}553
}554
554
555
/// A call keeps the older line but moves the chat time to the call, like Teams.555
pub(crate) fn update_chat_preview(&self, records: &[MessageRecord]) -> Result<()> {556
pub(crate) fn update_chat_preview(&self, records: &[MessageRecord]) -> Result<()> {556
let Some(newest) = records557
let (calls, lines): (Vec<&MessageRecord>, Vec<&MessageRecord>) = records557
.iter()558
.iter()558
.filter(|record| !crate::preview::keeps_previous_line(record))559
.partition(|record| crate::preview::keeps_previous_line(record));559
.max_by_key(|record| record.created_at)560
let mut changed = false;560
else {561
if let Some(newest) = lines.into_iter().max_by_key(|record| record.created_at) {561
return Ok(());562
let preview = OwnedPreview::from_record(newest);562
};563
changed |= self.store.update_chat_preview(563
let preview = OwnedPreview::from_record(newest);564
&newest.conversation_id,564
let changed = self.store.update_chat_preview(565
newest.created_at,565
&newest.conversation_id,566
&preview.as_store(),566
newest.created_at,567
)?;567
&preview.as_store(),568
}568
)?;569
if let Some(call) = calls.into_iter().max_by_key(|record| record.created_at) {570
changed |= self571
.store572
.touch_chat_time(&call.conversation_id, call.created_at)?;573
}569
if changed {574
if changed {570
self.preview_changed.store(true, Ordering::SeqCst);575
self.preview_changed.store(true, Ordering::SeqCst);571
}576
}@@ -2823,7 +2823,7 @@ async fn a_newer_message_updates_the_preview_and_announces_the_sidebar() {
2823
2823
}
2824
2824
2825
2825
#[tokio::test]
2826
async fn a_newer_call_event_leaves_the_preview_alone() {
2826
async fn a_newer_call_event_keeps_the_line_and_moves_the_time() {
2827
2827
let fake = Arc::new(Fake::default());
2828
2828
let mut before = numbered(3);
2829
2829
before.push(message(4, 30, "<p>before the call</p>"));
@@ -2849,7 +2849,7 @@ async fn a_newer_call_event_leaves_the_preview_alone() {
2849
2849
record.last_message_preview.as_deref(),
2850
2850
Some("before the call")
2851
2851
);
2852
assert_eq!(record.last_message_at, Some(base() + Duration::minutes(30)));
2852
assert_eq!(record.last_message_at, Some(base() + Duration::minutes(40)));
2853
2853
}
2854
2854
2855
2855
#[tokio::test]
@@ -2823,7 +2823,7 @@ async fn a_newer_message_updates_the_preview_and_announces_the_sidebar() {2823
}2823
}2824
2824
2825
#[tokio::test]2825
#[tokio::test]2826
async fn a_newer_call_event_leaves_the_preview_alone() {2826
async fn a_newer_call_event_keeps_the_line_and_moves_the_time() {2827
let fake = Arc::new(Fake::default());2827
let fake = Arc::new(Fake::default());2828
let mut before = numbered(3);2828
let mut before = numbered(3);2829
before.push(message(4, 30, "<p>before the call</p>"));2829
before.push(message(4, 30, "<p>before the call</p>"));@@ -2849,7 +2849,7 @@ async fn a_newer_call_event_leaves_the_preview_alone() {2849
record.last_message_preview.as_deref(),2849
record.last_message_preview.as_deref(),2850
Some("before the call")2850
Some("before the call")2851
);2851
);2852
assert_eq!(record.last_message_at, Some(base() + Duration::minutes(30)));2852
assert_eq!(record.last_message_at, Some(base() + Duration::minutes(40)));2853
}2853
}2854
2854
2855
#[tokio::test]2855
#[tokio::test]@@ -176,6 +176,16 @@ impl Store {
176
176
Ok(changed > 0)
177
177
}
178
178
179
pub fn touch_chat_time(&self, chat_id: &str, at: DateTime<Utc>) -> Result<bool> {
180
let connection = self.lock()?;
181
let changed = connection.execute(
182
"UPDATE chats SET last_message_at = ?2
183
WHERE id = ?1 AND (last_message_at IS NULL OR last_message_at < ?2)",
184
params![chat_id, to_millis(at)],
185
)?;
186
Ok(changed > 0)
187
}
188
179
189
pub fn recent_chats(&self, limit: usize) -> Result<Vec<ChatRecord>> {
180
190
let connection = self.lock()?;
181
191
let mut statement = connection.prepare_cached(&format!(
@@ -176,6 +176,16 @@ impl Store {176
Ok(changed > 0)176
Ok(changed > 0)177
}177
}178
178
179
pub fn touch_chat_time(&self, chat_id: &str, at: DateTime<Utc>) -> Result<bool> {180
let connection = self.lock()?;181
let changed = connection.execute(182
"UPDATE chats SET last_message_at = ?2183
WHERE id = ?1 AND (last_message_at IS NULL OR last_message_at < ?2)",184
params![chat_id, to_millis(at)],185
)?;186
Ok(changed > 0)187
}188
179
pub fn recent_chats(&self, limit: usize) -> Result<Vec<ChatRecord>> {189
pub fn recent_chats(&self, limit: usize) -> Result<Vec<ChatRecord>> {180
let connection = self.lock()?;190
let connection = self.lock()?;181
let mut statement = connection.prepare_cached(&format!(191
let mut statement = connection.prepare_cached(&format!(@@ -768,6 +768,18 @@ fn preview_updates_only_for_newer_messages() {
768
768
assert!(!record.last_message_deleted);
769
769
}
770
770
771
#[test]
772
fn a_call_moves_the_chat_time_and_keeps_the_line() {
773
let store = Store::open_in_memory().unwrap();
774
store.upsert_chats(&[chat("a", "A", Some(at(5)))]).unwrap();
775
let before = store.chat("a").unwrap().unwrap().last_message_preview;
776
assert!(!store.touch_chat_time("a", at(4)).unwrap());
777
assert!(store.touch_chat_time("a", at(7)).unwrap());
778
let record = store.chat("a").unwrap().unwrap();
779
assert_eq!(record.last_message_at, Some(at(7)));
780
assert_eq!(record.last_message_preview, before);
781
}
782
771
783
#[test]
772
784
fn avatars_round_trip_and_report_stale_ids() {
773
785
let store = Store::open_in_memory().unwrap();
@@ -768,6 +768,18 @@ fn preview_updates_only_for_newer_messages() {768
assert!(!record.last_message_deleted);768
assert!(!record.last_message_deleted);769
}769
}770
770
771
#[test]772
fn a_call_moves_the_chat_time_and_keeps_the_line() {773
let store = Store::open_in_memory().unwrap();774
store.upsert_chats(&[chat("a", "A", Some(at(5)))]).unwrap();775
let before = store.chat("a").unwrap().unwrap().last_message_preview;776
assert!(!store.touch_chat_time("a", at(4)).unwrap());777
assert!(store.touch_chat_time("a", at(7)).unwrap());778
let record = store.chat("a").unwrap().unwrap();779
assert_eq!(record.last_message_at, Some(at(7)));780
assert_eq!(record.last_message_preview, before);781
}782
771
#[test]783
#[test]772
fn avatars_round_trip_and_report_stale_ids() {784
fn avatars_round_trip_and_report_stale_ids() {773
let store = Store::open_in_memory().unwrap();785
let store = Store::open_in_memory().unwrap();