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
+53 -6
View File
@@ -19,8 +19,12 @@ import (
const (
ChannelX = "x"
ChannelLinkedIn = "linkedin"
ChannelReddit = "reddit"
)
// RedditTitleLimit is the longest title Reddit takes.
const RedditTitleLimit = 300
// Channel describes where drafts are published.
type Channel struct {
ID string
@@ -32,6 +36,7 @@ type Channel struct {
var Channels = []Channel{
{ChannelX, "X", 280},
{ChannelLinkedIn, "LinkedIn", 3000},
{ChannelReddit, "Reddit", 40000},
}
// ChannelByID returns the channel, or false.
@@ -61,6 +66,8 @@ type Draft struct {
InventoryName string
Kind string // the entry's relationship
Channel string
Title string // Reddit only
Target string // Reddit: the subreddit, without r/
Content string
SourceURL string // added when publishing: appended on X, a link preview on LinkedIn
ItemIDs []string
@@ -94,7 +101,36 @@ func (d *Draft) Limit() int {
}
// TooLong reports whether the draft can't be published as it is.
func (d *Draft) TooLong() bool { return d.Length() > d.Limit() }
func (d *Draft) TooLong() bool {
return d.Length() > d.Limit() || utf8.RuneCountInString(d.Title) > RedditTitleLimit
}
// Problem says what stops the draft from being published, or "".
func (d *Draft) Problem() string {
switch {
case d.Channel == ChannelReddit && strings.TrimSpace(d.Title) == "":
return "the Reddit post needs a title"
case d.Channel == ChannelReddit && d.Target == "":
return "the Reddit post has no subreddit"
case utf8.RuneCountInString(d.Title) > RedditTitleLimit:
return fmt.Sprintf("the title is %d characters; Reddit allows %d", utf8.RuneCountInString(d.Title), RedditTitleLimit)
case d.Length() > d.Limit():
withLink := ""
if d.Channel == ChannelX && d.SourceURL != "" {
withLink = " including the link"
}
return fmt.Sprintf("the post is %d characters%s; %s allows %d", d.Length(), withLink, d.ChannelName(), d.Limit())
}
return ""
}
// Where is the channel plus, for Reddit, the subreddit: "Reddit r/golang".
func (d *Draft) Where() string {
if d.Target != "" {
return d.ChannelName() + " r/" + d.Target
}
return d.ChannelName()
}
// ChannelName is the channel's display name.
func (d *Draft) ChannelName() string {
@@ -131,9 +167,9 @@ func CreateDrafts(d *db.DB, drafts []Draft) (string, error) {
return "", fmt.Errorf("unknown channel %q", dr.Channel)
}
ids, _ := json.Marshal(nonNil(dr.ItemIDs))
if _, err := tx.Exec(`INSERT INTO drafts (batch, inventory_name, kind, channel, content, source_url, item_ids)
VALUES (?, ?, ?, ?, ?, ?, ?)`, batch, dr.InventoryName, dr.Kind, dr.Channel,
strings.TrimSpace(dr.Content), dr.SourceURL, string(ids)); err != nil {
if _, err := tx.Exec(`INSERT INTO drafts (batch, inventory_name, kind, channel, title, target, content, source_url, item_ids)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, batch, dr.InventoryName, dr.Kind, dr.Channel,
strings.TrimSpace(dr.Title), dr.Target, strings.TrimSpace(dr.Content), dr.SourceURL, string(ids)); err != nil {
return "", err
}
for _, id := range dr.ItemIDs {
@@ -157,7 +193,7 @@ func nonNil(s []string) []string {
}
const draftColumns = `id, batch, inventory_name, kind, channel, content, source_url, item_ids, status, error,
post_url, created_at, updated_at, COALESCE(published_at, ''), COALESCE(publish_at, '')`
post_url, created_at, updated_at, COALESCE(published_at, ''), COALESCE(publish_at, ''), title, target`
func scanDrafts(rows *sql.Rows) ([]Draft, error) {
defer rows.Close()
@@ -166,7 +202,7 @@ func scanDrafts(rows *sql.Rows) ([]Draft, error) {
var dr Draft
var ids, created, updated, published, publishAt string
if err := rows.Scan(&dr.ID, &dr.Batch, &dr.InventoryName, &dr.Kind, &dr.Channel, &dr.Content,
&dr.SourceURL, &ids, &dr.Status, &dr.Error, &dr.PostURL, &created, &updated, &published, &publishAt); err != nil {
&dr.SourceURL, &ids, &dr.Status, &dr.Error, &dr.PostURL, &created, &updated, &published, &publishAt, &dr.Title, &dr.Target); err != nil {
return nil, err
}
_ = json.Unmarshal([]byte(ids), &dr.ItemIDs)
@@ -320,3 +356,14 @@ func FailScheduled(d *db.DB, id int64, reason string) error {
StatusFailed, reason, formatTime(time.Now()), id, StatusScheduled)
return oneRow(res, err)
}
// UpdateTitle saves an edited Reddit title.
func UpdateTitle(d *db.DB, id int64, title string) error {
title = strings.Join(strings.Fields(title), " ")
if title == "" {
return errors.New("the title is empty")
}
res, err := d.Exec(`UPDATE drafts SET title = ?, updated_at = ? WHERE id = ? AND channel = ? AND status IN (?, ?)`,
title, formatTime(time.Now()), id, ChannelReddit, StatusDraft, StatusFailed)
return oneRow(res, err)
}