category.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api // import "miniflux.app/v2/internal/api"
  4. import (
  5. json_parser "encoding/json"
  6. "log/slog"
  7. "net/http"
  8. "time"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/http/request"
  11. "miniflux.app/v2/internal/http/response/json"
  12. "miniflux.app/v2/internal/model"
  13. "miniflux.app/v2/internal/validator"
  14. )
  15. func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
  16. userID := request.UserID(r)
  17. var categoryRequest model.CategoryRequest
  18. if err := json_parser.NewDecoder(r.Body).Decode(&categoryRequest); err != nil {
  19. json.BadRequest(w, r, err)
  20. return
  21. }
  22. if validationErr := validator.ValidateCategoryCreation(h.store, userID, &categoryRequest); validationErr != nil {
  23. json.BadRequest(w, r, validationErr.Error())
  24. return
  25. }
  26. category, err := h.store.CreateCategory(userID, &categoryRequest)
  27. if err != nil {
  28. json.ServerError(w, r, err)
  29. return
  30. }
  31. json.Created(w, r, category)
  32. }
  33. func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
  34. userID := request.UserID(r)
  35. categoryID := request.RouteInt64Param(r, "categoryID")
  36. category, err := h.store.Category(userID, categoryID)
  37. if err != nil {
  38. json.ServerError(w, r, err)
  39. return
  40. }
  41. if category == nil {
  42. json.NotFound(w, r)
  43. return
  44. }
  45. var categoryRequest model.CategoryRequest
  46. if err := json_parser.NewDecoder(r.Body).Decode(&categoryRequest); err != nil {
  47. json.BadRequest(w, r, err)
  48. return
  49. }
  50. if validationErr := validator.ValidateCategoryModification(h.store, userID, category.ID, &categoryRequest); validationErr != nil {
  51. json.BadRequest(w, r, validationErr.Error())
  52. return
  53. }
  54. categoryRequest.Patch(category)
  55. err = h.store.UpdateCategory(category)
  56. if err != nil {
  57. json.ServerError(w, r, err)
  58. return
  59. }
  60. json.Created(w, r, category)
  61. }
  62. func (h *handler) markCategoryAsRead(w http.ResponseWriter, r *http.Request) {
  63. userID := request.UserID(r)
  64. categoryID := request.RouteInt64Param(r, "categoryID")
  65. category, err := h.store.Category(userID, categoryID)
  66. if err != nil {
  67. json.ServerError(w, r, err)
  68. return
  69. }
  70. if category == nil {
  71. json.NotFound(w, r)
  72. return
  73. }
  74. if err = h.store.MarkCategoryAsRead(userID, categoryID, time.Now()); err != nil {
  75. json.ServerError(w, r, err)
  76. return
  77. }
  78. json.NoContent(w, r)
  79. }
  80. func (h *handler) getCategories(w http.ResponseWriter, r *http.Request) {
  81. var categories model.Categories
  82. var err error
  83. includeCounts := request.QueryStringParam(r, "counts", "false")
  84. if includeCounts == "true" {
  85. categories, err = h.store.CategoriesWithFeedCount(request.UserID(r))
  86. } else {
  87. categories, err = h.store.Categories(request.UserID(r))
  88. }
  89. if err != nil {
  90. json.ServerError(w, r, err)
  91. return
  92. }
  93. json.OK(w, r, categories)
  94. }
  95. func (h *handler) removeCategory(w http.ResponseWriter, r *http.Request) {
  96. userID := request.UserID(r)
  97. categoryID := request.RouteInt64Param(r, "categoryID")
  98. if !h.store.CategoryIDExists(userID, categoryID) {
  99. json.NotFound(w, r)
  100. return
  101. }
  102. if err := h.store.RemoveCategory(userID, categoryID); err != nil {
  103. json.ServerError(w, r, err)
  104. return
  105. }
  106. json.NoContent(w, r)
  107. }
  108. func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) {
  109. userID := request.UserID(r)
  110. categoryID := request.RouteInt64Param(r, "categoryID")
  111. batchBuilder := h.store.NewBatchBuilder()
  112. batchBuilder.WithErrorLimit(config.Opts.PollingParsingErrorLimit())
  113. batchBuilder.WithoutDisabledFeeds()
  114. batchBuilder.WithUserID(userID)
  115. batchBuilder.WithCategoryID(categoryID)
  116. batchBuilder.WithNextCheckExpired()
  117. jobs, err := batchBuilder.FetchJobs()
  118. if err != nil {
  119. json.ServerError(w, r, err)
  120. return
  121. }
  122. slog.Info(
  123. "Triggered a manual refresh of all feeds for a given category from the API",
  124. slog.Int64("user_id", userID),
  125. slog.Int64("category_id", categoryID),
  126. slog.Int("nb_jobs", len(jobs)),
  127. )
  128. go h.pool.Push(jobs)
  129. json.NoContent(w, r)
  130. }