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.
|
|
|
|
|
|
|
|
package payload
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// EntriesResponse represents the response sent when fetching entries.
|
2017-11-20 06:10:04 +01:00
|
|
|
type EntriesResponse struct {
|
|
|
|
Total int `json:"total"`
|
|
|
|
Entries model.Entries `json:"entries"`
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// DecodeUserPayload unserialize JSON user object.
|
2017-11-20 06:10:04 +01:00
|
|
|
func DecodeUserPayload(data io.Reader) (*model.User, error) {
|
|
|
|
var user model.User
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
if err := decoder.Decode(&user); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode user JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// DecodeURLPayload unserialize JSON subscription object.
|
2017-11-20 06:10:04 +01:00
|
|
|
func DecodeURLPayload(data io.Reader) (string, error) {
|
|
|
|
type payload struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var p payload
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
if err := decoder.Decode(&p); err != nil {
|
|
|
|
return "", fmt.Errorf("invalid JSON payload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.URL, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// DecodeEntryStatusPayload unserialize JSON entry statuses object.
|
2017-11-25 07:29:20 +01:00
|
|
|
func DecodeEntryStatusPayload(data io.Reader) ([]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
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// DecodeFeedCreationPayload unserialize JSON feed creation object.
|
2017-11-20 06:10:04 +01:00
|
|
|
func DecodeFeedCreationPayload(data io.Reader) (string, int64, error) {
|
|
|
|
type payload struct {
|
|
|
|
FeedURL string `json:"feed_url"`
|
|
|
|
CategoryID int64 `json:"category_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var p payload
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
if err := decoder.Decode(&p); err != nil {
|
|
|
|
return "", 0, fmt.Errorf("invalid JSON payload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.FeedURL, p.CategoryID, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// DecodeFeedModificationPayload unserialize JSON feed object.
|
2017-11-20 06:10:04 +01:00
|
|
|
func DecodeFeedModificationPayload(data io.Reader) (*model.Feed, error) {
|
|
|
|
var feed model.Feed
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
if err := decoder.Decode(&feed); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode feed JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &feed, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// DecodeCategoryPayload unserialize JSON category object.
|
2017-11-20 06:10:04 +01:00
|
|
|
func DecodeCategoryPayload(data io.Reader) (*model.Category, error) {
|
|
|
|
var category model.Category
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
if err := decoder.Decode(&category); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode category JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &category, nil
|
|
|
|
}
|