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-01-03 07:04:48 +01:00
|
|
|
package api
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-11-25 07:29:20 +01:00
|
|
|
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
type feedIcon struct {
|
2017-12-16 20:25:18 +01:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
type entriesResponse struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
Total int `json:"total"`
|
|
|
|
Entries model.Entries `json:"entries"`
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
func decodeUserPayload(r io.ReadCloser) (*model.User, error) {
|
2017-11-20 06:10:04 +01:00
|
|
|
var user model.User
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
defer r.Close()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := decoder.Decode(&user); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode user JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
func decodeURLPayload(r io.ReadCloser) (*subscriptionDiscovery, error) {
|
|
|
|
defer r.Close()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
var s subscriptionDiscovery
|
2018-04-30 01:35:04 +02:00
|
|
|
decoder := json.NewDecoder(r)
|
2018-06-20 07:58:29 +02:00
|
|
|
if err := decoder.Decode(&s); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid JSON payload: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
return &s, nil
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) {
|
2017-11-20 06:10:04 +01:00
|
|
|
type payload struct {
|
2017-11-25 07:29:20 +01:00
|
|
|
EntryIDs []int64 `json:"entry_ids"`
|
|
|
|
Status string `json:"status"`
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var p payload
|
2018-04-30 01:35:04 +02:00
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
defer r.Close()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := decoder.Decode(&p); err != nil {
|
2017-11-25 07:29:20 +01:00
|
|
|
return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 07:29:20 +01:00
|
|
|
return p.EntryIDs, p.Status, nil
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
func decodeFeedCreationPayload(r io.ReadCloser) (*feedCreation, error) {
|
|
|
|
defer r.Close()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
var fc feedCreation
|
2018-04-30 01:35:04 +02:00
|
|
|
decoder := json.NewDecoder(r)
|
2018-06-20 07:58:29 +02:00
|
|
|
if err := decoder.Decode(&fc); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid JSON payload: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 07:58:29 +02:00
|
|
|
return &fc, nil
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
func decodeFeedModificationPayload(r io.ReadCloser) (*model.Feed, error) {
|
2017-11-20 06:10:04 +01:00
|
|
|
var feed model.Feed
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
defer r.Close()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := decoder.Decode(&feed); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode feed JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &feed, nil
|
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
func decodeCategoryPayload(r io.ReadCloser) (*model.Category, error) {
|
2017-11-20 06:10:04 +01:00
|
|
|
var category model.Category
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
defer r.Close()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := decoder.Decode(&category); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode category JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &category, nil
|
|
|
|
}
|