diff --git a/docs/configuration.md b/docs/configuration.md index 6ccd238..a452a0a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -508,6 +508,8 @@ Preview: | limit | integer | no | 15 | | collapse-after | integer | no | 5 | | comments-url-template | string | no | https://news.ycombinator.com/item?id={POST-ID} | +| sort-by | string | no | top | +| extra-sort-by | string | no | | ##### `comments-url-template` Used to replace the default link for post comments. Useful if you want to use an alternative front-end. Example: @@ -520,6 +522,14 @@ Placeholders: `{POST-ID}` - the ID of the post +##### `sort-by` +Used to specify the order in which the posts should get returned. Possible values are `top`, `new`, and `best`. + +##### `extra-sort-by` +Can be used to specify an additional sort which will be applied on top of the already sorted posts. By default does not apply any extra sorting and the only available option is `engagement`. + +The `engagement` sort tries to place the posts with the most points and comments on top, also prioritizing recent over old posts. + ### Reddit Display a list of posts from a specific subreddit. diff --git a/internal/feed/hacker-news.go b/internal/feed/hacker-news.go index baacdf2..f1db111 100644 --- a/internal/feed/hacker-news.go +++ b/internal/feed/hacker-news.go @@ -18,8 +18,8 @@ type hackerNewsPostResponseJson struct { TimePosted int64 `json:"time"` } -func getHackerNewsTopPostIds() ([]int, error) { - request, _ := http.NewRequest("GET", "https://hacker-news.firebaseio.com/v0/topstories.json", nil) +func getHackerNewsPostIds(sort string) ([]int, error) { + request, _ := http.NewRequest("GET", fmt.Sprintf("https://hacker-news.firebaseio.com/v0/%sstories.json", sort), nil) response, err := decodeJsonFromRequest[[]int](defaultClient, request) if err != nil { @@ -83,8 +83,8 @@ func getHackerNewsPostsFromIds(postIds []int, commentsUrlTemplate string) (Forum return posts, nil } -func FetchHackerNewsTopPosts(limit int, commentsUrlTemplate string) (ForumPosts, error) { - postIds, err := getHackerNewsTopPostIds() +func FetchHackerNewsPosts(sort string, limit int, commentsUrlTemplate string) (ForumPosts, error) { + postIds, err := getHackerNewsPostIds(sort) if err != nil { return nil, err diff --git a/internal/widget/hacker-news.go b/internal/widget/hacker-news.go index ff9ece0..9beccb7 100644 --- a/internal/widget/hacker-news.go +++ b/internal/widget/hacker-news.go @@ -13,6 +13,8 @@ type HackerNews struct { widgetBase `yaml:",inline"` Posts feed.ForumPosts `yaml:"-"` Limit int `yaml:"limit"` + SortBy string `yaml:"sort-by"` + ExtraSortBy string `yaml:"extra-sort-by"` CollapseAfter int `yaml:"collapse-after"` CommentsUrlTemplate string `yaml:"comments-url-template"` ShowThumbnails bool `yaml:"-"` @@ -29,18 +31,24 @@ func (widget *HackerNews) Initialize() error { widget.CollapseAfter = 5 } + if widget.SortBy != "top" && widget.SortBy != "new" && widget.SortBy != "best" { + widget.SortBy = "top" + } + return nil } func (widget *HackerNews) Update(ctx context.Context) { - posts, err := feed.FetchHackerNewsTopPosts(40, widget.CommentsUrlTemplate) + posts, err := feed.FetchHackerNewsPosts(widget.SortBy, 40, widget.CommentsUrlTemplate) if !widget.canContinueUpdateAfterHandlingErr(err) { return } - posts.CalculateEngagement() - posts.SortByEngagement() + if widget.ExtraSortBy == "engagement" { + posts.CalculateEngagement() + posts.SortByEngagement() + } if widget.Limit < len(posts) { posts = posts[:widget.Limit]