From 735ce02304422cec6f65efd637658a81c247b814 Mon Sep 17 00:00:00 2001 From: Mark Mennell Date: Mon, 9 Nov 2026 19:20:39 +0800 Subject: [PATCH] GET /fmsg: list most recently arrived first, so a fresh add-to comes first The inbox was ordered by message id, so an older message an identity was just added to sat at its original position, far below newer messages, and clients scanning the latest page missed it. Order by when the message arrived for that identity: the later of its send time (as a recipient) and the time_added of any add-to batch that added the identity. Ties fall back to id descending. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- .../handlers/list_order_integration_test.go | 61 +++++++++++++++++++ internal/handlers/messages.go | 24 ++++++-- 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 internal/handlers/list_order_integration_test.go diff --git a/README.md b/README.md index 81d093c..7a211ed 100644 --- a/README.md +++ b/README.md @@ -589,7 +589,7 @@ ws.onmessage = (e) => { ### GET `/fmsg` -Returns messages where the authenticated identity is a recipient (listed in `msg_to` or `msg_add_to`), ordered by message ID descending. +Returns messages where the authenticated identity is a recipient (listed in `msg_to` or `msg_add_to`), most recently arrived first. A message arrives when it is sent to the identity or, later, when an add-to batch adds the identity to it (the batch's `time_added`), whichever is latest; ties are broken by message ID descending. So an older message the identity was just added to is listed first, not at its original position. Only sent messages are listed. A draft is its sender's alone: until it is sent, its recipients get `404` for it on every route (get, data, attachments, thread, diff --git a/internal/handlers/list_order_integration_test.go b/internal/handlers/list_order_integration_test.go new file mode 100644 index 0000000..619c800 --- /dev/null +++ b/internal/handlers/list_order_integration_test.go @@ -0,0 +1,61 @@ +package handlers + +import ( + "encoding/json" + "reflect" + "testing" +) + +// inboxIDs returns the ids in identity's inbox, in listed order. +func (a *finalizationAPI) inboxIDs(t *testing.T, identity, query string) []string { + t.Helper() + w := a.request(identity, "GET", "/fmsg"+query, nil) + if w.Code != 200 { + t.Fatalf("list for %s: %d %s", identity, w.Code, w.Body) + } + var items []struct { + ID json.Number `json:"id"` + } + if err := json.Unmarshal(w.Body.Bytes(), &items); err != nil { + t.Fatal(err) + } + ids := make([]string, len(items)) + for i, it := range items { + ids[i] = it.ID.String() + } + return ids +} + +// An older message an identity has just been added to is listed first: the +// inbox is ordered by when each message arrived for that identity, not by id. +func TestListOrdersByArrival(t *testing.T) { + a := newFinalizationAPI(t) + + old := a.sendAs(t, bob, []string{alice}, "an older message", nil) + ageBy(t, a, mustID(t, old), 3600) + newer := a.sendAs(t, alice, []string{carol}, "a newer message", nil) + ageBy(t, a, mustID(t, newer), 60) + later := a.sendAs(t, bob, []string{alice}, "sent after the older one", nil) + ageBy(t, a, mustID(t, later), 60) + + if got, want := a.inboxIDs(t, carol, ""), []string{newer}; !reflect.DeepEqual(got, want) { + t.Fatalf("carol before add-to: %v, want %v", got, want) + } + + jsonResponse(t, a.request(bob, "POST", "/fmsg/"+old+"/add-to", map[string]any{"add_to": []string{carol}}), 200) + + if got, want := a.inboxIDs(t, carol, ""), []string{old, newer}; !reflect.DeepEqual(got, want) { + t.Errorf("carol after add-to: %v, want the message she was just added to first %v", got, want) + } + if got, want := a.inboxIDs(t, carol, "?limit=1"), []string{old}; !reflect.DeepEqual(got, want) { + t.Errorf("carol first page: %v, want %v", got, want) + } + if got, want := a.inboxIDs(t, carol, "?limit=1&offset=1"), []string{newer}; !reflect.DeepEqual(got, want) { + t.Errorf("carol second page: %v, want %v", got, want) + } + + // Adding someone else doesn't move the message for its other recipients. + if got, want := a.inboxIDs(t, alice, ""), []string{later, old}; !reflect.DeepEqual(got, want) { + t.Errorf("alice: %v, want %v", got, want) + } +} diff --git a/internal/handlers/messages.go b/internal/handlers/messages.go index 6ac264d..e93ff67 100644 --- a/internal/handlers/messages.go +++ b/internal/handlers/messages.go @@ -307,7 +307,10 @@ type messageInput struct { Data string `json:"data"` } -// List handles GET /fmsg — lists messages where the authenticated user is a recipient. +// List handles GET /fmsg — lists messages where the authenticated user is a +// recipient, most recently arrived first. A message arrives for the identity +// when it is sent to them or, later, when they are added to it, so an older +// message someone has just added them to comes first rather than at its id. func (h *MessageHandler) List(c *gin.Context) { addrs, err := h.visibleAddrs(c) if err != nil { @@ -339,13 +342,24 @@ func (h *MessageHandler) List(c *gin.Context) { (SELECT mt.time_read FROM msg_to mt WHERE mt.msg_id = m.id AND lower(mt.addr) = ANY($1)), (SELECT mat.time_read FROM msg_add_to mat WHERE mat.msg_id = m.id AND lower(mat.addr) = ANY($1)) ) AS time_read, t.state - FROM msg m + FROM ( + SELECT r.msg_id, max(r.arrived) AS arrived + FROM ( + SELECT mt.msg_id, m0.time_sent AS arrived + FROM msg_to mt JOIN msg m0 ON m0.id = mt.msg_id + WHERE lower(mt.addr) = ANY($1) + UNION ALL + SELECT mat.msg_id, b.time_added + FROM msg_add_to mat JOIN msg_add_to_batch b ON b.id = mat.batch_id + WHERE lower(mat.addr) = ANY($1) + ) r + GROUP BY r.msg_id + ) inbox + JOIN msg m ON m.id = inbox.msg_id CROSS JOIN LATERAL (SELECT CASE WHEN $4 THEN fmsg_api_trash_state($5, m.id) END AS state) t WHERE m.time_sent IS NOT NULL - AND (EXISTS (SELECT 1 FROM msg_to mt WHERE mt.msg_id = m.id AND lower(mt.addr) = ANY($1)) - OR EXISTS (SELECT 1 FROM msg_add_to mat WHERE mat.msg_id = m.id AND lower(mat.addr) = ANY($1))) AND `+trashFilter+` - ORDER BY m.id DESC + ORDER BY inbox.arrived DESC NULLS LAST, m.id DESC LIMIT $2 OFFSET $3`, addrs, limit, offset, hasTrash, addrs[0], )