1
0
Fork 0
miniflux/api/feed.go

215 lines
4.7 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 00:51:50 -04:00
package api // import "miniflux.app/api"
2017-11-20 00:10:04 -05:00
import (
2021-01-04 16:49:28 -05:00
json_parser "encoding/json"
"net/http"
2020-11-25 14:46:05 -05:00
"time"
2018-08-25 00:51:50 -04:00
"miniflux.app/http/request"
"miniflux.app/http/response/json"
2021-01-04 16:49:28 -05:00
"miniflux.app/model"
feedHandler "miniflux.app/reader/handler"
2021-01-04 16:49:28 -05:00
"miniflux.app/validator"
2017-11-20 00:10:04 -05:00
)
func (h *handler) createFeed(w http.ResponseWriter, r *http.Request) {
2018-09-03 17:26:40 -04:00
userID := request.UserID(r)
2021-01-04 16:49:28 -05:00
var feedCreationRequest model.FeedCreationRequest
if err := json_parser.NewDecoder(r.Body).Decode(&feedCreationRequest); err != nil {
json.BadRequest(w, r, err)
2017-12-24 21:04:34 -05:00
return
}
2021-01-04 16:49:28 -05:00
if validationErr := validator.ValidateFeedCreation(h.store, userID, &feedCreationRequest); validationErr != nil {
json.BadRequest(w, r, validationErr.Error())
2017-12-24 21:04:34 -05:00
return
}
2021-01-04 16:49:28 -05:00
feed, err := feedHandler.CreateFeed(h.store, userID, &feedCreationRequest)
2017-11-20 00:10:04 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
json.Created(w, r, &feedCreationResponse{FeedID: feed.ID})
2017-11-20 00:10:04 -05:00
}
func (h *handler) refreshFeed(w http.ResponseWriter, r *http.Request) {
feedID := request.RouteInt64Param(r, "feedID")
2018-09-03 17:26:40 -04:00
userID := request.UserID(r)
if !h.store.FeedExists(userID, feedID) {
2018-10-07 21:42:43 -04:00
json.NotFound(w, r)
2017-12-24 21:04:34 -05:00
return
}
err := feedHandler.RefreshFeed(h.store, userID, feedID)
2017-11-20 00:10:04 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
2018-10-07 21:42:43 -04:00
json.NoContent(w, r)
2017-11-20 00:10:04 -05:00
}
func (h *handler) refreshAllFeeds(w http.ResponseWriter, r *http.Request) {
userID := request.UserID(r)
jobs, err := h.store.NewUserBatch(userID, h.store.CountFeeds(userID))
if err != nil {
json.ServerError(w, r, err)
return
}
go func() {
h.pool.Push(jobs)
}()
json.NoContent(w, r)
}
func (h *handler) updateFeed(w http.ResponseWriter, r *http.Request) {
2021-01-04 16:49:28 -05:00
var feedModificationRequest model.FeedModificationRequest
if err := json_parser.NewDecoder(r.Body).Decode(&feedModificationRequest); err != nil {
2018-10-07 21:42:43 -04:00
json.BadRequest(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
2018-09-03 17:26:40 -04:00
userID := request.UserID(r)
2021-01-04 16:49:28 -05:00
feedID := request.RouteInt64Param(r, "feedID")
originalFeed, err := h.store.FeedByID(userID, feedID)
2017-11-20 00:10:04 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.NotFound(w, r)
2017-11-20 00:10:04 -05:00
return
}
if originalFeed == nil {
2018-10-07 21:42:43 -04:00
json.NotFound(w, r)
2017-11-20 00:10:04 -05:00
return
}
2021-01-04 16:49:28 -05:00
if validationErr := validator.ValidateFeedModification(h.store, userID, &feedModificationRequest); validationErr != nil {
json.BadRequest(w, r, validationErr.Error())
return
}
2021-01-04 16:49:28 -05:00
feedModificationRequest.Patch(originalFeed)
if err := h.store.UpdateFeed(originalFeed); err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
originalFeed, err = h.store.FeedByID(userID, feedID)
2017-12-24 21:04:34 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-12-24 21:04:34 -05:00
return
}
2018-10-07 21:42:43 -04:00
json.Created(w, r, originalFeed)
2017-11-20 00:10:04 -05:00
}
2020-11-25 14:46:05 -05: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)
}
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)
}
func (h *handler) getFeeds(w http.ResponseWriter, r *http.Request) {
feeds, err := h.store.Feeds(request.UserID(r))
2017-11-20 00:10:04 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
json.OK(w, r, feeds)
2017-11-20 00:10:04 -05: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)
}
func (h *handler) getFeed(w http.ResponseWriter, r *http.Request) {
feedID := request.RouteInt64Param(r, "feedID")
feed, err := h.store.FeedByID(request.UserID(r), feedID)
2017-11-20 00:10:04 -05:00
if err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
if feed == nil {
2018-10-07 21:42:43 -04:00
json.NotFound(w, r)
2017-11-20 00:10:04 -05:00
return
}
json.OK(w, r, feed)
2017-11-20 00:10:04 -05:00
}
func (h *handler) removeFeed(w http.ResponseWriter, r *http.Request) {
feedID := request.RouteInt64Param(r, "feedID")
2018-09-03 17:26:40 -04:00
userID := request.UserID(r)
if !h.store.FeedExists(userID, feedID) {
2018-10-07 21:42:43 -04:00
json.NotFound(w, r)
2017-11-20 00:10:04 -05:00
return
}
if err := h.store.RemoveFeed(userID, feedID); err != nil {
2018-10-07 21:42:43 -04:00
json.ServerError(w, r, err)
2017-11-20 00:10:04 -05:00
return
}
2018-10-07 21:42:43 -04:00
json.NoContent(w, r)
2017-11-20 00:10:04 -05:00
}