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"
|
2017-11-25 07:29:20 +01:00
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
"github.com/miniflux/miniflux/http/handler"
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-12-22 20:33:01 +01:00
|
|
|
// GetFeedEntry is the API handler to get a single feed entry.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) GetFeedEntry(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-22 03:14:45 +01:00
|
|
|
entryID, err := request.IntegerParam("entryID")
|
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-29 04:20:14 +01:00
|
|
|
builder := c.store.NewEntryQueryBuilder(userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
builder.WithFeedID(feedID)
|
|
|
|
builder.WithEntryID(entryID)
|
|
|
|
|
|
|
|
entry, err := builder.GetEntry()
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().NotFound(errors.New("Entry not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().Standard(entry)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 20:33:01 +01:00
|
|
|
// GetEntry is the API handler to get a single entry.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) GetEntry(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-12-22 20:33:01 +01:00
|
|
|
userID := ctx.UserID()
|
|
|
|
entryID, err := request.IntegerParam("entryID")
|
|
|
|
if err != nil {
|
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:20:14 +01:00
|
|
|
builder := c.store.NewEntryQueryBuilder(userID)
|
2017-12-22 20:33:01 +01:00
|
|
|
builder.WithEntryID(entryID)
|
|
|
|
|
|
|
|
entry, err := builder.GetEntry()
|
|
|
|
if err != nil {
|
|
|
|
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
|
|
|
response.JSON().NotFound(errors.New("Entry not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.JSON().Standard(entry)
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
// GetFeedEntries is the API handler to get all feed entries.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) GetFeedEntries(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-22 03:14:45 +01:00
|
|
|
status := request.QueryStringParam("status", "")
|
2017-11-20 06:10:04 +01:00
|
|
|
if status != "" {
|
|
|
|
if err := model.ValidateEntryStatus(status); 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-27 00:07:59 +01:00
|
|
|
order := request.QueryStringParam("order", model.DefaultSortingOrder)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := model.ValidateEntryOrder(order); 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-27 00:07:59 +01:00
|
|
|
direction := request.QueryStringParam("direction", model.DefaultSortingDirection)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := model.ValidateDirection(direction); 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-22 03:14:45 +01:00
|
|
|
limit := request.QueryIntegerParam("limit", 100)
|
|
|
|
offset := request.QueryIntegerParam("offset", 0)
|
2017-11-27 00:07:59 +01:00
|
|
|
if err := model.ValidateRange(offset, limit); err != nil {
|
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-12-29 04:20:14 +01:00
|
|
|
builder := c.store.NewEntryQueryBuilder(userID)
|
2017-11-20 06:10:04 +01:00
|
|
|
builder.WithFeedID(feedID)
|
|
|
|
builder.WithStatus(status)
|
2017-11-27 00:07:59 +01:00
|
|
|
builder.WithOrder(order)
|
|
|
|
builder.WithDirection(direction)
|
|
|
|
builder.WithOffset(offset)
|
|
|
|
builder.WithLimit(limit)
|
|
|
|
|
|
|
|
entries, err := builder.GetEntries()
|
|
|
|
if err != nil {
|
|
|
|
response.JSON().ServerError(errors.New("Unable to fetch the list of entries"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := builder.CountEntries()
|
|
|
|
if err != nil {
|
|
|
|
response.JSON().ServerError(errors.New("Unable to count the number of entries"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
response.JSON().Standard(&entriesResponse{Total: count, Entries: entries})
|
2017-11-27 00:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEntries is the API handler to fetch entries.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) GetEntries(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-27 00:07:59 +01:00
|
|
|
userID := ctx.UserID()
|
|
|
|
|
|
|
|
status := request.QueryStringParam("status", "")
|
|
|
|
if status != "" {
|
|
|
|
if err := model.ValidateEntryStatus(status); err != nil {
|
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
order := request.QueryStringParam("order", model.DefaultSortingOrder)
|
|
|
|
if err := model.ValidateEntryOrder(order); err != nil {
|
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := request.QueryStringParam("direction", model.DefaultSortingDirection)
|
|
|
|
if err := model.ValidateDirection(direction); err != nil {
|
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limit := request.QueryIntegerParam("limit", 100)
|
|
|
|
offset := request.QueryIntegerParam("offset", 0)
|
|
|
|
if err := model.ValidateRange(offset, limit); err != nil {
|
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:20:14 +01:00
|
|
|
builder := c.store.NewEntryQueryBuilder(userID)
|
2017-11-27 00:07:59 +01:00
|
|
|
builder.WithStatus(status)
|
|
|
|
builder.WithOrder(order)
|
|
|
|
builder.WithDirection(direction)
|
2017-11-20 06:10:04 +01:00
|
|
|
builder.WithOffset(offset)
|
|
|
|
builder.WithLimit(limit)
|
|
|
|
|
|
|
|
entries, err := builder.GetEntries()
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to fetch the list of entries"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := builder.CountEntries()
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().ServerError(errors.New("Unable to count the number of entries"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
response.JSON().Standard(&entriesResponse{Total: count, Entries: entries})
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 07:29:20 +01:00
|
|
|
// SetEntryStatus is the API handler to change the status of entries.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) SetEntryStatus(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
userID := ctx.UserID()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
entryIDs, status, err := decodeEntryStatusPayload(request.Body())
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.JSON().BadRequest(errors.New("Invalid JSON payload"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := model.ValidateEntryStatus(status); 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-25 07:29:20 +01:00
|
|
|
if err := c.store.SetEntriesStatus(userID, entryIDs, status); err != nil {
|
|
|
|
response.JSON().ServerError(errors.New("Unable to change entries status"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-25 07:29:20 +01:00
|
|
|
response.JSON().NoContent()
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2017-12-22 20:33:01 +01:00
|
|
|
|
|
|
|
// ToggleBookmark is the API handler to toggle bookmark status.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) ToggleBookmark(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-12-22 20:33:01 +01:00
|
|
|
userID := ctx.UserID()
|
|
|
|
entryID, err := request.IntegerParam("entryID")
|
|
|
|
if err != nil {
|
|
|
|
response.JSON().BadRequest(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.store.ToggleBookmark(userID, entryID); err != nil {
|
|
|
|
response.JSON().ServerError(errors.New("Unable to toggle bookmark value"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.JSON().NoContent()
|
|
|
|
}
|