feat(sdk): ChatMessage.id — join a chat row to its outbox row
All checks were successful
ci / test (pull_request) Successful in 3m58s
ci / sdk (pull_request) Successful in 1m3s

`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.

# Conflicts:
#	crates/onionwire-sdk/src/lib.rs
This commit is contained in:
rust-dev 2026-09-12 16:38:47 -04:00 • committed by agent
commit 938232d690
No known key found for this signature in database

View file

@ -28,6 +28,7 @@
use std::path::PathBuf;
use std::sync::{Arc, Mutex, OnceLock};
use onionwire::Message;
use onionwire::hs::TorState as CoreTorState;
use onionwire::node::{FlushOutcome as CoreFlushOutcome, Node};
use onionwire::outbox::{DEFAULT_MAX_ATTEMPTS, OutboxEntry, OutboxStatus};
@ -121,6 +122,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,
@ -246,7 +252,10 @@ impl Wire {
let guard = self.node.lock().map_err(|_| WireError::Stopped {
message: "node handle lock poisoned by a panic; reopen the identity".to_string(),
})?;
guard.as_ref().cloned().ok_or_else(|| WireError::stopped("this call"))
guard
.as_ref()
.cloned()
.ok_or_else(|| WireError::stopped("this call"))
}
}
@ -417,7 +426,8 @@ impl Wire {
.send(&pk, body.as_bytes())
.await
.map_err(WireError::new)?;
Ok(send_state(status)) }
Ok(send_state(status))
}
/// Chat history for a friend, oldest first. Bodies are decrypted here;
/// they are stored encrypted at rest. Outgoing rows carry their delivery
@ -431,10 +441,7 @@ impl Wire {
.node()?
.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)
}
@ -518,6 +525,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<OutboxStatus>) -> 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.
@ -552,9 +570,11 @@ mod tests {
//! No Tor here on purpose: these exercise the *lifecycle* contract of the
//! handle, which is what a shut-down node must guarantee. A `Wire` with no
//! node is exactly the state `shutdown()` leaves behind.
use super::*;
use onionwire::Store;
use onionwire::outbox::OutboxStatus;
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
fn stopped_wire() -> Wire {
Wire {
@ -616,7 +636,7 @@ mod tests {
assert_eq!(TorState::from(CoreTorState::Stopped), TorState::Stopped);
}
#[test]
#[test]
fn sent_is_sent_everything_else_is_queued() {
assert_eq!(send_state(OutboxStatus::Sent), SendState::Sent);
assert_eq!(send_state(OutboxStatus::Queued), SendState::Queued);
@ -719,5 +739,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<OutboxRow> = 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();
}
}