319a2fa689
- internal/mail: MailBackend interface (Send/Start/Capabilities) + NullBackend. - internal/core: inbox/message/thread services over pgx; default-pod bootstrap. - IngestRaw: real mox/message parse → body extraction (text/html walk) → quote-stripping (extracted_text) → reference-based threading → store, all in one tx; emits message.received event; populates FTS tsvector. - core implements mail.InboundSink (Deliver) so any backend feeds the same path. - internal/api: v0 routes wired live (was 501) — create/list/get inbox, ingest, list/get messages, list/get thread(+messages), send/reply (503 via NullBackend). - cmd/openmail serve: builds core + NullBackend, ensures default pod. Verified e2e vs Postgres 16: inbox create; ingest original+reply → same thread via In-Reply-To/References; extracted_text strips quoted history; thread detail ordered; FTS matches 'invoice'; events written; send → honest 503. vet clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
245 lines
6.6 KiB
Go
245 lines
6.6 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/karti-ai/openmail/internal/core"
|
|
"github.com/karti-ai/openmail/internal/mail"
|
|
)
|
|
|
|
const maxIngestBytes = 30 << 20 // 30 MiB raw message cap
|
|
|
|
func (s *Server) createInbox(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
var body struct {
|
|
Address string `json:"address"`
|
|
DisplayName *string `json:"display_name"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Address == "" {
|
|
writeErr(w, http.StatusBadRequest, "invalid_request", "address is required")
|
|
return
|
|
}
|
|
ib, err := s.core.CreateInbox(r.Context(), s.podID, body.Address, body.DisplayName)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "create_failed", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, ib)
|
|
}
|
|
|
|
func (s *Server) listInboxes(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
list, err := s.core.ListInboxes(r.Context(), s.podID)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"inboxes": list})
|
|
}
|
|
|
|
func (s *Server) getInbox(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
ib, err := s.core.GetInbox(r.Context(), chi.URLParam(r, "id"))
|
|
if handleErr(w, err) {
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, ib)
|
|
}
|
|
|
|
// ingest accepts a raw RFC 5322 message body and stores it in the inbox — the
|
|
// milestone-1 seed path, and the same path a MailBackend uses for real inbound.
|
|
func (s *Server) ingest(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
inboxID := chi.URLParam(r, "id")
|
|
if _, err := s.core.GetInbox(r.Context(), inboxID); handleErr(w, err) {
|
|
return
|
|
}
|
|
raw, err := io.ReadAll(io.LimitReader(r.Body, maxIngestBytes))
|
|
if err != nil || len(raw) == 0 {
|
|
writeErr(w, http.StatusBadRequest, "invalid_request", "raw message body required")
|
|
return
|
|
}
|
|
msg, err := s.core.IngestRaw(r.Context(), inboxID, raw)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "ingest_failed", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, msg)
|
|
}
|
|
|
|
func (s *Server) listMessages(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
list, err := s.core.ListMessages(r.Context(), chi.URLParam(r, "id"), limit)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"messages": list})
|
|
}
|
|
|
|
func (s *Server) getMessage(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
m, err := s.core.GetMessage(r.Context(), chi.URLParam(r, "msgID"))
|
|
if handleErr(w, err) {
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, m)
|
|
}
|
|
|
|
func (s *Server) listThreads(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
list, err := s.core.ListThreads(r.Context(), chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"threads": list})
|
|
}
|
|
|
|
func (s *Server) getThread(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
threadID := chi.URLParam(r, "threadID")
|
|
t, err := s.core.GetThread(r.Context(), threadID)
|
|
if handleErr(w, err) {
|
|
return
|
|
}
|
|
msgs, err := s.core.GetThreadMessages(r.Context(), threadID)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"thread": t, "messages": msgs})
|
|
}
|
|
|
|
func (s *Server) sendMessage(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
inboxID := chi.URLParam(r, "id")
|
|
ib, err := s.core.GetInbox(r.Context(), inboxID)
|
|
if handleErr(w, err) {
|
|
return
|
|
}
|
|
var body struct {
|
|
To []string `json:"to"`
|
|
Cc []string `json:"cc"`
|
|
Bcc []string `json:"bcc"`
|
|
Subject string `json:"subject"`
|
|
Text string `json:"text"`
|
|
HTML string `json:"html"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || len(body.To) == 0 {
|
|
writeErr(w, http.StatusBadRequest, "invalid_request", "at least one 'to' recipient is required")
|
|
return
|
|
}
|
|
s.dispatch(w, r, &mail.OutgoingMessage{
|
|
InboxID: inboxID, From: ib.Address,
|
|
To: body.To, Cc: body.Cc, Bcc: body.Bcc,
|
|
Subject: body.Subject, Text: body.Text, HTML: body.HTML,
|
|
})
|
|
}
|
|
|
|
func (s *Server) replyMessage(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireCore(w) {
|
|
return
|
|
}
|
|
inboxID := chi.URLParam(r, "id")
|
|
ib, err := s.core.GetInbox(r.Context(), inboxID)
|
|
if handleErr(w, err) {
|
|
return
|
|
}
|
|
orig, err := s.core.GetMessage(r.Context(), chi.URLParam(r, "msgID"))
|
|
if handleErr(w, err) {
|
|
return
|
|
}
|
|
var body struct {
|
|
Text string `json:"text"`
|
|
HTML string `json:"html"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
writeErr(w, http.StatusBadRequest, "invalid_request", "invalid body")
|
|
return
|
|
}
|
|
out := &mail.OutgoingMessage{
|
|
InboxID: inboxID, From: ib.Address,
|
|
Subject: replySubject(orig.Subject),
|
|
Text: body.Text, HTML: body.HTML,
|
|
References: append(append([]string{}, orig.References...), strPtr(orig.MessageIDHdr)...),
|
|
}
|
|
if orig.MessageIDHdr != nil {
|
|
out.InReplyTo = *orig.MessageIDHdr
|
|
}
|
|
if orig.FromAddr != nil {
|
|
out.To = []string{*orig.FromAddr}
|
|
}
|
|
s.dispatch(w, r, out)
|
|
}
|
|
|
|
// dispatch hands an outgoing message to the active backend, translating the
|
|
// NullBackend's ErrNotSupported into a clear 503 (no send path configured yet).
|
|
func (s *Server) dispatch(w http.ResponseWriter, r *http.Request, out *mail.OutgoingMessage) {
|
|
res, err := s.backend.Send(r.Context(), out)
|
|
if errors.Is(err, mail.ErrNotSupported) {
|
|
writeErr(w, http.StatusServiceUnavailable, "send_unavailable",
|
|
"no send-capable mail backend configured (relay/imap_smtp/embedded land in later milestones)")
|
|
return
|
|
}
|
|
if err != nil {
|
|
writeErr(w, http.StatusBadGateway, "send_failed", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusAccepted, res)
|
|
}
|
|
|
|
func handleErr(w http.ResponseWriter, err error) bool {
|
|
switch {
|
|
case err == nil:
|
|
return false
|
|
case errors.Is(err, core.ErrNotFound):
|
|
writeErr(w, http.StatusNotFound, "not_found", "resource not found")
|
|
default:
|
|
writeErr(w, http.StatusInternalServerError, "internal_error", err.Error())
|
|
}
|
|
return true
|
|
}
|
|
|
|
func replySubject(s *string) string {
|
|
if s == nil || *s == "" {
|
|
return "Re:"
|
|
}
|
|
if len(*s) >= 3 && (*s)[:3] == "Re:" {
|
|
return *s
|
|
}
|
|
return "Re: " + *s
|
|
}
|
|
|
|
func strPtr(s *string) []string {
|
|
if s == nil || *s == "" {
|
|
return nil
|
|
}
|
|
return []string{*s}
|
|
}
|