Suggest subreddits when filling in an inventory entry, checked with Reddit
If the Subreddits box is empty, the AI suggests up to eight, by the relationship, each with who is there. Each is looked up on Reddit (with the app's client ID and secret) and left out if it doesn't exist, is private, 18+, restricted or archived; the first five kept fill the box. The form lists every suggestion with members, post types and why any was left out. Lookups are cached for drafting. Without Reddit keys the suggestions are kept and the form says they weren't checked. The Reddit client now returns ErrNotFound and ErrForbidden, and treats an empty about listing as not found. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
||||
|
||||
"supermarket/examples"
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/api/reddit"
|
||||
"supermarket/internal/content"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/profile"
|
||||
)
|
||||
@@ -35,6 +37,8 @@ type entryData struct {
|
||||
NoAI bool // no AI provider yet: filling in from the website can't work
|
||||
DraftURL string // the address to fill in from
|
||||
Filled []string // after filling in: what was filled
|
||||
Suggestions []inventory.Suggestion
|
||||
CanCheck bool // Reddit could be asked about the suggestions
|
||||
Drafted bool
|
||||
}
|
||||
|
||||
@@ -148,8 +152,10 @@ func (s *Server) inventoryFill(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Minute)
|
||||
defer cancel()
|
||||
filled, err := inventory.Fill(ctx, client, e, d.DraftURL, ours)
|
||||
d.Filled, d.Drafted = filled, true
|
||||
check := s.subredditCheck(ctx)
|
||||
d.CanCheck = check != nil
|
||||
res, err := inventory.Fill(ctx, client, e, d.DraftURL, ours, check)
|
||||
d.Filled, d.Suggestions, d.Drafted = res.Filled, res.Suggestions, true
|
||||
if err != nil {
|
||||
// Links read before the model failed are kept.
|
||||
show("Couldn't fill in everything: " + err.Error())
|
||||
@@ -243,3 +249,27 @@ func (s *Server) inventoryExport(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="supermarket-inventory.yaml"`)
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
// subredditCheck looks subreddits up on Reddit for Fill, using the cache
|
||||
// and caching what it reads (drafting for the subreddit uses it later). It's
|
||||
// nil if Reddit can't be asked: no client ID and secret.
|
||||
func (s *Server) subredditCheck(ctx context.Context) inventory.SubredditCheck {
|
||||
c, err := s.redditReader(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return func(ctx context.Context, name string) (*reddit.Subreddit, error) {
|
||||
if cached, err := content.GetSubreddit(s.DB, name); err == nil && cached != nil &&
|
||||
time.Since(cached.FetchedAt) < content.SubredditMaxAge {
|
||||
return cached, nil
|
||||
}
|
||||
info, err := c.SubredditInfo(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := content.SaveSubreddit(s.DB, info); err != nil {
|
||||
s.Log.Error("caching subreddit", "name", name, "err", err)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,3 +349,12 @@ form.inline { display: inline-block; margin: 0 0 0.75rem; }
|
||||
.results .detail { display: block; color: hsl(var(--muted-foreground)); font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.search-page { display: flex; gap: 0.5rem; margin-bottom: 1.25rem; max-width: 40rem; }
|
||||
.search-page input { margin: 0; }
|
||||
|
||||
/* Subreddits suggested when filling in an inventory entry */
|
||||
.suggestions { margin-top: 0.75rem; font-size: 0.92rem; }
|
||||
.suggestions summary { cursor: pointer; font-weight: 600; }
|
||||
.suggestions ul { list-style: none; padding: 0; margin: 0.5rem 0; }
|
||||
.suggestions li { padding: 0.35rem 0; border-bottom: 1px solid hsl(var(--border)); display: flex; flex-wrap: wrap; gap: 0 0.6rem; align-items: baseline; }
|
||||
.suggestions li:last-child { border-bottom: 0; }
|
||||
.suggestions li .help { flex-basis: 100%; margin: 0; }
|
||||
.suggestions li.dropped a { text-decoration: line-through; color: hsl(var(--muted-foreground)); }
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<input type="hidden" name="id" value="{{$e.ID}}">
|
||||
<section class="card">
|
||||
<h2>Fill in with AI</h2>
|
||||
<p class="help">Reads their website and fills in the boxes that are still empty: links and accounts straight from the pages, and a description, talking points, keywords and hashtags from the AI, set against your <a href="/settings/profile">company profile</a>. What you've typed stays. Nothing is saved until you press Save.</p>
|
||||
<p class="help">Reads their website and fills in the boxes that are still empty: links and accounts straight from the pages, and a description, talking points, keywords, hashtags and subreddits from the AI (subreddits checked with Reddit first), set against your <a href="/settings/profile">company profile</a>. What you've typed stays. Nothing is saved until you press Save.</p>
|
||||
{{if .NoAI}}<p class="warn-text">This needs the AI: first <a href="/settings/ai">set up the AI</a>, then come back. Or fill in the form by hand.</p>{{end}}
|
||||
<div class="row">
|
||||
<input name="draft_url" type="text" inputmode="url" value="{{.DraftURL}}" placeholder="https://example.com" aria-label="Their website">
|
||||
@@ -14,6 +14,23 @@
|
||||
</div>
|
||||
<p class="help">Takes up to a minute or two.</p>
|
||||
{{if .Drafted}}{{if .Filled}}<p class="flash">Filled in: {{join .Filled ", "}}. Check them, correct what's wrong, then Save.</p>{{else}}<p class="muted">Nothing to fill in: every box the website could answer is already filled, or the site doesn't say.</p>{{end}}{{end}}
|
||||
{{with .Suggestions}}
|
||||
<details class="suggestions" open>
|
||||
<summary>Subreddits the AI suggested</summary>
|
||||
{{if not $.Data.CanCheck}}<p class="warn-text small">Not checked with Reddit, so some may not exist: add Reddit's client ID and secret under <a href="/settings/accounts">Settings → Social accounts</a> and fill in again to check them.</p>{{end}}
|
||||
<ul>
|
||||
{{range .}}
|
||||
<li class="{{if .Kept}}kept{{else}}dropped{{end}}">
|
||||
<a href="https://www.reddit.com/r/{{.Name}}/" target="_blank" rel="noopener noreferrer">r/{{.Name}}</a>
|
||||
{{with .Info}}<span class="muted small">{{.Subscribers}} members · {{if eq .SubmissionType "self"}}text posts only{{else if eq .SubmissionType "link"}}link posts only{{else}}text or links{{end}}</span>{{end}}
|
||||
{{if .Kept}}{{if and .Problem $.Data.CanCheck}}<span class="warn-text small">{{.Problem}}</span>{{end}}{{else}}<span class="small">left out: {{.Problem}}</span>{{end}}
|
||||
{{with .Why}}<span class="help">{{.}}</span>{{end}}
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
<p class="help">The ones kept are in the Subreddits box below. Look at each on Reddit before posting there: the AI picks by topic, not by how a community treats companies.</p>
|
||||
</details>
|
||||
{{end}}
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
|
||||
Reference in New Issue
Block a user