Files
warmbly/internal/repository/http_email_message_map.go
T

255 lines
8.3 KiB
Go

package repository
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/pkg/nodeauth"
)
// httpEmailMessageMapRepository is the worker-side adapter. Workers cannot
// connect to Postgres directly (per CLAUDE.md), so they reach the message map
// over HTTPS by calling the backend's internal endpoint, mirroring the DEK HTTP
// store (internal/infrastructure/encryptedkeys/http.go).
//
// PUT {BaseURL}/api/v1/internal/email-message-map body: emailMessageMapPayload -> 204
// GET {BaseURL}/api/v1/internal/email-message-map?user_id=&email_id=&message_id=
// 200 emailMessageMapPayload | 404 (no mapping -> nil)
// DELETE {BaseURL}/api/v1/internal/email-message-map?user_id=&email_id=&message_id=&id= -> 204
//
// Auth: Authorization: Bearer <config.NodeAPIToken()>.
type httpEmailMessageMapRepository struct {
baseURL string
token string
client *http.Client
batchUnsupportedUntil atomic.Int64
}
// NewHTTPEmailMessageMapRepository returns the worker-side implementation.
func NewHTTPEmailMessageMapRepository(baseURL, token string) (EmailMessageMapRepository, error) {
if baseURL == "" {
return nil, errors.New("email_message_map.http: baseURL is required")
}
if token == "" {
return nil, errors.New("email_message_map.http: token is required")
}
return &httpEmailMessageMapRepository{
baseURL: strings.TrimRight(baseURL, "/"),
token: token,
client: newControlPlaneClient(10 * time.Second),
}, nil
}
// emailMessageMapPayload is the wire shape shared with the backend handler. It
// is field-compatible with EmailMessageData (same field names/types/order), so
// the two convert directly.
type emailMessageMapPayload struct {
UserID string `json:"user_id"`
EmailID string `json:"email_id"`
MessageID string `json:"message_id"`
ID string `json:"id"`
ThreadID string `json:"thread_id"`
}
func (r *httpEmailMessageMapRepository) endpoint() string {
return r.baseURL + "/api/v1/internal/email-message-map"
}
func (r *httpEmailMessageMapRepository) authed(req *http.Request) {
nodeauth.Authorize(req, r.token)
req.Header.Set("User-Agent", "warmbly-worker/email-message-map-http")
}
func (r *httpEmailMessageMapRepository) Add(ctx context.Context, data EmailMessageData) error {
body, err := json.Marshal(emailMessageMapPayload(data))
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, r.endpoint(), bytes.NewReader(body))
if err != nil {
return err
}
r.authed(req)
req.Header.Set("Content-Type", "application/json")
resp, err := r.client.Do(req)
if err != nil {
return fmt.Errorf("email_message_map.http: add: %w", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusNoContent, http.StatusOK, http.StatusCreated:
return nil
default:
return &ControlPlaneHTTPError{Status: resp.StatusCode}
}
}
func (r *httpEmailMessageMapRepository) AdmitArrival(ctx context.Context, data EmailMessageData, pending *PendingArrival) error {
body, err := json.Marshal(struct {
Map emailMessageMapPayload `json:"map"`
Pending *PendingArrival `json:"pending"`
}{emailMessageMapPayload(data), pending})
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, r.endpoint()+"/arrival", bytes.NewReader(body))
if err != nil {
return err
}
r.authed(req)
req.Header.Set("Content-Type", "application/json")
resp, err := r.client.Do(req)
if err != nil {
return fmt.Errorf("arrival admission: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusMethodNotAllowed {
return ErrArrivalOutboxUnsupported
}
if resp.StatusCode == http.StatusServiceUnavailable && resp.Header.Get("X-Warmbly-Arrival-Durable") == "" &&
resp.Request != nil && resp.Request.URL.String() == req.URL.String() && resp.Request.Method == req.Method {
markers := resp.Header.Values("X-Warmbly-Arrival-Ownership-Lost")
if mailbox, parseErr := uuid.Parse(data.EmailID); parseErr == nil && mailbox != uuid.Nil && len(markers) == 1 && markers[0] == mailbox.String() {
return ErrArrivalMailboxOwnershipLost
}
}
if resp.StatusCode != http.StatusNoContent || resp.Header.Get("X-Warmbly-Arrival-Durable") != "1" {
return &ControlPlaneHTTPError{Status: resp.StatusCode}
}
return nil
}
func (r *httpEmailMessageMapRepository) Get(ctx context.Context, userID, emailID uuid.UUID, messageID string) (*EmailMessageData, error) {
q := url.Values{}
q.Set("user_id", userID.String())
q.Set("email_id", emailID.String())
q.Set("message_id", messageID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.endpoint()+"?"+q.Encode(), nil)
if err != nil {
return nil, err
}
r.authed(req)
resp, err := r.client.Do(req)
if err != nil {
return nil, fmt.Errorf("email_message_map.http: get: %w", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusNotFound:
return nil, nil
case http.StatusOK:
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var p emailMessageMapPayload
if err := json.Unmarshal(b, &p); err != nil {
return nil, fmt.Errorf("email_message_map.http: decode: %w", err)
}
d := EmailMessageData(p)
return &d, nil
default:
return nil, &ControlPlaneHTTPError{Status: resp.StatusCode}
}
}
func (r *httpEmailMessageMapRepository) GetMany(ctx context.Context, userID, emailID uuid.UUID, messageIDs []string) (map[string]*EmailMessageData, error) {
if len(messageIDs) > EmailMessageMapBatchSize {
return nil, fmt.Errorf("email_message_map.http: batch exceeds %d messages", EmailMessageMapBatchSize)
}
if time.Now().Unix() < r.batchUnsupportedUntil.Load() {
return nil, ErrMessageMapBatchUnsupported
}
body, err := json.Marshal(struct {
UserID string `json:"user_id"`
EmailID string `json:"email_id"`
MessageIDs []string `json:"message_ids"`
}{userID.String(), emailID.String(), messageIDs})
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, r.endpoint()+"/lookup", bytes.NewReader(body))
if err != nil {
return nil, err
}
r.authed(req)
req.Header.Set("Content-Type", "application/json")
resp, err := r.client.Do(req)
if err != nil {
return nil, fmt.Errorf("email_message_map.http: get many: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusMethodNotAllowed {
r.batchUnsupportedUntil.Store(time.Now().Add(10 * time.Minute).Unix())
return nil, ErrMessageMapBatchUnsupported
}
if resp.StatusCode != http.StatusOK {
return nil, &ControlPlaneHTTPError{Status: resp.StatusCode}
}
var payload struct {
Mappings []emailMessageMapPayload `json:"mappings"`
}
if err := json.NewDecoder(io.LimitReader(resp.Body, 2<<20)).Decode(&payload); err != nil {
return nil, fmt.Errorf("email_message_map.http: decode batch: %w", err)
}
if payload.Mappings == nil {
return nil, errors.New("email_message_map.http: missing batch mappings")
}
wanted := make(map[string]bool, len(messageIDs))
for _, id := range messageIDs {
wanted[id] = true
}
result := make(map[string]*EmailMessageData, len(payload.Mappings))
for _, p := range payload.Mappings {
id, err := uuid.Parse(p.ID)
if err != nil || id == uuid.Nil || p.UserID != userID.String() || p.EmailID != emailID.String() || !wanted[p.MessageID] || result[p.MessageID] != nil {
return nil, errors.New("email_message_map.http: invalid batch mapping")
}
mapping := EmailMessageData(p)
result[p.MessageID] = &mapping
}
return result, nil
}
func (r *httpEmailMessageMapRepository) Del(ctx context.Context, userID, emailID uuid.UUID, messageID string, id uuid.UUID) error {
q := url.Values{}
q.Set("user_id", userID.String())
q.Set("email_id", emailID.String())
q.Set("message_id", messageID)
if id != uuid.Nil {
q.Set("id", id.String())
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, r.endpoint()+"?"+q.Encode(), nil)
if err != nil {
return err
}
r.authed(req)
resp, err := r.client.Do(req)
if err != nil {
return fmt.Errorf("email_message_map.http: del: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusNotFound {
return &ControlPlaneHTTPError{Status: resp.StatusCode}
}
return nil
}