use std::collections::{HashMap, HashSet}; use std::sync::Mutex; use std::time::Duration; use chatsvc::{EventKind, MemberHorizon, MessageEvent, StatusEvent, StatusKind}; use chrono::{DateTime, Utc}; use store::{MessageRecord, ReadHorizonRecord}; use crate::engine::{Conversation, META_USER_ID, SyncEngine}; use crate::error::Result; use crate::events::CoreEvent; use crate::remote::Remote; pub const DEFAULT_RECEIPT_DEBOUNCE: Duration = Duration::from_millis(500); #[derive(Debug, Clone, PartialEq, Eq)] pub struct ReceiptReader { pub name: String, pub at: DateTime, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum ReceiptState { Sent, Read { readers: Vec, total_others: usize, }, Unknown, } #[derive(Debug, Clone, PartialEq, Eq)] struct OtherMember { name: String, read_until: DateTime, read_at: DateTime, } #[derive(Debug, Clone, PartialEq, Eq)] struct ConversationReceipts { my_user_id: String, others: Vec, total_others: usize, } pub(crate) struct ReceiptCache { conversations: Mutex>>, pending: Mutex>, read_from_store: Mutex>, pub(crate) debounce: Duration, } impl Default for ReceiptCache { fn default() -> Self { ReceiptCache { conversations: Mutex::new(HashMap::new()), pending: Mutex::new(HashSet::new()), read_from_store: Mutex::new(HashSet::new()), debounce: DEFAULT_RECEIPT_DEBOUNCE, } } } pub(crate) fn locked(mutex: &Mutex) -> std::sync::MutexGuard<'_, T> { mutex .lock() .unwrap_or_else(|poisoned| poisoned.into_inner()) } fn same_user(left: &str, right: &str) -> bool { left.eq_ignore_ascii_case(right) } fn user_of(member_id: &str) -> &str { member_id.rsplit(':').next().unwrap_or(member_id) } fn stored_horizon(horizon: MemberHorizon) -> ReadHorizonRecord { ReadHorizonRecord { member_id: horizon.member_id, read_until: horizon.read_until, read_at: horizon.read_at, } } fn build( my_user_id: &str, horizons: &[ReadHorizonRecord], members: &[store::MemberRecord], ) -> Option { let mut others: Vec = horizons .iter() .filter(|horizon| !same_user(user_of(&horizon.member_id), my_user_id)) .map(|horizon| { let user_id = user_of(&horizon.member_id); let name = members .iter() .find(|member| { member .user_id .as_deref() .is_some_and(|known| same_user(known, user_id)) }) .map(|member| member.display_name.clone()) .unwrap_or_default(); OtherMember { name, read_until: horizon.read_until, read_at: horizon.read_at.unwrap_or(horizon.read_until), } }) .collect(); if others.is_empty() { return None; } others.sort_by(|left, right| left.name.cmp(&right.name)); let known_others = members .iter() .filter(|member| { member .user_id .as_deref() .is_none_or(|known| !same_user(known, my_user_id)) }) .count(); Some(ConversationReceipts { my_user_id: my_user_id.to_owned(), total_others: known_others.max(others.len()), others, }) } fn state_of(receipts: &ConversationReceipts, record: &MessageRecord) -> ReceiptState { let own = record .sender_id .as_deref() .is_some_and(|sender| same_user(sender, &receipts.my_user_id)); if !own || record.deleted { return ReceiptState::Unknown; } let mut readers: Vec = receipts .others .iter() .filter(|member| member.read_until >= record.created_at) .map(|member| ReceiptReader { name: member.name.clone(), at: member.read_at, }) .collect(); if readers.is_empty() { return ReceiptState::Sent; } readers.sort_by(|left, right| { left.at .cmp(&right.at) .then_with(|| left.name.cmp(&right.name)) }); ReceiptState::Read { readers, total_others: receipts.total_others, } } impl SyncEngine { pub fn with_receipt_debounce(mut self, debounce: Duration) -> Self { self.receipts.debounce = debounce; self } /// Own messages in chats only. Unknown for others' messages, channels, deleted messages and disabled receipts. pub fn receipt_state(&self, conversation_id: &str, message_id: &str) -> ReceiptState { let record = self .store .messages_by_id(conversation_id, &[message_id.to_owned()]) .ok() .and_then(|mut found| found.remove(message_id)); record.map_or(ReceiptState::Unknown, |record| { self.receipt_state_for(&record) }) } /// Memory first; the stored horizons of a chat are read once per session, so receipts show before the refresh. pub fn receipt_state_for(&self, record: &MessageRecord) -> ReceiptState { self.load_stored_receipts(&record.conversation_id); let conversations = locked(&self.receipts.conversations); match conversations.get(&record.conversation_id) { Some(Some(receipts)) => state_of(receipts, record), _ => ReceiptState::Unknown, } } fn load_stored_receipts(&self, conversation_id: &str) { if !locked(&self.receipts.read_from_store).insert(conversation_id.to_owned()) { return; } if locked(&self.receipts.conversations).contains_key(conversation_id) { return; } let Some(cached) = self.stored_receipts(conversation_id) else { return; }; locked(&self.receipts.conversations) .entry(conversation_id.to_owned()) .or_insert(Some(cached)); } fn stored_receipts(&self, conversation_id: &str) -> Option { let horizons = self.store.read_horizons(conversation_id).ok()?; if horizons.is_empty() { return None; } let my_user_id = self.store.meta(META_USER_ID).ok()??; let members = self.store.chat(conversation_id).ok()??.members; build(&my_user_id, &horizons, &members) } /// Call when a chat opens. Channels are skipped. pub async fn refresh_receipts(&self, conversation_id: &str) -> Result<()> { if matches!( self.resolve(conversation_id).await?, Conversation::Channel { .. } ) { return Ok(()); } let my_user_id = self.my_user_id().await?; let horizons: Vec = self .remote .consumption_horizons(conversation_id) .await? .into_iter() .map(stored_horizon) .collect(); self.store.set_read_horizons(conversation_id, &horizons)?; locked(&self.receipts.read_from_store).insert(conversation_id.to_owned()); let members = self .store .chat(conversation_id)? .map(|chat| chat.members) .unwrap_or_default(); let fresh = build(&my_user_id, &horizons, &members); let changed = locked(&self.receipts.conversations) .insert(conversation_id.to_owned(), fresh.clone()) .is_none_or(|previous| previous != fresh); if changed { let _ = self.events.send(CoreEvent::ReceiptsChanged { conversation_id: conversation_id.to_owned(), }); } Ok(()) } /// Calls within the debounce window return at once, the first call does the single refresh. pub async fn refresh_receipts_debounced(&self, conversation_id: &str) -> Result<()> { if !locked(&self.receipts.pending).insert(conversation_id.to_owned()) { return Ok(()); } tokio::time::sleep(self.receipts.debounce).await; locked(&self.receipts.pending).remove(conversation_id); self.refresh_receipts(conversation_id).await } /// Only conversations opened before are refreshed. Opening a chat fetches anyway. pub async fn handle_receipt_event(&self, event: &MessageEvent) { if event.kind != EventKind::ReadReceipt { return; } let Some(conversation_id) = event.conversation_id.as_deref() else { return; }; if locked(&self.receipts.conversations).contains_key(conversation_id) { let _ = self.refresh_receipts_debounced(conversation_id).await; } } /// Refetches every known conversation after a reconnect or lost messages. pub async fn handle_receipt_status(&self, status: &StatusEvent) { if !matches!(status.kind, StatusKind::Connected | StatusKind::MessageLoss) { return; } let known: Vec = locked(&self.receipts.conversations) .keys() .cloned() .collect(); for conversation_id in known { let _ = self.refresh_receipts(&conversation_id).await; } } }