actions.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package routes
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "codeberg.org/vnpower/pixivfe/v2/core"
  7. "codeberg.org/vnpower/pixivfe/v2/session"
  8. "codeberg.org/vnpower/pixivfe/v2/utils"
  9. )
  10. func AddBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
  11. token := session.GetPixivToken(r)
  12. csrf := session.GetCookie(r, session.Cookie_CSRF)
  13. if token == "" || csrf == "" {
  14. return PromptUserToLoginPage(w, r)
  15. }
  16. id := GetPathVar(r, "id")
  17. if id == "" {
  18. return errors.New("No ID provided.")
  19. }
  20. URL := "https://www.pixiv.net/ajax/illusts/bookmarks/add"
  21. payload := fmt.Sprintf(`{
  22. "illust_id": "%s",
  23. "restrict": 0,
  24. "comment": "",
  25. "tags": []
  26. }`, id)
  27. if err := core.API_POST(r.Context(), URL, payload, token, csrf, true); err != nil {
  28. return err
  29. }
  30. utils.RedirectToWhenceYouCame(w, r)
  31. return nil
  32. }
  33. func DeleteBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
  34. token := session.GetPixivToken(r)
  35. csrf := session.GetCookie(r, session.Cookie_CSRF)
  36. if token == "" || csrf == "" {
  37. return PromptUserToLoginPage(w, r)
  38. }
  39. id := GetPathVar(r, "id")
  40. if id == "" {
  41. return errors.New("No ID provided.")
  42. }
  43. // You can't unlike
  44. URL := "https://www.pixiv.net/ajax/illusts/bookmarks/delete"
  45. payload := fmt.Sprintf(`bookmark_id=%s`, id)
  46. if err := core.API_POST(r.Context(), URL, payload, token, csrf, false); err != nil {
  47. return err
  48. }
  49. utils.RedirectToWhenceYouCame(w, r)
  50. return nil
  51. }
  52. func LikeRoute(w http.ResponseWriter, r *http.Request) error {
  53. token := session.GetPixivToken(r)
  54. csrf := session.GetCookie(r, session.Cookie_CSRF)
  55. if token == "" || csrf == "" {
  56. return PromptUserToLoginPage(w, r)
  57. }
  58. id := GetPathVar(r, "id")
  59. if id == "" {
  60. return errors.New("No ID provided.")
  61. }
  62. URL := "https://www.pixiv.net/ajax/illusts/like"
  63. payload := fmt.Sprintf(`{"illust_id": "%s"}`, id)
  64. if err := core.API_POST(r.Context(), URL, payload, token, csrf, true); err != nil {
  65. return err
  66. }
  67. utils.RedirectToWhenceYouCame(w, r)
  68. return nil
  69. }