2023-06-19 14:42:47 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2023-08-10 19:46:45 -07:00
|
|
|
package api // import "miniflux.app/v2/internal/api"
|
2017-11-19 21:10:04 -08:00
|
|
|
|
|
|
|
import (
|
2021-01-03 22:28:04 -08:00
|
|
|
json_parser "encoding/json"
|
2023-10-16 21:20:58 -07:00
|
|
|
"log/slog"
|
2018-04-29 16:35:04 -07:00
|
|
|
"net/http"
|
2020-11-25 11:46:05 -08:00
|
|
|
"time"
|
2017-11-27 21:30:04 -08:00
|
|
|
|
2023-10-20 15:12:02 -07:00
|
|
|
"miniflux.app/v2/internal/config"
|
2023-08-10 19:46:45 -07:00
|
|
|
"miniflux.app/v2/internal/http/request"
|
|
|
|
"miniflux.app/v2/internal/http/response/json"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/validator"
|
2017-11-19 21:10:04 -08:00
|
|
|
)
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) createCategory(w http.ResponseWriter, r *http.Request) {
|
2020-12-13 18:50:28 -08:00
|
|
|
userID := request.UserID(r)
|
|
|
|
|
2021-01-03 22:28:04 -08:00
|
|
|
var categoryRequest model.CategoryRequest
|
|
|
|
if err := json_parser.NewDecoder(r.Body).Decode(&categoryRequest); err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.BadRequest(w, r, err)
|
2017-12-24 18:04:34 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:28:04 -08:00
|
|
|
if validationErr := validator.ValidateCategoryCreation(h.store, userID, &categoryRequest); validationErr != nil {
|
|
|
|
json.BadRequest(w, r, validationErr.Error())
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:28:04 -08:00
|
|
|
category, err := h.store.CreateCategory(userID, &categoryRequest)
|
|
|
|
if err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.ServerError(w, r, err)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-07 18:42:43 -07:00
|
|
|
json.Created(w, r, category)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) updateCategory(w http.ResponseWriter, r *http.Request) {
|
2020-12-13 18:50:28 -08:00
|
|
|
userID := request.UserID(r)
|
2018-09-23 21:02:26 -07:00
|
|
|
categoryID := request.RouteInt64Param(r, "categoryID")
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2020-12-13 18:50:28 -08:00
|
|
|
category, err := h.store.Category(userID, categoryID)
|
|
|
|
if err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if category == nil {
|
|
|
|
json.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:28:04 -08:00
|
|
|
var categoryRequest model.CategoryRequest
|
|
|
|
if err := json_parser.NewDecoder(r.Body).Decode(&categoryRequest); err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.BadRequest(w, r, err)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:28:04 -08:00
|
|
|
if validationErr := validator.ValidateCategoryModification(h.store, userID, category.ID, &categoryRequest); validationErr != nil {
|
|
|
|
json.BadRequest(w, r, validationErr.Error())
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:28:04 -08:00
|
|
|
categoryRequest.Patch(category)
|
2018-11-11 10:22:47 -08:00
|
|
|
err = h.store.UpdateCategory(category)
|
2017-11-19 21:10:04 -08:00
|
|
|
if err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.ServerError(w, r, err)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-07 18:42:43 -07:00
|
|
|
json.Created(w, r, category)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2020-11-25 11:46:05 -08:00
|
|
|
func (h *handler) markCategoryAsRead(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID := request.UserID(r)
|
|
|
|
categoryID := request.RouteInt64Param(r, "categoryID")
|
|
|
|
|
|
|
|
category, err := h.store.Category(userID, categoryID)
|
|
|
|
if err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if category == nil {
|
|
|
|
json.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.store.MarkCategoryAsRead(userID, categoryID, time.Now()); err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NoContent(w, r)
|
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) getCategories(w http.ResponseWriter, r *http.Request) {
|
2023-06-19 09:50:08 -07:00
|
|
|
var categories model.Categories
|
|
|
|
var err error
|
|
|
|
includeCounts := request.QueryStringParam(r, "counts", "false")
|
|
|
|
|
|
|
|
if includeCounts == "true" {
|
|
|
|
categories, err = h.store.CategoriesWithFeedCount(request.UserID(r))
|
|
|
|
} else {
|
|
|
|
categories, err = h.store.Categories(request.UserID(r))
|
|
|
|
}
|
|
|
|
|
2017-11-19 21:10:04 -08:00
|
|
|
if err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.ServerError(w, r, err)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
2018-07-19 19:27:05 -07:00
|
|
|
json.OK(w, r, categories)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) removeCategory(w http.ResponseWriter, r *http.Request) {
|
2018-09-03 14:26:40 -07:00
|
|
|
userID := request.UserID(r)
|
2018-09-23 21:02:26 -07:00
|
|
|
categoryID := request.RouteInt64Param(r, "categoryID")
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2021-01-03 22:28:04 -08:00
|
|
|
if !h.store.CategoryIDExists(userID, categoryID) {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.NotFound(w, r)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
if err := h.store.RemoveCategory(userID, categoryID); err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.ServerError(w, r, err)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-07 18:42:43 -07:00
|
|
|
json.NoContent(w, r)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
2022-12-11 18:07:01 +01:00
|
|
|
|
|
|
|
func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID := request.UserID(r)
|
|
|
|
categoryID := request.RouteInt64Param(r, "categoryID")
|
|
|
|
|
2023-10-20 15:12:02 -07:00
|
|
|
batchBuilder := h.store.NewBatchBuilder()
|
|
|
|
batchBuilder.WithErrorLimit(config.Opts.PollingParsingErrorLimit())
|
|
|
|
batchBuilder.WithoutDisabledFeeds()
|
|
|
|
batchBuilder.WithUserID(userID)
|
|
|
|
batchBuilder.WithCategoryID(categoryID)
|
|
|
|
batchBuilder.WithNextCheckExpired()
|
|
|
|
|
|
|
|
jobs, err := batchBuilder.FetchJobs()
|
2022-12-11 18:07:01 +01:00
|
|
|
if err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-16 21:20:58 -07:00
|
|
|
slog.Info(
|
|
|
|
"Triggered a manual refresh of all feeds for a given category from the API",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int64("category_id", categoryID),
|
|
|
|
slog.Int("nb_jobs", len(jobs)),
|
|
|
|
)
|
|
|
|
|
|
|
|
go h.pool.Push(jobs)
|
2022-12-11 18:07:01 +01:00
|
|
|
|
|
|
|
json.NoContent(w, r)
|
|
|
|
}
|