diff --git a/docs/openapi.yaml b/docs/openapi.yaml index ad0b348..a966df3 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -287,6 +287,7 @@ paths: - {name: id, in: path, required: true, schema: {type: integer}} delete: summary: Discard a queued delivery + description: A waiting delivery of a post counts as dropped in the post's counters; a failed one stays counted as failed. responses: '204': {description: Discarded} '404': {$ref: '#/components/responses/Error'} @@ -319,3 +320,34 @@ paths: errors: {type: array, items: {type: string}, description: The first 20 failures} '400': {$ref: '#/components/responses/Error'} '404': {$ref: '#/components/responses/Error'} + /lists/{id}/posts: + parameters: + - {name: id, in: path, required: true, schema: {type: integer}} + get: + summary: Posts queued to the list with their delivery counters, newest first + description: > + One entry per post queued to the list (delivered or approved), kept for 400 days. Confirmation mail is not a post. + Bodies are never read. + parameters: + - {name: limit, in: query, schema: {type: integer, default: 50, maximum: 200}, description: 0 means the default; more than 200 gives 200} + responses: + '200': + description: OK + content: + application/json: + schema: + type: array + items: + type: object + properties: + id: {type: integer} + message_id: {type: string, description: The Message-ID header, at most 200 characters; empty when there was none} + subject: {type: string, description: Decoded, at most 200 characters, control characters removed} + created_at: {type: string, format: date-time} + recipients: {type: integer, description: Deliveries queued (members at the time of the post)} + sent: {type: integer, description: Accepted by the relay} + failed: {type: integer, description: Given up for good (refused by the relay, or still failing after 3 days)} + dropped: {type: integer, description: The mailbox does not exist, or the delivery was discarded} + pending: {type: integer, description: "Still in the queue: recipients - sent - failed - dropped"} + '400': {$ref: '#/components/responses/Error'} + '404': {$ref: '#/components/responses/Error'} diff --git a/internal/api/api.go b/internal/api/api.go index eb03fd7..0a36925 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -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) +} diff --git a/internal/api/posts_test.go b/internal/api/posts_test.go new file mode 100644 index 0000000..1e1b9dc --- /dev/null +++ b/internal/api/posts_test.go @@ -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) + } +}