| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- // 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"
- "net/http"
- "time"
- "github.com/miniflux/miniflux/http/context"
- "github.com/miniflux/miniflux/http/request"
- "github.com/miniflux/miniflux/http/response/json"
- "github.com/miniflux/miniflux/model"
- "github.com/miniflux/miniflux/storage"
- )
- // GetFeedEntry is the API handler to get a single feed entry.
- func (c *Controller) GetFeedEntry(w http.ResponseWriter, r *http.Request) {
- feedID, err := request.IntParam(r, "feedID")
- if err != nil {
- json.BadRequest(w, err)
- return
- }
- entryID, err := request.IntParam(r, "entryID")
- if err != nil {
- json.BadRequest(w, err)
- return
- }
- ctx := context.New(r)
- userID := ctx.UserID()
- builder := c.store.NewEntryQueryBuilder(userID)
- builder.WithFeedID(feedID)
- builder.WithEntryID(entryID)
- entry, err := builder.GetEntry()
- if err != nil {
- json.ServerError(w, errors.New("Unable to fetch this entry from the database"))
- return
- }
- if entry == nil {
- json.NotFound(w, errors.New("Entry not found"))
- return
- }
- json.OK(w, r, entry)
- }
- // GetEntry is the API handler to get a single entry.
- func (c *Controller) GetEntry(w http.ResponseWriter, r *http.Request) {
- entryID, err := request.IntParam(r, "entryID")
- if err != nil {
- json.BadRequest(w, err)
- return
- }
- builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
- builder.WithEntryID(entryID)
- entry, err := builder.GetEntry()
- if err != nil {
- json.ServerError(w, errors.New("Unable to fetch this entry from the database"))
- return
- }
- if entry == nil {
- json.NotFound(w, errors.New("Entry not found"))
- return
- }
- json.OK(w, r, entry)
- }
- // GetFeedEntries is the API handler to get all feed entries.
- func (c *Controller) GetFeedEntries(w http.ResponseWriter, r *http.Request) {
- feedID, err := request.IntParam(r, "feedID")
- if err != nil {
- json.BadRequest(w, err)
- return
- }
- status := request.QueryParam(r, "status", "")
- if status != "" {
- if err := model.ValidateEntryStatus(status); err != nil {
- json.BadRequest(w, err)
- return
- }
- }
- order := request.QueryParam(r, "order", model.DefaultSortingOrder)
- if err := model.ValidateEntryOrder(order); err != nil {
- json.BadRequest(w, err)
- return
- }
- direction := request.QueryParam(r, "direction", model.DefaultSortingDirection)
- if err := model.ValidateDirection(direction); err != nil {
- json.BadRequest(w, err)
- return
- }
- limit := request.QueryIntParam(r, "limit", 100)
- offset := request.QueryIntParam(r, "offset", 0)
- if err := model.ValidateRange(offset, limit); err != nil {
- json.BadRequest(w, err)
- return
- }
- builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
- builder.WithFeedID(feedID)
- builder.WithStatus(status)
- builder.WithOrder(order)
- builder.WithDirection(direction)
- builder.WithOffset(offset)
- builder.WithLimit(limit)
- configureFilters(builder, r)
- entries, err := builder.GetEntries()
- if err != nil {
- json.ServerError(w, errors.New("Unable to fetch the list of entries"))
- return
- }
- count, err := builder.CountEntries()
- if err != nil {
- json.ServerError(w, errors.New("Unable to count the number of entries"))
- return
- }
- json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
- }
- // GetEntries is the API handler to fetch entries.
- func (c *Controller) GetEntries(w http.ResponseWriter, r *http.Request) {
- status := request.QueryParam(r, "status", "")
- if status != "" {
- if err := model.ValidateEntryStatus(status); err != nil {
- json.BadRequest(w, err)
- return
- }
- }
- order := request.QueryParam(r, "order", model.DefaultSortingOrder)
- if err := model.ValidateEntryOrder(order); err != nil {
- json.BadRequest(w, err)
- return
- }
- direction := request.QueryParam(r, "direction", model.DefaultSortingDirection)
- if err := model.ValidateDirection(direction); err != nil {
- json.BadRequest(w, err)
- return
- }
- limit := request.QueryIntParam(r, "limit", 100)
- offset := request.QueryIntParam(r, "offset", 0)
- if err := model.ValidateRange(offset, limit); err != nil {
- json.BadRequest(w, err)
- return
- }
- builder := c.store.NewEntryQueryBuilder(context.New(r).UserID())
- builder.WithStatus(status)
- builder.WithOrder(order)
- builder.WithDirection(direction)
- builder.WithOffset(offset)
- builder.WithLimit(limit)
- configureFilters(builder, r)
- entries, err := builder.GetEntries()
- if err != nil {
- json.ServerError(w, errors.New("Unable to fetch the list of entries"))
- return
- }
- count, err := builder.CountEntries()
- if err != nil {
- json.ServerError(w, errors.New("Unable to count the number of entries"))
- return
- }
- json.OK(w, r, &entriesResponse{Total: count, Entries: entries})
- }
- // SetEntryStatus is the API handler to change the status of entries.
- func (c *Controller) SetEntryStatus(w http.ResponseWriter, r *http.Request) {
- entryIDs, status, err := decodeEntryStatusPayload(r.Body)
- if err != nil {
- json.BadRequest(w, errors.New("Invalid JSON payload"))
- return
- }
- if err := model.ValidateEntryStatus(status); err != nil {
- json.BadRequest(w, err)
- return
- }
- if err := c.store.SetEntriesStatus(context.New(r).UserID(), entryIDs, status); err != nil {
- json.ServerError(w, errors.New("Unable to change entries status"))
- return
- }
- json.NoContent(w)
- }
- // ToggleBookmark is the API handler to toggle bookmark status.
- func (c *Controller) ToggleBookmark(w http.ResponseWriter, r *http.Request) {
- entryID, err := request.IntParam(r, "entryID")
- if err != nil {
- json.BadRequest(w, err)
- return
- }
- if err := c.store.ToggleBookmark(context.New(r).UserID(), entryID); err != nil {
- json.ServerError(w, errors.New("Unable to toggle bookmark value"))
- return
- }
- json.NoContent(w)
- }
- 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))
- }
- if request.HasQueryParam(r, "starred") {
- builder.WithStarred()
- }
- searchQuery := request.QueryParam(r, "search", "")
- if searchQuery != "" {
- builder.WithSearchQuery(searchQuery)
- }
- }
|