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>
73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
type Inbox struct {
|
|
ID string `json:"id"`
|
|
PodID string `json:"pod_id"`
|
|
Address string `json:"address"`
|
|
DisplayName *string `json:"display_name,omitempty"`
|
|
Metadata json.RawMessage `json:"metadata,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
const inboxCols = `id::text, pod_id::text, address, display_name, metadata, created_at, updated_at`
|
|
|
|
func scanInbox(row pgx.Row) (Inbox, error) {
|
|
var ib Inbox
|
|
err := row.Scan(&ib.ID, &ib.PodID, &ib.Address, &ib.DisplayName, &ib.Metadata, &ib.CreatedAt, &ib.UpdatedAt)
|
|
return ib, err
|
|
}
|
|
|
|
// CreateInbox provisions a new agent-owned address within a pod.
|
|
func (s *Service) CreateInbox(ctx context.Context, podID, address string, displayName *string) (Inbox, error) {
|
|
row := s.pool.QueryRow(ctx,
|
|
`INSERT INTO inboxes (pod_id, address, display_name)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING `+inboxCols,
|
|
podID, address, nullStr(displayName))
|
|
return scanInbox(row)
|
|
}
|
|
|
|
func (s *Service) GetInbox(ctx context.Context, id string) (Inbox, error) {
|
|
ib, err := scanInbox(s.pool.QueryRow(ctx, `SELECT `+inboxCols+` FROM inboxes WHERE id = $1`, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return Inbox{}, ErrNotFound
|
|
}
|
|
return ib, err
|
|
}
|
|
|
|
func (s *Service) GetInboxByAddress(ctx context.Context, address string) (Inbox, error) {
|
|
ib, err := scanInbox(s.pool.QueryRow(ctx, `SELECT `+inboxCols+` FROM inboxes WHERE address = $1`, address))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return Inbox{}, ErrNotFound
|
|
}
|
|
return ib, err
|
|
}
|
|
|
|
func (s *Service) ListInboxes(ctx context.Context, podID string) ([]Inbox, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT `+inboxCols+` FROM inboxes WHERE pod_id = $1 ORDER BY created_at DESC`, podID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []Inbox{}
|
|
for rows.Next() {
|
|
ib, err := scanInbox(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, ib)
|
|
}
|
|
return out, rows.Err()
|
|
}
|