personal.go 1.5 KB

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