153 lines
5.8 KiB
Go
153 lines
5.8 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/connector/v1"
|
|
)
|
|
|
|
// The deliveries core itself sends on a channel connector. They are part of
|
|
// the extension contract: a connector that declares one must declare a schema
|
|
// that accepts what core sends.
|
|
const (
|
|
DeliveryMessage = "message"
|
|
DeliveryChatMessage = "chat_message"
|
|
)
|
|
|
|
// coreDeliveries lists, for each delivery core sends itself, every payload
|
|
// shape core may put in it: a message always has a title, and may also carry
|
|
// a body and, for an approval request, its request id; a chat message always
|
|
// has a chat id and text.
|
|
var coreDeliveries = map[string][]jsontext.Value{
|
|
DeliveryMessage: {
|
|
jsontext.Value(`{"title":"a title"}`),
|
|
jsontext.Value(`{"title":"a title","body":"a body"}`),
|
|
jsontext.Value(`{"title":"a title","approval_request_id":"00000000-0000-0000-0000-000000000000"}`),
|
|
jsontext.Value(`{"title":"a title","body":"a body","approval_request_id":"00000000-0000-0000-0000-000000000000"}`),
|
|
},
|
|
DeliveryChatMessage: {
|
|
jsontext.Value(`{"chat_id":"a chat","text":"a message"}`),
|
|
},
|
|
}
|
|
|
|
// checkCoreDeliveries refuses a declaration of a delivery core sends itself
|
|
// whose input schema rejects a payload core sends, so the mismatch surfaces
|
|
// when the connector loads rather than when a delivery is mid-flight.
|
|
func checkCoreDeliveries(caps Capabilities) error {
|
|
for _, name := range []string{DeliveryMessage, DeliveryChatMessage} {
|
|
d, ok := caps.Delivery(name)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, payload := range coreDeliveries[name] {
|
|
if err := d.CheckInput(payload); err != nil {
|
|
return fmt.Errorf("delivery %q doesn't accept what core sends: %w", name, err)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Capability is one event type, action, delivery or lookup a connector
|
|
// declared. Schema is the JSON Schema of an event's fields or of the input the
|
|
// others take.
|
|
type Capability struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Schema jsontext.Value `json:"schema"`
|
|
}
|
|
|
|
// CheckInput reports why input doesn't match the declared schema, or nil.
|
|
func (c Capability) CheckInput(input jsontext.Value) error {
|
|
schema, err := compile(c.Schema)
|
|
if err != nil {
|
|
return fmt.Errorf("%q: %w", c.Name, err)
|
|
}
|
|
return validate(schema, input)
|
|
}
|
|
|
|
// Capabilities is what a connector declared when it last loaded. It is stored
|
|
// with the accepted connector, so it stays available while the connector is
|
|
// stopped.
|
|
type Capabilities struct {
|
|
Events []Capability `json:"events"`
|
|
Actions []Capability `json:"actions"`
|
|
Deliveries []Capability `json:"deliveries"`
|
|
Lookups []Capability `json:"lookups"`
|
|
// Listens says the connector streams events from its own connection to a
|
|
// source.
|
|
Listens bool `json:"listens"`
|
|
// TargetFacts says the connector answers target fact requests.
|
|
TargetFacts bool `json:"target_facts"`
|
|
// LinksAccounts says the connector can link a person's channel identity
|
|
// to their account.
|
|
LinksAccounts bool `json:"links_accounts"`
|
|
}
|
|
|
|
// Event returns the named event type.
|
|
func (c Capabilities) Event(name string) (Capability, bool) { return find(c.Events, name) }
|
|
|
|
// Action returns the named action.
|
|
func (c Capabilities) Action(name string) (Capability, bool) { return find(c.Actions, name) }
|
|
|
|
// Delivery returns the named delivery.
|
|
func (c Capabilities) Delivery(name string) (Capability, bool) { return find(c.Deliveries, name) }
|
|
|
|
// Lookup returns the named lookup.
|
|
func (c Capabilities) Lookup(name string) (Capability, bool) { return find(c.Lookups, name) }
|
|
|
|
func find(list []Capability, name string) (Capability, bool) {
|
|
i := slices.IndexFunc(list, func(c Capability) bool { return c.Name == name })
|
|
if i < 0 {
|
|
return Capability{}, false
|
|
}
|
|
return list[i], true
|
|
}
|
|
|
|
// capabilitiesOf lists what a connector described, each kind in name order.
|
|
func capabilitiesOf(desc *pb.DescribeResponse) Capabilities {
|
|
caps := Capabilities{
|
|
Events: make([]Capability, 0, len(desc.GetEvents())),
|
|
Actions: make([]Capability, 0, len(desc.GetActions())),
|
|
Deliveries: make([]Capability, 0, len(desc.GetDeliveries())),
|
|
Lookups: make([]Capability, 0, len(desc.GetLookups())),
|
|
Listens: desc.GetListens(),
|
|
TargetFacts: desc.GetTargetFacts(),
|
|
LinksAccounts: desc.GetLinksAccounts(),
|
|
}
|
|
for _, e := range desc.GetEvents() {
|
|
caps.Events = append(caps.Events, Capability{Name: e.GetName(), Description: e.GetDescription(), Schema: schemaOrEmpty(e.GetSchema())})
|
|
}
|
|
for _, a := range desc.GetActions() {
|
|
caps.Actions = append(caps.Actions, Capability{Name: a.GetName(), Description: a.GetDescription(), Schema: schemaOrEmpty(a.GetInputSchema())})
|
|
}
|
|
for _, d := range desc.GetDeliveries() {
|
|
caps.Deliveries = append(caps.Deliveries, Capability{Name: d.GetName(), Description: d.GetDescription(), Schema: schemaOrEmpty(d.GetInputSchema())})
|
|
}
|
|
for _, l := range desc.GetLookups() {
|
|
caps.Lookups = append(caps.Lookups, Capability{Name: l.GetName(), Description: l.GetDescription(), Schema: schemaOrEmpty(l.GetInputSchema())})
|
|
}
|
|
for _, list := range [][]Capability{caps.Events, caps.Actions, caps.Deliveries, caps.Lookups} {
|
|
slices.SortFunc(list, func(a, b Capability) int { return strings.Compare(a.Name, b.Name) })
|
|
}
|
|
return caps
|
|
}
|
|
|
|
// Capabilities returns what the named connector declared when it last loaded.
|
|
// ok is false when the connector isn't accepted or has never loaded.
|
|
func (m *Module) Capabilities(ctx context.Context, connector string) (caps Capabilities, ok bool, err error) {
|
|
raw, ok, err := m.store.Capabilities(ctx, m.orgID, connector)
|
|
if err != nil || !ok {
|
|
return Capabilities{}, false, err
|
|
}
|
|
if err := json.Unmarshal(raw, &caps); err != nil {
|
|
return Capabilities{}, false, fmt.Errorf("connector %q: stored capabilities: %w", connector, err)
|
|
}
|
|
return caps, true, nil
|
|
}
|