Files
supermarket/internal/web/channels.go
T
davidandClaude Opus 5.5 13bf18facc Phase 1, part 1: fetch, write and publish posts from the web app
Brings back posting, which Phase 0 removed with the terminal interface.

- Content page: per inventory entry, fetch its X account, X hashtag
  search, YouTube channel and RSS/Atom feeds (new), then select, dismiss
  or restore the fetched items, which are kept in the database.
- Writing: one AI call returns a draft per channel (X and LinkedIn),
  prompted from the company profile, the entry's relationship, talking
  points and mention policy, and each channel's rules. Fetched text is
  quoted as untrusted. An over-long X draft gets one request to shorten.
- Drafts page: edit with a live length counter that counts like X, then
  publish each draft on its own. A draft is claimed before posting so it
  can't go out twice; failures are kept on the draft for a retry; X
  tokens are refreshed and saved.
- Migration 2 adds content_items and drafts, and turns queued posts into
  drafts for the platforms they hadn't reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 12:58:24 -07:00

82 lines
2.8 KiB
Go

package web
import (
"context"
"supermarket/internal/api/linkedin"
"supermarket/internal/api/rss"
"supermarket/internal/api/twitter"
"supermarket/internal/api/youtube"
"supermarket/internal/content"
"supermarket/internal/fetcher"
"supermarket/internal/oauth"
"supermarket/internal/secrets"
)
// fetchMaxResults is how many posts or videos one source returns per fetch.
const fetchMaxResults = 20
// fetcher builds a fetcher from the saved credentials.
func (s *Server) fetcher() *fetcher.Fetcher {
f := &fetcher.Fetcher{RSS: rss.New("Supermarket/" + s.Version + " (feed reader)")}
if tok := s.Secrets.Get(secrets.XBearerToken); tok != "" {
f.Twitter = twitter.New(tok, "", fetchMaxResults)
}
if key := s.Secrets.Get(secrets.YouTubeAPIKey); key != "" {
f.YouTube = youtube.New(key, fetchMaxResults)
}
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}
}
if tok, urn := s.Secrets.Get(secrets.LinkedInAccessToken), s.Secrets.Get(secrets.LinkedInAuthorURN); tok != "" && urn != "" {
pubs[content.ChannelLinkedIn] = linkedInPublisher{linkedin.New(tok, urn)}
}
return pubs
}
type xPublisher struct{ c *twitter.Client }
// Publish appends the source link to the post, as the old app did; X shows
// it as a card.
func (p xPublisher) Publish(ctx context.Context, text, sourceURL string) (string, error) {
if sourceURL != "" {
text += "\n\n" + sourceURL
}
return p.c.PostTweet(ctx, text)
}
type linkedInPublisher struct{ c *linkedin.Client }
// Publish attaches the source link as an article preview.
func (p linkedInPublisher) Publish(ctx context.Context, text, sourceURL string) (string, error) {
return p.c.PostUpdate(ctx, text, sourceURL)
}