From 0fea1e46a3372e93dcb79b859cef4eb9976d40ed Mon Sep 17 00:00:00 2001 From: orangix Date: Tue, 29 Aug 2023 22:59:59 +0000 Subject: [PATCH] Add user favorites (#133) Closes #10 Co-authored-by: video-prize-ranch Reviewed-on: https://codeberg.org/rimgo/rimgo/pulls/133 Reviewed-by: video-prize-ranch Co-authored-by: orangix Co-committed-by: orangix --- api/user.go | 53 +++++++++++++++++++++++++++++++++++++++-- main.go | 3 ++- pages/user.go | 49 +++++++++++++++++++++++++++++++++++++ views/user.hbs | 1 + views/userComments.hbs | 1 + views/userFavorites.hbs | 52 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 views/userFavorites.hbs diff --git a/api/user.go b/api/user.go index 25dca50..576373f 100644 --- a/api/user.go +++ b/api/user.go @@ -70,7 +70,7 @@ func (client *Client) FetchUser(username string) (User, error) { } func (client *Client) FetchSubmissions(username string, sort string, page string) ([]Submission, error) { - cacheData, found := client.Cache.Get(username + "-submissions") + cacheData, found := client.Cache.Get(username + "-submissions-" + sort + page) if found { return cacheData.([]Submission), nil } @@ -98,7 +98,56 @@ func (client *Client) FetchSubmissions(username string, sort string, page string ) wg.Wait() - client.Cache.Set(username+"-submissions", submissions, 15*time.Minute) + client.Cache.Set(username+"-submissions-"+sort+page, submissions, 15*time.Minute) + return submissions, nil +} + +func (client *Client) FetchUserFavorites(username string, sort string, page string) ([]Submission, error) { + cacheData, found := client.Cache.Get(username + "-favorites-" + sort + page) + if found { + return cacheData.([]Submission), nil + } + + req, err := http.NewRequest("GET", "https://api.imgur.com/3/account/"+username+"/gallery_favorites/"+page+"/"+sort, nil) + if err != nil { + return []Submission{}, err + } + utils.SetReqHeaders(req) + q := req.URL.Query() + q.Add("client_id", client.ClientID) + req.URL.RawQuery = q.Encode() + + res, err := http.DefaultClient.Do(req) + if err != nil { + return []Submission{}, err + } + + body, err := io.ReadAll(res.Body) + if err != nil { + return []Submission{}, err + } + + data := gjson.Parse(string(body)) + + submissions := []Submission{} + + wg := sync.WaitGroup{} + data.Get("data").ForEach( + func(key, value gjson.Result) bool { + wg.Add(1) + + go func() { + defer wg.Done() + + submissions = append(submissions, parseSubmission(value)) + }() + + return true + }, + ) + wg.Wait() + + client.Cache.Set(username+"-favorites-"+sort+page, submissions, 15*time.Minute) return submissions, nil } diff --git a/main.go b/main.go index 4360b09..5c81495 100644 --- a/main.go +++ b/main.go @@ -88,7 +88,7 @@ func main() { } else { app.Use("/static", filesystem.New(filesystem.Config{ MaxAge: 2592000, - Root: http.FS(static.GetFiles()), + Root: http.FS(static.GetFiles()), })) app.Use(cache.New(cache.Config{ Expiration: 30 * time.Minute, @@ -123,6 +123,7 @@ func main() { app.Get("/t/:tag/:postID", pages.HandlePost) app.Get("/r/:sub/:postID", pages.HandlePost) app.Get("/user/:userID", pages.HandleUser) + app.Get("/user/:userID/favorites", pages.HandleUserFavorites) app.Get("/user/:userID/comments", pages.HandleUserComments) app.Get("/user/:userID/cover", pages.HandleUserCover) app.Get("/user/:userID/avatar", pages.HandleUserAvatar) diff --git a/pages/user.go b/pages/user.go index e8460c4..42da651 100644 --- a/pages/user.go +++ b/pages/user.go @@ -91,3 +91,52 @@ func HandleUserComments(c *fiber.Ctx) error { "comments": comments, }) } + +func HandleUserFavorites(c *fiber.Ctx) error { + utils.SetHeaders(c) + c.Set("X-Frame-Options", "DENY") + c.Set("Cache-Control", "public,max-age=604800") + c.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; media-src 'self'; style-src 'unsafe-inline' 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content") + + page := "0" + if c.Query("page") != "" { + page = c.Query("page") + } + + pageNumber, err := strconv.Atoi(c.Query("page")) + if err != nil { + pageNumber = 0 + } + + user, err := ApiClient.FetchUser(c.Params("userID")) + if err != nil && err.Error() == "ratelimited by imgur" { + return c.Status(429).Render("errors/429", fiber.Map{ + "path": c.Path(), + }) + } + if err != nil { + return err + } + if user.Username == "" { + return c.Status(404).Render("errors/404", nil) + } + + favorites, err := ApiClient.FetchUserFavorites(c.Params("userID"), "newest", page) + if err != nil && err.Error() == "ratelimited by imgur" { + c.Status(429) + return c.Render("errors/429", fiber.Map{ + "path": c.Path(), + }) + } + if err != nil { + return err + } + + return c.Render("userFavorites", fiber.Map{ + "user": user, + "favorites": favorites, + "page": page, + "nextPage": pageNumber + 1, + "prevPage": pageNumber - 1, + }) +} diff --git a/views/user.hbs b/views/user.hbs index 02f5ddc..b05da03 100644 --- a/views/user.hbs +++ b/views/user.hbs @@ -24,6 +24,7 @@
diff --git a/views/userComments.hbs b/views/userComments.hbs index b0f223e..ca1e947 100644 --- a/views/userComments.hbs +++ b/views/userComments.hbs @@ -24,6 +24,7 @@
diff --git a/views/userFavorites.hbs b/views/userFavorites.hbs new file mode 100644 index 0000000..7bab5f4 --- /dev/null +++ b/views/userFavorites.hbs @@ -0,0 +1,52 @@ + + + + + {{user.Username}}'s favorites - rimgo + + {{> partials/head }} + + + + {{> partials/nav }} + +
+ {{> partials/searchBar }} +
+ +
+
+ +
+

{{user.Username}}

+

{{user.Points}} pts ยท {{user.CreatedAt}}

+
+
+ +
+

{{user.Bio}}

+
+ +
+
+ {{#each favorites}} + {{> partials/post }} + {{/each}} +
+ +
+ {{#noteq page "0" }} + Previous page + {{/noteq}} + Next page +
+
+ + {{> partials/footer }} + + +