Reddit: posted by hand, with the rules pasted

Reddit's API isn't open to apps like this one, so Supermarket no longer
signs in to Reddit, publishes or schedules there, or reads subreddits and
the account's posts. The internal/api/reddit client, Reddit's OAuth, its
keys and its Social accounts card are gone.

Supermarket still writes the post. Each Reddit draft in Posts has a box
for its subreddit's rules, with a link to them on Reddit: pasted once,
they're kept (subreddits.data, as pasted_rules) and given to the model
for later drafts. Open in Reddit saves the edits and opens the submit
page filled in; Copy title and Copy text (with the tagged source link)
cover a box that comes up empty; Mark as posted records it. Warnings come
from the pasted rules and the posts marked as posted. Fill's subreddit
suggestions are kept unchecked, and say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 11:48:54 -07:00
co-authored by Claude Opus 5.5
parent bae468e01c
commit 5451fe6d63
33 changed files with 382 additions and 1107 deletions
+28 -44
View File
@@ -2,12 +2,11 @@ package inventory
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"supermarket/internal/ai"
"supermarket/internal/api/reddit"
"supermarket/internal/profile"
"supermarket/internal/site"
)
@@ -18,17 +17,12 @@ const maxFeeds = 3
// maxSubreddits is how many suggested subreddits Fill puts in the box.
const maxSubreddits = 5
// SubredditCheck looks a subreddit up on Reddit. It returns reddit.ErrNotFound
// or reddit.ErrForbidden for one that doesn't exist or is private.
type SubredditCheck func(ctx context.Context, name string) (*reddit.Subreddit, error)
// Suggestion is a subreddit the AI suggested, and what Reddit said about it.
// Suggestion is a subreddit the AI suggested. Reddit can't be asked
// whether it exists, so the user looks before posting there.
type Suggestion struct {
Name string
Why string // the model's reason
Info *reddit.Subreddit // nil if not checked
Kept bool // put in the Subreddits box
Problem string // why it was left out, or why it's unchecked
Name string
Why string // the model's reason
Kept bool // put in the Subreddits box; false: more than enough already
}
// FillResult says what Fill did.
@@ -47,7 +41,7 @@ type FillResult struct {
// there is one), keywords and hashtags. If the site links to no subreddit,
// the model suggests some, and check (if not nil) drops the ones that don't
// exist or that can't be posted to; models invent believable names.
func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours *profile.Profile, check SubredditCheck) (*FillResult, error) {
func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours *profile.Profile) (*FillResult, error) {
res := &FillResult{}
st, err := site.Read(ctx, siteURL, "Supermarket/1 (inventory draft)")
if err != nil {
@@ -125,16 +119,14 @@ func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours
var keep []string
seen := map[string]bool{}
for _, sub := range d.Subreddits {
name := reddit.CleanName(sub.Name)
name := cleanSubreddit(sub.Name)
if name == "" || seen[strings.ToLower(name)] {
continue
}
seen[strings.ToLower(name)] = true
sg := checkSubreddit(ctx, check, name, sub.Why)
if sg.Kept && len(keep) < maxSubreddits {
keep = append(keep, sg.Name)
} else if sg.Kept {
sg.Kept, sg.Problem = false, "more than enough already"
sg := Suggestion{Name: name, Why: strings.TrimSpace(sub.Why), Kept: len(keep) < maxSubreddits}
if sg.Kept {
keep = append(keep, name)
}
res.Suggestions = append(res.Suggestions, sg)
}
@@ -143,34 +135,26 @@ func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours
return res, nil
}
// checkSubreddit asks Reddit about a suggested subreddit. One Reddit can't
// be asked about (no keys, rate limit) is kept, marked unchecked.
func checkSubreddit(ctx context.Context, check SubredditCheck, name, why string) Suggestion {
sg := Suggestion{Name: name, Why: strings.TrimSpace(why), Kept: true}
if check == nil {
sg.Problem = "not checked"
return sg
// cleanSubreddit turns "r/selfhosted" or a subreddit URL into
// "selfhosted", and drops what can't be a subreddit's name.
func cleanSubreddit(s string) string {
s = strings.TrimSpace(s)
if i := strings.Index(s, "/r/"); i >= 0 {
s = s[i+3:]
}
info, err := check(ctx, name)
switch {
case errors.Is(err, reddit.ErrNotFound):
sg.Kept, sg.Problem = false, "doesn't exist, or was banned"
case errors.Is(err, reddit.ErrForbidden):
sg.Kept, sg.Problem = false, "private"
case err != nil:
sg.Problem = "not checked: " + err.Error()
case info.Type == "private":
sg.Kept, sg.Problem = false, "private"
case info.Over18:
sg.Kept, sg.Problem = false, "marked 18+"
case info.Type == "restricted" || info.Type == "archived":
sg.Kept, sg.Problem = false, info.Type+": only approved accounts can post"
default:
sg.Name, sg.Info = info.Name, info // Reddit's spelling of the name
s = strings.TrimPrefix(s, "r/")
if i := strings.IndexAny(s, "/?#"); i >= 0 {
s = s[:i]
}
return sg
if !subredditName.MatchString(s) {
return ""
}
return s
}
// subredditName is what Reddit allows in a subreddit's name.
var subredditName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_]{1,20}$`)
const fillSystem = `You fill in an entry in a company's marketing inventory: another company or project it posts about, as a partner, a competitor, or one to watch. The marketing team reviews and corrects what you write.
Rules:
@@ -185,7 +169,7 @@ Rules:
Without a company profile, leave talking_points empty.
- keywords: up to eight phrases they own or compete on.
- hashtags: up to five, only ones their pages use or that name them or their product.
- subreddits: up to eight subreddits where talking about them would reach the company's audience: for a partner, where their users and the company's customers talk; for a competitor, where people compare or ask about tools like theirs; for one to watch, where their field is discussed. Names without r/, real subreddits you're confident exist, active and on topic; general ones (like technology) only if nothing narrower fits. why: one short phrase on who is there. They'll be checked against Reddit before use.`
- subreddits: up to eight subreddits where talking about them would reach the company's audience: for a partner, where their users and the company's customers talk; for a competitor, where people compare or ask about tools like theirs; for one to watch, where their field is discussed. Names without r/, real subreddits you're confident exist, active and on topic; general ones (like technology) only if nothing narrower fits. why: one short phrase on who is there. The team looks at each on Reddit before posting there.`
func fillPrompt(e *Entry, ours *profile.Profile, st *site.Site) string {
var b strings.Builder
+9 -38
View File
@@ -3,7 +3,6 @@ package inventory
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
@@ -11,7 +10,6 @@ import (
"testing"
"supermarket/internal/ai"
"supermarket/internal/api/reddit"
"supermarket/internal/profile"
)
@@ -52,7 +50,7 @@ func TestFillKeepsWhatWasTyped(t *testing.T) {
}}
e := &Entry{Name: "Rival", Relationship: Competitor, TwitterHandle: "typed"}
ours := &profile.Profile{Name: "Acme", Description: "Self-hosted widgets."}
res, err := Fill(context.Background(), fake, e, srv.URL, ours, nil)
res, err := Fill(context.Background(), fake, e, srv.URL, ours)
if err != nil {
t.Fatal(err)
}
@@ -83,7 +81,7 @@ func TestFillSkipsTheModelWhenNothingIsLeft(t *testing.T) {
fake := &fakeAI{}
e := &Entry{Name: "Rival", Relationship: Partner, Description: "d", TalkingPoints: "t",
Keywords: []string{"k"}, Hashtags: []string{"#h"}, Subreddits: []string{"selfhosted"}}
if _, err := Fill(context.Background(), fake, e, srv.URL, nil, nil); err != nil {
if _, err := Fill(context.Background(), fake, e, srv.URL, nil); err != nil {
t.Fatal(err)
}
if fake.calls != 0 {
@@ -94,58 +92,31 @@ func TestFillSkipsTheModelWhenNothingIsLeft(t *testing.T) {
}
}
func TestFillChecksSuggestedSubreddits(t *testing.T) {
func TestFillSuggestsSubreddits(t *testing.T) {
srv := rivalSite(t)
fake := &fakeAI{reply: map[string]any{
"name": "", "description": "", "talking_points": "", "keywords": []string{}, "hashtags": []string{},
"subreddits": []map[string]string{
{"name": "r/selfhosted", "why": "self-hosters"},
{"name": "SelfHosted", "why": "again"},
{"name": "madeupwidgets", "why": "invented"},
{"name": "secretclub", "why": "private"},
{"name": "announcements", "why": "restricted"},
{"name": "homelab", "why": "home servers"},
{"name": "slowreddit", "why": "rate limited"},
{"name": "not a name!", "why": "junk"},
{"name": "https://www.reddit.com/r/homelab/", "why": "home servers"},
{"name": "one", "why": ""}, {"name": "two", "why": ""}, {"name": "three", "why": ""}, {"name": "four", "why": ""},
},
}}
check := func(_ context.Context, name string) (*reddit.Subreddit, error) {
switch name {
case "selfhosted":
return &reddit.Subreddit{Name: "selfhosted", Type: "public", Subscribers: 500000}, nil
case "homelab":
return &reddit.Subreddit{Name: "homelab", Type: "public"}, nil
case "secretclub":
return nil, reddit.ErrForbidden
case "announcements":
return &reddit.Subreddit{Name: "announcements", Type: "restricted"}, nil
case "slowreddit":
return nil, errors.New("rate limited")
}
return nil, reddit.ErrNotFound
}
e := &Entry{Name: "Rival", Relationship: Partner}
res, err := Fill(context.Background(), fake, e, srv.URL, nil, check)
res, err := Fill(context.Background(), fake, e, srv.URL, nil)
if err != nil {
t.Fatal(err)
}
if got := strings.Join(e.Subreddits, ","); got != "selfhosted,homelab,slowreddit" {
if got := strings.Join(e.Subreddits, ","); got != "selfhosted,homelab,one,two,three" {
t.Errorf("subreddits = %q", got)
}
var notes []string
for _, sg := range res.Suggestions {
notes = append(notes, fmt.Sprintf("%s:%v", sg.Name, sg.Kept))
}
if got := strings.Join(notes, " "); got != "selfhosted:true madeupwidgets:false secretclub:false announcements:false homelab:true slowreddit:true" {
if got := strings.Join(notes, " "); got != "selfhosted:true homelab:true one:true two:true three:true four:false" {
t.Errorf("suggestions = %s", got)
}
if res.Suggestions[5].Problem == "" || res.Suggestions[0].Info == nil {
t.Errorf("unchecked one should say so, checked one should carry its info: %+v", res.Suggestions)
}
// Without a check, suggestions are kept, marked unchecked.
e = &Entry{Name: "Rival", Relationship: Partner}
res, _ = Fill(context.Background(), fake, e, srv.URL, nil, nil)
if len(e.Subreddits) != 5 || !strings.Contains(res.Suggestions[0].Problem, "not checked") {
t.Errorf("unchecked: %q %+v", e.Subreddits, res.Suggestions)
}
}