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"
|
2018-06-09 22:13:41 -04:00
|
|
|
"time"
|
2017-11-25 01:29:20 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/json"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) getFeedEntry(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
|
|
|
entryID := request.RouteInt64Param(r, "entryID")
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
builder := h.store.NewEntryQueryBuilder(request.UserID(r))
|
2017-11-20 00:10:04 -05:00
|
|
|
builder.WithFeedID(feedID)
|
|
|
|
builder.WithEntryID(entryID)
|
|
|
|
|
|
|
|
entry, err := builder.GetEntry()
|
|
|
|
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 entry == 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, entry)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) getEntry(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
entryID := request.RouteInt64Param(r, "entryID")
|
2018-11-11 13:22:47 -05:00
|
|
|
builder := h.store.NewEntryQueryBuilder(request.UserID(r))
|
2017-12-22 14:33:01 -05:00
|
|
|
builder.WithEntryID(entryID)
|
|
|
|
|
|
|
|
entry, err := builder.GetEntry()
|
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.ServerError(w, r, err)
|
2017-12-22 14:33:01 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.NotFound(w, r)
|
2017-12-22 14:33:01 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:27:05 -04:00
|
|
|
json.OK(w, r, entry)
|
2017-12-22 14:33:01 -05:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) getFeedEntries(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
feedID := request.RouteInt64Param(r, "feedID")
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
status := request.QueryStringParam(r, "status", "")
|
2017-11-20 00:10:04 -05:00
|
|
|
if status != "" {
|
|
|
|
if err := model.ValidateEntryStatus(status); 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-24 00:02:26 -04:00
|
|
|
order := request.QueryStringParam(r, "order", model.DefaultSortingOrder)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := model.ValidateEntryOrder(order); 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-24 00:02:26 -04:00
|
|
|
direction := request.QueryStringParam(r, "direction", model.DefaultSortingDirection)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := model.ValidateDirection(direction); 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-04-29 19:35:04 -04:00
|
|
|
limit := request.QueryIntParam(r, "limit", 100)
|
|
|
|
offset := request.QueryIntParam(r, "offset", 0)
|
2017-11-26 18:07:59 -05:00
|
|
|
if err := model.ValidateRange(offset, limit); err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, err)
|
2017-11-26 18:07:59 -05:00
|
|
|
return
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
builder := h.store.NewEntryQueryBuilder(request.UserID(r))
|
2017-11-20 00:10:04 -05:00
|
|
|
builder.WithFeedID(feedID)
|
|
|
|
builder.WithStatus(status)
|
2017-11-26 18:07:59 -05:00
|
|
|
builder.WithOrder(order)
|
|
|
|
builder.WithDirection(direction)
|
|
|
|
builder.WithOffset(offset)
|
|
|
|
builder.WithLimit(limit)
|
2018-06-09 22:13:41 -04:00
|
|
|
configureFilters(builder, r)
|
2017-11-26 18:07:59 -05:00
|
|
|
|
|
|
|
entries, err := builder.GetEntries()
|
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.ServerError(w, r, err)
|
2017-11-26 18:07:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := builder.CountEntries()
|
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.ServerError(w, r, err)
|
2017-11-26 18:07:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:27:05 -04:00
|
|
|
json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
|
2017-11-26 18:07:59 -05:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
status := request.QueryStringParam(r, "status", "")
|
2017-11-26 18:07:59 -05:00
|
|
|
if status != "" {
|
|
|
|
if err := model.ValidateEntryStatus(status); err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, err)
|
2017-11-26 18:07:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
order := request.QueryStringParam(r, "order", model.DefaultSortingOrder)
|
2017-11-26 18:07:59 -05:00
|
|
|
if err := model.ValidateEntryOrder(order); err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, err)
|
2017-11-26 18:07:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
direction := request.QueryStringParam(r, "direction", model.DefaultSortingDirection)
|
2017-11-26 18:07:59 -05:00
|
|
|
if err := model.ValidateDirection(direction); err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, err)
|
2017-11-26 18:07:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
limit := request.QueryIntParam(r, "limit", 100)
|
|
|
|
offset := request.QueryIntParam(r, "offset", 0)
|
2017-11-26 18:07:59 -05:00
|
|
|
if err := model.ValidateRange(offset, limit); err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.BadRequest(w, r, err)
|
2017-11-26 18:07:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
builder := h.store.NewEntryQueryBuilder(request.UserID(r))
|
2017-11-26 18:07:59 -05:00
|
|
|
builder.WithStatus(status)
|
|
|
|
builder.WithOrder(order)
|
|
|
|
builder.WithDirection(direction)
|
2017-11-20 00:10:04 -05:00
|
|
|
builder.WithOffset(offset)
|
|
|
|
builder.WithLimit(limit)
|
2018-06-09 22:13:41 -04:00
|
|
|
configureFilters(builder, r)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
entries, err := builder.GetEntries()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := builder.CountEntries()
|
|
|
|
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, &entriesResponse{Total: count, Entries: entries})
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) setEntryStatus(w http.ResponseWriter, r *http.Request) {
|
2018-04-29 19:35:04 -04:00
|
|
|
entryIDs, status, err := decodeEntryStatusPayload(r.Body)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2019-11-18 01:53:11 -05:00
|
|
|
json.BadRequest(w, r, errors.New("Invalid JSON payload"))
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := model.ValidateEntryStatus(status); 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-11-11 13:22:47 -05:00
|
|
|
if err := h.store.SetEntriesStatus(request.UserID(r), entryIDs, status); 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
|
|
|
}
|
2017-12-22 14:33:01 -05:00
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
func (h *handler) toggleBookmark(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:02:26 -04:00
|
|
|
entryID := request.RouteInt64Param(r, "entryID")
|
2018-11-11 13:22:47 -05:00
|
|
|
if err := h.store.ToggleBookmark(request.UserID(r), entryID); err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.ServerError(w, r, err)
|
2017-12-22 14:33:01 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-07 21:42:43 -04:00
|
|
|
json.NoContent(w, r)
|
2017-12-22 14:33:01 -05:00
|
|
|
}
|
2018-06-09 22:13:41 -04:00
|
|
|
|
|
|
|
func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
|
|
|
|
beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0)
|
2019-11-18 01:53:11 -05:00
|
|
|
if beforeEntryID > 0 {
|
2018-06-09 22:13:41 -04:00
|
|
|
builder.BeforeEntryID(beforeEntryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0)
|
2019-11-18 01:53:11 -05:00
|
|
|
if afterEntryID > 0 {
|
2018-06-09 22:13:41 -04:00
|
|
|
builder.AfterEntryID(afterEntryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeTimestamp := request.QueryInt64Param(r, "before", 0)
|
2019-11-18 01:53:11 -05:00
|
|
|
if beforeTimestamp > 0 {
|
2018-06-09 22:13:41 -04:00
|
|
|
builder.BeforeDate(time.Unix(beforeTimestamp, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
afterTimestamp := request.QueryInt64Param(r, "after", 0)
|
2019-11-18 01:53:11 -05:00
|
|
|
if afterTimestamp > 0 {
|
2018-06-09 22:13:41 -04:00
|
|
|
builder.AfterDate(time.Unix(afterTimestamp, 0))
|
|
|
|
}
|
|
|
|
|
2019-11-18 01:53:11 -05:00
|
|
|
categoryID := request.QueryInt64Param(r, "category_id", 0)
|
|
|
|
if categoryID > 0 {
|
|
|
|
builder.WithCategoryID(categoryID)
|
|
|
|
}
|
|
|
|
|
2018-06-09 22:13:41 -04:00
|
|
|
if request.HasQueryParam(r, "starred") {
|
|
|
|
builder.WithStarred()
|
|
|
|
}
|
2018-07-04 20:40:03 -04:00
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
searchQuery := request.QueryStringParam(r, "search", "")
|
2018-07-04 20:40:03 -04:00
|
|
|
if searchQuery != "" {
|
|
|
|
builder.WithSearchQuery(searchQuery)
|
|
|
|
}
|
2018-06-09 22:13:41 -04:00
|
|
|
}
|