personal.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package routes
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "codeberg.org/vnpower/pixivfe/v2/core"
  7. "codeberg.org/vnpower/pixivfe/v2/request_context"
  8. "codeberg.org/vnpower/pixivfe/v2/session"
  9. )
  10. func PromptUserToLoginPage(w http.ResponseWriter, r *http.Request) error {
  11. request_context.Get(r).RenderStatusCode = http.StatusUnauthorized
  12. err := Render(w, r, Data_unauthorized{})
  13. if err != nil {
  14. return err
  15. }
  16. return nil
  17. }
  18. func LoginUserPage(w http.ResponseWriter, r *http.Request) error {
  19. token := session.GetPixivToken(r)
  20. if token == "" {
  21. return PromptUserToLoginPage(w, r)
  22. }
  23. // The left part of the token is the member ID
  24. userId := strings.Split(token, "_")
  25. http.Redirect(w, r, "/users/"+userId[0], http.StatusSeeOther)
  26. return nil
  27. }
  28. func LoginBookmarkPage(w http.ResponseWriter, r *http.Request) error {
  29. token := session.GetPixivToken(r)
  30. if token == "" {
  31. return PromptUserToLoginPage(w, r)
  32. }
  33. // The left part of the token is the member ID
  34. userId := strings.Split(token, "_")
  35. http.Redirect(w, r, "/users/"+userId[0]+"/bookmarks#checkpoint", http.StatusSeeOther)
  36. return nil
  37. }
  38. func FollowingWorksPage(w http.ResponseWriter, r *http.Request) error {
  39. if token := session.GetPixivToken(r); token == "" {
  40. return PromptUserToLoginPage(w, r)
  41. }
  42. mode := GetQueryParam(r, "mode", "all")
  43. page := GetQueryParam(r, "page", "1")
  44. pageInt, err := strconv.Atoi(page)
  45. if err != nil {
  46. return err
  47. }
  48. works, err := core.GetNewestFromFollowing(r, mode, page)
  49. if err != nil {
  50. return err
  51. }
  52. return Render(w, r, Data_following{Title: "Following works", Mode: mode, Artworks: works, CurPage: page, Page: pageInt})
  53. }