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:
+28
-44
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user