miniflux/api/feed.go

193 lines
5.1 KiB
Go
Raw Normal View History

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.
package api
import (
"errors"
2018-01-12 22:42:36 +01:00
"github.com/miniflux/miniflux/reader/opml"
"github.com/miniflux/miniflux/http/handler"
2017-11-20 06:10:04 +01:00
)
// CreateFeed is the API handler to create a new feed.
func (c *Controller) CreateFeed(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
feedURL, categoryID, err := decodeFeedCreationPayload(request.Body())
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
2017-12-25 03:04:34 +01:00
if feedURL == "" {
response.JSON().BadRequest(errors.New("The feed_url is required"))
return
}
if categoryID <= 0 {
response.JSON().BadRequest(errors.New("The category_id is required"))
return
}
if c.store.FeedURLExists(userID, feedURL) {
response.JSON().BadRequest(errors.New("This feed_url already exists"))
return
}
if !c.store.CategoryExists(userID, categoryID) {
response.JSON().BadRequest(errors.New("This category_id doesn't exists or doesn't belongs to this user"))
return
}
feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL, false)
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to create this feed"))
2017-11-20 06:10:04 +01:00
return
}
type result struct {
FeedID int64 `json:"feed_id"`
}
response.JSON().Created(&result{FeedID: feed.ID})
2017-11-20 06:10:04 +01:00
}
// RefreshFeed is the API handler to refresh a feed.
func (c *Controller) RefreshFeed(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
2017-12-25 03:04:34 +01:00
if !c.store.FeedExists(userID, feedID) {
response.JSON().NotFound(errors.New("Unable to find this feed"))
return
}
2017-11-20 06:10:04 +01:00
err = c.feedHandler.RefreshFeed(userID, feedID)
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to refresh this feed"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().NoContent()
2017-11-20 06:10:04 +01:00
}
// UpdateFeed is the API handler that is used to update a feed.
func (c *Controller) UpdateFeed(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
newFeed, err := decodeFeedModificationPayload(request.Body())
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
2017-12-25 03:04:34 +01:00
if newFeed.Category != nil && newFeed.Category.ID != 0 && !c.store.CategoryExists(userID, newFeed.Category.ID) {
response.JSON().BadRequest(errors.New("This category_id doesn't exists or doesn't belongs to this user"))
return
}
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 {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Unable to find this feed"))
2017-11-20 06:10:04 +01:00
return
}
if originalFeed == nil {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Feed not found"))
2017-11-20 06:10:04 +01:00
return
}
originalFeed.Merge(newFeed)
if err := c.store.UpdateFeed(originalFeed); err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to update this feed"))
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 {
response.JSON().ServerError(errors.New("Unable to fetch this feed"))
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Created(originalFeed)
2017-11-20 06:10:04 +01:00
}
// GetFeeds is the API handler that get all feeds that belongs to the given user.
func (c *Controller) GetFeeds(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-28 06:30:04 +01:00
feeds, err := c.store.Feeds(ctx.UserID())
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch feeds from the database"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Standard(feeds)
2017-11-20 06:10:04 +01:00
}
2018-01-12 22:42:36 +01:00
// Export is the API handler that incoves an OPML export.
func (c *Controller) Export(ctx *handler.Context, request *handler.Request, response *handler.Response) {
opmlHandler := opml.NewHandler(c.store)
opml, err := opmlHandler.Export(ctx.LoggedUser().ID)
if err != nil {
response.JSON().ServerError(errors.New("unable to export feeds to OPML"))
}
response.XML().Serve(opml)
}
2017-11-20 06:10:04 +01:00
// GetFeed is the API handler to get a feed.
func (c *Controller) GetFeed(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
2017-11-28 06:30:04 +01:00
feed, err := c.store.FeedByID(userID, feedID)
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to fetch this feed"))
2017-11-20 06:10:04 +01:00
return
}
if feed == nil {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Feed not found"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().Standard(feed)
2017-11-20 06:10:04 +01:00
}
// RemoveFeed is the API handler to remove a feed.
func (c *Controller) RemoveFeed(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-22 03:37:08 +01:00
userID := ctx.UserID()
2017-11-22 03:14:45 +01:00
feedID, err := request.IntegerParam("feedID")
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().BadRequest(err)
2017-11-20 06:10:04 +01:00
return
}
if !c.store.FeedExists(userID, feedID) {
2017-11-22 03:30:16 +01:00
response.JSON().NotFound(errors.New("Feed not found"))
2017-11-20 06:10:04 +01:00
return
}
if err := c.store.RemoveFeed(userID, feedID); err != nil {
2017-11-22 03:30:16 +01:00
response.JSON().ServerError(errors.New("Unable to remove this feed"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.JSON().NoContent()
2017-11-20 06:10:04 +01:00
}