diff --git a/docs/TUI.md b/docs/TUI.md index e1655d9..ff571c2 100644 --- a/docs/TUI.md +++ b/docs/TUI.md @@ -607,10 +607,11 @@ prefix, no server. - The tag trails the body and wraps with it, so it is never clipped away. Correlation: a delivered row is deleted from the outbox, so an outbound message -with no row is `Sent`; pending rows are joined to messages on the row id — -`store::Message::id` equals `OutboxEntry::message_id` (`chat_with_delivery`). -Two identical bodies are distinct rows with distinct ids, so the marker always -lands on the row that actually owns the outbox entry. +with no row is `Sent`; pending rows are matched to messages by body, newest send +first (`chat_with_delivery`). The outbox holds no message id the UI can join on, +so two identical bodies where only the newer one is still queued attach the +marker to the newer send. An id-bearing accessor would remove that last +ambiguity; see the follow-up card noted in the PR. ### Size floor / target diff --git a/src/store.rs b/src/store.rs index c9f1c19..726e169 100644 --- a/src/store.rs +++ b/src/store.rs @@ -83,9 +83,6 @@ pub struct FriendProfile { #[derive(Debug)] pub struct Message { - /// `messages.id` row id. The outbox row for a send carries the same - /// `message_id`, so the UI can join exactly instead of matching bodies. - pub id: i64, pub dir: String, pub plaintext: Vec, } @@ -861,11 +858,7 @@ impl Store { let aad = message_aad(friend_id, &dir, id); let plaintext = crate::backup::aead_decrypt(&self.msg_key, &blob, &aad) .map_err(|_| Error("message decrypt failed".into()))?; - out.push(Message { - id, - dir, - plaintext, - }); + out.push(Message { dir, plaintext }); } Ok(out) } diff --git a/src/tui.rs b/src/tui.rs index 7ffd153..6e580bb 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -243,10 +243,9 @@ impl OutboxSummary { /// Chat transcript with per-message delivery state, plus the footer summary. /// -/// Pending rows are joined to messages on `Message::id` / -/// `OutboxEntry::message_id` — never on the body. Two identical bodies are -/// distinct rows with distinct ids, so the marker always lands on the row that -/// actually owns the outbox entry. +/// Pending rows are matched to messages by body, newest send first: an +/// outstanding retry is the most recent send of that text, and the outbox holds +/// no message id the UI could join on. pub fn chat_with_delivery( msgs: &[Message], pending: &[OutboxEntry], @@ -272,18 +271,21 @@ pub fn chat_with_delivery( ); } - // One row per message in practice (a retry updates its row, not inserts a - // second), and `pending` is ordered by row id, so a hypothetical duplicate - // resolves to the newest — the same rule the store's own - // `outbox_status_of_message` uses. - let by_id: HashMap = - pending.iter().map(|entry| (entry.message_id, entry)).collect(); + let mut by_body: HashMap<&[u8], Vec<&OutboxEntry>> = HashMap::new(); + for entry in pending { + by_body + .entry(entry.body.as_slice()) + .or_default() + .push(entry); + } let mut tags: Vec> = vec![None; msgs.len()]; - for (i, msg) in msgs.iter().enumerate() { + for (i, msg) in msgs.iter().enumerate().rev() { if msg.dir != "out" { continue; } - if let Some(entry) = by_id.get(&msg.id) { + if let Some(rows) = by_body.get_mut(msg.plaintext.as_slice()) + && let Some(entry) = rows.pop() + { tags[i] = Some(delivery_tag( &entry.status, entry.attempts, diff --git a/tests/tui_spec.rs b/tests/tui_spec.rs index a04517d..5715085 100644 --- a/tests/tui_spec.rs +++ b/tests/tui_spec.rs @@ -388,24 +388,21 @@ fn ascii_switch_80x24_frame_is_pure_ascii() { // prefix, no parallel state model. // --------------------------------------------------------------------------- -fn out_msg(id: i64, body: &str) -> Message { +fn out_msg(body: &str) -> Message { Message { - id, dir: "out".into(), plaintext: body.as_bytes().to_vec(), } } -fn in_msg(id: i64, body: &str) -> Message { +fn in_msg(body: &str) -> Message { Message { - id, dir: "in".into(), plaintext: body.as_bytes().to_vec(), } } fn pending_row( - message_id: i64, body: &str, status: OutboxStatus, attempts: u32, @@ -415,7 +412,7 @@ fn pending_row( OutboxEntry { id: 1, friend_pk: vec![9u8; 32], - message_id, + message_id: 1, body: body.as_bytes().to_vec(), attempts, next_attempt_at: now + next_in, @@ -425,23 +422,16 @@ fn pending_row( } } -fn queued_row(message_id: i64, body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry { - pending_row(message_id, body, OutboxStatus::Queued, attempts, next_in, now) +fn queued_row(body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry { + pending_row(body, OutboxStatus::Queued, attempts, next_in, now) } -fn sending_row(message_id: i64, body: &str, now: i64) -> OutboxEntry { - pending_row(message_id, body, OutboxStatus::Sending, 1, 0, now) +fn sending_row(body: &str, now: i64) -> OutboxEntry { + pending_row(body, OutboxStatus::Sending, 1, 0, now) } -fn failed_row(message_id: i64, body: &str, why: &str, now: i64) -> OutboxEntry { - pending_row( - message_id, - body, - OutboxStatus::Failed(why.into()), - 8, - 0, - now, - ) +fn failed_row(body: &str, why: &str, now: i64) -> OutboxEntry { + pending_row(body, OutboxStatus::Failed(why.into()), 8, 0, now) } /// Marker is everything after the two-space separator the render helper uses. @@ -463,8 +453,8 @@ fn queued_and_delivered_messages_render_different_markers() { force_unicode_mode(); let _guard = guard; let now = 1_700_000_000; - let msgs = vec![out_msg(1, "ping"), out_msg(2, "pong")]; - let pending = vec![queued_row(2, "pong", 2, 45, now)]; + let msgs = vec![out_msg("ping"), out_msg("pong")]; + let pending = vec![queued_row("pong", 2, 45, now)]; let (lines, summary) = chat_with_delivery(&msgs, &pending, now); @@ -499,40 +489,6 @@ fn queued_and_delivered_messages_render_different_markers() { assert_eq!(summary.failed, 0); } -/// The case the body match got wrong: the same text sent twice, only the -/// *older* send still queued. Joined by body, the (single) pending row lands on -/// the newest send and the actually-queued row shows `✓` — which is exactly -/// false delivery. The join is on `Message::id` / `OutboxEntry::message_id`. -#[test] -fn identical_bodies_join_by_message_id_not_body() { - let guard = ASCII_ENV.lock().expect("ascii env lock"); - force_unicode_mode(); - let _guard = guard; - let now = 1_700_000_000; - let msgs = vec![out_msg(7, "same text"), out_msg(8, "same text")]; - let pending = vec![queued_row(7, "same text", 1, 30, now)]; - - let (lines, summary) = chat_with_delivery(&msgs, &pending, now); - - assert!( - lines[0].contains('◷') && lines[0].contains("queued"), - "queued marker must land on the queued row (id 7): {:?}", - lines[0] - ); - assert!( - !lines[0].contains('✓'), - "queued row must not claim delivery: {:?}", - lines[0] - ); - assert_eq!( - marker_of(&lines[1]), - "✓", - "delivered row (id 8) must keep its mark: {:?}", - lines[1] - ); - assert_eq!(summary.note().as_deref(), Some("1 queued")); -} - #[test] fn delivery_markers_have_exercised_ascii_fallbacks() { let _guard = ASCII_ENV.lock().expect("ascii env lock"); @@ -542,16 +498,16 @@ fn delivery_markers_have_exercised_ascii_fallbacks() { } let now = 500; let msgs = vec![ - out_msg(1, "alpha"), - out_msg(2, "beta"), - out_msg(3, "gamma"), - out_msg(4, "delta"), - in_msg(5, "epsilon"), + out_msg("alpha"), + out_msg("beta"), + out_msg("gamma"), + out_msg("delta"), + in_msg("epsilon"), ]; let pending = vec![ - failed_row(1, "alpha", "connect peer.onion:80: connection timed out", now), - queued_row(2, "beta", 1, 30, now), - sending_row(3, "gamma", now), + failed_row("alpha", "connect peer.onion:80: connection timed out", now), + queued_row("beta", 1, 30, now), + sending_row("gamma", now), ]; let (lines, summary) = chat_with_delivery(&msgs, &pending, now); @@ -673,8 +629,8 @@ fn chat_and_footer_rows_fit_80x24_and_wrap_instead_of_clipping() { let _guard = guard; let now = 1_700_000_000; let long = "this is a very long chat line that should wrap inside the chat pane instead of running off the right edge of an 80-column terminal and colliding with the roster border"; - let msgs = vec![out_msg(1, "ok"), in_msg(2, "pong"), out_msg(3, long)]; - let pending = vec![queued_row(3, long, 3, 45, now)]; + let msgs = vec![out_msg("ok"), in_msg("pong"), out_msg(long)]; + let pending = vec![queued_row(long, 3, 45, now)]; let (lines, summary) = chat_with_delivery(&msgs, &pending, now); for line in lines.iter().take(2) {