feat(api): import an mbox into a list's archive

- Add POST /api/v1/lists/{id}/archive/import, streaming the body
- Return a summary of imported, duplicate and failed messages; failures do not fail the request
- Import regardless of the archive policy
This commit is contained in:
tgiachi 2026-11-09 17:15:05 +01:00
commit cd596e1810
2 changed files with 96 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import (
"crypto/subtle"
"encoding/json"
"errors"
"fmt"
"mime"
"net/http"
"net/mail"
@ -14,6 +15,7 @@ import (
"strings"
"time"
"codeberg.org/squidd/parsimony/internal/archive"
"codeberg.org/squidd/parsimony/internal/store"
)
@ -46,6 +48,7 @@ func New(s *store.Store, token string, rel Releaser) http.Handler {
mux.Handle("POST /api/v1/lists/{id}/members", srv.auth(srv.addMember))
mux.Handle("DELETE /api/v1/lists/{id}/members/{address}", srv.auth(srv.removeMember))
mux.Handle("GET /api/v1/lists/{id}/archive", srv.auth(srv.listArchive))
mux.Handle("POST /api/v1/lists/{id}/archive/import", srv.auth(srv.importArchive))
mux.Handle("GET /api/v1/archive/{id}", srv.auth(srv.getArchived))
mux.Handle("DELETE /api/v1/archive/{id}", srv.auth(srv.deleteArchived))
mux.Handle("GET /api/v1/queue", srv.auth(srv.getQueue))
@ -543,3 +546,22 @@ func (s *server) discardQueued(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusNoContent)
}
// importArchive reads an mbox from the request body, streaming, and archives it.
// Failed messages are reported in the summary; the request itself succeeds.
func (s *server) importArchive(w http.ResponseWriter, r *http.Request) {
id, ok := listID(w, r)
if !ok {
return
}
if _, err := s.store.GetList(id); err != nil {
storeError(w, err)
return
}
sum, err := archive.ImportMbox(s.store, id, r.Body, time.Now())
if err != nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("import stopped after %d message(s): %v", sum.Imported, err))
return
}
writeJSON(w, http.StatusOK, sum)
}

View file

@ -522,3 +522,77 @@ func TestQueueRequiresAuth(t *testing.T) {
expect(t, do(t, h, "GET", "/api/v1/queue", "", false), 401)
expect(t, do(t, h, "DELETE", "/api/v1/queue/1", "", false), 401)
}
const importMbox = "From alice@example.com Tue Jan 2 15:04:05 2024\nFrom: Alice <alice@example.com>\nSubject: Kickoff\nMessage-ID: <1@x>\nDate: Mon, 01 Jan 2024 10:00:00 +0000\n\nlet's start\n\n" +
"From bob@example.com Tue Jan 2 15:04:05 2024\nFrom: Bob <bob@example.com>\nSubject: Re: Kickoff\nMessage-ID: <2@x>\nIn-Reply-To: <1@x>\nDate: Mon, 01 Jan 2024 11:00:00 +0000\n\nagreed\n"
type importJSON struct {
Imported int `json:"imported"`
Duplicates int `json:"duplicates"`
Failed int `json:"failed"`
Errors []string `json:"errors"`
}
func TestArchiveImport(t *testing.T) {
h, s := queueServer(t)
s.CreateList("dev", "example.org", "Dev")
rec := do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, true)
expect(t, rec, 200)
var sum importJSON
decode(t, rec, &sum)
if sum.Imported != 2 || sum.Duplicates != 0 || sum.Failed != 0 || sum.Errors == nil || len(sum.Errors) != 0 {
t.Fatalf("summary: %+v (errors must be [] not null)", sum)
}
var page archiveJSON
decode(t, do(t, h, "GET", "/api/v1/lists/1/archive", "", true), &page)
if page.Total != 2 || page.Messages[0].Subject != "Re: Kickoff" {
t.Fatalf("archive: %+v", page)
}
if page.Messages[0].ThreadID != page.Messages[1].ThreadID {
t.Fatal("imported replies must be threaded")
}
rec = do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, true)
expect(t, rec, 200)
decode(t, rec, &sum)
if sum.Imported != 0 || sum.Duplicates != 2 {
t.Fatalf("second import: %+v", sum)
}
}
func TestArchiveImportReportsFailuresWithoutFailingTheRequest(t *testing.T) {
h, s := queueServer(t)
s.CreateList("dev", "example.org", "Dev")
body := importMbox + "\nFrom x@y.z Tue Jan 2 15:04:05 2024\n\nno headers\n"
rec := do(t, h, "POST", "/api/v1/lists/1/archive/import", body, true)
expect(t, rec, 200)
var sum importJSON
decode(t, rec, &sum)
if sum.Imported != 2 || sum.Failed != 1 || len(sum.Errors) != 1 || !strings.Contains(sum.Errors[0], "message 3") {
t.Fatalf("summary: %+v", sum)
}
}
func TestArchiveImportEdgeCases(t *testing.T) {
h, s := queueServer(t)
l, _ := s.CreateList("dev", "example.org", "Dev")
s.SetPolicies(l.ID, store.PostMembers, store.JoinOpen, store.ArchiveOff)
expect(t, do(t, h, "POST", "/api/v1/lists/99/archive/import", importMbox, true), 404)
expect(t, do(t, h, "POST", "/api/v1/lists/abc/archive/import", importMbox, true), 404)
expect(t, do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, false), 401)
rec := do(t, h, "POST", "/api/v1/lists/1/archive/import", "", true)
expect(t, rec, 200)
var sum importJSON
decode(t, rec, &sum)
if sum.Imported != 0 || sum.Failed != 0 {
t.Fatalf("empty body: %+v", sum)
}
rec = do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, true)
expect(t, rec, 200)
decode(t, rec, &sum)
if sum.Imported != 2 {
t.Fatalf("an import works even when the archive policy is off: %+v", sum)
}
}