feat(sdk): ChatMessage.id — join a chat row to its outbox row
`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.
This commit is contained in:
parent
c6a586c2da
commit
109f64905f
1 changed files with 85 additions and 5 deletions
|
|
@ -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<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.
|
||||
|
|
@ -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<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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue