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
+29 -26
View File
@@ -11,7 +11,6 @@ import (
"testing"
"time"
"supermarket/internal/api/reddit"
"supermarket/internal/content"
"supermarket/internal/inventory"
"supermarket/internal/models"
@@ -210,17 +209,22 @@ func TestRedditInInbox(t *testing.T) {
}); err != nil {
t.Fatal(err)
}
if err := content.SaveSubreddit(s.DB, &reddit.Subreddit{Name: "selfhosted", SubmitText: "Flair it.", SubmissionType: "self",
Rules: []reddit.Rule{{Name: "No self-promotion", Description: "Limit it."}}, FetchedAt: time.Now()}); err != nil {
if err := content.SaveRules(s.DB, "selfhosted", "1. Flair it.\n2. No self-promotion", time.Now()); err != nil {
t.Fatal(err)
}
body := do(h, http.MethodGet, "/posts", nil, nil).Body.String()
for _, want := range []string{"Reddit r/selfhosted", `name="title" value="Jitsi 3"`, "Flair it.", "No self-promotion",
"has a rule about this", "Couldn&#39;t read r/homelab&#39;s rules", `action="/reddit/rules/homelab"`, "Read rules", "text only"} {
for _, want := range []string{"Reddit r/selfhosted", `name="title" id="title-`, `value="Jitsi 3"`, "1. Flair it.", "has a rule about this",
"r/homelab's rules · not pasted yet", `action="/reddit/rules/homelab"`, `href="https://www.reddit.com/r/homelab/about/rules"`,
"Copy title", `data-copy-suffix="
Source: https://jitsi.example/3"`} {
if !strings.Contains(body, want) {
t.Errorf("inbox lacks %q", want)
}
}
if strings.Contains(body, "Publish now") || strings.Contains(body, "Schedule</button>") {
t.Error("Reddit drafts mustn't offer Publish now")
}
ds, _ := content.ListDrafts(s.DB, 10, content.StatusDraft)
var id string
@@ -238,22 +242,28 @@ func TestRedditInInbox(t *testing.T) {
if rec := postForm(h, "/drafts/"+id+"/save", url.Values{"title": {""}}); !strings.Contains(rec.Body.String(), "The title is empty") {
t.Errorf("empty title: %s", rec.Body.String())
}
if rec := postForm(h, "/drafts/"+id+"/publish", nil); !strings.Contains(rec.Body.String(), "Reddit isn&#39;t connected") {
t.Errorf("publish without Reddit: %s", rec.Body.String())
for _, action := range []string{"publish", "schedule"} {
if rec := postForm(h, "/drafts/"+id+"/"+action, url.Values{"when": {time.Now().Add(time.Hour).Format("2006-01-02T15:04")}}); !strings.Contains(rec.Body.String(), "posted by hand") {
t.Errorf("%s a Reddit draft: %s", action, rec.Body.String())
}
}
// Reading rules needs Reddit credentials.
if rec := postForm(h, "/reddit/rules/homelab", nil); !strings.Contains(rec.Body.String(), "client ID and secret") {
t.Errorf("refresh without credentials: %d %s", rec.Code, rec.Body.String())
// Pasting the rules keeps them; emptying the box forgets them.
if rec := postForm(h, "/reddit/rules/homelab", url.Values{"rules": {"No memes."}}); rec.Code != http.StatusSeeOther {
t.Fatalf("save rules: %d %s", rec.Code, rec.Body.String())
}
// Signing in needs the client ID and secret first.
if rec := postForm(h, "/auth/reddit/start", nil); rec.Code != http.StatusBadRequest {
t.Errorf("reddit start without credentials: %d", rec.Code)
if sr, _ := content.GetSubreddit(s.DB, "homelab"); sr == nil || sr.Rules != "No memes." {
t.Errorf("rules = %+v", sr)
}
_ = s.Secrets.Set(map[string]string{secrets.RedditClientID: "id", secrets.RedditClientSecret: "sec"})
rec := postForm(h, "/auth/reddit/start", nil)
if loc := rec.Header().Get("Location"); rec.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "https://www.reddit.com/api/v1/authorize?") ||
!strings.Contains(loc, "duration=permanent") || !strings.Contains(loc, "submit") {
t.Errorf("reddit start: %d %s", rec.Code, loc)
postForm(h, "/reddit/rules/homelab", url.Values{"rules": {""}})
if sr, _ := content.GetSubreddit(s.DB, "homelab"); sr != nil {
t.Error("emptied rules kept")
}
// Reddit has no sign-in any more.
if rec := postForm(h, "/auth/reddit/start", nil); rec.Code != http.StatusNotFound {
t.Errorf("reddit start: %d", rec.Code)
}
if strings.Contains(do(h, http.MethodGet, "/settings/accounts", nil, nil).Body.String(), "network-Reddit") {
t.Error("Social accounts still has a Reddit card")
}
}
@@ -328,14 +338,13 @@ func TestPostRedditByHand(t *testing.T) {
}); err != nil {
t.Fatal(err)
}
_ = content.SaveSubreddit(s.DB, &reddit.Subreddit{Name: "linksonly", SubmissionType: "link", FetchedAt: time.Now()})
ids := map[string]string{}
ds, _ := content.ListDrafts(s.DB, 10, content.StatusDraft)
for _, d := range ds {
ids[d.Target] = strconv.FormatInt(d.ID, 10)
}
if body := do(h, http.MethodGet, "/posts", nil, nil).Body.String(); !strings.Contains(body, "Open in Reddit") ||
!strings.Contains(body, "can't post to Reddit for you yet") || !strings.Contains(body, `action="/drafts/`+ids["selfhosted"]+`/posted"`) {
!strings.Contains(body, "Post it yourself") || !strings.Contains(body, `action="/drafts/`+ids["selfhosted"]+`/posted"`) {
t.Error("inbox lacks the by-hand controls")
}
@@ -352,12 +361,6 @@ func TestPostRedditByHand(t *testing.T) {
t.Errorf("open should save the edits and leave it a draft: %+v", d)
}
// A links-only subreddit gets a link post.
loc, _ = url.Parse(postForm(h, "/drafts/"+ids["linksonly"]+"/open", nil).Header().Get("Location"))
if q := loc.Query(); q.Get("text") != "" || !strings.HasPrefix(q.Get("url"), "https://acme.example/w?") {
t.Errorf("link post: %s", loc)
}
// Mark as posted: a bad link is refused, a good one recorded.
if rec := postForm(h, "/drafts/"+ids["selfhosted"]+"/posted", url.Values{"post_url": {"not a link"}}); !strings.Contains(rec.Body.String(), "isn&#39;t a link to a post") {
t.Error("bad post link accepted")