diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt index 579de57..6b72d75 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt @@ -21,6 +21,7 @@ import uniffi.onionwire_sdk.ChatMessage import uniffi.onionwire_sdk.FriendInfo import uniffi.onionwire_sdk.InviteInfo import uniffi.onionwire_sdk.RotateOutcome +import uniffi.onionwire_sdk.SendState import uniffi.onionwire_sdk.Wire import uniffi.onionwire_sdk.decodeInvite import java.io.File @@ -361,7 +362,15 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { suspend fun messages(pubkeyHex: String): List = requireWire().messages(pubkeyHex) - suspend fun send(pubkeyHex: String, body: String) = + /** + * Hands the body to the node and reports what happened to it. + * + * `SENT` means the peer's onion took the frame during this call. `QUEUED` + * means it did not: the message is in the local outbox, local-only until + * the retry worker gets a circuit through. The caller must not render + * `QUEUED` as delivered. + */ + suspend fun send(pubkeyHex: String, body: String): SendState = requireWire().send(pubkeyHex, body) suspend fun wipeMessages() { diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/ui/ChatScreen.kt b/android/app/src/main/java/com/siriusdevops/onionwire/ui/ChatScreen.kt index fbfa709..9bffca7 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/ui/ChatScreen.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/ui/ChatScreen.kt @@ -37,6 +37,12 @@ import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.unit.dp import kotlinx.coroutines.delay import uniffi.onionwire_sdk.ChatMessage +import uniffi.onionwire_sdk.SendState + +/** Shown after a `Queued` send — the TUI's composer keeps `{n} queued`. */ +private const val QUEUED_HINT = + "Queued: their onion is unreachable. The message is local-only in this " + + "device's outbox until the retry worker gets a circuit through." @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -45,13 +51,14 @@ fun ChatScreen( fingerprint: String, onion: String, loadMessages: suspend () -> List, - onSend: suspend (String) -> Unit, + onSend: suspend (String) -> SendState, onBack: () -> Unit, ) { var messages by remember { mutableStateOf?>(null) } var draft by remember { mutableStateOf("") } var pending by remember { mutableStateOf(null) } var error by remember { mutableStateOf(null) } + var hint by remember { mutableStateOf(null) } val listState = rememberLazyListState() // Poll: the node appends inbound messages in the background; there is no @@ -65,16 +72,32 @@ fun ChatScreen( } } - // Fail-closed send. `send` retries the peer's onion for up to 3 minutes and - // then errors — nothing is spooled, so a failure is a real failure. + // Fail closed — but not fail lost. `send` returns `Queued` when the peer's + // onion is unreachable: the body is in this device's local outbox and the + // retry worker keeps trying. It only surfaces as a hard error when the + // node itself refuses the write. LaunchedEffect(pending) { val body = pending ?: return@LaunchedEffect error = null + hint = null runCatching { onSend(body) } + .onSuccess { state -> + // Never dress a queued send up as delivered. + hint = if (state == SendState.QUEUED) QUEUED_HINT else null + } .onFailure { error = it.message ?: it.toString() } pending = null } + // The hint is a toast, not a log line: the row marker and the `N queued` + // footer are what stay on screen. + LaunchedEffect(hint) { + if (hint != null) { + delay(6_000) + hint = null + } + } + LaunchedEffect(messages?.size) { val n = messages?.size ?: 0 if (n > 0) listState.animateScrollToItem(n - 1) @@ -127,8 +150,13 @@ fun ChatScreen( Text("No messages", style = MaterialTheme.typography.titleSmall) Spacer(Modifier.height(6.dp)) Text( - "Sends fail closed: if their onion is down the message is " + - "not spooled anywhere. There is no outbox in v1.", + "Sends fail closed — nothing goes to a third party. If their " + + "onion is unreachable the message is queued in this " + + "device's outbox instead of being dropped: it stays " + + "local-only until the retry worker gets a circuit " + + "through (8 attempts, backoff capped at 30 min), then it " + + "lands. \u2713 means delivered, \u25f7 queued, " + + "\u2717 failed.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) @@ -149,25 +177,77 @@ fun ChatScreen( horizontalArrangement = if (mine) Arrangement.End else Arrangement.Start, ) { - Text( - m.body, - style = MaterialTheme.typography.bodyMedium, - color = if (mine) MaterialTheme.colorScheme.onPrimary - else MaterialTheme.colorScheme.onSurface, - modifier = Modifier - .background( - if (mine) MaterialTheme.colorScheme.primary - else MaterialTheme.colorScheme.surfaceVariant, - RoundedCornerShape(10.dp), - ) - .padding(horizontal = 10.dp, vertical = 8.dp), - ) + Column( + horizontalAlignment = + if (mine) Alignment.End else Alignment.Start, + ) { + Text( + m.body, + style = MaterialTheme.typography.bodyMedium, + color = if (mine) MaterialTheme.colorScheme.onPrimary + else MaterialTheme.colorScheme.onSurface, + modifier = Modifier + .background( + if (mine) MaterialTheme.colorScheme.primary + else MaterialTheme.colorScheme.surfaceVariant, + RoundedCornerShape(10.dp), + ) + .padding(horizontal = 10.dp, vertical = 8.dp), + ) + // Outgoing rows only: the SDK reports `null` for + // inbound, and an outgoing row with no outbox + // entry reads "sent". + if (mine) { + deliveryMarker(m.delivery)?.let { marker -> + Text( + marker.text, + style = MaterialTheme.typography.labelSmall, + fontFamily = FontFamily.Monospace, + color = when (marker.tone) { + DeliveryTone.SENT -> + MaterialTheme.colorScheme.onSurfaceVariant + + DeliveryTone.PENDING -> + MaterialTheme.colorScheme.tertiary + + DeliveryTone.FAILED -> + MaterialTheme.colorScheme.error + }, + modifier = Modifier.padding( + top = 2.dp, + end = 2.dp, + ), + ) + } + } + } } } } } } + // TUI footer: `{n} queued` while the outbox still owns rows. + val queued = list?.count { it.direction == "out" && isOutboxPending(it.delivery) } ?: 0 + if (queued > 0) { + Text( + "$queued queued", + style = MaterialTheme.typography.labelSmall, + fontFamily = FontFamily.Monospace, + color = MaterialTheme.colorScheme.tertiary, + modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp), + ) + } + + hint?.let { + Text( + it, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.tertiary, + modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp), + ) + } + error?.let { Text( it, diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/ui/Delivery.kt b/android/app/src/main/java/com/siriusdevops/onionwire/ui/Delivery.kt new file mode 100644 index 0000000..a26766d --- /dev/null +++ b/android/app/src/main/java/com/siriusdevops/onionwire/ui/Delivery.kt @@ -0,0 +1,78 @@ +package com.siriusdevops.onionwire.ui + +/** + * Per-row delivery markers for outgoing chat messages. + * + * The vocabulary is the TUI's (`docs/TUI.md` → "Delivery state (outbox)"), so + * both clients read the same: + * + * ``` + * ✓ delivered — the peer's onion took the frame + * ◷ queued in this device's local outbox, waiting for a circuit + * » sending the retry worker has it in flight + * ✗ failed: … terminally failed (8 attempts, backoff capped at 30 min) + * ``` + * + * The string comes from `ChatMessage.delivery` and is produced by the store's + * own outbox status — `"sent"` / `"queued"` / `"sending"` / + * `"failed: "` for outgoing rows, `null` for incoming ones. An outgoing + * row with no outbox row was accepted on the first attempt and reads `"sent"`. + * + * Nothing here guesses: a delivery string this build does not recognise renders + * no marker rather than a tick, because a missing marker never claims delivery + * while a wrong tick does. + */ + +/** Which colour the marker is drawn in. */ +internal enum class DeliveryTone { + /** Delivered to the peer's onion. */ + SENT, + + /** Still local-only: queued, or being retried right now. */ + PENDING, + + /** Out of attempts — the message never left this device. */ + FAILED, +} + +internal data class DeliveryMarker(val text: String, val tone: DeliveryTone) + +/** + * Longest `failed: ` tail we render before eliding. The TUI caps the + * same reason at 24 cells for its 80-column row; a phone bubble wraps, so this + * only exists to stop a pathological Tor error from filling the screen. + */ +private const val MAX_REASON_CHARS = 48 + +/** Marker for one outgoing row, or `null` when there is nothing to say. */ +internal fun deliveryMarker(delivery: String?): DeliveryMarker? { + val state = delivery?.trim().orEmpty() + if (state.isEmpty()) return null + return when { + state == "sent" -> DeliveryMarker("\u2713", DeliveryTone.SENT) + state == "queued" -> DeliveryMarker("\u25f7 queued", DeliveryTone.PENDING) + state == "sending" -> DeliveryMarker("\u00bb sending", DeliveryTone.PENDING) + state.startsWith("failed") -> { + val reason = shortReason(state.substringAfter(':', "").trim()) + DeliveryMarker( + if (reason.isEmpty()) "\u2717 failed" else "\u2717 failed: $reason", + DeliveryTone.FAILED, + ) + } + // Unknown state: never render a tick for something we cannot read. + else -> null + } +} + +/** True while the outbox still owns the row — the TUI's `{n} queued` footer. */ +internal fun isOutboxPending(delivery: String?): Boolean { + val state = delivery?.trim().orEmpty() + return state == "queued" || state == "sending" +} + +private fun shortReason(reason: String): String = + if (reason.length <= MAX_REASON_CHARS) { + reason + } else { + reason.take(MAX_REASON_CHARS - 1).trimEnd() + "\u2026" + } diff --git a/android/app/src/test/java/com/siriusdevops/onionwire/ui/DeliveryMarkerTest.kt b/android/app/src/test/java/com/siriusdevops/onionwire/ui/DeliveryMarkerTest.kt new file mode 100644 index 0000000..6340190 --- /dev/null +++ b/android/app/src/test/java/com/siriusdevops/onionwire/ui/DeliveryMarkerTest.kt @@ -0,0 +1,85 @@ +package com.siriusdevops.onionwire.ui + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * The delivery vocabulary is shared with the TUI (`docs/TUI.md` → "Delivery + * state (outbox)"). These strings are the SDK's contract + * (`crates/onionwire-sdk/src/lib.rs`), so a drift here is a client that lies + * about a message having landed. + */ +class DeliveryMarkerTest { + + @Test + fun delivered_row_gets_a_tick() { + val m = deliveryMarker("sent") + assertEquals(DeliveryMarker("\u2713", DeliveryTone.SENT), m) + } + + @Test + fun queued_row_says_queued_not_delivered() { + val m = deliveryMarker("queued") + assertEquals(DeliveryMarker("\u25f7 queued", DeliveryTone.PENDING), m) + } + + @Test + fun sending_row_is_pending() { + val m = deliveryMarker("sending") + assertEquals(DeliveryMarker("\u00bb sending", DeliveryTone.PENDING), m) + } + + @Test + fun failed_row_carries_the_reason() { + val m = deliveryMarker("failed: connection timed out") + assertEquals(DeliveryMarker("\u2717 failed: connection timed out", DeliveryTone.FAILED), m) + } + + @Test + fun failed_row_without_a_reason_still_reads_failed() { + assertEquals( + DeliveryMarker("\u2717 failed", DeliveryTone.FAILED), + deliveryMarker("failed"), + ) + } + + @Test + fun a_long_reason_is_elided() { + val m = deliveryMarker("failed: " + "x".repeat(200)) + val text = m?.text.orEmpty() + assertTrue(text.startsWith("\u2717 failed: xxx")) + assertTrue(text.endsWith("\u2026")) + assertTrue("marker grew to ${text.length} chars", text.length <= 48 + "\u2717 failed: ".length) + } + + @Test + fun incoming_row_has_no_marker() { + assertNull(deliveryMarker(null)) + assertNull(deliveryMarker("")) + assertNull(deliveryMarker(" ")) + } + + @Test + fun unknown_state_never_claims_delivery() { + // Fail-safe: an unreadable state renders nothing rather than a tick. + assertNull(deliveryMarker("scheduled")) + assertNull(deliveryMarker("SENT")) + } + + @Test + fun surrounding_whitespace_is_tolerated() { + assertEquals(DeliveryTone.SENT, deliveryMarker(" sent ")?.tone) + } + + @Test + fun pending_counts_only_queued_and_sending() { + assertTrue(isOutboxPending("queued")) + assertTrue(isOutboxPending("sending")) + assertFalse(isOutboxPending("sent")) + assertFalse(isOutboxPending("failed: no route")) + assertFalse(isOutboxPending(null)) + } +}