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) }