1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Allow ignore_http_cache field to be changed via API

This commit is contained in:
Frédéric Guillot 2020-12-13 20:31:19 -08:00 committed by fguillot
parent 5922a7a051
commit 4e5240c5ac
10 changed files with 196 additions and 146 deletions

View file

@ -132,7 +132,7 @@ func (h *handler) findEntries(w http.ResponseWriter, r *http.Request, feedID int
}
func (h *handler) setEntryStatus(w http.ResponseWriter, r *http.Request) {
entryIDs, status, err := decodeEntryStatusPayload(r.Body)
entryIDs, status, err := decodeEntryStatusRequest(r.Body)
if err != nil {
json.BadRequest(w, r, errors.New("Invalid JSON payload"))
return

View file

@ -14,7 +14,7 @@ import (
)
func (h *handler) createFeed(w http.ResponseWriter, r *http.Request) {
feedInfo, err := decodeFeedCreationPayload(r.Body)
feedInfo, err := decodeFeedCreationRequest(r.Body)
if err != nil {
json.BadRequest(w, r, err)
return
@ -61,11 +61,7 @@ func (h *handler) createFeed(w http.ResponseWriter, r *http.Request) {
return
}
type result struct {
FeedID int64 `json:"feed_id"`
}
json.Created(w, r, &result{FeedID: feed.ID})
json.Created(w, r, &feedCreationResponse{FeedID: feed.ID})
}
func (h *handler) refreshFeed(w http.ResponseWriter, r *http.Request) {
@ -103,7 +99,7 @@ func (h *handler) refreshAllFeeds(w http.ResponseWriter, r *http.Request) {
func (h *handler) updateFeed(w http.ResponseWriter, r *http.Request) {
feedID := request.RouteInt64Param(r, "feedID")
feedChanges, err := decodeFeedModificationPayload(r.Body)
feedChanges, err := decodeFeedModificationRequest(r.Body)
if err != nil {
json.BadRequest(w, r, err)
return

View file

@ -30,7 +30,7 @@ func (h *handler) feedIcon(w http.ResponseWriter, r *http.Request) {
return
}
json.OK(w, r, &feedIcon{
json.OK(w, r, &feedIconResponse{
ID: icon.ID,
MimeType: icon.MimeType,
Data: icon.DataURL(),

View file

@ -12,7 +12,7 @@ import (
"miniflux.app/model"
)
type feedIcon struct {
type feedIconResponse struct {
ID int64 `json:"id"`
MimeType string `json:"mime_type"`
Data string `json:"data"`
@ -23,7 +23,31 @@ type entriesResponse struct {
Entries model.Entries `json:"entries"`
}
type feedCreation struct {
type subscriptionDiscoveryRequest struct {
URL string `json:"url"`
UserAgent string `json:"user_agent"`
Username string `json:"username"`
Password string `json:"password"`
FetchViaProxy bool `json:"fetch_via_proxy"`
}
func decodeSubscriptionDiscoveryRequest(r io.ReadCloser) (*subscriptionDiscoveryRequest, error) {
defer r.Close()
var s subscriptionDiscoveryRequest
decoder := json.NewDecoder(r)
if err := decoder.Decode(&s); err != nil {
return nil, fmt.Errorf("invalid JSON payload: %v", err)
}
return &s, nil
}
type feedCreationResponse struct {
FeedID int64 `json:"feed_id"`
}
type feedCreationRequest struct {
FeedURL string `json:"feed_url"`
CategoryID int64 `json:"category_id"`
UserAgent string `json:"user_agent"`
@ -37,31 +61,37 @@ type feedCreation struct {
KeeplistRules string `json:"keeplist_rules"`
}
type subscriptionDiscovery struct {
URL string `json:"url"`
UserAgent string `json:"user_agent"`
Username string `json:"username"`
Password string `json:"password"`
FetchViaProxy bool `json:"fetch_via_proxy"`
func decodeFeedCreationRequest(r io.ReadCloser) (*feedCreationRequest, error) {
defer r.Close()
var fc feedCreationRequest
decoder := json.NewDecoder(r)
if err := decoder.Decode(&fc); err != nil {
return nil, fmt.Errorf("Invalid JSON payload: %v", err)
}
return &fc, nil
}
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"`
BlocklistRules *string `json:"blocklist_rules"`
KeeplistRules *string `json:"keeplist_rules"`
Crawler *bool `json:"crawler"`
UserAgent *string `json:"user_agent"`
Username *string `json:"username"`
Password *string `json:"password"`
CategoryID *int64 `json:"category_id"`
Disabled *bool `json:"disabled"`
type feedModificationRequest 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"`
BlocklistRules *string `json:"blocklist_rules"`
KeeplistRules *string `json:"keeplist_rules"`
Crawler *bool `json:"crawler"`
UserAgent *string `json:"user_agent"`
Username *string `json:"username"`
Password *string `json:"password"`
CategoryID *int64 `json:"category_id"`
Disabled *bool `json:"disabled"`
IgnoreHTTPCache *bool `json:"ignore_http_cache"`
FetchViaProxy *bool `json:"fetch_via_proxy"`
}
func (f *feedModification) Update(feed *model.Feed) {
func (f *feedModificationRequest) Update(feed *model.Feed) {
if f.FeedURL != nil && *f.FeedURL != "" {
feed.FeedURL = *f.FeedURL
}
@ -113,9 +143,41 @@ func (f *feedModification) Update(feed *model.Feed) {
if f.Disabled != nil {
feed.Disabled = *f.Disabled
}
if f.IgnoreHTTPCache != nil {
feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
}
if f.FetchViaProxy != nil {
feed.FetchViaProxy = *f.FetchViaProxy
}
}
type userModification struct {
func decodeFeedModificationRequest(r io.ReadCloser) (*feedModificationRequest, error) {
defer r.Close()
var feed feedModificationRequest
decoder := json.NewDecoder(r)
if err := decoder.Decode(&feed); err != nil {
return nil, fmt.Errorf("Unable to decode feed modification JSON object: %v", err)
}
return &feed, nil
}
func decodeUserCreationRequest(r io.ReadCloser) (*model.User, error) {
defer r.Close()
var user model.User
decoder := json.NewDecoder(r)
if err := decoder.Decode(&user); err != nil {
return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
}
return &user, nil
}
type userModificationRequest struct {
Username *string `json:"username"`
Password *string `json:"password"`
IsAdmin *bool `json:"is_admin"`
@ -126,7 +188,7 @@ type userModification struct {
EntriesPerPage *int `json:"entries_per_page"`
}
func (u *userModification) Update(user *model.User) {
func (u *userModificationRequest) Update(user *model.User) {
if u.Username != nil {
user.Username = *u.Username
}
@ -160,10 +222,10 @@ func (u *userModification) Update(user *model.User) {
}
}
func decodeUserModificationPayload(r io.ReadCloser) (*userModification, error) {
func decodeUserModificationRequest(r io.ReadCloser) (*userModificationRequest, error) {
defer r.Close()
var user userModification
var user userModificationRequest
decoder := json.NewDecoder(r)
if err := decoder.Decode(&user); err != nil {
return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
@ -172,31 +234,7 @@ func decodeUserModificationPayload(r io.ReadCloser) (*userModification, error) {
return &user, nil
}
func decodeUserCreationPayload(r io.ReadCloser) (*model.User, error) {
defer r.Close()
var user model.User
decoder := json.NewDecoder(r)
if err := decoder.Decode(&user); err != nil {
return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err)
}
return &user, nil
}
func decodeURLPayload(r io.ReadCloser) (*subscriptionDiscovery, error) {
defer r.Close()
var s subscriptionDiscovery
decoder := json.NewDecoder(r)
if err := decoder.Decode(&s); err != nil {
return nil, fmt.Errorf("invalid JSON payload: %v", err)
}
return &s, nil
}
func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) {
func decodeEntryStatusRequest(r io.ReadCloser) ([]int64, string, error) {
type payload struct {
EntryIDs []int64 `json:"entry_ids"`
Status string `json:"status"`
@ -212,30 +250,6 @@ func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) {
return p.EntryIDs, p.Status, nil
}
func decodeFeedCreationPayload(r io.ReadCloser) (*feedCreation, error) {
defer r.Close()
var fc feedCreation
decoder := json.NewDecoder(r)
if err := decoder.Decode(&fc); err != nil {
return nil, fmt.Errorf("invalid JSON payload: %v", err)
}
return &fc, nil
}
func decodeFeedModificationPayload(r io.ReadCloser) (*feedModification, error) {
defer r.Close()
var feed feedModification
decoder := json.NewDecoder(r)
if err := decoder.Decode(&feed); err != nil {
return nil, fmt.Errorf("Unable to decode feed modification JSON object: %v", err)
}
return &feed, nil
}
type categoryRequest struct {
Title string `json:"title"`
}

View file

@ -12,129 +12,129 @@ import (
func TestUpdateFeedURL(t *testing.T) {
feedURL := "http://example.com/"
changes := &feedModification{FeedURL: &feedURL}
changes := &feedModificationRequest{FeedURL: &feedURL}
feed := &model.Feed{FeedURL: "http://example.org/"}
changes.Update(feed)
if feed.FeedURL != feedURL {
t.Fatalf(`Unexpected value, got %q instead of %q`, feed.FeedURL, feedURL)
t.Errorf(`Unexpected value, got %q instead of %q`, feed.FeedURL, feedURL)
}
}
func TestUpdateFeedURLWithEmptyString(t *testing.T) {
feedURL := ""
changes := &feedModification{FeedURL: &feedURL}
changes := &feedModificationRequest{FeedURL: &feedURL}
feed := &model.Feed{FeedURL: "http://example.org/"}
changes.Update(feed)
if feed.FeedURL == feedURL {
t.Fatal(`The FeedURL should not be modified`)
t.Error(`The FeedURL should not be modified`)
}
}
func TestUpdateFeedURLWhenNotSet(t *testing.T) {
changes := &feedModification{}
changes := &feedModificationRequest{}
feed := &model.Feed{FeedURL: "http://example.org/"}
changes.Update(feed)
if feed.FeedURL != "http://example.org/" {
t.Fatal(`The FeedURL should not be modified`)
t.Error(`The FeedURL should not be modified`)
}
}
func TestUpdateFeedSiteURL(t *testing.T) {
siteURL := "http://example.com/"
changes := &feedModification{SiteURL: &siteURL}
changes := &feedModificationRequest{SiteURL: &siteURL}
feed := &model.Feed{SiteURL: "http://example.org/"}
changes.Update(feed)
if feed.SiteURL != siteURL {
t.Fatalf(`Unexpected value, got %q instead of %q`, feed.SiteURL, siteURL)
t.Errorf(`Unexpected value, got %q instead of %q`, feed.SiteURL, siteURL)
}
}
func TestUpdateFeedSiteURLWithEmptyString(t *testing.T) {
siteURL := ""
changes := &feedModification{FeedURL: &siteURL}
changes := &feedModificationRequest{FeedURL: &siteURL}
feed := &model.Feed{SiteURL: "http://example.org/"}
changes.Update(feed)
if feed.SiteURL == siteURL {
t.Fatal(`The FeedURL should not be modified`)
t.Error(`The FeedURL should not be modified`)
}
}
func TestUpdateFeedSiteURLWhenNotSet(t *testing.T) {
changes := &feedModification{}
changes := &feedModificationRequest{}
feed := &model.Feed{SiteURL: "http://example.org/"}
changes.Update(feed)
if feed.SiteURL != "http://example.org/" {
t.Fatal(`The SiteURL should not be modified`)
t.Error(`The SiteURL should not be modified`)
}
}
func TestUpdateFeedTitle(t *testing.T) {
title := "Example 2"
changes := &feedModification{Title: &title}
changes := &feedModificationRequest{Title: &title}
feed := &model.Feed{Title: "Example"}
changes.Update(feed)
if feed.Title != title {
t.Fatalf(`Unexpected value, got %q instead of %q`, feed.Title, title)
t.Errorf(`Unexpected value, got %q instead of %q`, feed.Title, title)
}
}
func TestUpdateFeedTitleWithEmptyString(t *testing.T) {
title := ""
changes := &feedModification{Title: &title}
changes := &feedModificationRequest{Title: &title}
feed := &model.Feed{Title: "Example"}
changes.Update(feed)
if feed.Title == title {
t.Fatal(`The Title should not be modified`)
t.Error(`The Title should not be modified`)
}
}
func TestUpdateFeedTitleWhenNotSet(t *testing.T) {
changes := &feedModification{}
changes := &feedModificationRequest{}
feed := &model.Feed{Title: "Example"}
changes.Update(feed)
if feed.Title != "Example" {
t.Fatal(`The Title should not be modified`)
t.Error(`The Title should not be modified`)
}
}
func TestUpdateFeedUsername(t *testing.T) {
username := "Alice"
changes := &feedModification{Username: &username}
changes := &feedModificationRequest{Username: &username}
feed := &model.Feed{Username: "Bob"}
changes.Update(feed)
if feed.Username != username {
t.Fatalf(`Unexpected value, got %q instead of %q`, feed.Username, username)
t.Errorf(`Unexpected value, got %q instead of %q`, feed.Username, username)
}
}
func TestUpdateFeedUsernameWithEmptyString(t *testing.T) {
username := ""
changes := &feedModification{Username: &username}
changes := &feedModificationRequest{Username: &username}
feed := &model.Feed{Username: "Bob"}
changes.Update(feed)
if feed.Username != "" {
t.Fatal(`The Username should be empty now`)
t.Error(`The Username should be empty now`)
}
}
func TestUpdateFeedUsernameWhenNotSet(t *testing.T) {
changes := &feedModification{}
changes := &feedModificationRequest{}
feed := &model.Feed{Username: "Alice"}
changes.Update(feed)
if feed.Username != "Alice" {
t.Fatal(`The Username should not be modified`)
t.Error(`The Username should not be modified`)
}
}
@ -142,16 +142,16 @@ func TestUpdateFeedDisabled(t *testing.T) {
valueTrue := true
valueFalse := false
scenarios := []struct {
changes *feedModification
changes *feedModificationRequest
feed *model.Feed
expected bool
}{
{&feedModification{}, &model.Feed{Disabled: true}, true},
{&feedModification{Disabled: &valueTrue}, &model.Feed{Disabled: true}, true},
{&feedModification{Disabled: &valueFalse}, &model.Feed{Disabled: true}, false},
{&feedModification{}, &model.Feed{Disabled: false}, false},
{&feedModification{Disabled: &valueTrue}, &model.Feed{Disabled: false}, true},
{&feedModification{Disabled: &valueFalse}, &model.Feed{Disabled: false}, false},
{&feedModificationRequest{}, &model.Feed{Disabled: true}, true},
{&feedModificationRequest{Disabled: &valueTrue}, &model.Feed{Disabled: true}, true},
{&feedModificationRequest{Disabled: &valueFalse}, &model.Feed{Disabled: true}, false},
{&feedModificationRequest{}, &model.Feed{Disabled: false}, false},
{&feedModificationRequest{Disabled: &valueTrue}, &model.Feed{Disabled: false}, true},
{&feedModificationRequest{Disabled: &valueFalse}, &model.Feed{Disabled: false}, false},
}
for _, scenario := range scenarios {
@ -167,53 +167,75 @@ func TestUpdateFeedDisabled(t *testing.T) {
func TestUpdateFeedCategory(t *testing.T) {
categoryID := int64(1)
changes := &feedModification{CategoryID: &categoryID}
changes := &feedModificationRequest{CategoryID: &categoryID}
feed := &model.Feed{Category: &model.Category{ID: 42}}
changes.Update(feed)
if feed.Category.ID != categoryID {
t.Fatalf(`Unexpected value, got %q instead of %q`, feed.Username, categoryID)
t.Errorf(`Unexpected value, got %q instead of %q`, feed.Username, categoryID)
}
}
func TestUpdateFeedCategoryWithZero(t *testing.T) {
categoryID := int64(0)
changes := &feedModification{CategoryID: &categoryID}
changes := &feedModificationRequest{CategoryID: &categoryID}
feed := &model.Feed{Category: &model.Category{ID: 42}}
changes.Update(feed)
if feed.Category.ID != 42 {
t.Fatal(`The CategoryID should not be modified`)
t.Error(`The CategoryID should not be modified`)
}
}
func TestUpdateFeedCategoryWhenNotSet(t *testing.T) {
changes := &feedModification{}
changes := &feedModificationRequest{}
feed := &model.Feed{Category: &model.Category{ID: 42}}
changes.Update(feed)
if feed.Category.ID != 42 {
t.Fatal(`The CategoryID should not be modified`)
t.Error(`The CategoryID should not be modified`)
}
}
func TestUpdateFeedToIgnoreCache(t *testing.T) {
value := true
changes := &feedModificationRequest{IgnoreHTTPCache: &value}
feed := &model.Feed{IgnoreHTTPCache: false}
changes.Update(feed)
if feed.IgnoreHTTPCache != value {
t.Errorf(`The field IgnoreHTTPCache should be %v`, value)
}
}
func TestUpdateFeedToFetchViaProxy(t *testing.T) {
value := true
changes := &feedModificationRequest{FetchViaProxy: &value}
feed := &model.Feed{FetchViaProxy: false}
changes.Update(feed)
if feed.FetchViaProxy != value {
t.Errorf(`The field FetchViaProxy should be %v`, value)
}
}
func TestUpdateUserTheme(t *testing.T) {
theme := "Example 2"
changes := &userModification{Theme: &theme}
changes := &userModificationRequest{Theme: &theme}
user := &model.User{Theme: "Example"}
changes.Update(user)
if user.Theme != theme {
t.Fatalf(`Unexpected value, got %q instead of %q`, user.Theme, theme)
t.Errorf(`Unexpected value, got %q instead of %q`, user.Theme, theme)
}
}
func TestUserThemeWhenNotSet(t *testing.T) {
changes := &userModification{}
changes := &userModificationRequest{}
user := &model.User{Theme: "Example"}
changes.Update(user)
if user.Theme != "Example" {
t.Fatal(`The user Theme should not be modified`)
t.Error(`The user Theme should not be modified`)
}
}

View file

@ -12,18 +12,18 @@ import (
)
func (h *handler) getSubscriptions(w http.ResponseWriter, r *http.Request) {
subscriptionInfo, bodyErr := decodeURLPayload(r.Body)
subscriptionRequest, bodyErr := decodeSubscriptionDiscoveryRequest(r.Body)
if bodyErr != nil {
json.BadRequest(w, r, bodyErr)
return
}
subscriptions, finderErr := subscription.FindSubscriptions(
subscriptionInfo.URL,
subscriptionInfo.UserAgent,
subscriptionInfo.Username,
subscriptionInfo.Password,
subscriptionInfo.FetchViaProxy,
subscriptionRequest.URL,
subscriptionRequest.UserAgent,
subscriptionRequest.Username,
subscriptionRequest.Password,
subscriptionRequest.FetchViaProxy,
)
if finderErr != nil {
json.ServerError(w, r, finderErr)

View file

@ -28,7 +28,7 @@ func (h *handler) createUser(w http.ResponseWriter, r *http.Request) {
return
}
user, err := decodeUserCreationPayload(r.Body)
user, err := decodeUserCreationRequest(r.Body)
if err != nil {
json.BadRequest(w, r, err)
return
@ -61,7 +61,7 @@ func (h *handler) updateUser(w http.ResponseWriter, r *http.Request) {
}
userID := request.RouteInt64Param(r, "userID")
userChanges, err := decodeUserModificationPayload(r.Body)
userChanges, err := decodeUserModificationRequest(r.Body)
if err != nil {
json.BadRequest(w, r, err)
return