user.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package routes
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "codeberg.org/vnpower/pixivfe/v2/core"
  7. "net/http"
  8. )
  9. type userPageData struct {
  10. user core.User
  11. category core.UserArtCategory
  12. pageLimit int
  13. page int
  14. }
  15. func fetchData(r *http.Request, getTags bool) (userPageData, error) {
  16. id := GetPathVar(r, "id")
  17. if _, err := strconv.Atoi(id); err != nil {
  18. return userPageData{}, err
  19. }
  20. category := core.UserArtCategory(GetPathVar(r, "category", string(core.UserArt_Any)))
  21. err := category.Validate()
  22. if err != nil {
  23. return userPageData{}, err
  24. }
  25. page_param := GetQueryParam(r, "page", "1")
  26. page, err := strconv.Atoi(page_param)
  27. if err != nil {
  28. return userPageData{}, err
  29. }
  30. user, err := core.GetUserArtwork(r, 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_Bookmarks {
  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(w http.ResponseWriter, r *http.Request) error {
  46. data, err := fetchData(r, true)
  47. if err != nil {
  48. return err
  49. }
  50. return Render(w, r, Data_user{Title: data.user.Name, User: data.user, Category: data.category, PageLimit: data.pageLimit, Page: data.page, MetaImage: data.user.BackgroundImage})
  51. }
  52. func UserAtomFeed(w http.ResponseWriter, r *http.Request) error {
  53. data, err := fetchData(r, false)
  54. if err != nil {
  55. return err
  56. }
  57. w.Header().Set("content-type", "application/atom+xml")
  58. return Render(w, r, Data_userAtom{
  59. URL: r.RequestURI,
  60. Title: data.user.Name,
  61. User: data.user,
  62. Category: data.category,
  63. Updated: time.Now().Format(time.RFC3339),
  64. PageLimit: data.pageLimit,
  65. Page: data.page,
  66. // MetaImage: data.user.BackgroundImage,
  67. })
  68. }