aboutsummaryrefslogtreecommitdiffhomepage
path: root/api/feed.go
blob: b7c46ea1a0ec077c05f4855c1be5169c4602d229 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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"

	"github.com/miniflux/miniflux/http/context"
	"github.com/miniflux/miniflux/http/request"
	"github.com/miniflux/miniflux/http/response/json"
)

// CreateFeed is the API handler to create a new feed.
func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
	feedInfo, err := decodeFeedCreationPayload(r.Body)
	if err != nil {
		json.BadRequest(w, err)
		return
	}

	if feedInfo.FeedURL == "" {
		json.BadRequest(w, errors.New("The feed_url is required"))
		return
	}

	if feedInfo.CategoryID <= 0 {
		json.BadRequest(w, errors.New("The category_id is required"))
		return
	}

	ctx := context.New(r)
	userID := ctx.UserID()

	if c.store.FeedURLExists(userID, feedInfo.FeedURL) {
		json.BadRequest(w, errors.New("This feed_url already exists"))
		return
	}

	if !c.store.CategoryExists(userID, feedInfo.CategoryID) {
		json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
		return
	}

	feed, err := c.feedHandler.CreateFeed(
		userID,
		feedInfo.CategoryID,
		feedInfo.FeedURL,
		feedInfo.Crawler,
		feedInfo.Username,
		feedInfo.Password,
	)
	if err != nil {
		json.ServerError(w, errors.New("Unable to create this feed"))
		return
	}

	type result struct {
		FeedID int64 `json:"feed_id"`
	}

	json.Created(w, &result{FeedID: feed.ID})
}

// RefreshFeed is the API handler to refresh a feed.
func (c *Controller) RefreshFeed(w http.ResponseWriter, r *http.Request) {
	feedID, err := request.IntParam(r, "feedID")
	if err != nil {
		json.BadRequest(w, err)
		return
	}

	ctx := context.New(r)
	userID := ctx.UserID()

	if !c.store.FeedExists(userID, feedID) {
		json.NotFound(w, errors.New("Unable to find this feed"))
		return
	}

	err = c.feedHandler.RefreshFeed(userID, feedID)
	if err != nil {
		json.ServerError(w, errors.New("Unable to refresh this feed"))
		return
	}

	json.NoContent(w)
}

// UpdateFeed is the API handler that is used to update a feed.
func (c *Controller) UpdateFeed(w http.ResponseWriter, r *http.Request) {
	feedID, err := request.IntParam(r, "feedID")
	if err != nil {
		json.BadRequest(w, err)
		return
	}

	feedChanges, err := decodeFeedModificationPayload(r.Body)
	if err != nil {
		json.BadRequest(w, err)
		return
	}

	ctx := context.New(r)
	userID := ctx.UserID()

	originalFeed, err := c.store.FeedByID(userID, feedID)
	if err != nil {
		json.NotFound(w, errors.New("Unable to find this feed"))
		return
	}

	if originalFeed == nil {
		json.NotFound(w, errors.New("Feed not found"))
		return
	}

	feedChanges.Update(originalFeed)

	if !c.store.CategoryExists(userID, originalFeed.Category.ID) {
		json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user"))
		return
	}

	if err := c.store.UpdateFeed(originalFeed); err != nil {
		json.ServerError(w, errors.New("Unable to update this feed"))
		return
	}

	originalFeed, err = c.store.FeedByID(userID, feedID)
	if err != nil {
		json.ServerError(w, errors.New("Unable to fetch this feed"))
		return
	}

	json.Created(w, originalFeed)
}

// GetFeeds is the API handler that get all feeds that belongs to the given user.
func (c *Controller) GetFeeds(w http.ResponseWriter, r *http.Request) {
	feeds, err := c.store.Feeds(context.New(r).UserID())
	if err != nil {
		json.ServerError(w, errors.New("Unable to fetch feeds from the database"))
		return
	}

	json.OK(w, feeds)
}

// GetFeed is the API handler to get a feed.
func (c *Controller) GetFeed(w http.ResponseWriter, r *http.Request) {
	feedID, err := request.IntParam(r, "feedID")
	if err != nil {
		json.BadRequest(w, err)
		return
	}

	feed, err := c.store.FeedByID(context.New(r).UserID(), feedID)
	if err != nil {
		json.ServerError(w, errors.New("Unable to fetch this feed"))
		return
	}

	if feed == nil {
		json.NotFound(w, errors.New("Feed not found"))
		return
	}

	json.OK(w, feed)
}

// RemoveFeed is the API handler to remove a feed.
func (c *Controller) RemoveFeed(w http.ResponseWriter, r *http.Request) {
	feedID, err := request.IntParam(r, "feedID")
	if err != nil {
		json.BadRequest(w, err)
		return
	}

	ctx := context.New(r)
	userID := ctx.UserID()

	if !c.store.FeedExists(userID, feedID) {
		json.NotFound(w, errors.New("Feed not found"))
		return
	}

	if err := c.store.RemoveFeed(userID, feedID); err != nil {
		json.ServerError(w, errors.New("Unable to remove this feed"))
		return
	}

	json.NoContent(w)
}