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:
@@ -2,19 +2,15 @@ package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/linkedin"
|
||||
"supermarket/internal/api/reddit"
|
||||
"supermarket/internal/api/rss"
|
||||
"supermarket/internal/api/twitter"
|
||||
"supermarket/internal/api/youtube"
|
||||
"supermarket/internal/content"
|
||||
"supermarket/internal/fetcher"
|
||||
"supermarket/internal/oauth"
|
||||
"supermarket/internal/secrets"
|
||||
"supermarket/internal/site"
|
||||
)
|
||||
@@ -85,140 +81,3 @@ func (p linkedInPublisher) Publish(ctx context.Context, post content.Post) (stri
|
||||
link.Title, link.Description, _ = site.PageInfo(ictx, post.SourceURL, "Supermarket/1 (link preview)")
|
||||
return c.PostUpdate(ctx, post.Text, link)
|
||||
}
|
||||
|
||||
type redditPublisher struct {
|
||||
s *Server
|
||||
c *reddit.Client
|
||||
}
|
||||
|
||||
// Publish makes a text post with the source link at the end, or, where the
|
||||
// subreddit only takes links, a link post of the title and the link.
|
||||
func (p redditPublisher) Publish(ctx context.Context, post content.Post) (string, error) {
|
||||
info, _ := p.s.subreddit(ctx, post.Target, false)
|
||||
if info != nil && !info.AllowsText() {
|
||||
if post.SourceURL == "" {
|
||||
return "", fmt.Errorf("r/%s only takes link posts, and this draft has no link", post.Target)
|
||||
}
|
||||
return p.c.Submit(ctx, reddit.Post{Subreddit: post.Target, Title: post.Title, URL: post.SourceURL})
|
||||
}
|
||||
text := post.Text
|
||||
if post.SourceURL != "" && !strings.Contains(text, post.SourceURL) {
|
||||
text += "\n\nSource: " + post.SourceURL
|
||||
}
|
||||
return p.c.Submit(ctx, reddit.Post{Subreddit: post.Target, Title: post.Title, Text: text})
|
||||
}
|
||||
|
||||
// --- Reddit ---
|
||||
|
||||
func (s *Server) redditUserAgent() string {
|
||||
name := ""
|
||||
if a := s.companyReddit(); a != nil {
|
||||
name = a.External
|
||||
}
|
||||
return reddit.UserAgent(s.Version, name)
|
||||
}
|
||||
|
||||
// redditUser is a client signed in as the company's Reddit account, or nil.
|
||||
func (s *Server) redditUser() *reddit.Client {
|
||||
a := s.companyReddit()
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
return s.redditClient(a)
|
||||
}
|
||||
|
||||
// redditReader reads subreddits: as the user if signed in, else as the
|
||||
// app (with its client ID and secret), else nil.
|
||||
func (s *Server) redditReader(ctx context.Context) (*reddit.Client, error) {
|
||||
if c := s.redditUser(); c != nil {
|
||||
return c, nil
|
||||
}
|
||||
id, secret := s.Secrets.Get(secrets.RedditClientID), s.Secrets.Get(secrets.RedditClientSecret)
|
||||
if id == "" || secret == "" {
|
||||
return nil, errors.New("add Reddit's client ID and secret under Settings → Social accounts to read subreddits")
|
||||
}
|
||||
tc := s.cache()
|
||||
tc.mu.Lock()
|
||||
tok, exp := tc.redditAppToken, tc.redditAppTokenExp
|
||||
tc.mu.Unlock()
|
||||
if tok == "" || time.Now().After(exp) {
|
||||
res, err := oauth.RedditAppToken(ctx, id, secret, s.redditUserAgent())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tok = res.AccessToken
|
||||
tc.mu.Lock()
|
||||
// Reddit's app tokens last an hour; renew a little early.
|
||||
tc.redditAppToken, tc.redditAppTokenExp = tok, time.Now().Add(50*time.Minute)
|
||||
tc.mu.Unlock()
|
||||
}
|
||||
return reddit.New(tok, s.redditUserAgent(), nil), nil
|
||||
}
|
||||
|
||||
// subreddit returns a subreddit's rules: cached if fresh enough (or if
|
||||
// fetching fails), fetched otherwise. refresh forces a fetch.
|
||||
func (s *Server) subreddit(ctx context.Context, name string, refresh bool) (*reddit.Subreddit, error) {
|
||||
name = reddit.CleanName(name)
|
||||
cached, err := content.GetSubreddit(s.DB, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cached != nil && !refresh && time.Since(cached.FetchedAt) < content.SubredditMaxAge {
|
||||
return cached, nil
|
||||
}
|
||||
c, err := s.redditReader(ctx)
|
||||
if err == nil {
|
||||
var info *reddit.Subreddit
|
||||
if info, err = c.SubredditInfo(ctx, name); err == nil {
|
||||
if serr := content.SaveSubreddit(s.DB, info); serr != nil {
|
||||
s.Log.Error("caching subreddit", "name", name, "err", serr)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
}
|
||||
if cached != nil {
|
||||
return cached, nil // stale beats nothing
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// redditHistoryTTL is how long the account's recent posts are kept in
|
||||
// memory for the warnings.
|
||||
const redditHistoryTTL = 10 * time.Minute
|
||||
|
||||
// redditHistory is the signed-in account's recent posts, or nil if they
|
||||
// can't be read.
|
||||
func (s *Server) redditHistory(ctx context.Context) []reddit.Submission {
|
||||
a := s.companyReddit()
|
||||
if a == nil || a.External == "" {
|
||||
return nil
|
||||
}
|
||||
user, c := a.External, s.redditClient(a)
|
||||
tc := s.cache()
|
||||
tc.mu.Lock()
|
||||
h, at := tc.redditHist, tc.redditHistAt
|
||||
tc.mu.Unlock()
|
||||
if h != nil && time.Since(at) < redditHistoryTTL {
|
||||
return h
|
||||
}
|
||||
h, err := c.Submitted(ctx, user)
|
||||
if err != nil {
|
||||
s.Log.Warn("reddit: couldn't read the account's posts", "err", err)
|
||||
return nil
|
||||
}
|
||||
if h == nil {
|
||||
h = []reddit.Submission{}
|
||||
}
|
||||
tc.mu.Lock()
|
||||
tc.redditHist, tc.redditHistAt = h, time.Now()
|
||||
tc.mu.Unlock()
|
||||
return h
|
||||
}
|
||||
|
||||
// forgetRedditHistory drops the cached posts after posting.
|
||||
func (s *Server) forgetRedditHistory() {
|
||||
tc := s.cache()
|
||||
tc.mu.Lock()
|
||||
tc.redditHist = nil
|
||||
tc.mu.Unlock()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user