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:
+12
-75
@@ -34,58 +34,6 @@ func (s *Server) fetcher() *fetcher.Fetcher {
|
||||
return f
|
||||
}
|
||||
|
||||
// publishers are the channels signed in for posting.
|
||||
func (s *Server) publishers() content.Publishers {
|
||||
pubs := content.Publishers{}
|
||||
if tok := s.Secrets.Get(secrets.XAccessToken); tok != "" {
|
||||
c := twitter.New("", tok, 0)
|
||||
// X access tokens last two hours; refresh tokens are single-use, so
|
||||
// each new one is saved before the old is gone.
|
||||
if s.Secrets.Has(secrets.XRefreshToken) && s.Secrets.Has(secrets.XClientID) {
|
||||
c.SetTokenRefresher(func(ctx context.Context) (string, string, error) {
|
||||
res, err := oauth.RefreshX(ctx, s.Secrets.Get(secrets.XClientID),
|
||||
s.Secrets.Get(secrets.XClientSecret), s.Secrets.Get(secrets.XRefreshToken))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
updates := map[string]string{secrets.XAccessToken: res.AccessToken}
|
||||
if res.RefreshToken != "" {
|
||||
updates[secrets.XRefreshToken] = res.RefreshToken
|
||||
}
|
||||
if err := s.Secrets.Set(updates); err != nil {
|
||||
// Keep going with the new token; the next start will need
|
||||
// a new sign-in.
|
||||
s.Log.Error("x: couldn't save the refreshed token; sign in to X again later", "err", err)
|
||||
}
|
||||
return res.AccessToken, res.RefreshToken, nil
|
||||
})
|
||||
}
|
||||
pubs[content.ChannelX] = xPublisher{c}
|
||||
}
|
||||
var li linkedInPublisher
|
||||
if tok, urn := s.Secrets.Get(secrets.LinkedInAccessToken), s.Secrets.Get(secrets.LinkedInAuthorURN); tok != "" && urn != "" {
|
||||
if e := s.linkedInExpiry(); e != nil && e.Expired() {
|
||||
li.meErr = expiredPublisher{*e}.err()
|
||||
} else {
|
||||
li.me = linkedin.New(tok, urn)
|
||||
}
|
||||
}
|
||||
if tok, org := s.Secrets.Get(secrets.LinkedInPageAccessToken), s.liPage(); tok != "" && org != nil {
|
||||
if e := s.linkedInPageExpiry(); e != nil && e.Expired() {
|
||||
li.pageErr = expiredPublisher{*e}.err()
|
||||
} else {
|
||||
li.page = linkedin.New(tok, org.URN)
|
||||
}
|
||||
}
|
||||
if li.me != nil || li.page != nil || li.meErr != nil || li.pageErr != nil {
|
||||
pubs[content.ChannelLinkedIn] = li
|
||||
}
|
||||
if c := s.redditUser(); c != nil {
|
||||
pubs[content.ChannelReddit] = redditPublisher{s: s, c: c}
|
||||
}
|
||||
return pubs
|
||||
}
|
||||
|
||||
// expiredPublisher stands in for a network whose sign-in ran out: it posts
|
||||
// nothing and says so, rather than letting the network answer 401.
|
||||
type expiredPublisher struct{ e Expiry }
|
||||
@@ -163,31 +111,20 @@ func (p redditPublisher) Publish(ctx context.Context, post content.Post) (string
|
||||
// --- Reddit ---
|
||||
|
||||
func (s *Server) redditUserAgent() string {
|
||||
return reddit.UserAgent(s.Version, s.Secrets.Get(secrets.RedditUsername))
|
||||
name := ""
|
||||
if a := s.companyReddit(); a != nil {
|
||||
name = a.External
|
||||
}
|
||||
return reddit.UserAgent(s.Version, name)
|
||||
}
|
||||
|
||||
// redditUser is a client signed in as the user, or nil. Access tokens last
|
||||
// an hour; a refused one is refreshed and saved.
|
||||
// redditUser is a client signed in as the company's Reddit account, or nil.
|
||||
func (s *Server) redditUser() *reddit.Client {
|
||||
tok := s.Secrets.Get(secrets.RedditAccessToken)
|
||||
if tok == "" {
|
||||
a := s.companyReddit()
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
return reddit.New(tok, s.redditUserAgent(), func(ctx context.Context) (string, error) {
|
||||
res, err := oauth.RefreshReddit(ctx, s.Secrets.Get(secrets.RedditClientID), s.Secrets.Get(secrets.RedditClientSecret),
|
||||
s.redditUserAgent(), s.Secrets.Get(secrets.RedditRefreshToken))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
updates := map[string]string{secrets.RedditAccessToken: res.AccessToken}
|
||||
if res.RefreshToken != "" {
|
||||
updates[secrets.RedditRefreshToken] = res.RefreshToken
|
||||
}
|
||||
if err := s.Secrets.Set(updates); err != nil {
|
||||
s.Log.Error("reddit: couldn't save the refreshed token", "err", err)
|
||||
}
|
||||
return res.AccessToken, nil
|
||||
})
|
||||
return s.redditClient(a)
|
||||
}
|
||||
|
||||
// redditReader reads subreddits: as the user if signed in, else as the
|
||||
@@ -251,11 +188,11 @@ const redditHistoryTTL = 10 * time.Minute
|
||||
// redditHistory is the signed-in account's recent posts, or nil if they
|
||||
// can't be read.
|
||||
func (s *Server) redditHistory(ctx context.Context) []reddit.Submission {
|
||||
user := s.Secrets.Get(secrets.RedditUsername)
|
||||
c := s.redditUser()
|
||||
if c == nil || user == "" {
|
||||
a := s.companyReddit()
|
||||
if a == nil || a.External == "" {
|
||||
return nil
|
||||
}
|
||||
user, c := a.External, s.redditClient(a)
|
||||
s.mu.Lock()
|
||||
h, at := s.redditHist, s.redditHistAt
|
||||
s.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user