From 109f64905f1c16b7abd7143d229bc2382e8127f4 Mon Sep 17 00:00:00 2001 From: rust-dev Date: Sat, 12 Sep 2026 16:38:47 -0400 Subject: [PATCH] =?UTF-8?q?feat(sdk):=20ChatMessage.id=20=E2=80=94=20join?= =?UTF-8?q?=20a=20chat=20row=20to=20its=20outbox=20row?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `OutboxRow::message_id` documents itself as *the* join key (the `messages.id` a `ChatMessage` belongs to), but the record carried no id, so the Android client could only pair the two lists positionally. Add `ChatMessage.id: i64` as the first field, copied straight from the `messages.id` the store already selects. Additive over UniFFI: Kotlin consumers only gain a constructor field and the bindings regenerate. The id mapping moves into a `chat_message` helper so the join property is testable without a Node: the new test builds a real store, queues an outbox row for a message, and asserts the id on the message equals the `message_id` the outbox row reports for that same row. --- crates/onionwire-sdk/src/lib.rs | 90 +++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/crates/onionwire-sdk/src/lib.rs b/crates/onionwire-sdk/src/lib.rs index 253d416..92e55fd 100644 --- a/crates/onionwire-sdk/src/lib.rs +++ b/crates/onionwire-sdk/src/lib.rs @@ -22,6 +22,7 @@ use std::path::PathBuf; use std::sync::{Arc, OnceLock}; +use onionwire::Message; use onionwire::node::Node; use onionwire::outbox::{DEFAULT_MAX_ATTEMPTS, OutboxEntry, OutboxStatus}; use onionwire::qr; @@ -102,6 +103,11 @@ pub enum SendState { #[derive(Debug, Clone, uniffi::Record)] pub struct ChatMessage { + /// `messages.id` of the stored row. This is the join key an + /// [`OutboxRow::message_id`] refers to, so a client matches an outbox row to + /// this message by id instead of matching bodies or counting rows. Stable + /// for the life of the row; never `0`. + pub id: i64, /// "in" or "out". pub direction: String, pub body: String, @@ -288,11 +294,7 @@ impl Wire { .inner .outbox_status_of_message(m.id) .map_err(WireError::new)?; - out.push(ChatMessage { - body: String::from_utf8_lossy(&m.plaintext).into_owned(), - delivery: delivery_of(&m.dir, status), - direction: m.dir, - }); + out.push(chat_message(m, status)); } Ok(out) } @@ -368,6 +370,17 @@ fn send_state(status: OutboxStatus) -> SendState { } } +/// One stored chat row as the record a client renders. `id` is copied through +/// untouched: it is the `messages.id` an [`OutboxRow::message_id`] joins to. +fn chat_message(m: Message, status: Option) -> ChatMessage { + ChatMessage { + id: m.id, + direction: m.dir.clone(), + body: String::from_utf8_lossy(&m.plaintext).into_owned(), + delivery: delivery_of(&m.dir, status), + } +} + /// Delivery string for one stored chat row. `None` for incoming rows. An /// outgoing row with no outbox row was accepted on the first attempt, so it is /// reported as `"sent"` rather than left blank. @@ -399,7 +412,11 @@ fn outbox_row(entry: &OutboxEntry) -> OutboxRow { #[cfg(test)] mod tests { + use std::path::PathBuf; + use std::sync::atomic::{AtomicUsize, Ordering}; + use super::*; + use onionwire::Store; use onionwire::outbox::OutboxStatus; #[test] @@ -505,4 +522,67 @@ mod tests { // the core's retry budget cannot drift apart silently. assert_eq!(outbox_max_attempts(), 8); } + + /// An empty dir for a store-backed test. `tempfile` is not a dependency of + /// this crate and a store only needs somewhere private to live. + fn temp_home() -> PathBuf { + static NEXT: AtomicUsize = AtomicUsize::new(0); + let dir = std::env::temp_dir().join(format!( + "onionwire-sdk-join-{}-{}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + )); + let _ = std::fs::remove_dir_all(&dir); + dir + } + + /// The property the Android client joins on: a chat row and the outbox row + /// behind it report the same `messages.id`. Reads both sides out of a real + /// store through the same helpers `Wire::messages` / `Wire::outbox` use, so + /// a client can match on the id instead of counting rows. + #[test] + fn a_chat_row_joins_to_its_outbox_row_by_message_id() { + let home = temp_home(); + let store = Store::open_at_with_passphrase(&home, "join-key-pass").unwrap(); + let friend = [0x11u8; 32]; + store.upsert_friend(&friend, "peer.onion", None).unwrap(); + let row_id = store + .append_message_id(&friend, "out", b"still queued") + .unwrap(); + store + .enqueue_outbox( + &friend, + row_id, + 3, + 1_700_000_045, + "peer onion unreachable", + 1_700_000_000, + ) + .unwrap(); + + // Same two reads `Wire::messages` / `Wire::outbox` do. + let mut messages = Vec::new(); + for m in store.list_messages(&friend).unwrap() { + let status = store.outbox_status_of_message(m.id).unwrap(); + messages.push(chat_message(m, status)); + } + let rows: Vec = store + .pending_outbox(&friend) + .unwrap() + .iter() + .map(outbox_row) + .collect(); + + assert_eq!(messages.len(), 1); + assert_eq!(rows.len(), 1); + // The outbox side really is the stored row (not a defaulted id) … + assert_eq!(rows[0].message_id, row_id); + // … and the id the client gets on the message is that same row id. + assert_eq!(messages[0].id, rows[0].message_id); + // Both sides describe the same row, queued with its attempt count. + assert_eq!(messages[0].delivery.as_deref(), Some("queued")); + assert_eq!(rows[0].attempts, 3); + + std::fs::remove_dir_all(&home).ok(); + } }