Phase 1, part 3: Reddit

- Sign in with a Reddit web app; access tokens refresh automatically,
  and an app-only token reads rules before sign-in.
- Reddit drafts: a title and text for one subreddit, chosen when
  writing. The subreddit's guidelines and rules go into the prompt, which
  asks for a community member's post that discloses the affiliation and
  avoids marketing language.
- The Inbox shows the subreddit's rules (cached for a day, with Refresh)
  and warns, without blocking, about promotion rules, restricted
  subreddits, link-only subreddits, posting in the same subreddit within
  a week, more than 1 in 10 recent posts linking to the company's sites,
  and new accounts.
- Publishing makes a text post with the source link at the end, or a
  link post where the subreddit takes only links.
- Migration 4 adds the draft's title and subreddit, and the rules cache.
  Publishers now take a Post, which carries the title and subreddit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 13:51:16 -07:00
co-authored by Claude Opus 5.5
parent b0f06058ec
commit 0e34f574b7
27 changed files with 1404 additions and 91 deletions
+25 -14
View File
@@ -14,6 +14,7 @@ import (
"sync"
"time"
"supermarket/internal/api/reddit"
"supermarket/internal/api/twitter"
"supermarket/internal/core"
"supermarket/internal/db"
@@ -49,6 +50,13 @@ type Server struct {
mu sync.Mutex
pending map[string]*oauth.Pending // OAuth flows by state
// Reddit: an app-only token for reading rules before sign-in, and the
// account's recent posts for the warnings.
redditAppToken string
redditAppTokenExp time.Time
redditHist []reddit.Submission
redditHistAt time.Time
}
// Handler builds the routes.
@@ -102,6 +110,7 @@ func (s *Server) Handler() (http.Handler, error) {
redirect(w, r, "/inbox", r.URL.Query().Get("msg"))
})
mux.HandleFunc("POST /drafts/{id}/{action}", s.draftAction)
mux.HandleFunc("POST /reddit/rules/{name}", s.refreshRules)
mux.HandleFunc("GET /core", s.corePage)
mux.HandleFunc("GET /core/checks", s.coreChecks)
@@ -131,7 +140,7 @@ func (s *Server) guard(next http.Handler) http.Handler {
h.Set("X-Frame-Options", "DENY")
h.Set("X-Content-Type-Options", "nosniff")
h.Set("Referrer-Policy", "same-origin")
h.Set("Content-Security-Policy", "default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; form-action 'self' https://twitter.com https://x.com https://www.linkedin.com; frame-ancestors 'none'")
h.Set("Content-Security-Policy", "default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; form-action 'self' https://twitter.com https://x.com https://www.linkedin.com https://www.reddit.com; frame-ancestors 'none'")
next.ServeHTTP(w, r)
})
}
@@ -243,19 +252,21 @@ func (s *Server) fragment(w http.ResponseWriter, page, name string, data any) {
// Messages shown after a redirect, by code, so no user text is reflected.
var flashes = map[string]string{
"saved": "Saved.",
"deleted": "Deleted.",
"x-connected": "X is connected.",
"li-connected": "LinkedIn is connected.",
"imported": "Imported.",
"oauth-expired": "That sign-in took too long or was already used. Try again.",
"dismissed": "Dismissed.",
"restored": "Restored.",
"drafted": "Drafts written. Review them below.",
"published": "Published.",
"discarded": "Discarded.",
"scheduled": "Scheduled.",
"unscheduled": "Back in the Inbox: edit it, then publish or schedule it again.",
"saved": "Saved.",
"deleted": "Deleted.",
"x-connected": "X is connected.",
"li-connected": "LinkedIn is connected.",
"reddit-connected": "Reddit is connected.",
"rules-refreshed": "Rules refreshed.",
"imported": "Imported.",
"oauth-expired": "That sign-in took too long or was already used. Try again.",
"dismissed": "Dismissed.",
"restored": "Restored.",
"drafted": "Drafts written. Review them below.",
"published": "Published.",
"discarded": "Discarded.",
"scheduled": "Scheduled.",
"unscheduled": "Back in the Inbox: edit it, then publish or schedule it again.",
}
func redirect(w http.ResponseWriter, r *http.Request, path, msg string) {