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-04-30 01:35:04 +02:00
|
|
|
"net/http"
|
2017-11-25 07:29:20 +01:00
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
"github.com/miniflux/miniflux/http/context"
|
|
|
|
"github.com/miniflux/miniflux/http/request"
|
|
|
|
"github.com/miniflux/miniflux/http/response/json"
|
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-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) GetFeedEntry(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-04-30 01:35:04 +02:00
|
|
|
entryID, err := request.IntParam(r, "entryID")
|
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-04-30 01:35:04 +02:00
|
|
|
ctx := context.New(r)
|
|
|
|
userID := ctx.UserID()
|
|
|
|
|
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 {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.ServerError(w, errors.New("Unable to fetch this entry from the database"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NotFound(w, errors.New("Entry not found"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.OK(w, 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-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) GetEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
entryID, err := request.IntParam(r, "entryID")
|
2017-12-22 20:33:01 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-12-22 20:33:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
|
2017-12-22 20:33:01 +01:00
|
|
|
builder.WithEntryID(entryID)
|
|
|
|
|
|
|
|
entry, err := builder.GetEntry()
|
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.ServerError(w, errors.New("Unable to fetch this entry from the database"))
|
2017-12-22 20:33:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NotFound(w, errors.New("Entry not found"))
|
2017-12-22 20:33:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.OK(w, entry)
|
2017-12-22 20:33:01 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
// GetFeedEntries is the API handler to get all feed entries.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) GetFeedEntries(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-04-30 01:35:04 +02:00
|
|
|
status := request.QueryParam(r, "status", "")
|
2017-11-20 06:10:04 +01:00
|
|
|
if status != "" {
|
|
|
|
if err := model.ValidateEntryStatus(status); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
order := request.QueryParam(r, "order", model.DefaultSortingOrder)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := model.ValidateEntryOrder(order); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
direction := request.QueryParam(r, "direction", model.DefaultSortingDirection)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := model.ValidateDirection(direction); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
limit := request.QueryIntParam(r, "limit", 100)
|
|
|
|
offset := request.QueryIntParam(r, "offset", 0)
|
2017-11-27 00:07:59 +01:00
|
|
|
if err := model.ValidateRange(offset, limit); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-27 00:07:59 +01:00
|
|
|
return
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
builder := c.store.NewEntryQueryBuilder(context.New(r).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 {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.ServerError(w, errors.New("Unable to fetch the list of entries"))
|
2017-11-27 00:07:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := builder.CountEntries()
|
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.ServerError(w, errors.New("Unable to count the number of entries"))
|
2017-11-27 00:07:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.OK(w, &entriesResponse{Total: count, Entries: entries})
|
2017-11-27 00:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEntries is the API handler to fetch entries.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) GetEntries(w http.ResponseWriter, r *http.Request) {
|
|
|
|
status := request.QueryParam(r, "status", "")
|
2017-11-27 00:07:59 +01:00
|
|
|
if status != "" {
|
|
|
|
if err := model.ValidateEntryStatus(status); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-27 00:07:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
order := request.QueryParam(r, "order", model.DefaultSortingOrder)
|
2017-11-27 00:07:59 +01:00
|
|
|
if err := model.ValidateEntryOrder(order); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-27 00:07:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
direction := request.QueryParam(r, "direction", model.DefaultSortingDirection)
|
2017-11-27 00:07:59 +01:00
|
|
|
if err := model.ValidateDirection(direction); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-27 00:07:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
limit := request.QueryIntParam(r, "limit", 100)
|
|
|
|
offset := request.QueryIntParam(r, "offset", 0)
|
2017-11-27 00:07:59 +01:00
|
|
|
if err := model.ValidateRange(offset, limit); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-27 00:07:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
builder := c.store.NewEntryQueryBuilder(context.New(r).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 {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.ServerError(w, 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 {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.ServerError(w, errors.New("Unable to count the number of entries"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.OK(w, &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-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) SetEntryStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
entryIDs, status, err := decodeEntryStatusPayload(r.Body)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, errors.New("Invalid JSON payload"))
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := model.ValidateEntryStatus(status); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
if err := c.store.SetEntriesStatus(context.New(r).UserID(), entryIDs, status); err != nil {
|
|
|
|
json.ServerError(w, errors.New("Unable to change entries status"))
|
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
|
|
|
}
|
2017-12-22 20:33:01 +01:00
|
|
|
|
|
|
|
// ToggleBookmark is the API handler to toggle bookmark status.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) {
|
|
|
|
entryID, err := request.IntParam(r, "entryID")
|
2017-12-22 20:33:01 +01:00
|
|
|
if err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
json.BadRequest(w, err)
|
2017-12-22 20:33:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
if err := c.store.ToggleBookmark(context.New(r).UserID(), entryID); err != nil {
|
|
|
|
json.ServerError(w, errors.New("Unable to toggle bookmark value"))
|
2017-12-22 20:33:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
json.NoContent(w)
|
2017-12-22 20:33:01 +01:00
|
|
|
}
|