From 7d09b8bf1b1c53399fcabaf720a3289053c4f6c3 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sun, 11 Aug 2024 14:04:18 +0100 Subject: [PATCH] Fix reddit crossposts & add show-flairs property --- docs/configuration.md | 4 +++ internal/assets/templates/forum-posts.html | 6 +++- internal/feed/primitives.go | 1 + internal/feed/reddit.go | 34 +++++++++++++++++++--- internal/widget/reddit.go | 2 ++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 7abafe1..0f076fd 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -654,6 +654,7 @@ Example: | subreddit | string | yes | | | style | string | no | vertical-list | | show-thumbnails | boolean | no | false | +| show-flairs | boolean | no | false | | limit | integer | no | 15 | | collapse-after | integer | no | 5 | | comments-url-template | string | no | https://www.reddit.com/{POST-PATH} | @@ -690,6 +691,9 @@ Shows or hides thumbnails next to the post. This only works if the `style` is `v > > Thumbnails don't work for some subreddits due to Reddit's API not returning the thumbnail URL. No workaround for this yet. +##### `show-flairs` +Shows post flairs when set to `true`. + ##### `limit` The maximum number of posts to show. diff --git a/internal/assets/templates/forum-posts.html b/internal/assets/templates/forum-posts.html index cf9cb0d..1ada8ec 100644 --- a/internal/assets/templates/forum-posts.html +++ b/internal/assets/templates/forum-posts.html @@ -6,7 +6,11 @@
  • {{ if $.ShowThumbnails }} - {{ if ne .ThumbnailUrl "" }} + {{ if .IsCrosspost }} + + + + {{ else if ne .ThumbnailUrl "" }} {{ else if .HasTargetUrl }} diff --git a/internal/feed/primitives.go b/internal/feed/primitives.go index 6e0c98f..7371983 100644 --- a/internal/feed/primitives.go +++ b/internal/feed/primitives.go @@ -17,6 +17,7 @@ type ForumPost struct { Engagement float64 TimePosted time.Time Tags []string + IsCrosspost bool } type ForumPosts []ForumPost diff --git a/internal/feed/reddit.go b/internal/feed/reddit.go index 676d948..adeb263 100644 --- a/internal/feed/reddit.go +++ b/internal/feed/reddit.go @@ -25,12 +25,26 @@ type subredditResponseJson struct { Pinned bool `json:"pinned"` IsSelf bool `json:"is_self"` Thumbnail string `json:"thumbnail"` + Flair string `json:"link_flair_text"` + ParentList []struct { + Id string `json:"id"` + Subreddit string `json:"subreddit"` + Permalink string `json:"permalink"` + } `json:"crosspost_parent_list"` } `json:"data"` } `json:"children"` } `json:"data"` } -func FetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate, requestUrlTemplate string) (ForumPosts, error) { +func templateRedditCommentsUrl(template, subreddit, postId, postPath string) string { + template = strings.ReplaceAll(template, "{SUBREDDIT}", subreddit) + template = strings.ReplaceAll(template, "{POST-ID}", postId) + template = strings.ReplaceAll(template, "{POST-PATH}", strings.TrimLeft(postPath, "/")) + + return template +} + +func FetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate, requestUrlTemplate string, showFlairs bool) (ForumPosts, error) { query := url.Values{} var requestUrl string @@ -85,9 +99,7 @@ func FetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate if commentsUrlTemplate == "" { commentsUrl = "https://www.reddit.com" + post.Permalink } else { - commentsUrl = strings.ReplaceAll(commentsUrlTemplate, "{SUBREDDIT}", subreddit) - commentsUrl = strings.ReplaceAll(commentsUrl, "{POST-ID}", post.Id) - commentsUrl = strings.ReplaceAll(commentsUrl, "{POST-PATH}", strings.TrimLeft(post.Permalink, "/")) + commentsUrl = templateRedditCommentsUrl(commentsUrlTemplate, subreddit, post.Id, post.Permalink) } forumPost := ForumPost{ @@ -107,6 +119,20 @@ func FetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate forumPost.TargetUrl = post.Url } + if showFlairs && post.Flair != "" { + forumPost.Tags = append(forumPost.Tags, post.Flair) + } + + if len(post.ParentList) > 0 { + forumPost.IsCrosspost = true + forumPost.TargetUrlDomain = "r/" + post.ParentList[0].Subreddit + if commentsUrlTemplate == "" { + forumPost.TargetUrl = "https://www.reddit.com" + post.ParentList[0].Permalink + } else { + forumPost.TargetUrl = templateRedditCommentsUrl(commentsUrlTemplate, post.ParentList[0].Subreddit, post.ParentList[0].Id, post.ParentList[0].Permalink) + } + } + posts = append(posts, forumPost) } diff --git a/internal/widget/reddit.go b/internal/widget/reddit.go index 1aa16fb..b1ddf0a 100644 --- a/internal/widget/reddit.go +++ b/internal/widget/reddit.go @@ -17,6 +17,7 @@ type Reddit struct { Subreddit string `yaml:"subreddit"` Style string `yaml:"style"` ShowThumbnails bool `yaml:"show-thumbnails"` + ShowFlairs bool `yaml:"show-flairs"` SortBy string `yaml:"sort-by"` TopPeriod string `yaml:"top-period"` Search string `yaml:"search"` @@ -87,6 +88,7 @@ func (widget *Reddit) Update(ctx context.Context) { widget.Search, widget.CommentsUrlTemplate, widget.RequestUrlTemplate, + widget.ShowFlairs, ) if !widget.canContinueUpdateAfterHandlingErr(err) {