Files

77 lines
3.4 KiB
Go

package storecontract
import (
"context"
"time"
"github.com/samcharles93/archie-core/internal/domain/eventtype"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// CaptureStore persists inbound webhook captures.
type CaptureStore interface {
InsertCapture(ctx context.Context, c CapturedEvent, retention time.Duration, maxEvents int) (string, error)
// ListCaptures returns the newest captures in the acting org. A non-empty
// workspace narrows them to that workspace's sources.
ListCaptures(ctx context.Context, workspace org.WorkspaceID, limit int) ([]CapturedEvent, error)
}
// CaptureRefusalStore counts the events network rules refused, per source.
type CaptureRefusalStore interface {
RecordCaptureRefusal(ctx context.Context, source, addr string, at time.Time) error
CaptureRefusals(ctx context.Context, since time.Time) ([]CaptureRefusals, error)
}
// CaptureRefusals is one source's refusals and accepted captures since a
// time, and the addresses it refused.
type CaptureRefusals struct {
Source string `json:"source"`
Refused int64 `json:"refused"`
Accepted int64 `json:"accepted"`
Addrs []string `json:"addrs"`
}
// EventTypeStore persists event types. Insert and Update refuse a type whose
// rule overlaps another on the same source with eventtype.ErrOverlap, and a
// malformed one with eventtype.ErrInvalid. Update rewrites the name and rule;
// the schema is the one the type was created with.
type EventTypeStore interface {
InsertEventType(ctx context.Context, t eventtype.EventType) (string, error)
UpdateEventType(ctx context.Context, t eventtype.EventType) error
DeleteEventType(ctx context.Context, id string) error
ListEventTypes(ctx context.Context) ([]eventtype.EventType, error)
}
// CapturedEvent is one unbound inbound webhook capture: no workflow binding,
// no forge/task association -- just what arrived, from where, and when.
type CapturedEvent struct {
ID string `json:"id"`
ReceivedAt time.Time `json:"received_at"`
Source string `json:"source"`
RemoteAddr string `json:"remote_addr"`
ContentType string `json:"content_type"`
// Headers and Body are redacted (webhookguard.RedactPayload) before
// they ever reach InsertCapture; the store persists what it is given.
Headers string `json:"headers"`
Body string `json:"body"`
Authenticated bool `json:"authenticated"`
// EventType is the ID of the event type the capture was identified as
// when it arrived, or empty when it matched none. An unidentified capture
// is never dispatched.
EventType string `json:"event_type"`
// Unsigned marks an event that arrived on an approved unsigned source.
// It dispatches without Authenticated and is flagged wherever it shows.
Unsigned bool `json:"unsigned"`
// Delivery names the sender's delivery; a retry of one delivery shares
// it, so a binding dispatches each delivery once. Intake sets it from the
// source's delivery header; the store fills the capture ID when empty.
Delivery string `json:"delivery"`
// OrgID is honoured only for a schedule-fired capture, whose source is
// reserved and so names no org; every other capture takes its source's.
OrgID string `json:"org_id,omitempty"`
}
// Dispatchable reports whether a binding may start a task from this event:
// a valid signature, or a source approved to take unsigned events.
func (c CapturedEvent) Dispatchable() bool { return c.Authenticated || c.Unsigned }