Admins see every service Supermarket uses, whether it's connected, failing or not set up, when it was last tested (every Test button now records its result), and who connected or removed it and when. Keys and sign-ins saved from the settings pages record the person; refreshed tokens don't. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
239 lines
8.2 KiB
Go
239 lines
8.2 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"supermarket/internal/ai"
|
|
"supermarket/internal/bridge"
|
|
"supermarket/internal/secrets"
|
|
)
|
|
|
|
// Settings → Connections, for admins: every service Supermarket uses, its
|
|
// state, when it was last checked, and who connected it.
|
|
|
|
// service is something Supermarket connects to.
|
|
type service struct {
|
|
ID, Name, For, Link string
|
|
Keys []string // the secrets it needs; any one set counts as connected
|
|
}
|
|
|
|
var services = []service{
|
|
{"ai", "AI provider", "Writing: posts, pages, briefs, the profile draft", "/settings/ai", []string{secrets.AnthropicAPIKey, secrets.OpenAIAPIKey}},
|
|
{"x", "X", "Posting, and reading partners' posts", "/settings/accounts#network-X", []string{secrets.XAccessToken, secrets.XBearerToken}},
|
|
{"linkedin", "LinkedIn", "Posting as a person", "/settings/accounts#network-LinkedIn", []string{secrets.LinkedInAccessToken}},
|
|
{"linkedin_page", "LinkedIn page", "Posting as the company's page", "/settings/accounts", []string{secrets.LinkedInPageAccessToken}},
|
|
{"reddit", "Reddit", "Posting to subreddits", "/settings/accounts#network-Reddit", []string{secrets.RedditAccessToken}},
|
|
{"youtube", "YouTube", "Reading partners' videos", "/settings/accounts", []string{secrets.YouTubeAPIKey}},
|
|
{"google", "Google Search Console", "SEO: what ranks, the morning check", "/settings/search", []string{secrets.GoogleRefreshToken}},
|
|
{"bing", "Bing Webmaster", "SEO: links to the sites", "/settings/search", []string{secrets.BingAPIKey}},
|
|
{"pagespeed", "PageSpeed Insights", "SEO: the audit's speed checks", "/settings/search", []string{secrets.PageSpeedAPIKey}},
|
|
{"gitea", "Gitea", "Sites and SEO: pull requests on the sites", "/settings/search", []string{secrets.GiteaToken}},
|
|
{"plausible", "Plausible", "Visits from tagged links", "/settings/tracking", []string{secrets.PlausibleAPIKey}},
|
|
{"stripe", "Stripe", "Sign-ups, payments, Metrics", "/settings/tracking", []string{secrets.StripeAPIKey}},
|
|
{"origami", "origami.chat", "Prospects: lead imports", "/settings/sources", []string{secrets.OrigamiAPIKey}},
|
|
{"explee", "explee.com", "Prospects: lead imports", "/settings/sources", []string{secrets.ExpleeAPIKey}},
|
|
{"hunter", "Hunter.io", "Prospects: finding and checking emails", "/settings/sources", []string{secrets.HunterAPIKey}},
|
|
{"mail", "Outreach mailbox", "Sending outreach, reading replies", "/settings/email", []string{secrets.MailPassword}},
|
|
{"listmonk", "Listmonk", "Mailing lists", "/settings/email", []string{secrets.ListmonkToken}},
|
|
}
|
|
|
|
// serviceOf is the service a secret belongs to, by its name's prefix.
|
|
func serviceOf(secret string) string {
|
|
prefix, _, _ := strings.Cut(secret, ".")
|
|
switch prefix {
|
|
case "anthropic", "openai":
|
|
return "ai"
|
|
}
|
|
for _, sv := range services {
|
|
if sv.ID == prefix {
|
|
return sv.ID
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// connChange is who last connected (or removed) a service, and when.
|
|
type connChange struct {
|
|
By string `json:"by"`
|
|
UserID int64 `json:"user_id,omitempty"`
|
|
At time.Time `json:"at"`
|
|
Removed bool `json:"removed,omitempty"`
|
|
}
|
|
|
|
// connCheck is the last test of a service: its page's Test button.
|
|
type connCheck struct {
|
|
At time.Time `json:"at"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
const (
|
|
connChangesKey = "connections:changed"
|
|
connChecksKey = "connections:checked"
|
|
)
|
|
|
|
// setSecrets saves secrets someone entered or connected, and records who
|
|
// did, for each service they belong to. (Tokens Supermarket refreshes
|
|
// itself are saved with Secrets.Set: nobody connected anything.)
|
|
func (s *Server) setSecrets(r *http.Request, updates map[string]string) error {
|
|
if err := s.Secrets.Set(updates); err != nil {
|
|
return err
|
|
}
|
|
touched := map[string]bool{} // service → whether anything was set (not only removed)
|
|
for name, v := range updates {
|
|
if sv := serviceOf(name); sv != "" {
|
|
touched[sv] = touched[sv] || v != ""
|
|
}
|
|
}
|
|
if len(touched) == 0 {
|
|
return nil
|
|
}
|
|
changes := map[string]connChange{}
|
|
if _, err := s.DB.GetJSON(connChangesKey, &changes); err != nil {
|
|
s.Log.Warn("connections: reading who connected what", "err", err)
|
|
}
|
|
v := viewerOf(r)
|
|
who, id := "you", int64(0)
|
|
if v.User != nil {
|
|
who, id = v.User.Name, v.User.ID
|
|
if who == "" {
|
|
who = v.User.Email
|
|
}
|
|
}
|
|
now := time.Now().UTC()
|
|
for sv, set := range touched {
|
|
removed := !set && !s.serviceSet(sv)
|
|
changes[sv] = connChange{By: who, UserID: id, At: now, Removed: removed}
|
|
s.Log.Info("connections: changed", "service", sv, "removed", removed, "by", who)
|
|
}
|
|
return s.DB.PutJSON(connChangesKey, changes)
|
|
}
|
|
|
|
// serviceSet says whether any of a service's secrets is set.
|
|
func (s *Server) serviceSet(id string) bool {
|
|
for _, sv := range services {
|
|
if sv.ID == id {
|
|
for _, k := range sv.Keys {
|
|
if s.Secrets.Has(k) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// noteCheck records a service's test, from its page's Test button: data
|
|
// is the button's result, with "Error" when it failed.
|
|
func (s *Server) noteCheck(service string, data map[string]any) {
|
|
msg := ""
|
|
if e, ok := data["Error"].(string); ok {
|
|
msg = e
|
|
}
|
|
s.noteCheckErr(service, msg)
|
|
}
|
|
|
|
func (s *Server) noteCheckErr(service, msg string) {
|
|
checks := map[string]connCheck{}
|
|
_, _ = s.DB.GetJSON(connChecksKey, &checks)
|
|
checks[service] = connCheck{At: time.Now().UTC(), Error: msg}
|
|
if err := s.DB.PutJSON(connChecksKey, checks); err != nil {
|
|
s.Log.Warn("connections: noting a check", "err", err)
|
|
}
|
|
}
|
|
|
|
// connRow is a line on the Connections page.
|
|
type connRow struct {
|
|
service
|
|
State string // connected, missing, failing
|
|
Detail string
|
|
Change *connChange
|
|
Checked *connCheck
|
|
}
|
|
|
|
func (s *Server) connectionsPage(w http.ResponseWriter, r *http.Request) {
|
|
if s.admin(w, r) == nil {
|
|
return
|
|
}
|
|
changes := map[string]connChange{}
|
|
checks := map[string]connCheck{}
|
|
_, _ = s.DB.GetJSON(connChangesKey, &changes)
|
|
_, _ = s.DB.GetJSON(connChecksKey, &checks)
|
|
signins := s.connections()
|
|
var rows []connRow
|
|
for _, sv := range services {
|
|
row := connRow{service: sv, State: "missing"}
|
|
if s.serviceSet(sv.ID) {
|
|
row.State = "connected"
|
|
}
|
|
if sv.ID == "ai" {
|
|
row.State = "missing"
|
|
if cfg, err := ai.LoadConfig(s.DB); err == nil {
|
|
if _, err := ai.New(cfg, s.Secrets); err == nil {
|
|
row.State, row.Detail = "connected", cfg.Provider+" · "+cfg.Model
|
|
}
|
|
}
|
|
}
|
|
if c, ok := signins[connectionName(sv.ID)]; ok && row.State == "connected" {
|
|
if c.Account != "" {
|
|
row.Detail = c.Account
|
|
}
|
|
if !c.Expires.IsZero() && time.Now().After(c.Expires) {
|
|
row.State, row.Detail = "failing", "The sign-in expired on "+c.Expires.Local().Format("2 Jan 2006")
|
|
}
|
|
}
|
|
if c, ok := changes[sv.ID]; ok {
|
|
row.Change = &c
|
|
}
|
|
if c, ok := checks[sv.ID]; ok {
|
|
row.Checked = &c
|
|
if c.Error != "" && row.State == "connected" {
|
|
row.State, row.Detail = "failing", c.Error
|
|
}
|
|
}
|
|
rows = append(rows, row)
|
|
}
|
|
// The Core and Hermes, on the local edition.
|
|
var core []connRow
|
|
if !s.signsIn() {
|
|
row := connRow{service: service{ID: "core", Name: "Federated Core", For: "The Core's services and the CRM, through Hermes", Link: "/settings/core"}, State: "missing"}
|
|
if s.Core != nil {
|
|
row.State, row.Detail = "connected", coreDomain(s.Core)
|
|
}
|
|
core = append(core, row)
|
|
hermes := connRow{service: service{ID: "hermes", Name: "Hermes bridge", For: "The CRM, through the Core's MCP hub", Link: "/settings/core"}, State: "missing"}
|
|
if last, err := bridge.Last(s.DB); err == nil && last != nil {
|
|
hermes.State, hermes.Detail = "connected", "Last check-in "+ago(last.At)
|
|
if time.Since(last.At) > 24*time.Hour {
|
|
hermes.State = "failing"
|
|
}
|
|
}
|
|
core = append(core, hermes)
|
|
}
|
|
rows = append(core, rows...)
|
|
// Connected and failing first, then what's missing; the list's order
|
|
// within each.
|
|
order := map[string]int{"failing": 0, "connected": 1, "missing": 2}
|
|
sort.SliceStable(rows, func(i, j int) bool { return order[rows[i].State] < order[rows[j].State] })
|
|
s.render(w, r, "connections", page{Title: "Connections", Nav: "settings/connections", Data: rows})
|
|
}
|
|
|
|
// connectionName is a service's name in the sign-ins record (s.connections).
|
|
func connectionName(id string) string {
|
|
switch id {
|
|
case "x":
|
|
return "x"
|
|
case "linkedin":
|
|
return "linkedin"
|
|
case "linkedin_page":
|
|
return liPageProvider
|
|
case "reddit":
|
|
return "reddit"
|
|
case "google":
|
|
return "google"
|
|
}
|
|
return ""
|
|
}
|