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:
2026-11-21 10:31:04 -07:00
co-authored by Claude Opus 5.5
parent 6f396d3ff6
commit d9b1467365
7 changed files with 233 additions and 22 deletions
+14 -2
View File
@@ -54,6 +54,13 @@ func UserAgent(version, username string) string {
// ErrUnauthorized means the token was refused even after a refresh.
var ErrUnauthorized = errors.New("Reddit refused the sign-in: connect Reddit again under Settings → Social accounts")
// ErrNotFound and ErrForbidden are what Reddit answers for a subreddit or
// user that doesn't exist (or was banned), and for a private one.
var (
ErrNotFound = errors.New("Reddit said 404: no such subreddit or user")
ErrForbidden = errors.New("Reddit said 403 Forbidden: the subreddit may be private, or the account banned from it")
)
func (c *Client) do(ctx context.Context, method, path string, form url.Values, out any) error {
for attempt := 0; ; attempt++ {
var body io.Reader
@@ -87,9 +94,9 @@ func (c *Client) do(ctx context.Context, method, path string, form url.Values, o
case res.StatusCode == http.StatusUnauthorized:
return ErrUnauthorized
case res.StatusCode == http.StatusForbidden:
return fmt.Errorf("Reddit said 403 Forbidden: the subreddit may be private, or the account banned from it")
return ErrForbidden
case res.StatusCode == http.StatusNotFound:
return fmt.Errorf("Reddit said 404: no such subreddit or user")
return ErrNotFound
case res.StatusCode == http.StatusTooManyRequests:
return fmt.Errorf("Reddit is rate-limiting this app; try again in a few minutes")
case res.StatusCode != http.StatusOK:
@@ -214,6 +221,11 @@ func (c *Client) SubredditInfo(ctx context.Context, name string) (*Subreddit, er
if err := c.do(ctx, http.MethodGet, "/r/"+url.PathEscape(name)+"/about", nil, &about); err != nil {
return nil, err
}
if about.Data.DisplayName == "" {
// Reddit sometimes answers a missing subreddit with an empty listing
// rather than a 404.
return nil, ErrNotFound
}
var rules struct {
Rules []struct {
ShortName string `json:"short_name"`