acks
This commit is contained in:
1 parent
10af480218
commit
47b73e6935
11 files changed
+470
-28
No files matched your search
@@ -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();
|
||||
#[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 {
|
||||
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![])),
|
||||
}
|
||||
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 (cid, history, files) in by_contact {
|
||||
for (c, mid, st) in batch.status {
|
||||
slot(&mut by_contact, &c).status.push((mid, st));
|
||||
}
|
||||
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(())
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user