Reddit warnings: count posts recorded by Supermarket when Reddit can't be asked
Without Reddit's API (no approved app), the account's history is unknown, so posts marked as posted didn't count. Now the Reddit posts Supermarket published or recorded feed the "posted in this subreddit in the past week" warning in that case. They aren't the whole account, so the own-link share and new-account warnings still need the API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -64,8 +64,13 @@ type RedditCheck struct {
|
|||||||
Promo bool // it promotes the company's own product
|
Promo bool // it promotes the company's own product
|
||||||
Info *reddit.Subreddit // nil if the rules couldn't be read
|
Info *reddit.Subreddit // nil if the rules couldn't be read
|
||||||
History []reddit.Submission // the account's recent posts; nil if unknown
|
History []reddit.Submission // the account's recent posts; nil if unknown
|
||||||
Own func(url string) bool
|
// Recorded is the Reddit posts Supermarket knows of (published or
|
||||||
Now time.Time
|
// marked as posted), used for the same-subreddit check when History
|
||||||
|
// is unknown. They're not the whole account, so they don't count for
|
||||||
|
// the own-link share or the new-account warning.
|
||||||
|
Recorded []reddit.Submission
|
||||||
|
Own func(url string) bool
|
||||||
|
Now time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedditWarnings lists reasons to think twice before posting. They never
|
// RedditWarnings lists reasons to think twice before posting. They never
|
||||||
@@ -95,6 +100,16 @@ func RedditWarnings(c RedditCheck) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if c.History == nil {
|
if c.History == nil {
|
||||||
|
var inSub []time.Time
|
||||||
|
for _, s := range c.Recorded {
|
||||||
|
if strings.EqualFold(s.Subreddit, c.Target) && c.Now.Sub(s.Created) < sameSubWindow {
|
||||||
|
inSub = append(inSub, s.Created)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(inSub) > 0 {
|
||||||
|
w = append(w, fmt.Sprintf("You posted in %s %d time%s in the past week through Supermarket, last on %s. Most communities expect less.",
|
||||||
|
sub, len(inSub), plural(len(inSub)), inSub[0].Local().Format("2 Jan")))
|
||||||
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
if len(c.History) == 0 {
|
if len(c.History) == 0 {
|
||||||
@@ -147,3 +162,26 @@ func plural(n int) string {
|
|||||||
}
|
}
|
||||||
return "s"
|
return "s"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RecordedRedditPosts are the Reddit posts Supermarket published, or the
|
||||||
|
// user marked as posted, since then, newest first.
|
||||||
|
func RecordedRedditPosts(d *db.DB, since time.Time) ([]reddit.Submission, error) {
|
||||||
|
rows, err := d.Query(`SELECT target, title, content, source_url, published_at FROM drafts
|
||||||
|
WHERE channel = ? AND status = ? AND published_at >= ? ORDER BY published_at DESC`,
|
||||||
|
ChannelReddit, StatusPublished, formatTime(since))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []reddit.Submission
|
||||||
|
for rows.Next() {
|
||||||
|
var s reddit.Submission
|
||||||
|
var at string
|
||||||
|
if err := rows.Scan(&s.Subreddit, &s.Title, &s.Text, &s.URL, &at); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.IsSelf, s.Created = true, parseTime(at)
|
||||||
|
out = append(out, s)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|||||||
@@ -376,4 +376,16 @@ func TestPostRedditByHand(t *testing.T) {
|
|||||||
if rec := postForm(h, "/drafts/"+ids["linksonly"]+"/posted", nil); rec.Code != http.StatusSeeOther {
|
if rec := postForm(h, "/drafts/"+ids["linksonly"]+"/posted", nil); rec.Code != http.StatusSeeOther {
|
||||||
t.Errorf("posted without a link: %d", rec.Code)
|
t.Errorf("posted without a link: %d", rec.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Without Reddit's API, the next draft for r/selfhosted is warned by
|
||||||
|
// Supermarket's own record; nothing about the whole account is claimed.
|
||||||
|
_, _ = content.CreateDrafts(s.DB, []content.Draft{{InventoryName: "Acme", Kind: "product", Channel: content.ChannelReddit,
|
||||||
|
Title: "Again", Target: "selfhosted", Content: "More."}})
|
||||||
|
body := do(h, http.MethodGet, "/inbox", nil, nil).Body.String()
|
||||||
|
if !strings.Contains(body, "You posted in r/selfhosted 1 time in the past week through Supermarket") {
|
||||||
|
t.Error("no same-subreddit warning from the recorded post")
|
||||||
|
}
|
||||||
|
if strings.Contains(body, "hasn't posted before") || strings.Contains(body, "hasn't posted before") {
|
||||||
|
t.Error("recorded posts mustn't stand in for the account's history")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,6 +119,10 @@ func (s *Server) redditViews(ctx context.Context, drafts []content.Draft) map[in
|
|||||||
var history []reddit.Submission
|
var history []reddit.Submission
|
||||||
historyRead := false
|
historyRead := false
|
||||||
tags := s.tagger()
|
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 _, d := range drafts {
|
||||||
if d.Channel != content.ChannelReddit {
|
if d.Channel != content.ChannelReddit {
|
||||||
continue
|
continue
|
||||||
@@ -131,7 +135,7 @@ func (s *Server) redditViews(ctx context.Context, drafts []content.Draft) map[in
|
|||||||
}
|
}
|
||||||
info, _ := content.GetSubreddit(s.DB, d.Target)
|
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, HasLink: d.SourceURL != "", Info: info, History: history, Now: time.Now(),
|
||||||
Promo: d.Kind == content.KindProduct}
|
Promo: d.Kind == content.KindProduct, Recorded: recorded}
|
||||||
if tags != nil {
|
if tags != nil {
|
||||||
check.Own = tags.Own
|
check.Own = tags.Own
|
||||||
check.OwnLink = tags.Own(d.SourceURL) || tags.HasOwnLink(d.Content)
|
check.OwnLink = tags.Own(d.SourceURL) || tags.HasOwnLink(d.Content)
|
||||||
|
|||||||
Reference in New Issue
Block a user