aboutsummaryrefslogtreecommitdiff
path: root/server/api/controller/entry.go
blob: f5833783b0043d3f61cc8ae58e3612a297f29548 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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"

	"github.com/miniflux/miniflux2/model"
	"github.com/miniflux/miniflux2/server/api/payload"
	"github.com/miniflux/miniflux2/server/core"
)

// GetEntry is the API handler to get a single feed entry.
func (c *Controller) GetEntry(ctx *core.Context, request *core.Request, response *core.Response) {
	userID := ctx.UserID()
	feedID, err := request.IntegerParam("feedID")
	if err != nil {
		response.JSON().BadRequest(err)
		return
	}

	entryID, err := request.IntegerParam("entryID")
	if err != nil {
		response.JSON().BadRequest(err)
		return
	}

	builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
	builder.WithFeedID(feedID)
	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)
}

// GetFeedEntries is the API handler to get all feed entries.
func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, response *core.Response) {
	userID := ctx.UserID()
	feedID, err := request.IntegerParam("feedID")
	if err != nil {
		response.JSON().BadRequest(err)
		return
	}

	status := request.QueryStringParam("status", "")
	if status != "" {
		if err := model.ValidateEntryStatus(status); err != nil {
			response.JSON().BadRequest(err)
			return
		}
	}

	order := request.QueryStringParam("order", "id")
	if err := model.ValidateEntryOrder(order); err != nil {
		response.JSON().BadRequest(err)
		return
	}

	direction := request.QueryStringParam("direction", "desc")
	if err := model.ValidateDirection(direction); err != nil {
		response.JSON().BadRequest(err)
		return
	}

	limit := request.QueryIntegerParam("limit", 100)
	offset := request.QueryIntegerParam("offset", 0)

	builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
	builder.WithFeedID(feedID)
	builder.WithStatus(status)
	builder.WithOrder(model.DefaultSortingOrder)
	builder.WithDirection(model.DefaultSortingDirection)
	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
	}

	response.JSON().Standard(&payload.EntriesResponse{Total: count, Entries: entries})
}

// SetEntryStatus is the API handler to change the status of entries.
func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, response *core.Response) {
	userID := ctx.UserID()

	entryIDs, status, err := payload.DecodeEntryStatusPayload(request.Body())
	if err != nil {
		response.JSON().BadRequest(errors.New("Invalid JSON payload"))
		return
	}

	if err := model.ValidateEntryStatus(status); err != nil {
		response.JSON().BadRequest(err)
		return
	}

	if err := c.store.SetEntriesStatus(userID, entryIDs, status); err != nil {
		response.JSON().ServerError(errors.New("Unable to change entries status"))
		return
	}

	response.JSON().NoContent()
}