feat(tui): join outbox rows to chat rows by message id
All checks were successful
ci / test (pull_request) Successful in 3m27s

`chat_with_delivery` matched pending outbox rows to chat rows by body
because `store::Message` carried no id, so an older send that was still
queued showed a delivered mark whenever a newer send of the same text had
already crossed the circuit.

`store::Message` now carries its `messages.id`, and the TUI joins
`OutboxEntry::message_id` == `Message::id`. The body match and its
newest-first fallback are gone.

`list_messages()` keeps its signature; callers that do not need the id
are untouched.
This commit is contained in:
rust-dev 2026-09-12 14:34:37 -04:00
commit afff5f8dff
No known key found for this signature in database
4 changed files with 89 additions and 41 deletions

View file

@ -80,6 +80,9 @@ 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<u8>,
}
@ -851,7 +854,11 @@ 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 { dir, plaintext });
out.push(Message {
id,
dir,
plaintext,
});
}
Ok(out)
}

View file

@ -242,9 +242,10 @@ impl OutboxSummary {
/// Chat transcript with per-message delivery state, plus the footer summary.
///
/// 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.
/// 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.
pub fn chat_with_delivery(
msgs: &[Message],
pending: &[OutboxEntry],
@ -270,21 +271,18 @@ pub fn chat_with_delivery(
);
}
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);
}
// 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<i64, &OutboxEntry> =
pending.iter().map(|entry| (entry.message_id, entry)).collect();
let mut tags: Vec<Option<String>> = vec![None; msgs.len()];
for (i, msg) in msgs.iter().enumerate().rev() {
for (i, msg) in msgs.iter().enumerate() {
if msg.dir != "out" {
continue;
}
if let Some(rows) = by_body.get_mut(msg.plaintext.as_slice())
&& let Some(entry) = rows.pop()
{
if let Some(entry) = by_id.get(&msg.id) {
tags[i] = Some(delivery_tag(
&entry.status,
entry.attempts,