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>
84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"supermarket/internal/api/linkedin"
|
|
"supermarket/internal/api/rss"
|
|
"supermarket/internal/api/twitter"
|
|
"supermarket/internal/api/youtube"
|
|
"supermarket/internal/content"
|
|
"supermarket/internal/fetcher"
|
|
"supermarket/internal/secrets"
|
|
"supermarket/internal/site"
|
|
)
|
|
|
|
// fetchMaxResults is how many posts or videos one source returns per fetch.
|
|
const fetchMaxResults = 20
|
|
|
|
// fetcher builds a fetcher from the saved credentials.
|
|
func (s *Server) fetcher() *fetcher.Fetcher {
|
|
f := &fetcher.Fetcher{RSS: rss.New("Supermarket/" + s.Version + " (feed reader)")}
|
|
if tok := s.Secrets.Get(secrets.XBearerToken); tok != "" {
|
|
f.Twitter = twitter.New(tok, "", fetchMaxResults)
|
|
}
|
|
if key := s.Secrets.Get(secrets.YouTubeAPIKey); key != "" {
|
|
f.YouTube = youtube.New(key, fetchMaxResults)
|
|
}
|
|
return f
|
|
}
|
|
|
|
// expiredPublisher stands in for a network whose sign-in ran out: it posts
|
|
// nothing and says so, rather than letting the network answer 401.
|
|
type expiredPublisher struct{ e Expiry }
|
|
|
|
func (p expiredPublisher) Publish(context.Context, content.Post) (string, error) {
|
|
return "", p.err()
|
|
}
|
|
|
|
func (p expiredPublisher) err() error {
|
|
return fmt.Errorf("%s's sign-in expired on %s: reconnect %s under Settings → Social accounts, then publish again",
|
|
p.e.Network, p.e.At.Local().Format("2 Jan 2006"), p.e.Network)
|
|
}
|
|
|
|
type xPublisher struct{ c *twitter.Client }
|
|
|
|
// Publish appends the source link to the post, as the old app did; X shows
|
|
// it as a card.
|
|
func (p xPublisher) Publish(ctx context.Context, post content.Post) (string, error) {
|
|
text := post.Text
|
|
if post.SourceURL != "" {
|
|
text += "\n\n" + post.SourceURL
|
|
}
|
|
return p.c.PostTweet(ctx, text)
|
|
}
|
|
|
|
// Publish attaches the source link as an article preview.
|
|
func (p linkedInPublisher) Publish(ctx context.Context, post content.Post) (string, error) {
|
|
c := p.me
|
|
switch {
|
|
case post.AsPage && p.pageErr != nil:
|
|
return "", p.pageErr
|
|
case post.AsPage && p.page == nil:
|
|
return "", errNoPage
|
|
case post.AsPage:
|
|
c = p.page
|
|
case p.meErr != nil:
|
|
return "", p.meErr
|
|
case p.me == nil:
|
|
return "", errNoMe
|
|
}
|
|
if post.SourceURL == "" {
|
|
return c.PostUpdate(ctx, post.Text, nil)
|
|
}
|
|
// LinkedIn wants the card's title from us. A page that can't be read
|
|
// still gets a card, titled with its address.
|
|
link := &linkedin.Link{URL: post.SourceURL}
|
|
ictx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
link.Title, link.Description, _ = site.PageInfo(ictx, post.SourceURL, "Supermarket/1 (link preview)")
|
|
return c.PostUpdate(ctx, post.Text, link)
|
|
}
|