From 720549ec7e3a74d1af1a1c841b88e40a2f6945b9 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sun, 2 Jun 2024 18:08:29 +0100 Subject: [PATCH] Allow specifying sort order and tags through config --- internal/feed/lobsters.go | 18 +++++++++++++++++- internal/widget/lobsters.go | 9 +++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/internal/feed/lobsters.go b/internal/feed/lobsters.go index dc53e65..e103f56 100644 --- a/internal/feed/lobsters.go +++ b/internal/feed/lobsters.go @@ -2,6 +2,7 @@ package feed import ( "net/http" + "strings" "time" ) @@ -54,7 +55,22 @@ func getLobstersPostsFromFeed(feedUrl string) (ForumPosts, error) { return posts, nil } -func FetchLobstersTopPosts(feedUrl string) (ForumPosts, error) { +func FetchLobstersPosts(sortBy string, tags []string) (ForumPosts, error) { + var feedUrl string + + if sortBy == "hot" { + sortBy = "hottest" + } else if sortBy == "new" { + sortBy = "newest" + } + + if len(tags) == 0 { + feedUrl = "https://lobste.rs/" + sortBy + ".json" + } else { + tags := strings.Join(tags, ",") + feedUrl = "https://lobste.rs/t/" + tags + ".json" + } + posts, err := getLobstersPostsFromFeed(feedUrl) if err != nil { diff --git a/internal/widget/lobsters.go b/internal/widget/lobsters.go index aab52a3..d9a8c43 100644 --- a/internal/widget/lobsters.go +++ b/internal/widget/lobsters.go @@ -11,18 +11,19 @@ import ( type Lobsters struct { widgetBase `yaml:",inline"` - FeedUrl string `yaml:"feed"` Posts feed.ForumPosts `yaml:"-"` Limit int `yaml:"limit"` CollapseAfter int `yaml:"collapse-after"` + SortBy string `yaml:"sort-by"` + Tags []string `yaml:"tags"` ShowThumbnails bool `yaml:"-"` } func (widget *Lobsters) Initialize() error { widget.withTitle("Lobsters").withCacheDuration(30 * time.Minute) - if widget.FeedUrl == "" { - widget.FeedUrl = "https://lobste.rs/hottest.json" + if widget.SortBy == "" || (widget.SortBy != "hot" && widget.SortBy != "new") { + widget.SortBy = "hot" } if widget.Limit <= 0 { @@ -37,7 +38,7 @@ func (widget *Lobsters) Initialize() error { } func (widget *Lobsters) Update(ctx context.Context) { - posts, err := feed.FetchLobstersTopPosts(widget.FeedUrl) + posts, err := feed.FetchLobstersPosts(widget.SortBy, widget.Tags) if !widget.canContinueUpdateAfterHandlingErr(err) { return