feat(sdk): expose the outbox rows behind a queued chat message
The TUI can say why a queued row is still queued ('queued (try n/8,
retry 45s)') because it reads OutboxEntry::attempts and next_attempt_at
directly. The SDK exposed neither: ChatMessage.delivery is a flat
'queued'/'sending'/'failed: <reason>' string and Wire had no outbox
accessor, so an Android chat row could only invent a countdown.
Adds OutboxRow { message_id, status, attempts, next_attempt_at,
last_error } and Wire::outbox(pubkey_hex) over Node::outbox_pending, plus
outbox_max_attempts() so the client renders 'try n/8' against the core's
own budget instead of a hardcoded 8. Additive: Wire::send() and
ChatMessage.delivery are untouched.
# Conflicts:
# crates/onionwire-sdk/src/lib.rs
This commit is contained in:
parent
c4b824a83d
commit
56bd8845e7
1 changed files with 140 additions and 1 deletions
|
|
@ -30,7 +30,8 @@ use std::sync::{Arc, Mutex, OnceLock};
|
|||
|
||||
use onionwire::hs::TorState as CoreTorState;
|
||||
use onionwire::node::{FlushOutcome as CoreFlushOutcome, Node};
|
||||
use onionwire::outbox::OutboxStatus;use onionwire::qr;
|
||||
use onionwire::outbox::{DEFAULT_MAX_ATTEMPTS, OutboxEntry, OutboxStatus};
|
||||
use onionwire::qr;
|
||||
|
||||
uniffi::setup_scaffolding!();
|
||||
|
||||
|
|
@ -131,6 +132,46 @@ pub struct ChatMessage {
|
|||
pub delivery: Option<String>,
|
||||
}
|
||||
|
||||
/// One row of the local outbox for a friend: a chat send the core has not
|
||||
/// finished delivering.
|
||||
///
|
||||
/// The join key is `message_id` — the `messages.id` a [`ChatMessage`] belongs
|
||||
/// to — so a client matches these against its own list instead of matching
|
||||
/// bodies. Delivered rows are deleted, which is why a message with no row here
|
||||
/// was accepted by the peer and never needed a retry.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
|
||||
pub struct OutboxRow {
|
||||
/// `messages.id` of the chat row this outbox row belongs to.
|
||||
pub message_id: i64,
|
||||
/// `"queued"`, `"sending"` or `"failed"` — the store's own encoding of
|
||||
/// `OutboxStatus`, the same string [`ChatMessage::delivery`] uses.
|
||||
/// `"sent"` does not occur: a delivered row is deleted before this is read.
|
||||
pub status: String,
|
||||
/// Attempts already spent on this row. Compare against
|
||||
/// [`outbox_max_attempts`] to render `try n/8`.
|
||||
pub attempts: u32,
|
||||
/// Absolute unix seconds of the next attempt, so the client renders its own
|
||||
/// countdown from `now` instead of a stale number baked into a record.
|
||||
/// Only meaningful while `status == "queued"`: a `"failed"` row is terminal
|
||||
/// and keeps the last time it was scheduled for, which must not be shown as
|
||||
/// a countdown.
|
||||
pub next_attempt_at: i64,
|
||||
/// Why the last attempt failed, as the core reported it. `None` until the
|
||||
/// row has been attempted once.
|
||||
pub last_error: Option<String>,
|
||||
}
|
||||
|
||||
/// Retry budget after which a queued message turns terminally `"failed"` — the
|
||||
/// denominator in `try n/8`.
|
||||
///
|
||||
/// A constant of the core's outbox, not per-node state, so it is a free
|
||||
/// function: fetch it once and render `attempts` against it. It is exposed at
|
||||
/// all because the client cannot read the Rust constant.
|
||||
#[uniffi::export]
|
||||
pub fn outbox_max_attempts() -> u32 {
|
||||
DEFAULT_MAX_ATTEMPTS
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, uniffi::Record)]
|
||||
pub struct RotateOutcome {
|
||||
pub notified: u32,
|
||||
|
|
@ -398,6 +439,21 @@ impl Wire {
|
|||
Ok(out)
|
||||
}
|
||||
|
||||
/// Outbox rows for one friend that the core has not delivered yet: the
|
||||
/// `"queued"` / `"sending"` / `"failed"` rows behind
|
||||
/// [`ChatMessage::delivery`], with the core's own attempt counter and next
|
||||
/// attempt time attached.
|
||||
///
|
||||
/// This is what lets a chat row say *why* it is still queued — `try 3/8,
|
||||
/// retry 45s` — instead of a flat `"queued"`: [`OutboxRow::attempts`] and
|
||||
/// [`OutboxRow::next_attempt_at`] are read from the store, never guessed by
|
||||
/// the client. An empty result means nothing is outstanding for that friend.
|
||||
pub async fn outbox(&self, pubkey_hex: String) -> WResult<Vec<OutboxRow>> {
|
||||
let pk = unhex(&pubkey_hex)?;
|
||||
let rows = self.node()?.outbox_pending(&pk).map_err(WireError::new)?;
|
||||
Ok(rows.iter().map(outbox_row).collect())
|
||||
}
|
||||
|
||||
/// Delete every message body. Identity and friends survive.
|
||||
pub async fn wipe_messages(&self) -> WResult<()> {
|
||||
self.node()?.wipe_messages().map_err(WireError::new)
|
||||
|
|
@ -475,6 +531,22 @@ fn delivery_of(dir: &str, status: Option<OutboxStatus>) -> Option<String> {
|
|||
})
|
||||
}
|
||||
|
||||
/// One core outbox row as the record a client renders.
|
||||
///
|
||||
/// Field-for-field, so the client shows the store's numbers instead of a
|
||||
/// timeout it invented: the row is keyed by `message_id` (the chat row to
|
||||
/// patch), carries the store's own status string, and next-attempt as an
|
||||
/// absolute unix second for the client to count down from.
|
||||
fn outbox_row(entry: &OutboxEntry) -> OutboxRow {
|
||||
OutboxRow {
|
||||
message_id: entry.message_id,
|
||||
status: entry.status.as_db_str().to_string(),
|
||||
attempts: entry.attempts,
|
||||
next_attempt_at: entry.next_attempt_at,
|
||||
last_error: entry.last_error.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
//! No Tor here on purpose: these exercise the *lifecycle* contract of the
|
||||
|
|
@ -580,5 +652,72 @@ mod tests {
|
|||
Some("failed: no route")
|
||||
);
|
||||
}
|
||||
|
||||
/// One core outbox row with only the fields a record carries filled in
|
||||
/// interestingly. `id`, `friend_pk`, `body` and `created_at` do not cross
|
||||
/// UniFFI, so their values are irrelevant.
|
||||
fn entry(status: OutboxStatus, attempts: u32, last_error: Option<&str>) -> OutboxEntry {
|
||||
OutboxEntry {
|
||||
id: 3,
|
||||
friend_pk: vec![0xaa; 32],
|
||||
message_id: 42,
|
||||
body: b"hello".to_vec(),
|
||||
attempts,
|
||||
next_attempt_at: 1_700_000_045,
|
||||
created_at: 1_700_000_000,
|
||||
status,
|
||||
last_error: last_error.map(str::to_string),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outbox_row_carries_the_join_key_the_attempt_count_and_the_next_attempt_time() {
|
||||
let row = outbox_row(&entry(
|
||||
OutboxStatus::Queued,
|
||||
3,
|
||||
Some("peer onion unreachable"),
|
||||
));
|
||||
assert_eq!(row.message_id, 42);
|
||||
assert_eq!(row.status, "queued");
|
||||
assert_eq!(row.attempts, 3);
|
||||
assert_eq!(row.next_attempt_at, 1_700_000_045);
|
||||
assert_eq!(row.last_error.as_deref(), Some("peer onion unreachable"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outbox_row_keeps_the_stores_own_status_encoding() {
|
||||
// These four strings are the `outbox.status` CHECK constraint in
|
||||
// `src/store.rs`; a client switches on them verbatim.
|
||||
for (status, want) in [
|
||||
(OutboxStatus::Queued, "queued"),
|
||||
(OutboxStatus::Sending, "sending"),
|
||||
(OutboxStatus::Sent, "sent"),
|
||||
(
|
||||
OutboxStatus::Failed("peer onion unreachable".into()),
|
||||
"failed",
|
||||
),
|
||||
] {
|
||||
assert_eq!(outbox_row(&entry(status, 0, None)).status, want);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_outbox_row_carries_the_cores_reason_verbatim() {
|
||||
let row = outbox_row(&entry(
|
||||
OutboxStatus::Failed("peer onion unreachable".into()),
|
||||
8,
|
||||
Some("peer onion unreachable"),
|
||||
));
|
||||
assert_eq!(row.status, "failed");
|
||||
assert_eq!(row.attempts, 8);
|
||||
assert_eq!(row.last_error.as_deref(), Some("peer onion unreachable"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outbox_max_attempts_is_the_denominator_a_client_renders_against() {
|
||||
// The Android chat row renders `try n/8`. Pin the number so the SDK and
|
||||
// the core's retry budget cannot drift apart silently.
|
||||
assert_eq!(outbox_max_attempts(), 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue