Phase 10, part 7: posting accounts, and who may post to which

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>
This commit is contained in:
2026-11-25 09:26:34 -07:00
co-authored by Claude Opus 5.5
parent a4dd36f5a4
commit bb4772637c
31 changed files with 1758 additions and 325 deletions
+87 -33
View File
@@ -14,6 +14,7 @@ import (
"supermarket/internal/api/reddit"
"supermarket/internal/api/twitter"
"supermarket/internal/oauth"
"supermarket/internal/posting"
"supermarket/internal/secrets"
)
@@ -236,6 +237,7 @@ type connectInfo struct {
Note string
Expiry *Expiry // when the sign-in runs out, for LinkedIn
Page *pageSetup // the company page's choices, for the LinkedIn page
Accounts []*posting.Account
}
func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
@@ -246,16 +248,24 @@ func (s *Server) accountsPage(w http.ResponseWriter, r *http.Request) {
// networks are the cards on the Social accounts tab, as things stand.
func (s *Server) networks() []network {
names := []string{secrets.XAccessToken, secrets.LinkedInAccessToken, secrets.LinkedInPageAccessToken, secrets.RedditAccessToken}
var names []string
for _, f := range accountFields {
names = append(names, f.Name)
}
set := s.secretState(names...)
conns := s.connections()
accts := s.postingAccounts()
netOf := map[string]string{"x": posting.X, "linkedin": posting.LinkedIn, liPageProvider: posting.LinkedInPage, "reddit": posting.Reddit}
connect := func(provider, token, setup, note string, ready bool, account func(string) string) *connectInfo {
c := conns[provider]
return &connectInfo{Provider: provider, Redirect: s.callbackURL(provider),
Setup: setup, Note: note, Ready: ready, Signed: set[token], Account: account(c.Account), At: c.At}
c := &connectInfo{Provider: provider, Redirect: s.callbackURL(provider), Setup: setup, Note: note, Ready: ready}
for _, a := range accts {
if a.Network == netOf[provider] {
c.Accounts = append(c.Accounts, a)
if a.Owner == 0 && !c.Signed {
c.Signed, c.Account, c.At = true, a.Name, a.Created
}
}
}
return c
}
at := func(prefix string) func(string) string {
return func(a string) string {
@@ -344,6 +354,20 @@ const oauthTTL = 15 * time.Minute
func (s *Server) oauthStart(w http.ResponseWriter, r *http.Request) {
provider := r.PathValue("provider")
// Admins connect the company's accounts (and Search Console); anyone
// with Content may connect their own posting account.
v := viewerOf(r)
forUser := int64(0)
if r.URL.Query().Get("for") == "me" && v.User != nil {
if provider == "google" || provider == liPageProvider || !v.Member.Can("content", true) {
s.forbidden(w, r)
return
}
forUser = v.User.ID
} else if !v.Member.Admin() {
s.forbidden(w, r)
return
}
var (
authURL string
p *oauth.Pending
@@ -393,6 +417,7 @@ func (s *Server) oauthStart(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
p.For = forUser
// Kept in the database: the callback may reach another copy of the app.
if err := s.DB.PutJSON(oauthFlowKey+p.State, p); err != nil {
s.serverError(w, err)
@@ -455,11 +480,40 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
if provider == "google" {
back, nav = "/settings/search", "settings/search"
}
if p != nil && p.For != 0 {
back, nav = "/settings/account#posting", "settings/account"
}
if p == nil || p.Provider != provider || time.Since(p.Started) > oauthTTL {
s.Log.Warn("oauth: callback with an unknown or expired state", "provider", provider)
redirect(w, r, back, "oauth-expired")
return
}
// The same person who started it: the company's sign-ins need an
// admin, a person's own the person.
v := viewerOf(r)
if (p.For != 0 && (v.User == nil || v.User.ID != p.For)) || (p.For == 0 && !v.Member.Admin()) {
s.forbidden(w, r)
return
}
who := int64(0)
if v.User != nil {
who = v.User.ID
}
// saveAccount keeps a posting account's sign-in.
saveAccount := func(network, name, external, access, refresh string, expires time.Time) error {
a := &posting.Account{Network: network, Name: name, External: external, Owner: p.For, CreatedBy: who, Expires: expires}
if err := posting.Save(s.DB, s.Secrets, a, access, refresh, time.Now()); err != nil {
return err
}
s.recordConnection(r, network, true)
return nil
}
ownMsg := func(msg string) string {
if p.For != 0 {
return "own-account-connected"
}
return msg
}
fail := func(msg string) {
s.Log.Warn("oauth: sign-in failed", "provider", provider, "reason", msg)
s.render(w, r, "message", page{Title: "Sign-in failed", Nav: nav, Error: msg,
@@ -515,14 +569,15 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
} else {
s.Log.Warn("x: couldn't verify the new token", "err", err)
}
if err := s.setSecrets(r, map[string]string{secrets.XAccessToken: res.AccessToken, secrets.XRefreshToken: res.RefreshToken}); err != nil {
name := "@" + account
if account == "" {
name = "X account"
}
if err := saveAccount(posting.X, name, account, res.AccessToken, res.RefreshToken, time.Time{}); err != nil {
fail("Signed in, but couldn't save the token: " + err.Error())
return
}
if err := s.saveConnection("x", Connection{Account: account, At: time.Now()}); err != nil {
s.Log.Error("saving connection", "err", err)
}
redirect(w, r, "/settings/accounts", "x-connected")
redirect(w, r, back, ownMsg("x-connected"))
case "linkedin":
res, err := oauth.LinkedInExchange(ctx, s.Secrets.Get(secrets.LinkedInClientID), s.Secrets.Get(secrets.LinkedInClientSecret), code, p)
if err != nil {
@@ -534,44 +589,47 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
fail("Signed in, but couldn't find your LinkedIn member ID: " + err.Error())
return
}
if err := s.setSecrets(r, map[string]string{secrets.LinkedInAccessToken: res.AccessToken, secrets.LinkedInAuthorURN: urn}); err != nil {
fail("Signed in, but couldn't save the token: " + err.Error())
return
}
life := res.ExpiresIn
if life <= 0 {
life = linkedInTokenLife
}
now := time.Now()
if err := s.saveConnection("linkedin", Connection{Account: urn, At: now, Expires: now.Add(life)}); err != nil {
s.Log.Error("saving connection", "err", err)
name := "LinkedIn member"
if v.User != nil {
name = v.User.Name
if name == "" {
name = v.User.Email
}
}
redirect(w, r, "/settings/accounts", "li-connected")
if err := saveAccount(posting.LinkedIn, name, urn, res.AccessToken, "", time.Now().Add(life)); err != nil {
fail("Signed in, but couldn't save the token: " + err.Error())
return
}
redirect(w, r, back, ownMsg("li-connected"))
case liPageProvider:
res, err := oauth.LinkedInExchange(ctx, s.Secrets.Get(secrets.LinkedInPageClientID), s.Secrets.Get(secrets.LinkedInPageClientSecret), code, p)
if err != nil {
fail(err.Error())
return
}
if err := s.setSecrets(r, map[string]string{secrets.LinkedInPageAccessToken: res.AccessToken}); err != nil {
fail("Signed in, but couldn't save the token: " + err.Error())
return
}
life := res.ExpiresIn
if life <= 0 {
life = linkedInTokenLife
}
now := time.Now()
_, listErr := s.pageOrgs(ctx, res.AccessToken)
if listErr != nil {
s.Log.Warn("linkedin page: couldn't list the pages", "err", listErr)
}
account := ""
name, urn := "the company page", ""
if o := s.liPage(); o != nil {
account = o.Name
name, urn = o.Name, o.URN
}
if err := s.saveConnection(liPageProvider, Connection{Account: account, At: now, Expires: now.Add(life)}); err != nil {
s.Log.Error("saving connection", "err", err)
// One page sign-in for the company: a new one replaces it.
if old, _ := posting.Company(s.DB, posting.LinkedInPage); old != nil && old.External != urn {
_ = posting.Rename(s.DB, old.ID, name, urn)
}
if err := saveAccount(posting.LinkedInPage, name, urn, res.AccessToken, "", time.Now().Add(life)); err != nil {
fail("Signed in, but couldn't save the token: " + err.Error())
return
}
redirect(w, r, "/settings/accounts", "li-page-connected")
case "reddit":
@@ -586,16 +644,12 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
fail("Signed in, but couldn't read the Reddit account's name: " + err.Error())
return
}
if err := s.setSecrets(r, map[string]string{secrets.RedditAccessToken: res.AccessToken,
secrets.RedditRefreshToken: res.RefreshToken, secrets.RedditUsername: name}); err != nil {
if err := saveAccount(posting.Reddit, "u/"+name, name, res.AccessToken, res.RefreshToken, time.Time{}); err != nil {
fail("Signed in, but couldn't save the token: " + err.Error())
return
}
s.forgetRedditHistory()
if err := s.saveConnection("reddit", Connection{Account: name, At: time.Now()}); err != nil {
s.Log.Error("saving connection", "err", err)
}
redirect(w, r, "/settings/accounts", "reddit-connected")
redirect(w, r, back, ownMsg("reddit-connected"))
default:
http.NotFound(w, r)
}