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 (
|
|
|
|
"errors"
|
2018-04-29 19:35:04 -04:00
|
|
|
"net/http"
|
2017-11-25 19:53:51 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/json"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) createFeed(w http.ResponseWriter, r *http.Request) {
|
2018-06-20 01:58:29 -04:00
|
|
|
feedInfo, err := decodeFeedCreationPayload(r.Body)
|
2017-11-20 00:10:04 -05:00
|
|
|
if 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-06-20 01:58:29 -04:00
|
|
|
if feedInfo.FeedURL == "" {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, errors.New("The feed_url is required"))
|
2017-12-24 21:04:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
if feedInfo.CategoryID <= 0 {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, errors.New("The category_id is required"))
|
2017-12-24 21:04:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 17:26:40 -04:00
|
|
|
userID := request.UserID(r)
|
2018-04-29 19:35:04 -04:00
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
if h.store.FeedURLExists(userID, feedInfo.FeedURL) {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, errors.New("This feed_url already exists"))
|
2017-12-24 21:04:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
if !h.store.CategoryExists(userID, feedInfo.CategoryID) {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
|
2017-12-24 21:04:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
feed, err := h.feedHandler.CreateFeed(
|
2018-06-20 01:58:29 -04:00
|
|
|
userID,
|
|
|
|
feedInfo.CategoryID,
|
|
|
|
feedInfo.FeedURL,
|
|
|
|
feedInfo.Crawler,
|
2018-09-19 21:19:24 -04:00
|
|
|
feedInfo.UserAgent,
|
2018-06-20 01:58:29 -04:00
|
|
|
feedInfo.Username,
|
|
|
|
feedInfo.Password,
|
2019-11-29 14:17:14 -05:00
|
|
|
feedInfo.ScraperRules,
|
|
|
|
feedInfo.RewriteRules,
|
2018-06-20 01:58:29 -04:00
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-25 19:53:51 -05:00
|
|
|
type result struct {
|
|
|
|
FeedID int64 `json:"feed_id"`
|
|
|
|
}
|
|
|
|
|
2018-10-07 21:42:43 -04:00
|
|
|
json.Created(w, r, &result{FeedID: feed.ID})
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) refreshFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-09-03 17:26:40 -04:00
|
|
|
userID := request.UserID(r)
|
2018-04-29 19:35:04 -04:00
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
err := h.feedHandler.RefreshFeed(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
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) updateFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-06-23 19:16:54 -04:00
|
|
|
feedChanges, err := decodeFeedModificationPayload(r.Body)
|
2017-11-20 00:10:04 -05:00
|
|
|
if 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)
|
2018-04-29 19:35:04 -04:00
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:16:54 -04:00
|
|
|
feedChanges.Update(originalFeed)
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
if !h.store.CategoryExists(userID, originalFeed.Category.ID) {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
|
2018-06-23 19:16:54 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:27:05 -04:00
|
|
|
json.OK(w, r, feeds)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) getFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:27:05 -04:00
|
|
|
json.OK(w, r, feed)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) removeFeed(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2018-09-03 17:26:40 -04:00
|
|
|
userID := request.UserID(r)
|
2018-04-29 19:35:04 -04:00
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
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
|
|
|
}
|