pages.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package views
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "codeberg.org/vnpower/pixivfe/configs"
  11. "codeberg.org/vnpower/pixivfe/models"
  12. "github.com/gofiber/fiber/v2"
  13. )
  14. func get_session_value(c *fiber.Ctx, key string) *string {
  15. sess, err := configs.Store.Get(c)
  16. if err != nil {
  17. panic(err)
  18. }
  19. value := sess.Get(key)
  20. if value != nil {
  21. placeholder := value.(string)
  22. return &placeholder
  23. }
  24. return nil
  25. }
  26. func artwork_page(c *fiber.Ctx) error {
  27. image_proxy := get_session_value(c, "image-proxy")
  28. if image_proxy == nil {
  29. image_proxy = &configs.ProxyServer
  30. }
  31. id := c.Params("id")
  32. if _, err := strconv.Atoi(id); err != nil {
  33. return errors.New("Bad id")
  34. }
  35. illust, err := PC.GetArtworkByID(id)
  36. if err != nil {
  37. return err
  38. }
  39. illust.ProxyImages(*image_proxy)
  40. // Optimize this
  41. return c.Render("pages/artwork", fiber.Map{
  42. "Illust": illust,
  43. "Title": illust.Title,
  44. "PageType": "artwork",
  45. })
  46. }
  47. func index_page(c *fiber.Ctx) error {
  48. had_token := true
  49. image_proxy := get_session_value(c, "image-proxy")
  50. if image_proxy == nil {
  51. image_proxy = &configs.ProxyServer
  52. }
  53. token := get_session_value(c, "token")
  54. if token == nil {
  55. had_token = false
  56. token = &configs.Token
  57. }
  58. PC := NewPixivClient(5000)
  59. PC.SetSessionID(*token)
  60. PC.SetUserAgent(configs.UserAgent)
  61. PC.AddHeader("Accept-Language", configs.AcceptLanguage)
  62. mode := c.Query("mode", "all")
  63. artworks, err := PC.GetLandingPage(mode)
  64. if err != nil {
  65. return err
  66. }
  67. if had_token {
  68. artworks.Following = models.ProxyShortArtworkSlice(artworks.Following, *image_proxy)
  69. artworks.Commissions = models.ProxyShortArtworkSlice(artworks.Commissions, *image_proxy)
  70. artworks.Recommended = models.ProxyShortArtworkSlice(artworks.Recommended, *image_proxy)
  71. artworks.Newest = models.ProxyShortArtworkSlice(artworks.Newest, *image_proxy)
  72. artworks.Users = models.ProxyShortArtworkSlice(artworks.Users, *image_proxy)
  73. artworks.RecommendByTags = models.ProxyRecommendedByTagsSlice(artworks.RecommendByTags, *image_proxy)
  74. }
  75. artworks.Rankings = models.ProxyShortArtworkSlice(artworks.Rankings, *image_proxy)
  76. artworks.Pixivision = models.ProxyPixivisionSlice(artworks.Pixivision, *image_proxy)
  77. return c.Render("pages/index", fiber.Map{"Title": "Landing", "Artworks": artworks, "Token": had_token})
  78. }
  79. func user_page(c *fiber.Ctx) error {
  80. image_proxy := get_session_value(c, "image-proxy")
  81. if image_proxy == nil {
  82. image_proxy = &configs.ProxyServer
  83. }
  84. id := c.Params("id")
  85. if _, err := strconv.Atoi(id); err != nil {
  86. return err
  87. }
  88. category := c.Params("category", "artworks")
  89. if !(category == "artworks" || category == "illustrations" || category == "manga" || category == "bookmarks") {
  90. return errors.New("Invalid work category: only illustrations, manga, artworks and bookmarks are available")
  91. }
  92. page := c.Query("page", "1")
  93. pageInt, _ := strconv.Atoi(page)
  94. user, err := PC.GetUserInformation(id, category, pageInt)
  95. if err != nil {
  96. return err
  97. }
  98. user.ProxyImages(*image_proxy)
  99. var worksCount int
  100. worksCount = user.ArtworksCount
  101. pageLimit := math.Ceil(float64(worksCount) / 30.0)
  102. return c.Render("pages/user", fiber.Map{"Title": user.Name, "User": user, "Category": category, "PageLimit": int(pageLimit), "Page": pageInt})
  103. }
  104. func ranking_page(c *fiber.Ctx) error {
  105. image_proxy := get_session_value(c, "image-proxy")
  106. if image_proxy == nil {
  107. image_proxy = &configs.ProxyServer
  108. }
  109. queries := make(map[string]string, 4)
  110. queries["Mode"] = c.Query("mode", "daily")
  111. queries["Content"] = c.Query("content", "all")
  112. queries["Date"] = c.Query("date", "")
  113. page := c.Query("page", "1")
  114. pageInt, _ := strconv.Atoi(page)
  115. response, err := PC.GetRanking(queries["Mode"], queries["Content"], queries["Date"], page)
  116. if err != nil {
  117. return err
  118. }
  119. response.ProxyImages(*image_proxy)
  120. return c.Render("pages/rank", fiber.Map{
  121. "Title": "Ranking",
  122. "Data": response,
  123. "Queries": queries,
  124. "Page": pageInt,
  125. })
  126. }
  127. func newest_artworks_page(c *fiber.Ctx) error {
  128. image_proxy := get_session_value(c, "image-proxy")
  129. if image_proxy == nil {
  130. image_proxy = &configs.ProxyServer
  131. }
  132. worktype := c.Query("type", "illust")
  133. r18 := c.Query("r18", "false")
  134. works, err := PC.GetNewestArtworks(worktype, r18)
  135. if err != nil {
  136. return err
  137. }
  138. works = models.ProxyShortArtworkSlice(works, *image_proxy)
  139. return c.Render("pages/newest", fiber.Map{
  140. "Items": works,
  141. "Title": "Newest works",
  142. })
  143. }
  144. func search_page(c *fiber.Ctx) error {
  145. image_proxy := get_session_value(c, "image-proxy")
  146. if image_proxy == nil {
  147. image_proxy = &configs.ProxyServer
  148. }
  149. queries := make(map[string]string, 3)
  150. queries["Mode"] = c.Query("mode", "safe")
  151. queries["Category"] = c.Query("category", "artworks")
  152. queries["Order"] = c.Query("order", "date_d")
  153. name := c.Params("name")
  154. page := c.Query("page", "1")
  155. pageInt, _ := strconv.Atoi(page)
  156. tag, err := PC.GetTagData(name)
  157. if err != nil {
  158. return err
  159. }
  160. if len(tag.Metadata.Image) > 0 {
  161. tag.Metadata.Image = models.ProxyImage(tag.Metadata.Image, *image_proxy)
  162. }
  163. result, err := PC.GetSearch(queries["Category"], name, queries["Order"], queries["Mode"], page)
  164. if err != nil {
  165. return err
  166. }
  167. result.ProxyImages(*image_proxy)
  168. return c.Render("pages/tag", fiber.Map{"Title": "Results for " + tag.Name, "Tag": tag, "Data": result, "Queries": queries, "Page": pageInt})
  169. }
  170. func search(c *fiber.Ctx) error {
  171. name := c.FormValue("name")
  172. return c.Redirect("/tags/"+name, http.StatusFound)
  173. }
  174. func discovery_page(c *fiber.Ctx) error {
  175. image_proxy := get_session_value(c, "image-proxy")
  176. if image_proxy == nil {
  177. image_proxy = &configs.ProxyServer
  178. }
  179. mode := c.Query("mode", "safe")
  180. artworks, err := PC.GetDiscoveryArtwork(mode, 100)
  181. if err != nil {
  182. return err
  183. }
  184. artworks = models.ProxyShortArtworkSlice(artworks, *image_proxy)
  185. return c.Render("pages/discovery", fiber.Map{"Title": "Discovery", "Artworks": artworks})
  186. }
  187. func ranking_log_page(c *fiber.Ctx) error {
  188. image_proxy := get_session_value(c, "image-proxy")
  189. if image_proxy == nil {
  190. image_proxy = &configs.ProxyServer
  191. }
  192. mode := c.Query("mode", "daily")
  193. date := c.Query("date", "")
  194. var year int
  195. var month int
  196. var monthLit string
  197. // If the user supplied a date
  198. if len(date) == 6 {
  199. var err error
  200. year, err = strconv.Atoi(date[:4])
  201. if err != nil {
  202. return err
  203. }
  204. month, err = strconv.Atoi(date[4:])
  205. if err != nil {
  206. return err
  207. }
  208. } else {
  209. now := time.Now()
  210. year = now.Year()
  211. month = int(now.Month())
  212. }
  213. realDate := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
  214. monthLit = realDate.Month().String()
  215. monthBefore := realDate.AddDate(0, -1, 0)
  216. monthAfter := realDate.AddDate(0, 1, 0)
  217. thisMonthLink := fmt.Sprintf("%d%02d", realDate.Year(), realDate.Month())
  218. monthBeforeLink := fmt.Sprintf("%d%02d", monthBefore.Year(), monthBefore.Month())
  219. monthAfterLink := fmt.Sprintf("%d%02d", monthAfter.Year(), monthAfter.Month())
  220. render, err := PC.GetRankingLog(mode, year, month, *image_proxy)
  221. if err != nil {
  222. return err
  223. }
  224. return c.Render("pages/ranking_log", fiber.Map{"Title": "Ranking calendar", "Render": render, "Mode": mode, "Month": monthLit, "Year": year, "MonthBefore": monthBeforeLink, "MonthAfter": monthAfterLink, "ThisMonth": thisMonthLink})
  225. }
  226. func following_works_page(c *fiber.Ctx) error {
  227. image_proxy := get_session_value(c, "image-proxy")
  228. if image_proxy == nil {
  229. image_proxy = &configs.ProxyServer
  230. }
  231. token := get_session_value(c, "token")
  232. if token == nil {
  233. return c.Redirect("/login")
  234. }
  235. queries := make(map[string]string, 2)
  236. queries["Mode"] = c.Query("mode", "all")
  237. queries["Page"] = c.Query("page", "1")
  238. pageInt, _ := strconv.Atoi(queries["Page"])
  239. artworks, err := PC.GetNewestFromFollowing(queries["Mode"], queries["Page"], *token)
  240. if err != nil {
  241. return err
  242. }
  243. artworks = models.ProxyShortArtworkSlice(artworks, *image_proxy)
  244. return c.Render("pages/following", fiber.Map{"Title": "Following works", "Queries": queries, "Artworks": artworks, "Page": pageInt})
  245. }
  246. func your_bookmark_page(c *fiber.Ctx) error {
  247. token := get_session_value(c, "token")
  248. if token == nil {
  249. return c.Redirect("/login")
  250. }
  251. // The left part of the token is the member ID
  252. userId := strings.Split(*token, "_")
  253. c.Redirect("/users/" + userId[0] + "/bookmarks#checkpoint")
  254. return nil
  255. }
  256. func login_page(c *fiber.Ctx) error {
  257. return c.Render("pages/login", fiber.Map{})
  258. }
  259. func settings_page(c *fiber.Ctx) error {
  260. return c.Render("pages/settings", fiber.Map{})
  261. }
  262. func settings_post(c *fiber.Ctx) error {
  263. t := c.Params("type")
  264. error := ""
  265. switch t {
  266. case "image_server":
  267. error = set_image_server(c)
  268. case "token":
  269. error = set_token(c)
  270. case "logout":
  271. error = set_logout(c)
  272. default:
  273. error = "No method available"
  274. }
  275. if error != "" {
  276. return errors.New(error)
  277. }
  278. c.Redirect("/settings")
  279. return nil
  280. }
  281. func get_logged_in_user(c *fiber.Ctx) error {
  282. token := get_session_value(c, "token")
  283. if token == nil {
  284. return c.Redirect("/login")
  285. }
  286. // The left part of the token is the member ID
  287. userId := strings.Split(*token, "_")
  288. c.Redirect("/users/" + userId[0])
  289. return nil
  290. }
  291. func about_page(c *fiber.Ctx) error {
  292. info := fiber.Map{
  293. "Time": configs.StartingTime,
  294. "BaseURL": configs.BaseURL,
  295. "Version": configs.Version,
  296. "ImageProxy": configs.ProxyServer,
  297. "AcceptLanguage": configs.AcceptLanguage,
  298. }
  299. return c.Render("pages/about", info)
  300. }