Social accounts become posting accounts: several per network, the company's and each person's own, with their tokens kept per account. The old sign-ins are adopted as the company's. Admins say who may post to which (Settings → Posting accounts); a draft can go out from one account or a copy to several, each recording who posted it and as what. Signing an account out, or removing its owner, holds the posts scheduled to it and tells whoever scheduled them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
280 lines
9.4 KiB
Go
280 lines
9.4 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.XBearerToken}},
|
|
{"linkedin", "LinkedIn", "Posting as a person", "/settings/accounts#network-LinkedIn", nil},
|
|
{"linkedin_page", "LinkedIn page", "Posting as the company's page", "/settings/accounts", nil},
|
|
{"reddit", "Reddit", "Posting to subreddits", "/settings/accounts#network-Reddit", nil},
|
|
{"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)
|
|
}
|
|
|
|
// recordConnection records who connected (or removed) a service, for
|
|
// sign-ins kept outside the secrets' names (posting accounts).
|
|
func (s *Server) recordConnection(r *http.Request, service string, set bool) {
|
|
changes := map[string]connChange{}
|
|
_, _ = s.DB.GetJSON(connChangesKey, &changes)
|
|
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
|
|
}
|
|
}
|
|
changes[service] = connChange{By: who, UserID: id, At: time.Now().UTC(), Removed: !set}
|
|
if err := s.DB.PutJSON(connChangesKey, changes); err != nil {
|
|
s.Log.Warn("connections: recording a change", "err", err)
|
|
}
|
|
}
|
|
|
|
// 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()
|
|
// The posting networks count their accounts.
|
|
postingCount := map[string]int{"x": 0, "linkedin": 0, "linkedin_page": 0, "reddit": 0}
|
|
postingNames := map[string]string{}
|
|
for _, a := range s.postingAccounts() {
|
|
if s.working(a) {
|
|
postingCount[a.Network]++
|
|
if postingNames[a.Network] != "" {
|
|
postingNames[a.Network] += ", "
|
|
}
|
|
postingNames[a.Network] += a.Name
|
|
}
|
|
}
|
|
var rows []connRow
|
|
for _, sv := range services {
|
|
row := connRow{service: sv, State: "missing"}
|
|
if s.serviceSet(sv.ID) {
|
|
row.State = "connected"
|
|
}
|
|
if n, ok := postingCount[sv.ID]; ok {
|
|
reading := row.State == "connected" // X's key for reading posts
|
|
row.State, row.Detail = "missing", ""
|
|
switch {
|
|
case n > 0:
|
|
row.State, row.Detail = "connected", postingNames[sv.ID]
|
|
case reading:
|
|
row.State, row.Detail = "connected", "Reading only: no account to post to"
|
|
}
|
|
}
|
|
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 ""
|
|
}
|