chat adjustments

This commit is contained in:
yves committed 2026-11-21 09:48:54 +01:00
1 parent 5532a7ce06
commit 6ac21ccf4c
5 files changed
+246 -44

No files matched your search

+42 -1
View File
@@ -26,7 +26,7 @@ use std::path::Path;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use futures_util::future::join_all;
use meow_core::contact::{Contact, ContactState};
use meow_core::contact::ContactState;
use meow_core::invitation::{self, OfferInfo};
use meow_core::server::canonical_url;
use meow_core::{messaging, rpc};
@@ -37,6 +37,7 @@ use meow_proto::Message;
use meow_store::{Batch, RetiredMailbox, SpaceId, State as SpaceState, Store};
pub use background::{BackgroundOptions, ClientHandle};
pub use meow_core::contact::Contact;
pub use meow_core::contact::ContactState as State;
pub use meow_core::server::ServerInfo;
pub use meow_proto::PROTOCOL_VERSION;
@@ -150,6 +151,17 @@ pub struct SendReport {
pub delivered_to: usize,
}
/// Last message and unread count of a conversation ([`MeowClient::conversation_summary`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConversationSummary {
pub last: Option<HistoryEntry>,
/// Received messages not read yet (counted back to the last read or sent one).
pub unread: usize,
}
/// Segments scanned at most to count unread messages.
const SUMMARY_MAX_PAGES: usize = 8;
/// Events kept for slow subscribers.
const EVENT_BUFFER: usize = 256;
@@ -552,6 +564,35 @@ impl MeowClient {
Ok(lock(&self.store)?.history(sp.id, &sp.state.contacts[ci].id, page)?)
}
/// Last message and number of unread received messages of a conversation. Unread
/// messages are counted from the latest segment backwards, stopping at the first
/// segment that contains a read or sent message (at most [`SUMMARY_MAX_PAGES`]).
pub fn conversation_summary(&self, name: &str) -> Result<ConversationSummary> {
let (si, ci) = self.locate(name)?;
let sp = &self.spaces[si];
let cid = &sp.state.contacts[ci].id;
let store = lock(&self.store)?;
let mut page = store.history(sp.id, cid, None)?;
let last = page.entries.last().cloned();
let mut unread = 0;
for _ in 0..SUMMARY_MAX_PAGES {
let mut stop = false;
for e in page.entries.iter().rev() {
if !e.outbound && e.status < Status::Read {
unread += 1;
} else {
stop = true;
break;
}
}
match page.prev {
Some(p) if !stop => page = store.history(sp.id, cid, Some(p))?,
_ => break,
}
}
Ok(ConversationSummary { last, unread })
}
/// Mark every received message of a conversation as read, and tell the contact
/// (unless read receipts are off). Returns how many messages changed.
pub async fn mark_read(&mut self, name: &str) -> Result<usize> {
+4
View File
@@ -587,10 +587,14 @@ async fn delivery_and_read_receipts() {
vec![Status::Delivered, Status::Delivered]
);
let summary = bob.conversation_summary("alice").unwrap();
assert_eq!(summary.unread, 2);
assert!(matches!(summary.last.unwrap().content, meow_client::Content::Text(ref t) if t == "two"));
// Bob reads.
assert_eq!(bob.mark_read("alice").await.unwrap(), 2);
assert_eq!(bob.mark_read("alice").await.unwrap(), 0);
assert_eq!(statuses(&bob, "alice", false), vec![Status::Read, Status::Read]);
assert_eq!(bob.conversation_summary("alice").unwrap().unread, 0);
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert_eq!(
ev,