This commit is contained in:
yves committed 2026-11-20 23:17:15 +01:00
1 parent 10af480218
commit 47b73e6935
11 files changed
+469 -27

No files matched your search

+18
View File
@@ -267,6 +267,24 @@ let mut events = client.subscribe(); // Strea
* C'est ici que Matriochka se branchera : un élément portera une route et une date de départ
plutôt qu'une simple liste de serveurs.
### 5.2 Statuts et accusés
* Chaque entrée d'historique porte un `Status` qui ne fait qu'avancer.
* Message envoyé : `Queued` (aucun serveur ne l'a encore accepté), puis `Sent` (un serveur l'a
accepté), puis `Delivered` (l'appareil du contact l'a traité), puis `Read`.
* Message reçu : `Delivered` (non lu), puis `Read`.
* Les accusés sont des `UserMessage { Receipt { kind, message_ids } }` **chiffrés dans le
ratchet** : le serveur ne les distingue pas d'un message.
* Un `DELIVERED` par contact et par lot reçu est mis en file dans la même sauvegarde que le
traitement.
* `READ` est envoyé par `mark_read(contact)`.
* Il n'y a jamais d'accusé d'accusé.
* Les accusés révèlent quand on est en ligne et quand on lit. Ils se règlent donc par identité
(`Settings { delivered_receipts, read_receipts }`, rangés dans l'espace principal). Désactiver
l'accusé de lecture ne change pas le statut local.
* Le store applique une mise à jour de statut dans le segment qui contient le message, en
partant du plus récent. Seuls les segments modifiés sont réécrits.
---
## 6. Stockage client (`meow-store`)
+3 -2
View File
@@ -16,7 +16,7 @@ Pour reprendre le travail : lire ce fichier, prendre le premier lot ⬜ dont les
1. ~~**3.8** livraison au moins une fois~~ ✅
2. ~~**4.5** brancher `meow-store` dans le client et la CLI~~ ✅
3. ~~5.5~~ ✅, puis **5.6, 5.9** accusés et flux d'événements (prérequis de l'UI), et **5.10**.
3. ~~5.5, 5.6~~ ✅, puis **5.9** flux d'événements (prérequis de l'UI), et **5.10**.
4. **7.1 → 7.3** FFI et intégration dans bastet.
5. Le reste (phases 8–10) selon les priorités.
@@ -24,6 +24,7 @@ Pour reprendre le travail : lire ce fichier, prendre le premier lot ⬜ dont les
| Date | Lots | Notes |
|---|---|---|
| 2026-11-20 | 5.6 | `Status` par message (`Queued`, `Sent`, `Delivered`, `Read`, sans retour en arrière), mis à jour dans le segment qui contient le message. Accusés `DELIVERED` (un par contact et par lot) et `READ` (`mark_read`) dans le ratchet, désactivables par identité (`Settings`). `Event::Receipt`. CLI : `read`, `identity receipts`, statuts dans `history`, `longpoll --exit` ignore les lots ne contenant que des accusés. Correction d'un test de store instable (motif de 3 octets trouvé par hasard dans l'aléa). 43 tests, e2e avec une section « accusés ». |
| 2026-11-20 | 5.5 | `OutboxItem` enregistrés dans le store (un objet par élément et par espace), dans la même sauvegarde que le ratchet. Backoff exponentiel avec aléa, expiration après 7 jours (`Event::SendFailed`), dépôts regroupés par serveur avec repli élément par élément. Les messages de handshake passent aussi par la file. CLI : `outbox [--flush]`, « queued for … ». ARCHITECTURE : §5.1, et §7 (réseau de serveurs et Matriochka, points à trancher). 41 tests, e2e avec une section « file hors ligne ». |
| 2026-11-20 | 4.5 | `meow-client` passe sur `meow-store` (un état par espace, sauvegarde des seuls espaces modifiés, acks par espace) ; API `unlock`, `hide_contact`, `reveal_contact`, `change_password`, `history_page`, `file`. CLI : `-u`, `contact hide/reveal`, `identity password`. Store derrière un `Mutex` pour garder le client `Send + Sync` (requis pour la FFI). Le fichier `.mwid2` provisoire disparaît. 38 tests, e2e avec sections « contact caché » et « changement de mot de passe ». |
| 2026-11-20 | 4.1–4.3 | `meow-store` repensé après relecture du stockage v1 : objectifs anti-saisie (rien ne relie les blocs sans mot de passe, espaces cachés à un ou plusieurs contacts). Sac de blocs uniformes, espaces, segments. Un premier brouillon avec une colonne `contact` en clair a été abandonné. 6 tests de store. Pas encore branché dans le client. |
@@ -96,7 +97,7 @@ Pour reprendre le travail : lire ce fichier, prendre le premier lot ⬜ dont les
| 5.3 | Envoi : dépôt sur tous les serveurs de pull du contact, repli en cas d'échec | e2e | ✅ |
| 5.4 | Réception : un pull par serveur regroupant toutes les mailbox, long-poll, traitement, événements | e2e | ✅ |
| 5.5 | File d'envoi persistante avec retries et backoff, envoi hors ligne | Tests | ✅ |
| 5.6 | Accusés de réception et de lecture chiffrés (dans le ratchet) | e2e | ⬜ |
| 5.6 | Accusés de réception et de lecture chiffrés (dans le ratchet) | e2e | ✅ |
| 5.7 | Fichiers : découpage en morceaux, reprise, déduplication, limite de taille. Le contenu reçu doit être écrit sur disque **avant** l'ack : aujourd'hui, la CLI écrit les fichiers après `sync`, alors que les acks sont déjà partis | e2e (`send-file`) | 🟡 envoi en un seul morceau |
| 5.8 | Trafic leurre : mailbox fictives dans les pulls, ordre aléatoire, padding des requêtes | Tests | ⬜ |
| 5.9 | Stream d'événements (`tokio::broadcast`) et tâche de synchronisation en arrière-plan | | ⬜ |
+54 -5
View File
@@ -8,7 +8,7 @@ use std::time::Duration;
use anyhow::{bail, Context, Result};
use clap::{Args, Parser, Subcommand};
use meow_client::{Content, Event, MeowClient, DEFAULT_INVITATION_TTL};
use meow_client::{Content, Event, MeowClient, ReceiptKind, Status, DEFAULT_INVITATION_TTL};
use meow_crypto::password::KdfParams;
#[derive(Parser)]
@@ -73,7 +73,7 @@ enum Cmd {
Longpoll {
#[arg(short, long, default_value_t = 30)]
wait: u64,
/// Exit after the first batch of events (or the first timeout).
/// Exit after the first batch of events other than receipts (or the first timeout).
#[arg(short, long)]
exit: bool,
},
@@ -83,6 +83,8 @@ enum Cmd {
#[arg(long)]
flush: bool,
},
/// Mark a conversation as read (sends a read receipt unless disabled).
Read { contact: String },
/// Show the conversation with a contact.
History { contact: String },
}
@@ -97,6 +99,15 @@ enum IdentityCmd {
},
/// Show the identity summary.
Show,
/// Show or change receipt preferences.
Receipts {
/// Tell contacts when their messages reach this device.
#[arg(long)]
delivered: Option<bool>,
/// Tell contacts when their messages are read.
#[arg(long)]
read: Option<bool>,
},
/// Change the main password.
Password {
#[arg(long)]
@@ -281,6 +292,17 @@ async fn run() -> Result<()> {
println!("servers: {}", c.servers().len());
println!("contacts: {}", c.contacts().len());
}
Cmd::Identity(IdentityCmd::Receipts { delivered, read }) => {
let mut c = ctx.open().await?;
let mut s = c.settings();
if delivered.is_some() || read.is_some() {
s.delivered_receipts = delivered.unwrap_or(s.delivered_receipts);
s.read_receipts = read.unwrap_or(s.read_receipts);
c.set_settings(s).await?;
}
println!("delivered receipts: {}", s.delivered_receipts);
println!("read receipts: {}", s.read_receipts);
}
Cmd::Identity(IdentityCmd::Password { new_password }) => {
ctx.open().await?.change_password(&new_password).await?;
println!("password changed");
@@ -398,7 +420,9 @@ async fn run() -> Result<()> {
loop {
let ev = c.sync(Duration::from_secs(wait)).await?;
print_events(&ctx, &ev)?;
if exit {
// Receipts are background traffic: keep waiting for something else.
let only_receipts = !ev.is_empty() && ev.iter().all(|e| matches!(e, Event::Receipt { .. }));
if exit && !only_receipts {
break;
}
}
@@ -425,6 +449,10 @@ async fn run() -> Result<()> {
);
}
}
Cmd::Read { contact } => {
let n = ctx.open().await?.mark_read(&contact).await?;
println!("{n} message(s) marked as read");
}
Cmd::History { contact } => {
let c = ctx.open().await?;
for h in c.history(&contact)? {
@@ -433,9 +461,19 @@ async fn run() -> Result<()> {
} else {
contact.clone()
};
let status = if h.outbound {
match h.status {
Status::Queued => " [queued]",
Status::Sent => " [sent]",
Status::Delivered => " [delivered]",
Status::Read => " [read]",
}
} else {
""
};
match &h.content {
Content::Text(t) => println!("[{who}] {t}"),
Content::File { name, size } => println!("[{who}] <file {name}, {size} bytes>"),
Content::Text(t) => println!("[{who}] {t}{status}"),
Content::File { name, size } => println!("[{who}] <file {name}, {size} bytes>{status}"),
}
}
}
@@ -545,6 +583,17 @@ fn print_events(ctx: &Ctx, events: &[Event]) -> Result<()> {
}
Event::InvitationAccepted { contact } => println!("invitation accepted by {contact}, confirmation sent"),
Event::ContactActivated { contact } => println!("contact {contact} is active"),
Event::Receipt {
contact,
kind,
message_ids,
} => {
let verb = match kind {
ReceiptKind::Delivered => "received",
ReceiptKind::Read => "read",
};
println!("{contact} {verb} {} message(s)", message_ids.len());
}
Event::SendFailed { contact, .. } => println!("a message to {contact} could not be delivered (expired)"),
}
}
+95 -1
View File
@@ -36,7 +36,7 @@ use meow_proto::Message;
use meow_store::{Batch, RetiredMailbox, SpaceId, State as SpaceState, Store};
pub use meow_core::contact::ContactState as State;
pub use meow_store::{Content, HistoryEntry, HistoryPage, PendingAck};
pub use meow_store::{Content, HistoryEntry, HistoryPage, PendingAck, Settings, Status};
pub use outbox::{PendingSend, RetryPolicy};
/// Maximum file size sent in a single message (chunking: roadmap 5.7).
@@ -121,10 +121,22 @@ pub enum Event {
name: String,
data: Vec<u8>,
},
/// The contact's device received (or read) some of my messages.
Receipt {
contact: String,
kind: ReceiptKind,
message_ids: Vec<Vec<u8>>,
},
/// A queued message expired before any server accepted it.
SendFailed { contact: String, message_id: Vec<u8> },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReceiptKind {
Delivered,
Read,
}
/// Outcome of a send.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SendReport {
@@ -231,6 +243,16 @@ impl MeowClient {
&self.main().nickname
}
/// Identity preferences (receipts…).
pub fn settings(&self) -> Settings {
self.main().settings
}
pub async fn set_settings(&mut self, settings: Settings) -> Result<()> {
self.spaces[0].state.settings = settings;
self.save_space(0)
}
fn main(&self) -> &SpaceState {
&self.spaces[0].state
}
@@ -456,6 +478,39 @@ impl MeowClient {
Ok(lock(&self.store)?.history(sp.id, &sp.state.contacts[ci].id, page)?)
}
/// 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> {
let (si, ci) = self.locate(name)?;
let cid = self.spaces[si].state.contacts[ci].id.clone();
let unread: Vec<Vec<u8>> = lock(&self.store)?
.full_history(self.spaces[si].id, &cid)?
.into_iter()
.filter(|h| !h.outbound && h.status < Status::Read)
.map(|h| h.id)
.collect();
if unread.is_empty() {
return Ok(0);
}
let read_receipts = self.settings().read_receipts;
let sp = &mut self.spaces[si];
sp.batch
.status
.extend(unread.iter().map(|m| (cid.clone(), m.clone(), Status::Read)));
if read_receipts {
let contact = &mut sp.state.contacts[ci];
let r = messaging::receipt(v2::receipt::Kind::Read, unread.clone(), now_ms());
let env = messaging::encrypt(contact, &r)?;
let d = messaging::deposit_for(contact, &env)?;
sp.state
.outbox
.push(outbox::item(&cid, vec![], &d, peer_servers(contact)));
}
self.save_space(si)?;
self.flush(false).await?;
Ok(unread.len())
}
/// Content of a received file.
pub fn file(&self, name: &str, message_id: &[u8]) -> Result<Option<Vec<u8>>> {
let (si, ci) = self.locate(name)?;
@@ -662,6 +717,7 @@ impl MeowClient {
sent_at_ms: msg.sent_at_ms,
received_at: now(),
content,
status: Status::Queued,
});
// Persisted (ratchet + queue) before anything is sent.
self.save_space(si)?;
@@ -888,6 +944,8 @@ impl MeowClient {
envs.sort_by_key(|(rank, at, _, _, _)| (*rank, *at));
let mut events = Vec::new();
// (space, contact id) → ids of the messages received, for delivery receipts.
let mut received: Vec<(usize, meow_core::contact::ContactId, Vec<Vec<u8>>)> = Vec::new();
for (_, received_at, si, mailbox, env) in envs {
let sp = &mut self.spaces[si];
let Some(ci) = sp.state.contacts.iter().position(|c| {
@@ -950,9 +1008,15 @@ impl MeowClient {
sent_at_ms: msg.sent_at_ms,
received_at,
content,
status: Status::Delivered,
};
let mut got = |id: &[u8]| match received.iter_mut().find(|(s, c, _)| *s == si && *c == cid) {
Some(r) => r.2.push(id.to_vec()),
None => received.push((si, cid.clone(), vec![id.to_vec()])),
};
match &msg.body {
Some(v2::user_message::Body::Text(t)) => {
got(&msg.id);
sp.batch.history.push(entry(Content::Text(t.text.clone())));
events.push(Event::Message {
contact: name,
@@ -962,6 +1026,7 @@ impl MeowClient {
});
}
Some(v2::user_message::Body::File(f)) => {
got(&msg.id);
sp.batch.history.push(entry(Content::File {
name: f.filename.clone(),
size: f.size,
@@ -974,6 +1039,20 @@ impl MeowClient {
data: f.data.clone(),
});
}
Some(v2::user_message::Body::Receipt(r)) => {
let (kind, status) = match v2::receipt::Kind::try_from(r.kind) {
Ok(v2::receipt::Kind::Read) => (ReceiptKind::Read, Status::Read),
_ => (ReceiptKind::Delivered, Status::Delivered),
};
sp.batch
.status
.extend(r.message_ids.iter().map(|m| (cid.clone(), m.clone(), status)));
events.push(Event::Receipt {
contact: name,
kind,
message_ids: r.message_ids.clone(),
});
}
_ => {}
}
}
@@ -981,6 +1060,21 @@ impl MeowClient {
}
sp.dirty = true;
}
// One delivery receipt per contact for this batch (never for receipts).
if self.settings().delivered_receipts {
for (si, cid, ids) in received {
let sp = &mut self.spaces[si];
let Some(contact) = sp.state.contacts.iter_mut().find(|c| c.id == cid) else {
continue;
};
let r = messaging::receipt(v2::receipt::Kind::Delivered, ids, now_ms());
let env = messaging::encrypt(contact, &r)?;
let d = messaging::deposit_for(contact, &env)?;
sp.state
.outbox
.push(outbox::item(&cid, vec![], &d, peer_servers(contact)));
}
}
for (si, before) in mailboxes_before.into_iter().enumerate() {
if self.spaces[si].dirty {
self.retire_mailboxes(si, before);
+7 -1
View File
@@ -217,8 +217,14 @@ impl MeowClient {
// Update the items.
let mut now_accepted: HashMap<Vec<u8>, usize> = HashMap::new();
for (si, id, key) in accepted {
if let Some(o) = self.spaces[si].state.outbox.iter_mut().find(|o| o.id == id) {
let sp = &mut self.spaces[si];
if let Some(o) = sp.state.outbox.iter_mut().find(|o| o.id == id) {
o.pending.retain(|s| s.sign_key != key);
if o.accepted == 0 && !o.message_id.is_empty() {
sp.batch
.status
.push((o.contact.clone(), o.message_id.clone(), meow_store::Status::Sent));
}
o.accepted += 1;
*now_accepted.entry(id).or_default() += 1;
}
+103 -3
View File
@@ -55,6 +55,22 @@ async fn client(dir: &std::path::Path, name: &str, servers: &[String]) -> MeowCl
c
}
fn no_receipts(events: Vec<Event>) -> Vec<Event> {
events
.into_iter()
.filter(|e| !matches!(e, Event::Receipt { .. }))
.collect()
}
async fn no_receipt_settings(c: &mut MeowClient) {
c.set_settings(meow_client::Settings {
delivered_receipts: false,
read_receipts: false,
})
.await
.unwrap();
}
fn texts(events: &[Event]) -> Vec<String> {
events
.iter()
@@ -139,16 +155,16 @@ async fn file_and_server_invitations_then_messaging() {
.await
.unwrap();
assert_eq!(who, "bob");
let ev = bob.sync(Duration::ZERO).await.unwrap();
let ev = no_receipts(bob.sync(Duration::ZERO).await.unwrap());
assert_eq!(
ev,
vec![Event::InvitationAccepted {
contact: "remote".into()
}]
);
let ev = remote.sync(Duration::ZERO).await.unwrap();
let ev = no_receipts(remote.sync(Duration::ZERO).await.unwrap());
assert_eq!(ev, vec![Event::ContactActivated { contact: "bob".into() }]);
let ev = bob.sync(Duration::ZERO).await.unwrap();
let ev = no_receipts(bob.sync(Duration::ZERO).await.unwrap());
assert_eq!(
ev,
vec![Event::ContactActivated {
@@ -311,6 +327,9 @@ async fn no_message_lost_when_the_client_dies() {
let servers = vec![url];
let mut bob = client(&dir, "bob", &servers).await;
let mut alice = client(&dir, "alice", &servers).await;
// This test counts what stays on the server: no receipts traffic.
no_receipt_settings(&mut bob).await;
no_receipt_settings(&mut alice).await;
let offer = bob.create_invitation("alice", &[], "", 3600).await.unwrap();
let answer = alice.answer_invitation(&offer, None, &[]).await.unwrap();
let confirm = bob.accept_answer(&answer).await.unwrap();
@@ -350,6 +369,7 @@ async fn no_message_lost_when_the_client_dies() {
// 3. Mailboxes that disappear still get their last messages acknowledged:
// the temporary answer mailbox of a server invitation...
let mut carol = client(&dir, "carol", &servers).await;
no_receipt_settings(&mut carol).await;
let link = bob.publish_invitation("carol", &[], "", "", 3600).await.unwrap();
carol.accept_published_invitation(&link, "", None, &[]).await.unwrap();
bob.sync(Duration::ZERO).await.unwrap(); // step 2 → 3, answer mailbox retired
@@ -515,3 +535,83 @@ async fn offline_messages_are_queued_and_retried() {
assert!(bob.pending_sends().is_empty());
let _ = std::fs::remove_dir_all(&dir);
}
// ───────────── receipts (roadmap lot 5.6) ─────────────
fn statuses(c: &MeowClient, name: &str, outbound: bool) -> Vec<meow_client::Status> {
c.history(name)
.unwrap()
.iter()
.filter(|h| h.outbound == outbound)
.map(|h| h.status)
.collect()
}
#[tokio::test(flavor = "multi_thread")]
async fn delivery_and_read_receipts() {
use meow_client::{ReceiptKind, Settings, Status};
let servers = vec![spawn_server().await, spawn_server().await];
let dir = tmpdir();
let mut alice = client(&dir, "alice", &servers).await;
let mut bob = client(&dir, "bob", &servers).await;
connect(&mut alice, &mut bob, "bob").await;
bob.sync(Duration::ZERO).await.unwrap();
let m1 = alice.send_text("bob", "one").await.unwrap().message_id;
let m2 = alice.send_text("bob", "two").await.unwrap().message_id;
assert_eq!(statuses(&alice, "bob", true), vec![Status::Sent, Status::Sent]);
// Bob receives (from 2 servers, processed once): one delivery receipt.
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["one", "two"]);
assert_eq!(
statuses(&bob, "alice", false),
vec![Status::Delivered, Status::Delivered]
);
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert_eq!(
ev,
vec![Event::Receipt {
contact: "bob".into(),
kind: ReceiptKind::Delivered,
message_ids: vec![m1.clone(), m2.clone()]
}]
);
assert_eq!(
statuses(&alice, "bob", true),
vec![Status::Delivered, Status::Delivered]
);
// 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]);
let ev = alice.sync(Duration::ZERO).await.unwrap();
assert_eq!(
ev,
vec![Event::Receipt {
contact: "bob".into(),
kind: ReceiptKind::Read,
message_ids: vec![m1, m2]
}]
);
assert_eq!(statuses(&alice, "bob", true), vec![Status::Read, Status::Read]);
// Receipts are not acknowledged by receipts.
assert!(bob.sync(Duration::ZERO).await.unwrap().is_empty());
// Receipts can be turned off (persisted with the identity).
let off = Settings {
delivered_receipts: false,
read_receipts: false,
};
bob.set_settings(off).await.unwrap();
let path = dir.join("bob.mwid2");
drop(bob);
let mut bob = MeowClient::open(&path, "pw").await.unwrap();
assert_eq!(bob.settings(), off);
alice.send_text("bob", "three").await.unwrap();
assert_eq!(texts(&bob.sync(Duration::ZERO).await.unwrap()), vec!["three"]);
assert_eq!(bob.mark_read("alice").await.unwrap(), 1);
assert!(alice.sync(Duration::ZERO).await.unwrap().is_empty());
assert_eq!(statuses(&alice, "bob", true)[2], Status::Sent);
let _ = std::fs::remove_dir_all(&dir);
}
+11
View File
@@ -35,6 +35,17 @@ pub fn text(text: &str, now_ms: u64) -> v2::UserMessage {
)
}
/// Delivery or read receipt for messages received from the peer.
pub fn receipt(kind: v2::receipt::Kind, message_ids: Vec<Vec<u8>>, now_ms: u64) -> v2::UserMessage {
new_message(
v2::user_message::Body::Receipt(v2::Receipt {
kind: kind as i32,
message_ids,
}),
now_ms,
)
}
pub(crate) fn seal_with(session: &mut Session, msg: &v2::UserMessage, ad: &[u8]) -> Result<v2::RatchetMessage> {
let padded = pad::pad(&msg.encode_to_vec());
let (h, ciphertext) = session.encrypt(&padded, ad)?;
+62 -11
View File
@@ -44,7 +44,9 @@ use meow_crypto::password::KdfParams;
use rusqlite::{Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
pub use model::{Batch, Content, HistoryEntry, HistoryPage, OutboxItem, PendingAck, RetiredMailbox, SpaceId, State};
pub use model::{
Batch, Content, HistoryEntry, HistoryPage, OutboxItem, PendingAck, RetiredMailbox, Settings, SpaceId, State, Status,
};
#[derive(Debug, thiserror::Error)]
pub enum Error {
@@ -131,6 +133,7 @@ struct Meta {
servers: Vec<meow_core::server::ServerInfo>,
pending_acks: Vec<PendingAck>,
retired_mailboxes: Vec<RetiredMailbox>,
settings: Settings,
}
// ───────────────────────────── keys ─────────────────────────────
@@ -448,6 +451,7 @@ impl Store {
state.servers = m.servers;
state.pending_acks = m.pending_acks;
state.retired_mailboxes = m.retired_mailboxes;
state.settings = m.settings;
}
for e in &sp.index.contacts {
let bytes = blocks::get(&self.conn, &sp.key, &e.state)?;
@@ -492,6 +496,7 @@ impl Store {
servers: state.servers.clone(),
pending_acks: state.pending_acks.clone(),
retired_mailboxes: state.retired_mailboxes.clone(),
settings: state.settings,
})?;
let h = meow_crypto::sha256(&[&meta]);
if sp.written.get("meta") != Some(&h) || meta_ref.is_none() {
@@ -564,21 +569,34 @@ impl Store {
}
// History and files, per conversation.
type Pending = (ContactId, Vec<HistoryEntry>, Vec<(Vec<u8>, Vec<u8>)>);
let mut by_contact: Vec<Pending> = Vec::new();
for h in batch.history {
match by_contact.iter_mut().find(|(c, _, _)| *c == h.contact) {
Some(slot) => slot.1.push(h),
None => by_contact.push((h.contact.clone(), vec![h], vec![])),
#[derive(Default)]
struct Pending {
history: Vec<HistoryEntry>,
files: Vec<(Vec<u8>, Vec<u8>)>,
status: Vec<(Vec<u8>, Status)>,
}
fn slot<'a>(v: &'a mut Vec<(ContactId, Pending)>, c: &ContactId) -> &'a mut Pending {
let i = match v.iter().position(|(x, _)| x == c) {
Some(i) => i,
None => {
v.push((c.clone(), Pending::default()));
v.len() - 1
}
};
&mut v[i].1
}
let mut by_contact: Vec<(ContactId, Pending)> = Vec::new();
for h in batch.history {
let c = h.contact.clone();
slot(&mut by_contact, &c).history.push(h);
}
for (c, mid, data) in batch.files {
match by_contact.iter_mut().find(|(x, _, _)| *x == c) {
Some(slot) => slot.2.push((mid, data)),
None => by_contact.push((c, vec![], vec![(mid, data)])),
slot(&mut by_contact, &c).files.push((mid, data));
}
for (c, mid, st) in batch.status {
slot(&mut by_contact, &c).status.push((mid, st));
}
for (cid, history, files) in by_contact {
for (cid, Pending { history, files, status }) in by_contact {
let entry = contacts_idx
.iter_mut()
.find(|e| e.id == cid)
@@ -591,6 +609,7 @@ impl Store {
dir.files.push((mid, r));
}
}
update_status(&tx, &sp.key, &mut w, &mut dir, status)?;
entry.dir = w.put_value(&dir, Some(&entry.dir))?;
}
@@ -758,3 +777,35 @@ fn append_history(
dir.pages.push(r);
Ok(())
}
/// Apply status changes, searching segments from the latest one (where nearly all
/// updates land). Only rewritten segments change blocks.
fn update_status(
conn: &Connection,
key: &Key,
w: &mut Writer,
dir: &mut Dir,
mut updates: Vec<(Vec<u8>, Status)>,
) -> Result<()> {
for i in (0..dir.pages.len()).rev() {
if updates.is_empty() {
break;
}
let mut page: Vec<HistoryEntry> = blocks::get_value(conn, key, &dir.pages[i])?;
let mut changed = false;
updates.retain(|(mid, st)| match page.iter_mut().find(|e| e.id == *mid) {
Some(e) => {
if *st > e.status {
e.status = *st;
changed = true;
}
false
}
None => true,
});
if changed {
dir.pages[i] = w.put_value(&page, Some(&dir.pages[i]))?;
}
}
Ok(())
}
+38 -1
View File
@@ -15,6 +15,39 @@ pub struct HistoryEntry {
pub sent_at_ms: u64,
pub received_at: u64,
pub content: Content,
pub status: Status,
}
/// Delivery state of a message. Only ever moves forward.
///
/// Outbound: `Queued` (no server took it yet) → `Sent` (a server accepted it) →
/// `Delivered` (the contact's device processed it) → `Read`.
/// Inbound: `Delivered` (unread) → `Read`.
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Status {
#[default]
Queued,
Sent,
Delivered,
Read,
}
/// Per-identity preferences (main space).
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Settings {
/// Tell contacts when their messages reached this device (reveals when it is online).
pub delivered_receipts: bool,
/// Tell contacts when their messages were read.
pub read_receipts: bool,
}
impl Default for Settings {
fn default() -> Self {
Self {
delivered_receipts: true,
read_receipts: true,
}
}
}
/// Displayable content of a message.
@@ -99,6 +132,7 @@ pub struct State {
pub pending_acks: Vec<PendingAck>,
pub retired_mailboxes: Vec<RetiredMailbox>,
pub outbox: Vec<OutboxItem>,
pub settings: Settings,
}
/// Append-only data written in the same transaction as a [`State`].
@@ -107,10 +141,13 @@ pub struct Batch {
pub history: Vec<HistoryEntry>,
/// Received file contents: (contact, message id, data).
pub files: Vec<(ContactId, Vec<u8>, Vec<u8>)>,
/// Status changes of stored messages: (contact, message id, new status). Ignored
/// when the message is unknown or already further along.
pub status: Vec<(ContactId, Vec<u8>, Status)>,
}
impl Batch {
pub fn is_empty(&self) -> bool {
self.history.is_empty() && self.files.is_empty()
self.history.is_empty() && self.files.is_empty() && self.status.is_empty()
}
}
+64 -3
View File
@@ -5,7 +5,7 @@ use meow_core::contact::Contact;
use meow_core::server::{ServerInfo, ServerKeys};
use meow_crypto::password::KdfParams;
use meow_store::blocks::BLOCK_SIZE;
use meow_store::{Batch, Content, Error, HistoryEntry, PendingAck, SpaceId, State, Store};
use meow_store::{Batch, Content, Error, HistoryEntry, PendingAck, SpaceId, State, Status, Store};
const KDF: KdfParams = KdfParams::INSECURE_TEST;
const MAIN: SpaceId = SpaceId::MAIN;
@@ -40,6 +40,7 @@ fn text(c: &Contact, t: &str) -> HistoryEntry {
sent_at_ms: 1,
received_at: 2,
content: Content::Text(t.into()),
status: Status::Delivered,
}
}
@@ -141,6 +142,7 @@ fn state_segments_and_files() {
let later = Batch {
history: vec![text(&bob, "last")],
files: vec![],
status: vec![],
};
store.save(MAIN, &state, later).unwrap();
drop(store);
@@ -206,12 +208,14 @@ fn hidden_spaces() {
let batch = Batch {
history: vec![text(&secret, "for your eyes only")],
files: vec![],
status: vec![],
};
store.save(h, &hidden, batch).unwrap();
// A contact of another space cannot receive history here.
let wrong = Batch {
history: vec![text(&secret, "x")],
files: vec![],
status: vec![],
};
assert!(store.save(MAIN, &main, wrong).is_err());
assert!(matches!(
@@ -254,7 +258,8 @@ fn hidden_spaces() {
fn an_attacker_without_password_sees_only_uniform_blocks() {
let (path, dir) = tmp();
let s = server();
let bob = contact("bob", &s);
// Needles must be long: short ones (e.g. 3 bytes) occur by chance in megabytes of random filler.
let bob = contact("bob-the-contact-name", &s);
let mut store = Store::create(&path, "alice-the-nickname", "pw", KDF, false).unwrap();
let state = State {
nickname: "alice-the-nickname".into(),
@@ -265,6 +270,7 @@ fn an_attacker_without_password_sees_only_uniform_blocks() {
let batch = Batch {
history: vec![text(&bob, "very secret text one"), text(&bob, "very secret text two")],
files: vec![],
status: vec![],
};
store.save(MAIN, &state, batch).unwrap();
let h = store.create_space("hidden").unwrap();
@@ -293,7 +299,7 @@ fn an_attacker_without_password_sees_only_uniform_blocks() {
let wal = std::fs::metadata(format!("{}-wal", path.display())).map_or(0, |m| m.len());
assert_eq!(wal, 0);
let raw = std::fs::read(&path).unwrap();
for needle in ["alice-the-nickname", "very secret", "bob", "http://s"] {
for needle in ["alice-the-nickname", "very secret text", "bob-the-contact-name"] {
assert!(
!raw.windows(needle.len()).any(|w| w == needle.as_bytes()),
"{needle:?} found in clear"
@@ -342,6 +348,7 @@ fn rewrites_move_objects_and_wipe_old_blocks() {
Batch {
history: vec![text(&bob, "hello")],
files: vec![],
status: vec![],
},
)
.unwrap();
@@ -417,3 +424,57 @@ fn outbox_items_persist_per_space() {
assert!(store.load(h2).unwrap().outbox.is_empty());
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn status_only_moves_forward_in_any_segment() {
let (path, dir) = tmp();
let s = server();
let bob = contact("bob", &s);
let mut store = Store::create(&path, "alice", "pw", KDF, false).unwrap();
let mut state = store.load(MAIN).unwrap();
state.contacts.push(bob.clone());
let filler = "x".repeat(1000);
let msgs: Vec<HistoryEntry> = (0..30)
.map(|i| HistoryEntry {
outbound: true,
status: Status::Queued,
..text(&bob, &format!("{i}{filler}"))
})
.collect();
let (first, last) = (msgs[0].id.clone(), msgs[29].id.clone());
store
.save(
MAIN,
&state,
Batch {
history: msgs,
..Batch::default()
},
)
.unwrap();
// First message is in the oldest segment, last one in the latest.
let upd = |v: Vec<(Vec<u8>, Status)>| Batch {
status: v.into_iter().map(|(m, s)| (bob.id.clone(), m, s)).collect(),
..Batch::default()
};
store
.save(
MAIN,
&state,
upd(vec![
(first.clone(), Status::Read),
(last.clone(), Status::Delivered),
(b"unknown".to_vec(), Status::Read),
]),
)
.unwrap();
// A late, lower status does not go back.
store
.save(MAIN, &state, upd(vec![(first.clone(), Status::Sent)]))
.unwrap();
let all = store.full_history(MAIN, &bob.id).unwrap();
assert_eq!(all[0].status, Status::Read);
assert_eq!(all[29].status, Status::Delivered);
assert!(all[1..29].iter().all(|e| e.status == Status::Queued));
let _ = std::fs::remove_dir_all(dir);
}
+14
View File
@@ -160,6 +160,20 @@ meow alice send bob "ab2?" >/dev/null
expect "alice" "[bob] ba2?" meow alice longpoll --wait 5 --exit
expect "bob" "[alice] ab2?" meow bob longpoll --wait 5 --exit
# ── receipts ──
step "RECEIPTS"
meow alice send bob "receipt test" >/dev/null
expect "bob gets it" "[alice] receipt test" meow bob
expect "delivery receipt" "bob received 1 message(s)" meow alice
expect "status delivered" "receipt test [delivered]" meow alice history bob
expect "bob reads" "message(s) marked as read" meow bob read alice
expect "read receipt" "bob read" meow alice
expect "status read" "receipt test [read]" meow alice history bob
expect "receipts can be disabled" "read receipts: false" meow bob identity receipts --read false
meow alice send bob "no read receipt" >/dev/null
meow bob >/dev/null; meow bob read alice >/dev/null
expect "only delivered" "no read receipt [delivered]" bash -c "'$MEOW_BIN' -i '$WORK/alice.mwid2' sync >/dev/null; '$MEOW_BIN' -i '$WORK/alice.mwid2' history bob"
# ── hidden contact ──
step "HIDDEN CONTACT"
expect "hide" "remote is now hidden" meow bob contact hide remote --space-password hide-me