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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user