feat(api): list the posts of a list with their delivery counters

- GET /api/v1/lists/{id}/posts?limit= returns id, message_id, subject,
  created_at, recipients, sent, failed, dropped and pending, newest
  first; 404 for an unknown list, 400 for a bad limit, token required
- discarding a queued delivery counts it as dropped (store.DropOutbox)
- openapi: the new path and the discard note
This commit is contained in:
tgiachi 2026-11-10 12:40:55 +01:00
commit 2f2ee71f92
2 changed files with 179 additions and 1 deletions

View file

@ -75,6 +75,7 @@ func (s *server) routes() []route {
{"GET /api/v1/queue", s.auth(s.getQueue)},
{"DELETE /api/v1/queue/{id}", s.auth(s.discardQueued)},
{"GET /api/v1/lists/{id}/held", s.auth(s.listHeld)},
{"GET /api/v1/lists/{id}/posts", s.auth(s.listPosts)},
{"POST /api/v1/held/{id}/approve", s.auth(s.approveHeld)},
{"DELETE /api/v1/held/{id}", s.auth(s.discardHeld)},
}
@ -627,7 +628,7 @@ func (s *server) discardQueued(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
if err := s.store.CompleteOutbox(id); err != nil {
if err := s.store.DropOutbox(id); err != nil {
storeError(w, err)
return
}
@ -661,3 +662,45 @@ func (s *server) importArchive(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, http.StatusOK, sum)
}
// Handlers: posts.
type postDTO struct {
ID int64 `json:"id"`
MessageID string `json:"message_id"`
Subject string `json:"subject"`
CreatedAt time.Time `json:"created_at"`
Recipients int `json:"recipients"`
Sent int `json:"sent"`
Failed int `json:"failed"`
Dropped int `json:"dropped"`
Pending int `json:"pending"`
}
// listPosts returns the posts queued to a list with their delivery counters, newest first.
func (s *server) listPosts(w http.ResponseWriter, r *http.Request) {
id, ok := listID(w, r)
if !ok {
return
}
limit, ok := intParam(r, "limit", 0) // 0: the store's default
if !ok {
writeError(w, http.StatusBadRequest, "limit must be a non-negative integer")
return
}
if _, err := s.store.GetList(id); err != nil {
storeError(w, err)
return
}
ps, err := s.store.ListPosts(id, limit)
if err != nil {
storeError(w, err)
return
}
out := make([]postDTO, 0, len(ps))
for _, p := range ps {
out = append(out, postDTO{ID: p.ID, MessageID: p.MessageID, Subject: p.Subject, CreatedAt: p.CreatedAt,
Recipients: p.Recipients, Sent: p.Sent, Failed: p.Failed, Dropped: p.Dropped, Pending: p.Pending})
}
writeJSON(w, http.StatusOK, out)
}

103
internal/api/posts_test.go Normal file
View file

@ -0,0 +1,103 @@
package api
import (
"fmt"
"strings"
"testing"
"time"
"codeberg.org/squidd/parsimony/internal/store"
)
type postJSON struct {
ID int64 `json:"id"`
MessageID string `json:"message_id"`
Subject string `json:"subject"`
CreatedAt time.Time `json:"created_at"`
Recipients int `json:"recipients"`
Sent int `json:"sent"`
Failed int `json:"failed"`
Dropped int `json:"dropped"`
Pending int `json:"pending"`
}
func TestListPosts(t *testing.T) {
h, s := queueServer(t)
now := time.Unix(1_700_000_000, 0)
s.Now = func() time.Time { return now }
l, _ := s.CreateList("dev", "example.org", "Dev")
rcpts := []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}, {From: "b@x.org", Rcpt: "c@x.com"}}
s.EnqueuePost(l.ID, "mid:<1@x>", []byte("Message-ID: <1@x>\r\nSubject: first\r\n\r\nsecret body"), rcpts)
now = now.Add(time.Minute)
s.EnqueuePost(l.ID, "mid:<2@x>", []byte("Message-ID: <2@x>\r\nSubject: second\r\n\r\nsecret body"), rcpts[:1])
due, _ := s.DueOutbox(10)
s.CompleteOutbox(due[0].ID)
s.FailOutbox(due[1].ID, "no")
rec := do(t, h, "GET", fmt.Sprintf("/api/v1/lists/%d/posts", l.ID), "", true)
expect(t, rec, 200)
var ps []postJSON
decode(t, rec, &ps)
if len(ps) != 2 || ps[0].Subject != "second" || ps[1].Subject != "first" {
t.Fatalf("newest first: %+v", ps)
}
p := ps[1]
if p.ID == 0 || p.MessageID != "<1@x>" || !p.CreatedAt.Equal(now.Add(-time.Minute)) || p.Recipients != 3 ||
p.Sent != 1 || p.Failed != 1 || p.Dropped != 0 || p.Pending != 1 {
t.Fatalf("post: %+v", p)
}
if strings.Contains(rec.Body.String(), "secret body") {
t.Fatal("bodies are never listed")
}
decode(t, do(t, h, "GET", fmt.Sprintf("/api/v1/lists/%d/posts?limit=1", l.ID), "", true), &ps)
if len(ps) != 1 || ps[0].Subject != "second" {
t.Fatalf("limit=1: %+v", ps)
}
}
func TestListPostsEmptyIsAnArray(t *testing.T) {
h, s := queueServer(t)
l, _ := s.CreateList("dev", "example.org", "Dev")
rec := do(t, h, "GET", fmt.Sprintf("/api/v1/lists/%d/posts", l.ID), "", true)
expect(t, rec, 200)
if strings.TrimSpace(rec.Body.String()) != "[]" {
t.Fatalf("body %q", rec.Body.String())
}
}
func TestListPostsLimit(t *testing.T) {
h, s := queueServer(t)
l, _ := s.CreateList("dev", "example.org", "Dev")
for i := 0; i < 60; i++ {
s.EnqueuePost(l.ID, fmt.Sprintf("mid:%d", i), []byte("Subject: x\r\n\r\n"), nil)
}
path := fmt.Sprintf("/api/v1/lists/%d/posts", l.ID)
var ps []postJSON
decode(t, do(t, h, "GET", path, "", true), &ps)
if len(ps) != 50 {
t.Fatalf("default 50: %d", len(ps))
}
for _, bad := range []string{"-1", "x", "1.5"} {
expect(t, do(t, h, "GET", path+"?limit="+bad, "", true), 400)
}
}
func TestListPostsUnknownListAndAuth(t *testing.T) {
h, _ := queueServer(t)
expect(t, do(t, h, "GET", "/api/v1/lists/99/posts", "", true), 404)
expect(t, do(t, h, "GET", "/api/v1/lists/abc/posts", "", true), 404)
expect(t, do(t, h, "GET", "/api/v1/lists/99/posts", "", false), 401)
}
func TestDiscardingADeliveryOfAPostCountsAsDropped(t *testing.T) {
h, s := queueServer(t)
l, _ := s.CreateList("dev", "example.org", "Dev")
s.EnqueuePost(l.ID, "mid:<1@x>", []byte("Subject: x\r\n\r\n"), []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}})
due, _ := s.DueOutbox(10)
expect(t, do(t, h, "DELETE", fmt.Sprintf("/api/v1/queue/%d", due[0].ID), "", true), 204)
ps, _ := s.ListPosts(l.ID, 0)
if len(ps) != 1 || ps[0].Dropped != 1 || ps[0].Sent != 0 || ps[0].Pending != 0 {
t.Fatalf("posts: %+v", ps)
}
}