2017-11-20 06:10:04 +01: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 06:51:50 +02:00
|
|
|
package api // import "miniflux.app/api"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-04-30 01:35:04 +02:00
|
|
|
"net/http"
|
2017-11-26 01:53:51 +01:00
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/json"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateFeed is the API handler to create a new feed.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
|
2018-06-20 07:58:29 +02:00
|
|
|
feedInfo, err := decodeFeedCreationPayload(r.Body)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
if feedInfo.FeedURL == "" {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, errors.New("The feed_url is required"))
|
2017-12-25 03:04:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
if feedInfo.CategoryID <= 0 {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, errors.New("The category_id is required"))
|
2017-12-25 03:04:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
userID := request.UserID(r)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
if c.store.FeedURLExists(userID, feedInfo.FeedURL) {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, errors.New("This feed_url already exists"))
|
2017-12-25 03:04:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
if !c.store.CategoryExists(userID, feedInfo.CategoryID) {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
|
2017-12-25 03:04:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
feed, err := c.feedHandler.CreateFeed(
|
|
|
|
userID,
|
|
|
|
feedInfo.CategoryID,
|
|
|
|
feedInfo.FeedURL,
|
|
|
|
feedInfo.Crawler,
|
2018-09-20 03:19:24 +02:00
|
|
|
feedInfo.UserAgent,
|
2018-06-20 07:58:29 +02:00
|
|
|
feedInfo.Username,
|
|
|
|
feedInfo.Password,
|
|
|
|
)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-08-25 19:04:38 +02:00
|
|
|
json.ServerError(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-26 01:53:51 +01:00
|
|
|
type result struct {
|
|
|
|
FeedID int64 `json:"feed_id"`
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.Created(w, &result{FeedID: feed.ID})
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshFeed is the API handler to refresh a feed.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
feedID, err := request.IntParam(r, "feedID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
userID := request.UserID(r)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2017-12-25 03:04:34 +01:00
|
|
|
if !c.store.FeedExists(userID, feedID) {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NotFound(w, errors.New("Unable to find this feed"))
|
2017-12-25 03:04:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
err = c.feedHandler.RefreshFeed(userID, feedID)
|
|
|
|
if err != nil {
|
2018-08-25 19:04:38 +02:00
|
|
|
json.ServerError(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NoContent(w)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateFeed is the API handler that is used to update a feed.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
feedID, err := request.IntParam(r, "feedID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-24 01:16:54 +02:00
|
|
|
feedChanges, err := decodeFeedModificationPayload(r.Body)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
userID := request.UserID(r)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
originalFeed, err := c.store.FeedByID(userID, feedID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NotFound(w, errors.New("Unable to find this feed"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if originalFeed == nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NotFound(w, errors.New("Feed not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-24 01:16:54 +02:00
|
|
|
feedChanges.Update(originalFeed)
|
|
|
|
|
|
|
|
if !c.store.CategoryExists(userID, originalFeed.Category.ID) {
|
|
|
|
json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := c.store.UpdateFeed(originalFeed); err != nil {
|
2018-08-25 19:04:38 +02:00
|
|
|
json.ServerError(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-25 03:04:34 +01:00
|
|
|
originalFeed, err = c.store.FeedByID(userID, feedID)
|
|
|
|
if err != nil {
|
2018-08-25 19:04:38 +02:00
|
|
|
json.ServerError(w, err)
|
2017-12-25 03:04:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.Created(w, originalFeed)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeeds is the API handler that get all feeds that belongs to the given user.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) {
|
2018-09-03 23:26:40 +02:00
|
|
|
feeds, err := c.store.Feeds(request.UserID(r))
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-08-25 19:04:38 +02:00
|
|
|
json.ServerError(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-20 04:27:05 +02:00
|
|
|
json.OK(w, r, feeds)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeed is the API handler to get a feed.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
feedID, err := request.IntParam(r, "feedID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
feed, err := c.store.FeedByID(request.UserID(r), feedID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-08-25 19:04:38 +02:00
|
|
|
json.ServerError(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if feed == nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NotFound(w, errors.New("Feed not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-20 04:27:05 +02:00
|
|
|
json.OK(w, r, feed)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFeed is the API handler to remove a feed.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) RemoveFeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
feedID, err := request.IntParam(r, "feedID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
userID := request.UserID(r)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
if !c.store.FeedExists(userID, feedID) {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NotFound(w, errors.New("Feed not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.store.RemoveFeed(userID, feedID); err != nil {
|
2018-08-25 19:04:38 +02:00
|
|
|
json.ServerError(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NoContent(w)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|