glance/internal/widget/reddit.go

120 lines
2.8 KiB
Go
Raw Normal View History

2024-04-27 21:10:24 +02:00
package widget
import (
"context"
"errors"
"html/template"
"strings"
2024-04-27 21:10:24 +02:00
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
type Reddit struct {
widgetBase `yaml:",inline"`
Posts feed.ForumPosts `yaml:"-"`
Subreddit string `yaml:"subreddit"`
Style string `yaml:"style"`
2024-05-02 19:45:15 +02:00
ShowThumbnails bool `yaml:"show-thumbnails"`
SortBy string `yaml:"sort-by"`
TopPeriod string `yaml:"top-period"`
Search string `yaml:"search"`
ExtraSortBy string `yaml:"extra-sort-by"`
CommentsUrlTemplate string `yaml:"comments-url-template"`
Limit int `yaml:"limit"`
CollapseAfter int `yaml:"collapse-after"`
RequestUrlTemplate string `yaml:"request-url-template"`
2024-04-27 21:10:24 +02:00
}
func (widget *Reddit) Initialize() error {
if widget.Subreddit == "" {
return errors.New("no subreddit specified")
}
if widget.Limit <= 0 {
widget.Limit = 15
}
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
widget.CollapseAfter = 5
}
if !isValidRedditSortType(widget.SortBy) {
widget.SortBy = "hot"
}
if !isValidRedditTopPeriod(widget.TopPeriod) {
widget.TopPeriod = "day"
}
if widget.RequestUrlTemplate != "" {
if !strings.Contains(widget.RequestUrlTemplate, "{REQUEST-URL}") {
return errors.New("no `{REQUEST-URL}` placeholder specified")
}
}
2024-06-29 17:10:43 +02:00
widget.
withTitle("/r/" + widget.Subreddit).
withTitleURL("https://www.reddit.com/r/" + widget.Subreddit + "/").
withCacheDuration(30 * time.Minute)
2024-04-27 21:10:24 +02:00
return nil
}
func isValidRedditSortType(sortBy string) bool {
return sortBy == "hot" ||
sortBy == "new" ||
sortBy == "top" ||
sortBy == "rising"
}
func isValidRedditTopPeriod(period string) bool {
return period == "hour" ||
period == "day" ||
period == "week" ||
period == "month" ||
period == "year" ||
period == "all"
}
2024-04-27 21:10:24 +02:00
func (widget *Reddit) Update(ctx context.Context) {
// TODO: refactor, use a struct to pass all of these
posts, err := feed.FetchSubredditPosts(
widget.Subreddit,
widget.SortBy,
widget.TopPeriod,
widget.Search,
widget.CommentsUrlTemplate,
widget.RequestUrlTemplate,
)
2024-04-27 21:10:24 +02:00
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
if len(posts) > widget.Limit {
posts = posts[:widget.Limit]
}
if widget.ExtraSortBy == "engagement" {
posts.CalculateEngagement()
posts.SortByEngagement()
}
2024-04-27 21:10:24 +02:00
widget.Posts = posts
}
func (widget *Reddit) Render() template.HTML {
if widget.Style == "horizontal-cards" {
return widget.render(widget, assets.RedditCardsHorizontalTemplate)
}
if widget.Style == "vertical-cards" {
return widget.render(widget, assets.RedditCardsVerticalTemplate)
}
return widget.render(widget, assets.ForumPostsTemplate)
}