Phase 1, part 3: Reddit

- Sign in with a Reddit web app; access tokens refresh automatically,
  and an app-only token reads rules before sign-in.
- Reddit drafts: a title and text for one subreddit, chosen when
  writing. The subreddit's guidelines and rules go into the prompt, which
  asks for a community member's post that discloses the affiliation and
  avoids marketing language.
- The Inbox shows the subreddit's rules (cached for a day, with Refresh)
  and warns, without blocking, about promotion rules, restricted
  subreddits, link-only subreddits, posting in the same subreddit within
  a week, more than 1 in 10 recent posts linking to the company's sites,
  and new accounts.
- Publishing makes a text post with the source link at the end, or a
  link post where the subreddit takes only links.
- Migration 4 adds the draft's title and subreddit, and the rules cache.
  Publishers now take a Post, which carries the title and subreddit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 13:51:16 -07:00
co-authored by Claude Opus 5.5
parent b0f06058ec
commit 0e34f574b7
27 changed files with 1404 additions and 91 deletions
+40 -11
View File
@@ -10,6 +10,7 @@ import (
"time"
"supermarket/internal/ai"
"supermarket/internal/api/reddit"
"supermarket/internal/content"
"supermarket/internal/fetcher"
"supermarket/internal/inventory"
@@ -62,6 +63,7 @@ type entryPage struct {
Since time.Time
Channels []channelChoice
ProfileSet bool
Subreddits []string // suggestions: the entry's, then the profile's
// After a fetch
Fetched bool
NewItems int
@@ -105,7 +107,17 @@ func (s *Server) entryData(e *inventory.Entry, showDismissed bool) (*entryPage,
for _, c := range content.Channels {
chans = append(chans, channelChoice{c, pubs[c.ID] != nil})
}
return &entryPage{Entry: e, Sources: fetcher.Sources(e), Items: items, ShowDismissed: showDismissed,
var subs []string
seen := map[string]bool{}
for _, list := range [][]string{e.Subreddits, p.Accounts.Subreddits} {
for _, sr := range list {
if n := reddit.CleanName(sr); n != "" && !seen[strings.ToLower(n)] {
seen[strings.ToLower(n)] = true
subs = append(subs, n)
}
}
}
return &entryPage{Subreddits: subs, Entry: e, Sources: fetcher.Sources(e), Items: items, ShowDismissed: showDismissed,
LastChecked: last, Since: since, Channels: chans, ProfileSet: p.IsSet()}, nil
}
@@ -232,15 +244,6 @@ func (s *Server) writeDrafts(r *http.Request, e *inventory.Entry, ids []string)
if err != nil {
return err
}
groups := [][]content.Item{items}
if perItem {
groups = groups[:0]
for _, it := range items {
groups = append(groups, []content.Item{it})
}
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
// Keep channels in display order whatever order the form sent.
var ordered []string
for _, c := range content.Channels {
@@ -251,9 +254,35 @@ func (s *Server) writeDrafts(r *http.Request, e *inventory.Entry, ids []string)
}
}
}
req := content.Request{Profile: p, Entry: e}
for _, ch := range ordered {
if ch == content.ChannelReddit {
req.Subreddit = reddit.CleanName(r.PostForm.Get("subreddit"))
if req.Subreddit == "" {
return errors.New("choose a subreddit for the Reddit post")
}
// The rules go into the prompt; without them the model is told
// to be careful, and the Inbox warns.
info, err := s.subreddit(r.Context(), req.Subreddit, false)
if err != nil {
s.Log.Warn("reddit: couldn't read the subreddit's rules", "subreddit", req.Subreddit, "err", err)
}
req.SubredditInfo = info
}
}
groups := [][]content.Item{items}
if perItem {
groups = groups[:0]
for _, it := range items {
groups = append(groups, []content.Item{it})
}
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
written := 0
for _, g := range groups {
drafts, err := content.Generate(ctx, client, content.Request{Profile: p, Entry: e, Items: g, Channels: ordered})
req.Items, req.Channels = g, ordered
drafts, err := content.Generate(ctx, client, req)
if err != nil {
if written > 0 {
return fmt.Errorf("wrote %d set(s) of drafts, then: %w", written, err)