rimgo/pages/user.go

63 lines
1.3 KiB
Go
Raw Normal View History

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
"sync"
"codeberg.org/video-prize-ranch/rimgo/api"
"codeberg.org/video-prize-ranch/rimgo/types"
"codeberg.org/video-prize-ranch/rimgo/utils"
"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")
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'unsafe-inline' 'self'; img-src 'self'; font-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
}
2022-01-28 01:41:10 +00:00
wg := sync.WaitGroup{}
wg.Add(2)
user, err := types.User{}, error(nil)
go func() {
defer wg.Done()
user, err = api.FetchUser(c.Params("userID"))
}()
if err != nil {
return err
}
submissions, err := []types.Submission{}, error(nil)
go func() {
defer wg.Done()
2022-02-02 15:22:48 +00:00
submissions, err = api.FetchSubmissions(c.Params("userID"), "newest", page)
2022-01-28 01:41:10 +00:00
}()
if err != nil {
return err
}
wg.Wait()
2022-05-23 15:30:17 +00:00
if user.Username == "" {
c.Status(404)
return c.Render("errors/404", nil)
}
2022-01-28 01:41:10 +00:00
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
})
}