2017-11-20 00:10:04 -05: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-08-25 00:51:50 -04:00
|
|
|
package api // import "miniflux.app/api"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-11-25 01:29:20 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/model"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2018-01-03 01:04:48 -05:00
|
|
|
type feedIcon struct {
|
2017-12-16 14:25:18 -05:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
2018-01-03 01:04:48 -05:00
|
|
|
type entriesResponse struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
Total int `json:"total"`
|
|
|
|
Entries model.Entries `json:"entries"`
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
type feedCreation struct {
|
|
|
|
FeedURL string `json:"feed_url"`
|
|
|
|
CategoryID int64 `json:"category_id"`
|
2018-09-19 21:19:24 -04:00
|
|
|
UserAgent string `json:"user_agent"`
|
2018-06-20 01:58:29 -04:00
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Crawler bool `json:"crawler"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriptionDiscovery struct {
|
2018-09-19 21:19:24 -04:00
|
|
|
URL string `json:"url"`
|
|
|
|
UserAgent string `json:"user_agent"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2018-06-20 01:58:29 -04:00
|
|
|
}
|
|
|
|
|
2018-06-23 19:16:54 -04:00
|
|
|
type feedModification struct {
|
|
|
|
FeedURL *string `json:"feed_url"`
|
|
|
|
SiteURL *string `json:"site_url"`
|
|
|
|
Title *string `json:"title"`
|
|
|
|
ScraperRules *string `json:"scraper_rules"`
|
|
|
|
RewriteRules *string `json:"rewrite_rules"`
|
|
|
|
Crawler *bool `json:"crawler"`
|
2018-09-19 21:19:24 -04:00
|
|
|
UserAgent *string `json:"user_agent"`
|
2018-06-23 19:16:54 -04:00
|
|
|
Username *string `json:"username"`
|
|
|
|
Password *string `json:"password"`
|
|
|
|
CategoryID *int64 `json:"category_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *feedModification) Update(feed *model.Feed) {
|
|
|
|
if f.FeedURL != nil && *f.FeedURL != "" {
|
|
|
|
feed.FeedURL = *f.FeedURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.SiteURL != nil && *f.SiteURL != "" {
|
|
|
|
feed.SiteURL = *f.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Title != nil && *f.Title != "" {
|
|
|
|
feed.Title = *f.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.ScraperRules != nil {
|
|
|
|
feed.ScraperRules = *f.ScraperRules
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.RewriteRules != nil {
|
|
|
|
feed.RewriteRules = *f.RewriteRules
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Crawler != nil {
|
|
|
|
feed.Crawler = *f.Crawler
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:19:24 -04:00
|
|
|
if f.UserAgent != nil {
|
|
|
|
feed.UserAgent = *f.UserAgent
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:16:54 -04:00
|
|
|
if f.Username != nil {
|
|
|
|
feed.Username = *f.Username
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-06-23 19:16:54 -04:00
|
|
|
if f.Password != nil {
|
|
|
|
feed.Password = *f.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.CategoryID != nil && *f.CategoryID > 0 {
|
|
|
|
feed.Category.ID = *f.CategoryID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type userModification struct {
|
|
|
|
Username *string `json:"username"`
|
|
|
|
Password *string `json:"password"`
|
|
|
|
IsAdmin *bool `json:"is_admin"`
|
|
|
|
Theme *string `json:"theme"`
|
|
|
|
Language *string `json:"language"`
|
|
|
|
Timezone *string `json:"timezone"`
|
|
|
|
EntryDirection *string `json:"entry_sorting_direction"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *userModification) Update(user *model.User) {
|
|
|
|
if u.Username != nil {
|
|
|
|
user.Username = *u.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Password != nil {
|
|
|
|
user.Password = *u.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.IsAdmin != nil {
|
|
|
|
user.IsAdmin = *u.IsAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Theme != nil {
|
|
|
|
user.Theme = *u.Theme
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Language != nil {
|
|
|
|
user.Language = *u.Language
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Timezone != nil {
|
|
|
|
user.Timezone = *u.Timezone
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.EntryDirection != nil {
|
|
|
|
user.EntryDirection = *u.EntryDirection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeUserModificationPayload(r io.ReadCloser) (*userModification, error) {
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
var user userModification
|
2018-04-29 19:35:04 -04:00
|
|
|
decoder := json.NewDecoder(r)
|
2018-06-23 19:16:54 -04:00
|
|
|
if err := decoder.Decode(&user); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeUserCreationPayload(r io.ReadCloser) (*model.User, error) {
|
2018-04-29 19:35:04 -04:00
|
|
|
defer r.Close()
|
2018-06-23 19:16:54 -04:00
|
|
|
|
|
|
|
var user model.User
|
|
|
|
decoder := json.NewDecoder(r)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := decoder.Decode(&user); err != nil {
|
2018-06-23 19:16:54 -04:00
|
|
|
return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
func decodeURLPayload(r io.ReadCloser) (*subscriptionDiscovery, error) {
|
|
|
|
defer r.Close()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
var s subscriptionDiscovery
|
2018-04-29 19:35:04 -04:00
|
|
|
decoder := json.NewDecoder(r)
|
2018-06-20 01:58:29 -04:00
|
|
|
if err := decoder.Decode(&s); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid JSON payload: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
return &s, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) {
|
2017-11-20 00:10:04 -05:00
|
|
|
type payload struct {
|
2017-11-25 01:29:20 -05:00
|
|
|
EntryIDs []int64 `json:"entry_ids"`
|
|
|
|
Status string `json:"status"`
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var p payload
|
2018-04-29 19:35:04 -04:00
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
defer r.Close()
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := decoder.Decode(&p); err != nil {
|
2017-11-25 01:29:20 -05:00
|
|
|
return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-25 01:29:20 -05:00
|
|
|
return p.EntryIDs, p.Status, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
func decodeFeedCreationPayload(r io.ReadCloser) (*feedCreation, error) {
|
|
|
|
defer r.Close()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
var fc feedCreation
|
2018-04-29 19:35:04 -04:00
|
|
|
decoder := json.NewDecoder(r)
|
2018-06-20 01:58:29 -04:00
|
|
|
if err := decoder.Decode(&fc); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid JSON payload: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-06-20 01:58:29 -04:00
|
|
|
return &fc, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2018-06-23 19:16:54 -04:00
|
|
|
func decodeFeedModificationPayload(r io.ReadCloser) (*feedModification, error) {
|
|
|
|
defer r.Close()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2018-06-23 19:16:54 -04:00
|
|
|
var feed feedModification
|
2018-04-29 19:35:04 -04:00
|
|
|
decoder := json.NewDecoder(r)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := decoder.Decode(&feed); err != nil {
|
2018-06-23 19:16:54 -04:00
|
|
|
return nil, fmt.Errorf("Unable to decode feed modification JSON object: %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &feed, nil
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
func decodeCategoryPayload(r io.ReadCloser) (*model.Category, error) {
|
2017-11-20 00:10:04 -05:00
|
|
|
var category model.Category
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
defer r.Close()
|
2017-11-20 00:10:04 -05:00
|
|
|
if err := decoder.Decode(&category); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to decode category JSON object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &category, nil
|
|
|
|
}
|