Skip to content

refactor(widget/reddit): refactor argument passing to use a struct #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions internal/glance/widget-reddit.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,30 @@ func isValidRedditTopPeriod(period string) bool {
period == "all"
}

type redditPostsRequest struct {
Subreddit string
Sort string
TopPeriod string
Search string
CommentsUrlTemplate string
RequestUrlTemplate string
ProxyClient *http.Client
ShowFlairs bool
}

func (widget *redditWidget) update(ctx context.Context) {
// TODO: refactor, use a struct to pass all of these
posts, err := fetchSubredditPosts(
widget.Subreddit,
widget.SortBy,
widget.TopPeriod,
widget.Search,
widget.CommentsUrlTemplate,
widget.RequestUrlTemplate,
widget.Proxy.client,
widget.ShowFlairs,
)
req := redditPostsRequest{
Subreddit: widget.Subreddit,
Sort: widget.SortBy,
TopPeriod: widget.TopPeriod,
Search: widget.Search,
CommentsUrlTemplate: widget.CommentsUrlTemplate,
RequestUrlTemplate: widget.RequestUrlTemplate,
ProxyClient: widget.Proxy.client,
ShowFlairs: widget.ShowFlairs,
}

posts, err := fetchSubredditPosts(req)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
Expand All @@ -125,7 +136,6 @@ func (widget *redditWidget) Render() template.HTML {
}

return widget.renderTemplate(widget, forumPostsTemplate)

}

type subredditResponseJson struct {
Expand Down Expand Up @@ -159,44 +169,34 @@ func templateRedditCommentsURL(template, subreddit, postId, postPath string) str
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,
proxyClient *http.Client,
showFlairs bool,
) (forumPostList, error) {
func fetchSubredditPosts(req redditPostsRequest) (forumPostList, error) {
query := url.Values{}
var requestUrl string

if search != "" {
query.Set("q", search+" subreddit:"+subreddit)
query.Set("sort", sort)
if req.Search != "" {
query.Set("q", req.Search+" subreddit:"+req.Subreddit)
query.Set("sort", req.Sort)
}

if sort == "top" {
query.Set("t", topPeriod)
if req.Sort == "top" {
query.Set("t", req.TopPeriod)
}

if search != "" {
if req.Search != "" {
requestUrl = fmt.Sprintf("https://www.reddit.com/search.json?%s", query.Encode())
} else {
requestUrl = fmt.Sprintf("https://www.reddit.com/r/%s/%s.json?%s", subreddit, sort, query.Encode())
requestUrl = fmt.Sprintf("https://www.reddit.com/r/%s/%s.json?%s", req.Subreddit, req.Sort, query.Encode())
}

var client requestDoer = defaultHTTPClient

if requestUrlTemplate != "" {
requestUrl = strings.ReplaceAll(requestUrlTemplate, "{REQUEST-URL}", requestUrl)
} else if proxyClient != nil {
client = proxyClient
if req.RequestUrlTemplate != "" {
requestUrl = strings.ReplaceAll(req.RequestUrlTemplate, "{REQUEST-URL}", requestUrl)
} else if req.ProxyClient != nil {
client = req.ProxyClient
}

request, err := http.NewRequest("GET", requestUrl, nil)
Expand Down Expand Up @@ -226,10 +226,10 @@ func fetchSubredditPosts(

var commentsUrl string

if commentsUrlTemplate == "" {
if req.CommentsUrlTemplate == "" {
commentsUrl = "https://www.reddit.com" + post.Permalink
} else {
commentsUrl = templateRedditCommentsURL(commentsUrlTemplate, subreddit, post.Id, post.Permalink)
commentsUrl = templateRedditCommentsURL(req.CommentsUrlTemplate, req.Subreddit, post.Id, post.Permalink)
}

forumPost := forumPost{
Expand All @@ -249,19 +249,19 @@ func fetchSubredditPosts(
forumPost.TargetUrl = post.Url
}

if showFlairs && post.Flair != "" {
if req.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 == "" {
if req.CommentsUrlTemplate == "" {
forumPost.TargetUrl = "https://www.reddit.com" + post.ParentList[0].Permalink
} else {
forumPost.TargetUrl = templateRedditCommentsURL(
commentsUrlTemplate,
req.CommentsUrlTemplate,
post.ParentList[0].Subreddit,
post.ParentList[0].Id,
post.ParentList[0].Permalink,
Expand Down