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-04 13:49:28 -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-25 16:53:51 -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"
|
|
|
|
feedHandler "miniflux.app/v2/internal/reader/handler"
|
|
|
|
"miniflux.app/v2/internal/validator"
|
2017-11-19 21:10:04 -08:00
|
|
|
)
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) createFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-03 14:26:40 -07:00
|
|
|
userID := request.UserID(r)
|
2018-04-29 16:35:04 -07:00
|
|
|
|
2021-01-04 13:49:28 -08:00
|
|
|
var feedCreationRequest model.FeedCreationRequest
|
|
|
|
if err := json_parser.NewDecoder(r.Body).Decode(&feedCreationRequest); err != nil {
|
|
|
|
json.BadRequest(w, r, err)
|
2017-12-24 18:04:34 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-05 21:18:00 -07:00
|
|
|
// Make the feed category optional for clients who don't support categories.
|
|
|
|
if feedCreationRequest.CategoryID == 0 {
|
|
|
|
category, err := h.store.FirstCategory(userID)
|
|
|
|
if err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
feedCreationRequest.CategoryID = category.ID
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:49:28 -08:00
|
|
|
if validationErr := validator.ValidateFeedCreation(h.store, userID, &feedCreationRequest); validationErr != nil {
|
|
|
|
json.BadRequest(w, r, validationErr.Error())
|
2017-12-24 18:04:34 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:50:29 -07:00
|
|
|
feed, localizedError := feedHandler.CreateFeed(h.store, userID, &feedCreationRequest)
|
|
|
|
if localizedError != nil {
|
|
|
|
json.ServerError(w, r, localizedError.Error())
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-13 20:31:19 -08:00
|
|
|
json.Created(w, r, &feedCreationResponse{FeedID: feed.ID})
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) refreshFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-23 21:02:26 -07:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-09-03 14:26:40 -07:00
|
|
|
userID := request.UserID(r)
|
2018-04-29 16:35:04 -07:00
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
if !h.store.FeedExists(userID, feedID) {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.NotFound(w, r)
|
2017-12-24 18:04:34 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:50:29 -07:00
|
|
|
localizedError := feedHandler.RefreshFeed(h.store, userID, feedID, false)
|
|
|
|
if localizedError != nil {
|
|
|
|
json.ServerError(w, r, localizedError.Error())
|
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
|
|
|
}
|
|
|
|
|
2020-02-26 21:32:18 +00:00
|
|
|
func (h *handler) refreshAllFeeds(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userID := request.UserID(r)
|
2023-10-20 15:12:02 -07:00
|
|
|
|
|
|
|
batchBuilder := h.store.NewBatchBuilder()
|
|
|
|
batchBuilder.WithErrorLimit(config.Opts.PollingParsingErrorLimit())
|
|
|
|
batchBuilder.WithoutDisabledFeeds()
|
|
|
|
batchBuilder.WithNextCheckExpired()
|
|
|
|
batchBuilder.WithUserID(userID)
|
|
|
|
|
|
|
|
jobs, err := batchBuilder.FetchJobs()
|
2020-02-26 21:32:18 +00: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 from the API",
|
|
|
|
slog.Int64("user_id", userID),
|
|
|
|
slog.Int("nb_jobs", len(jobs)),
|
|
|
|
)
|
|
|
|
|
|
|
|
go h.pool.Push(jobs)
|
2020-02-26 21:32:18 +00:00
|
|
|
|
|
|
|
json.NoContent(w, r)
|
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) updateFeed(w http.ResponseWriter, r *http.Request) {
|
2021-01-04 13:49:28 -08:00
|
|
|
var feedModificationRequest model.FeedModificationRequest
|
|
|
|
if err := json_parser.NewDecoder(r.Body).Decode(&feedModificationRequest); err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.BadRequest(w, r, err)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:26:40 -07:00
|
|
|
userID := request.UserID(r)
|
2021-01-04 13:49:28 -08:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-04-29 16:35:04 -07:00
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
originalFeed, err := h.store.FeedByID(userID, feedID)
|
2017-11-19 21:10:04 -08:00
|
|
|
if err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.NotFound(w, r)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if originalFeed == nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.NotFound(w, r)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-15 18:17:13 -07:00
|
|
|
if validationErr := validator.ValidateFeedModification(h.store, userID, originalFeed.ID, &feedModificationRequest); validationErr != nil {
|
2021-01-04 13:49:28 -08:00
|
|
|
json.BadRequest(w, r, validationErr.Error())
|
2018-06-23 16:16:54 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:49:28 -08:00
|
|
|
feedModificationRequest.Patch(originalFeed)
|
2018-11-11 10:22:47 -08:00
|
|
|
if err := h.store.UpdateFeed(originalFeed); 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-11-11 10:22:47 -08:00
|
|
|
originalFeed, err = h.store.FeedByID(userID, feedID)
|
2017-12-24 18:04:34 -08:00
|
|
|
if err != nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.ServerError(w, r, err)
|
2017-12-24 18:04:34 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-07 18:42:43 -07:00
|
|
|
json.Created(w, r, originalFeed)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2020-11-25 11:46:05 -08:00
|
|
|
func (h *handler) markFeedAsRead(w http.ResponseWriter, r *http.Request) {
|
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
|
|
|
userID := request.UserID(r)
|
|
|
|
|
|
|
|
feed, err := h.store.FeedByID(userID, feedID)
|
|
|
|
if err != nil {
|
|
|
|
json.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if feed == nil {
|
|
|
|
json.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.store.MarkFeedAsRead(userID, feedID, time.Now()); err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NoContent(w, r)
|
|
|
|
}
|
|
|
|
|
2021-01-18 21:44:02 -06:00
|
|
|
func (h *handler) getCategoryFeeds(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
|
|
|
|
}
|
|
|
|
|
|
|
|
feeds, err := h.store.FeedsByCategoryWithCounters(userID, categoryID)
|
|
|
|
if err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.OK(w, r, feeds)
|
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) getFeeds(w http.ResponseWriter, r *http.Request) {
|
|
|
|
feeds, err := h.store.Feeds(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, feeds)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2022-05-21 20:44:56 +02:00
|
|
|
func (h *handler) fetchCounters(w http.ResponseWriter, r *http.Request) {
|
|
|
|
counters, err := h.store.FetchCounters(request.UserID(r))
|
|
|
|
if err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.OK(w, r, counters)
|
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) getFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-23 21:02:26 -07:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-11-11 10:22:47 -08:00
|
|
|
feed, err := h.store.FeedByID(request.UserID(r), feedID)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if feed == nil {
|
2018-10-07 18:42:43 -07:00
|
|
|
json.NotFound(w, r)
|
2017-11-19 21:10:04 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-19 19:27:05 -07:00
|
|
|
json.OK(w, r, feed)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
func (h *handler) removeFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-23 21:02:26 -07:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-09-03 14:26:40 -07:00
|
|
|
userID := request.UserID(r)
|
2018-04-29 16:35:04 -07:00
|
|
|
|
2018-11-11 10:22:47 -08:00
|
|
|
if !h.store.FeedExists(userID, feedID) {
|
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.RemoveFeed(userID, feedID); 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
|
|
|
}
|