mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Add new fields for feed username/password
This commit is contained in:
parent
261695c14c
commit
bddca15b69
27 changed files with 203 additions and 68 deletions
19
api/feed.go
19
api/feed.go
|
@ -15,18 +15,18 @@ import (
|
|||
|
||||
// CreateFeed is the API handler to create a new feed.
|
||||
func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
|
||||
feedURL, categoryID, err := decodeFeedCreationPayload(r.Body)
|
||||
feedInfo, err := decodeFeedCreationPayload(r.Body)
|
||||
if err != nil {
|
||||
json.BadRequest(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if feedURL == "" {
|
||||
if feedInfo.FeedURL == "" {
|
||||
json.BadRequest(w, errors.New("The feed_url is required"))
|
||||
return
|
||||
}
|
||||
|
||||
if categoryID <= 0 {
|
||||
if feedInfo.CategoryID <= 0 {
|
||||
json.BadRequest(w, errors.New("The category_id is required"))
|
||||
return
|
||||
}
|
||||
|
@ -34,17 +34,24 @@ func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) {
|
|||
ctx := context.New(r)
|
||||
userID := ctx.UserID()
|
||||
|
||||
if c.store.FeedURLExists(userID, feedURL) {
|
||||
if c.store.FeedURLExists(userID, feedInfo.FeedURL) {
|
||||
json.BadRequest(w, errors.New("This feed_url already exists"))
|
||||
return
|
||||
}
|
||||
|
||||
if !c.store.CategoryExists(userID, categoryID) {
|
||||
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, categoryID, feedURL, false)
|
||||
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
|
||||
|
|
|
@ -23,6 +23,20 @@ type entriesResponse struct {
|
|||
Entries model.Entries `json:"entries"`
|
||||
}
|
||||
|
||||
type feedCreation struct {
|
||||
FeedURL string `json:"feed_url"`
|
||||
CategoryID int64 `json:"category_id"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Crawler bool `json:"crawler"`
|
||||
}
|
||||
|
||||
type subscriptionDiscovery struct {
|
||||
URL string `json:"url"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func decodeUserPayload(r io.ReadCloser) (*model.User, error) {
|
||||
var user model.User
|
||||
|
||||
|
@ -35,19 +49,16 @@ func decodeUserPayload(r io.ReadCloser) (*model.User, error) {
|
|||
return &user, nil
|
||||
}
|
||||
|
||||
func decodeURLPayload(r io.ReadCloser) (string, error) {
|
||||
type payload struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
var p payload
|
||||
decoder := json.NewDecoder(r)
|
||||
func decodeURLPayload(r io.ReadCloser) (*subscriptionDiscovery, error) {
|
||||
defer r.Close()
|
||||
if err := decoder.Decode(&p); err != nil {
|
||||
return "", fmt.Errorf("invalid JSON payload: %v", err)
|
||||
|
||||
var s subscriptionDiscovery
|
||||
decoder := json.NewDecoder(r)
|
||||
if err := decoder.Decode(&s); err != nil {
|
||||
return nil, fmt.Errorf("invalid JSON payload: %v", err)
|
||||
}
|
||||
|
||||
return p.URL, nil
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) {
|
||||
|
@ -66,20 +77,16 @@ func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) {
|
|||
return p.EntryIDs, p.Status, nil
|
||||
}
|
||||
|
||||
func decodeFeedCreationPayload(r io.ReadCloser) (string, int64, error) {
|
||||
type payload struct {
|
||||
FeedURL string `json:"feed_url"`
|
||||
CategoryID int64 `json:"category_id"`
|
||||
}
|
||||
|
||||
var p payload
|
||||
decoder := json.NewDecoder(r)
|
||||
func decodeFeedCreationPayload(r io.ReadCloser) (*feedCreation, error) {
|
||||
defer r.Close()
|
||||
if err := decoder.Decode(&p); err != nil {
|
||||
return "", 0, fmt.Errorf("invalid JSON payload: %v", err)
|
||||
|
||||
var fc feedCreation
|
||||
decoder := json.NewDecoder(r)
|
||||
if err := decoder.Decode(&fc); err != nil {
|
||||
return nil, fmt.Errorf("invalid JSON payload: %v", err)
|
||||
}
|
||||
|
||||
return p.FeedURL, p.CategoryID, nil
|
||||
return &fc, nil
|
||||
}
|
||||
|
||||
func decodeFeedModificationPayload(r io.ReadCloser) (*model.Feed, error) {
|
||||
|
|
|
@ -15,13 +15,17 @@ import (
|
|||
|
||||
// GetSubscriptions is the API handler to find subscriptions.
|
||||
func (c *Controller) GetSubscriptions(w http.ResponseWriter, r *http.Request) {
|
||||
websiteURL, err := decodeURLPayload(r.Body)
|
||||
subscriptionInfo, err := decodeURLPayload(r.Body)
|
||||
if err != nil {
|
||||
json.BadRequest(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
subscriptions, err := subscription.FindSubscriptions(websiteURL)
|
||||
subscriptions, err := subscription.FindSubscriptions(
|
||||
subscriptionInfo.URL,
|
||||
subscriptionInfo.Username,
|
||||
subscriptionInfo.Password,
|
||||
)
|
||||
if err != nil {
|
||||
json.ServerError(w, errors.New("Unable to discover subscriptions"))
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue