2022-01-28 01:41:10 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2022-02-02 15:22:48 +00:00
|
|
|
"strconv"
|
2022-01-28 01:41:10 +00:00
|
|
|
|
2023-06-10 16:27:20 +00:00
|
|
|
"codeberg.org/rimgo/rimgo/utils"
|
2022-01-28 01:41:10 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HandleUser(c *fiber.Ctx) error {
|
|
|
|
utils.SetHeaders(c)
|
2022-07-16 20:02:59 +00:00
|
|
|
c.Set("X-Frame-Options", "DENY")
|
2022-01-28 01:41:10 +00:00
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
2022-11-18 17:08:18 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'; media-src 'self'; style-src 'unsafe-inline' 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
2022-01-28 01:41:10 +00:00
|
|
|
|
2022-02-02 15:22:48 +00:00
|
|
|
page := "0"
|
|
|
|
if c.Query("page") != "" {
|
|
|
|
page = c.Query("page")
|
|
|
|
}
|
|
|
|
|
|
|
|
pageNumber, err := strconv.Atoi(c.Query("page"))
|
|
|
|
if err != nil {
|
|
|
|
pageNumber = 0
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:12:03 +00:00
|
|
|
user, err := ApiClient.FetchUser(c.Params("userID"))
|
2022-09-10 14:11:17 +00:00
|
|
|
if err != nil && err.Error() == "ratelimited by imgur" {
|
2023-06-10 16:04:29 +00:00
|
|
|
return c.Status(429).Render("errors/429", fiber.Map{
|
|
|
|
"path": c.Path(),
|
|
|
|
})
|
2022-09-10 14:11:17 +00:00
|
|
|
}
|
2022-01-28 01:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-10 14:11:17 +00:00
|
|
|
if user.Username == "" {
|
|
|
|
return c.Status(404).Render("errors/404", nil)
|
|
|
|
}
|
2022-01-28 01:41:10 +00:00
|
|
|
|
2023-01-01 20:12:03 +00:00
|
|
|
submissions, err := ApiClient.FetchSubmissions(c.Params("userID"), "newest", page)
|
2022-09-10 14:11:17 +00:00
|
|
|
if err != nil && err.Error() == "ratelimited by imgur" {
|
|
|
|
c.Status(429)
|
2023-06-10 16:04:29 +00:00
|
|
|
return c.Render("errors/429", fiber.Map{
|
|
|
|
"path": c.Path(),
|
|
|
|
})
|
2022-09-10 14:11:17 +00:00
|
|
|
}
|
2022-01-28 01:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render("user", fiber.Map{
|
|
|
|
"user": user,
|
|
|
|
"submissions": submissions,
|
2022-02-02 15:22:48 +00:00
|
|
|
"page": page,
|
|
|
|
"nextPage": pageNumber + 1,
|
|
|
|
"prevPage": pageNumber - 1,
|
2022-01-28 01:41:10 +00:00
|
|
|
})
|
|
|
|
}
|