2024-04-27 21:10:24 +02:00
|
|
|
package widget
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
2024-04-30 03:21:33 +02:00
|
|
|
"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 {
|
2024-05-01 20:08:25 +02:00
|
|
|
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"`
|
2024-05-10 14:23:57 +02:00
|
|
|
SortBy string `yaml:"sort-by"`
|
|
|
|
TopPeriod string `yaml:"top-period"`
|
|
|
|
Search string `yaml:"search"`
|
|
|
|
ExtraSortBy string `yaml:"extra-sort-by"`
|
2024-05-01 20:08:25 +02:00
|
|
|
CommentsUrlTemplate string `yaml:"comments-url-template"`
|
|
|
|
Limit int `yaml:"limit"`
|
|
|
|
CollapseAfter int `yaml:"collapse-after"`
|
2024-04-30 03:21:33 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:23:57 +02:00
|
|
|
if !isValidRedditSortType(widget.SortBy) {
|
|
|
|
widget.SortBy = "hot"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isValidRedditTopPeriod(widget.TopPeriod) {
|
|
|
|
widget.TopPeriod = "day"
|
|
|
|
}
|
|
|
|
|
2024-04-30 03:21:33 +02:00
|
|
|
if widget.RequestUrlTemplate != "" {
|
|
|
|
if !strings.Contains(widget.RequestUrlTemplate, "{REQUEST-URL}") {
|
|
|
|
return errors.New("no `{REQUEST-URL}` placeholder specified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
widget.withTitle("/r/" + widget.Subreddit).withCacheDuration(30 * time.Minute)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:23:57 +02:00
|
|
|
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) {
|
2024-05-10 14:23:57 +02:00
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:23:57 +02:00
|
|
|
if widget.ExtraSortBy == "engagement" {
|
2024-05-12 12:00:45 +02:00
|
|
|
posts.CalculateEngagement()
|
2024-05-10 14:23:57 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|