miniflux/api/entry.go

201 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.
2018-08-25 06:51:50 +02:00
package api // import "miniflux.app/api"
2017-11-20 06:10:04 +01:00
import (
2021-01-05 00:32:32 +01:00
json_parser "encoding/json"
2017-11-20 06:10:04 +01:00
"errors"
"net/http"
"time"
2017-11-25 07:29:20 +01:00
2018-08-25 06:51:50 +02:00
"miniflux.app/http/request"
"miniflux.app/http/response/json"
"miniflux.app/model"
"miniflux.app/storage"
2021-01-05 00:32:32 +01:00
"miniflux.app/validator"
2017-11-20 06:10:04 +01:00
)
func (h *handler) getFeedEntry(w http.ResponseWriter, r *http.Request) {
feedID := request.RouteInt64Param(r, "feedID")
entryID := request.RouteInt64Param(r, "entryID")
2017-11-20 06:10:04 +01:00
builder := h.store.NewEntryQueryBuilder(request.UserID(r))
2017-11-20 06:10:04 +01:00
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
if err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
if entry == nil {
2018-10-08 03:42:43 +02:00
json.NotFound(w, r)
2017-11-20 06:10:04 +01:00
return
}
json.OK(w, r, entry)
2017-11-20 06:10:04 +01:00
}
func (h *handler) getEntry(w http.ResponseWriter, r *http.Request) {
entryID := request.RouteInt64Param(r, "entryID")
builder := h.store.NewEntryQueryBuilder(request.UserID(r))
2017-12-22 20:33:01 +01:00
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
if err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-12-22 20:33:01 +01:00
return
}
if entry == nil {
2018-10-08 03:42:43 +02:00
json.NotFound(w, r)
2017-12-22 20:33:01 +01:00
return
}
json.OK(w, r, entry)
2017-12-22 20:33:01 +01:00
}
func (h *handler) getFeedEntries(w http.ResponseWriter, r *http.Request) {
feedID := request.RouteInt64Param(r, "feedID")
h.findEntries(w, r, feedID)
2017-11-27 00:07:59 +01:00
}
func (h *handler) getEntries(w http.ResponseWriter, r *http.Request) {
h.findEntries(w, r, 0)
}
func (h *handler) findEntries(w http.ResponseWriter, r *http.Request, feedID int64) {
statuses := request.QueryStringParamList(r, "status")
for _, status := range statuses {
2021-01-05 00:32:32 +01:00
if err := validator.ValidateEntryStatus(status); err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-27 00:07:59 +01:00
return
}
}
order := request.QueryStringParam(r, "order", model.DefaultSortingOrder)
2021-01-05 00:32:32 +01:00
if err := validator.ValidateEntryOrder(order); err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-27 00:07:59 +01:00
return
}
direction := request.QueryStringParam(r, "direction", model.DefaultSortingDirection)
2021-01-05 00:32:32 +01:00
if err := validator.ValidateDirection(direction); err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-27 00:07:59 +01:00
return
}
limit := request.QueryIntParam(r, "limit", 100)
offset := request.QueryIntParam(r, "offset", 0)
2021-01-05 00:32:32 +01:00
if err := validator.ValidateRange(offset, limit); err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-27 00:07:59 +01:00
return
}
userID := request.UserID(r)
categoryID := request.QueryInt64Param(r, "category_id", 0)
2021-01-04 07:28:04 +01:00
if categoryID > 0 && !h.store.CategoryIDExists(userID, categoryID) {
json.BadRequest(w, r, errors.New("Invalid category ID"))
return
}
feedID = request.QueryInt64Param(r, "feed_id", feedID)
if feedID > 0 && !h.store.FeedExists(userID, feedID) {
json.BadRequest(w, r, errors.New("Invalid feed ID"))
return
}
builder := h.store.NewEntryQueryBuilder(userID)
builder.WithFeedID(feedID)
builder.WithCategoryID(categoryID)
builder.WithStatuses(statuses)
2017-11-27 00:07:59 +01:00
builder.WithOrder(order)
builder.WithDirection(direction)
2017-11-20 06:10:04 +01:00
builder.WithOffset(offset)
builder.WithLimit(limit)
configureFilters(builder, r)
2017-11-20 06:10:04 +01:00
entries, err := builder.GetEntries()
if err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
count, err := builder.CountEntries()
if err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
2017-11-20 06:10:04 +01:00
}
func (h *handler) setEntryStatus(w http.ResponseWriter, r *http.Request) {
2021-01-05 00:32:32 +01:00
var entriesStatusUpdateRequest model.EntriesStatusUpdateRequest
if err := json_parser.NewDecoder(r.Body).Decode(&entriesStatusUpdateRequest); err != nil {
json.BadRequest(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2021-01-05 00:32:32 +01:00
if err := validator.ValidateEntriesStatusUpdateRequest(&entriesStatusUpdateRequest); err != nil {
2018-10-08 03:42:43 +02:00
json.BadRequest(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2021-01-05 00:32:32 +01:00
if err := h.store.SetEntriesStatus(request.UserID(r), entriesStatusUpdateRequest.EntryIDs, entriesStatusUpdateRequest.Status); err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-11-20 06:10:04 +01:00
return
}
2018-10-08 03:42:43 +02:00
json.NoContent(w, r)
2017-11-20 06:10:04 +01:00
}
2017-12-22 20:33:01 +01:00
func (h *handler) toggleBookmark(w http.ResponseWriter, r *http.Request) {
entryID := request.RouteInt64Param(r, "entryID")
if err := h.store.ToggleBookmark(request.UserID(r), entryID); err != nil {
2018-10-08 03:42:43 +02:00
json.ServerError(w, r, err)
2017-12-22 20:33:01 +01:00
return
}
2018-10-08 03:42:43 +02:00
json.NoContent(w, r)
2017-12-22 20:33:01 +01:00
}
func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0)
if beforeEntryID > 0 {
builder.BeforeEntryID(beforeEntryID)
}
afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0)
if afterEntryID > 0 {
builder.AfterEntryID(afterEntryID)
}
beforeTimestamp := request.QueryInt64Param(r, "before", 0)
if beforeTimestamp > 0 {
builder.BeforeDate(time.Unix(beforeTimestamp, 0))
}
afterTimestamp := request.QueryInt64Param(r, "after", 0)
if afterTimestamp > 0 {
builder.AfterDate(time.Unix(afterTimestamp, 0))
}
categoryID := request.QueryInt64Param(r, "category_id", 0)
if categoryID > 0 {
builder.WithCategoryID(categoryID)
}
if request.HasQueryParam(r, "starred") {
builder.WithStarred()
}
searchQuery := request.QueryStringParam(r, "search", "")
if searchQuery != "" {
builder.WithSearchQuery(searchQuery)
}
}