Fix reddit crossposts & add show-flairs property

This commit is contained in:
Svilen Markov
2024-08-11 14:04:18 +01:00
parent 328e10b89f
commit 7d09b8bf1b
5 changed files with 42 additions and 5 deletions
+4
View File
@@ -654,6 +654,7 @@ Example:
| subreddit | string | yes | | | subreddit | string | yes | |
| style | string | no | vertical-list | | style | string | no | vertical-list |
| show-thumbnails | boolean | no | false | | show-thumbnails | boolean | no | false |
| show-flairs | boolean | no | false |
| limit | integer | no | 15 | | limit | integer | no | 15 |
| collapse-after | integer | no | 5 | | collapse-after | integer | no | 5 |
| comments-url-template | string | no | https://www.reddit.com/{POST-PATH} | | 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. > 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` ##### `limit`
The maximum number of posts to show. The maximum number of posts to show.
+5 -1
View File
@@ -6,7 +6,11 @@
<li> <li>
<div class="flex gap-10 row-reverse-on-mobile thumbnail-parent"> <div class="flex gap-10 row-reverse-on-mobile thumbnail-parent">
{{ if $.ShowThumbnails }} {{ if $.ShowThumbnails }}
{{ if ne .ThumbnailUrl "" }} {{ if .IsCrosspost }}
<svg title="Crosspost" class="forum-post-list-thumbnail hide-on-mobile" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="-9 -8 40 40" stroke-width="1.5" stroke="var(--color-text-subdue)">
<path stroke-linecap="round" stroke-linejoin="round" d="M7.5 21 3 16.5m0 0L7.5 12M3 16.5h13.5m0-13.5L21 7.5m0 0L16.5 12M21 7.5H7.5" />
</svg>
{{ else if ne .ThumbnailUrl "" }}
<img class="forum-post-list-thumbnail thumbnail" src="{{ .ThumbnailUrl }}" alt="" loading="lazy"> <img class="forum-post-list-thumbnail thumbnail" src="{{ .ThumbnailUrl }}" alt="" loading="lazy">
{{ else if .HasTargetUrl }} {{ else if .HasTargetUrl }}
<svg class="forum-post-list-thumbnail hide-on-mobile" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="-9 -8 40 40" stroke-width="1.5" stroke="var(--color-text-subdue)"> <svg class="forum-post-list-thumbnail hide-on-mobile" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="-9 -8 40 40" stroke-width="1.5" stroke="var(--color-text-subdue)">
+1
View File
@@ -17,6 +17,7 @@ type ForumPost struct {
Engagement float64 Engagement float64
TimePosted time.Time TimePosted time.Time
Tags []string Tags []string
IsCrosspost bool
} }
type ForumPosts []ForumPost type ForumPosts []ForumPost
+30 -4
View File
@@ -25,12 +25,26 @@ type subredditResponseJson struct {
Pinned bool `json:"pinned"` Pinned bool `json:"pinned"`
IsSelf bool `json:"is_self"` IsSelf bool `json:"is_self"`
Thumbnail string `json:"thumbnail"` 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:"data"`
} `json:"children"` } `json:"children"`
} `json:"data"` } `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{} query := url.Values{}
var requestUrl string var requestUrl string
@@ -85,9 +99,7 @@ func FetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate
if commentsUrlTemplate == "" { if commentsUrlTemplate == "" {
commentsUrl = "https://www.reddit.com" + post.Permalink commentsUrl = "https://www.reddit.com" + post.Permalink
} else { } else {
commentsUrl = strings.ReplaceAll(commentsUrlTemplate, "{SUBREDDIT}", subreddit) commentsUrl = templateRedditCommentsUrl(commentsUrlTemplate, subreddit, post.Id, post.Permalink)
commentsUrl = strings.ReplaceAll(commentsUrl, "{POST-ID}", post.Id)
commentsUrl = strings.ReplaceAll(commentsUrl, "{POST-PATH}", strings.TrimLeft(post.Permalink, "/"))
} }
forumPost := ForumPost{ forumPost := ForumPost{
@@ -107,6 +119,20 @@ func FetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate
forumPost.TargetUrl = post.Url 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) posts = append(posts, forumPost)
} }
+2
View File
@@ -17,6 +17,7 @@ type Reddit struct {
Subreddit string `yaml:"subreddit"` Subreddit string `yaml:"subreddit"`
Style string `yaml:"style"` Style string `yaml:"style"`
ShowThumbnails bool `yaml:"show-thumbnails"` ShowThumbnails bool `yaml:"show-thumbnails"`
ShowFlairs bool `yaml:"show-flairs"`
SortBy string `yaml:"sort-by"` SortBy string `yaml:"sort-by"`
TopPeriod string `yaml:"top-period"` TopPeriod string `yaml:"top-period"`
Search string `yaml:"search"` Search string `yaml:"search"`
@@ -87,6 +88,7 @@ func (widget *Reddit) Update(ctx context.Context) {
widget.Search, widget.Search,
widget.CommentsUrlTemplate, widget.CommentsUrlTemplate,
widget.RequestUrlTemplate, widget.RequestUrlTemplate,
widget.ShowFlairs,
) )
if !widget.canContinueUpdateAfterHandlingErr(err) { if !widget.canContinueUpdateAfterHandlingErr(err) {