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:
2026-11-30 11:48:54 -07:00
co-authored by Claude Opus 5.5
parent bae468e01c
commit 5451fe6d63
33 changed files with 382 additions and 1107 deletions
+49 -52
View File
@@ -10,7 +10,6 @@ import (
"strings"
"time"
"supermarket/internal/api/reddit"
"supermarket/internal/content"
"supermarket/internal/profile"
"supermarket/internal/utm"
@@ -109,7 +108,7 @@ func (s *Server) renderInbox(w http.ResponseWriter, r *http.Request, errMsg stri
s.render(w, r, "inbox", page{Title: "Posts", Nav: "content/posts", Error: errMsg, Data: map[string]any{
"Reminders": s.reminders(),
"Sections": sections,
"Reddit": s.redditViews(r.Context(), pending),
"Reddit": s.redditViews(pending),
"Connected": s.connected(viewerOf(r)),
"Accounts": s.usableAccounts(viewerOf(r)),
"Chosen": s.chosenAccounts(pending),
@@ -121,52 +120,47 @@ func (s *Server) renderInbox(w http.ResponseWriter, r *http.Request, errMsg stri
// redditView is what Posts shows with a Reddit draft.
type redditView struct {
Info *reddit.Subreddit
Info *content.Subreddit // the rules pasted; nil if none
RulesURL string
Suffix string // added after the text when it's copied: the source link
Warnings []string
}
// redditViews works out the rules and warnings for the Reddit drafts. It
// reads the account's recent posts once (cached for a few minutes).
func (s *Server) redditViews(ctx context.Context, drafts []content.Draft) map[int64]*redditView {
// redditViews works out the rules and warnings for the Reddit drafts.
func (s *Server) redditViews(drafts []content.Draft) map[int64]*redditView {
views := map[int64]*redditView{}
var history []reddit.Submission
historyRead := false
tags := s.tagger()
recorded, err := content.RecordedRedditPosts(s.DB, time.Now().Add(-8*24*time.Hour))
if err != nil {
s.Log.Error("reading recorded Reddit posts", "err", err)
}
for _, d := range drafts {
for i, d := range drafts {
if d.Channel != content.ChannelReddit {
continue
}
if !historyRead {
hctx, cancel := context.WithTimeout(ctx, 8*time.Second)
history = s.redditHistory(hctx)
cancel()
historyRead = true
info, err := content.GetSubreddit(s.DB, d.Target)
if err != nil {
s.Log.Error("reading a subreddit's rules", "subreddit", d.Target, "err", err)
}
info, _ := content.GetSubreddit(s.DB, d.Target)
check := content.RedditCheck{Target: d.Target, HasLink: d.SourceURL != "", Info: info, History: history, Now: time.Now(),
check := content.RedditCheck{Target: d.Target, Info: info, Now: time.Now(),
Promo: d.Kind == content.KindProduct, Recorded: recorded}
if tags != nil {
check.Own = tags.Own
check.OwnLink = tags.Own(d.SourceURL) || tags.HasOwnLink(d.Content)
}
views[d.ID] = &redditView{Info: info, Warnings: content.RedditWarnings(check)}
views[d.ID] = &redditView{Info: info, RulesURL: content.RulesURL(d.Target),
Suffix: redditSuffix(&drafts[i], tags), Warnings: content.RedditWarnings(check)}
}
return views
}
// refreshRules fetches a subreddit's rules again.
func (s *Server) refreshRules(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
if _, err := s.subreddit(ctx, r.PathValue("name"), true); err != nil {
s.renderInbox(w, r, "Couldn't read the subreddit's rules: "+err.Error())
// saveRules keeps the rules pasted for a subreddit.
func (s *Server) saveRules(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
redirect(w, r, "/posts", "rules-refreshed")
if err := content.SaveRules(s.DB, r.PathValue("name"), r.PostForm.Get("rules"), time.Now()); err != nil {
s.renderInbox(w, r, "Couldn't save the rules: "+err.Error())
return
}
redirect(w, r, "/posts", "rules-saved")
}
// The Content module's own lists of approved and published drafts.
@@ -308,7 +302,6 @@ func (s *Server) draftAction(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
var failed []string
reddit := false
for _, did := range append([]int64{id}, copies...) {
_ = content.SetPostedBy(s.DB, did, by)
d, err := content.Publish(ctx, s.DB, s.publishers(), s.tagger(), did)
@@ -320,13 +313,9 @@ func (s *Server) draftAction(w http.ResponseWriter, r *http.Request) {
failed = append(failed, fmt.Sprintf("Couldn't publish to %s: %v", name, err))
continue
}
reddit = reddit || d.Channel == content.ChannelReddit
s.Log.Info("content: published", "draft", did, "as", d.PostedAs, "by", by)
s.audit(r, "published", d.PostedAs, "post "+strconv.FormatInt(did, 10)+": "+d.PostURL)
}
if reddit {
s.forgetRedditHistory()
}
if len(failed) > 0 {
fail(errors.New(strings.Join(failed, " ")))
return
@@ -363,8 +352,7 @@ func (s *Server) draftAction(w http.ResponseWriter, r *http.Request) {
fail(err)
return
}
info, _ := content.GetSubreddit(s.DB, d.Target)
http.Redirect(w, r, redditSubmitURL(d, s.tagger(), info), http.StatusSeeOther)
http.Redirect(w, r, redditSubmitURL(d, s.tagger()), http.StatusSeeOther)
case "posted":
postURL := strings.TrimSpace(r.PostForm.Get("post_url"))
if postURL != "" {
@@ -377,7 +365,6 @@ func (s *Server) draftAction(w http.ResponseWriter, r *http.Request) {
fail(err)
return
}
s.forgetRedditHistory()
redirect(w, r, back, "posted")
case "unschedule":
if err := content.Unschedule(s.DB, id); err != nil {
@@ -484,26 +471,36 @@ func (s *Server) publishDue(ctx context.Context, now time.Time) {
// and still open; past it, the text is left for the user to paste.
const maxSubmitURL = 7500
// redditSubmitURL is Reddit's submit page for the draft's subreddit, filled
// in: a text post with the source link at the end, or a link post where the
// subreddit only takes links. It uses old.reddit.com, whose submit page
// reliably takes the title and text from the address.
func redditSubmitURL(d *content.Draft, tags *utm.Tagger, info *reddit.Subreddit) string {
// redditText is what goes in a Reddit post: the draft's text with links to
// the company's sites tagged, and the source link at the end.
func redditText(d *content.Draft, tags *utm.Tagger) string {
text, src := content.Manual(d, tags)
base := "https://old.reddit.com/submit"
if sub := reddit.CleanName(d.Target); sub != "" {
base = "https://old.reddit.com/r/" + url.PathEscape(sub) + "/submit"
}
q := url.Values{"title": {d.Title}}
if info != nil && !info.AllowsText() && src != "" {
q.Set("url", src)
return base + "?" + q.Encode()
}
if src != "" && !strings.Contains(text, src) {
text += "\n\nSource: " + src
}
q.Set("selftext", "true")
q.Set("text", text)
return text
}
// redditSuffix is what Copy text adds after the text on screen: the source
// link, tagged, unless the text already has it.
func redditSuffix(d *content.Draft, tags *utm.Tagger) string {
text, src := content.Manual(d, tags)
if src == "" || strings.Contains(text, src) {
return ""
}
return "\n\nSource: " + src
}
// redditSubmitURL is Reddit's submit page for the draft's subreddit, filled
// in as a text post. It uses old.reddit.com, whose submit page reliably
// takes the title and text from the address. Text too long for an address
// is left out, for the user to paste.
func redditSubmitURL(d *content.Draft, tags *utm.Tagger) string {
base := "https://old.reddit.com/submit"
if sub := content.CleanSubreddit(d.Target); sub != "" {
base = "https://old.reddit.com/r/" + url.PathEscape(sub) + "/submit"
}
q := url.Values{"title": {d.Title}, "selftext": {"true"}, "text": {redditText(d, tags)}}
if u := base + "?" + q.Encode(); len(u) <= maxSubmitURL {
return u
}