4
0

category.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 categoryCreationRequest model.CategoryCreationRequest
  18. if err := json_parser.NewDecoder(r.Body).Decode(&categoryCreationRequest); err != nil {
  19. json.BadRequest(w, r, err)
  20. return
  21. }
  22. if validationErr := validator.ValidateCategoryCreation(h.store, userID, &categoryCreationRequest); validationErr != nil {
  23. json.BadRequest(w, r, validationErr.Error())
  24. return
  25. }
  26. category, err := h.store.CreateCategory(userID, &categoryCreationRequest)
  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 categoryModificationRequest model.CategoryModificationRequest
  46. if err := json_parser.NewDecoder(r.Body).Decode(&categoryModificationRequest); err != nil {
  47. json.BadRequest(w, r, err)
  48. return
  49. }
  50. if validationErr := validator.ValidateCategoryModification(h.store, userID, category.ID, &categoryModificationRequest); validationErr != nil {
  51. json.BadRequest(w, r, validationErr.Error())
  52. return
  53. }
  54. categoryModificationRequest.Patch(category)
  55. if err := h.store.UpdateCategory(category); err != nil {
  56. json.ServerError(w, r, err)
  57. return
  58. }
  59. json.Created(w, r, category)
  60. }
  61. func (h *handler) markCategoryAsRead(w http.ResponseWriter, r *http.Request) {
  62. userID := request.UserID(r)
  63. categoryID := request.RouteInt64Param(r, "categoryID")
  64. category, err := h.store.Category(userID, categoryID)
  65. if err != nil {
  66. json.ServerError(w, r, err)
  67. return
  68. }
  69. if category == nil {
  70. json.NotFound(w, r)
  71. return
  72. }
  73. if err = h.store.MarkCategoryAsRead(userID, categoryID, time.Now()); err != nil {
  74. json.ServerError(w, r, err)
  75. return
  76. }
  77. json.NoContent(w, r)
  78. }
  79. func (h *handler) getCategories(w http.ResponseWriter, r *http.Request) {
  80. var categories model.Categories
  81. var err error
  82. includeCounts := request.QueryStringParam(r, "counts", "false")
  83. if includeCounts == "true" {
  84. categories, err = h.store.CategoriesWithFeedCount(request.UserID(r))
  85. } else {
  86. categories, err = h.store.Categories(request.UserID(r))
  87. }
  88. if err != nil {
  89. json.ServerError(w, r, err)
  90. return
  91. }
  92. json.OK(w, r, categories)
  93. }
  94. func (h *handler) removeCategory(w http.ResponseWriter, r *http.Request) {
  95. userID := request.UserID(r)
  96. categoryID := request.RouteInt64Param(r, "categoryID")
  97. if !h.store.CategoryIDExists(userID, categoryID) {
  98. json.NotFound(w, r)
  99. return
  100. }
  101. if err := h.store.RemoveCategory(userID, categoryID); err != nil {
  102. json.ServerError(w, r, err)
  103. return
  104. }
  105. json.NoContent(w, r)
  106. }
  107. func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) {
  108. userID := request.UserID(r)
  109. categoryID := request.RouteInt64Param(r, "categoryID")
  110. batchBuilder := h.store.NewBatchBuilder()
  111. batchBuilder.WithErrorLimit(config.Opts.PollingParsingErrorLimit())
  112. batchBuilder.WithoutDisabledFeeds()
  113. batchBuilder.WithUserID(userID)
  114. batchBuilder.WithCategoryID(categoryID)
  115. batchBuilder.WithNextCheckExpired()
  116. batchBuilder.WithLimitPerHost(config.Opts.PollingLimitPerHost())
  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. }