user.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package pages
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
  7. "github.com/gofiber/fiber/v2"
  8. )
  9. type userPageData struct {
  10. user core.User
  11. category core.UserArtCategory
  12. pageLimit int
  13. page int
  14. }
  15. func fetchData(c *fiber.Ctx, getTags bool) (userPageData, error) {
  16. id := c.Params("id")
  17. if _, err := strconv.Atoi(id); err != nil {
  18. return userPageData{}, err
  19. }
  20. category := core.UserArtCategory(c.Params("category", string(core.UserArt_Any)))
  21. err := category.Validate()
  22. if err != nil {
  23. return userPageData{}, err
  24. }
  25. page_param := c.Query("page", "1")
  26. page, err := strconv.Atoi(page_param)
  27. if err != nil {
  28. return userPageData{}, err
  29. }
  30. user, err := core.GetUserArtwork(c, id, category, page, getTags)
  31. if err != nil {
  32. return userPageData{}, err
  33. }
  34. var worksCount int
  35. var worksPerPage float64
  36. if category == core.UserArt_Bookmarked {
  37. worksPerPage = 48.0
  38. } else {
  39. worksPerPage = 30.0
  40. }
  41. worksCount = user.ArtworksCount
  42. pageLimit := int(math.Ceil(float64(worksCount) / worksPerPage))
  43. return userPageData{user, category, pageLimit, page}, nil
  44. }
  45. func UserPage(c *fiber.Ctx) error {
  46. data, err := fetchData(c, true)
  47. if err != nil {
  48. return err
  49. }
  50. return c.Render("user", fiber.Map{
  51. "Title": data.user.Name,
  52. "User": data.user,
  53. "Category": data.category,
  54. "PageLimit": data.pageLimit,
  55. "Page": data.page,
  56. "MetaImage": data.user.BackgroundImage,
  57. })
  58. }
  59. func UserAtomFeed(c *fiber.Ctx) error {
  60. data, err := fetchData(c, false)
  61. if err != nil {
  62. return err
  63. }
  64. err = c.Render("user.atom", fiber.Map{
  65. "URL": string(c.Request().RequestURI()),
  66. "Title": data.user.Name,
  67. "User": data.user,
  68. "Category": data.category,
  69. "Updated": time.Now().Format(time.RFC3339),
  70. "PageLimit": data.pageLimit,
  71. "Page": data.page,
  72. // "MetaImage": data.user.BackgroundImage,
  73. }, "")
  74. if err != nil {
  75. return err
  76. }
  77. c.Context().SetContentType("application/atom+xml")
  78. return nil
  79. }