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.
|
|
|
|
|
|
|
|
package form
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2017-11-28 00:30:04 -05:00
|
|
|
|
2017-12-13 00:48:13 -05:00
|
|
|
"github.com/miniflux/miniflux/errors"
|
|
|
|
"github.com/miniflux/miniflux/model"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// FeedForm represents a feed form in the UI
|
|
|
|
type FeedForm struct {
|
2017-12-10 23:51:04 -05:00
|
|
|
FeedURL string
|
|
|
|
SiteURL string
|
|
|
|
Title string
|
|
|
|
ScraperRules string
|
2017-12-12 01:16:32 -05:00
|
|
|
RewriteRules string
|
2017-12-12 22:19:36 -05:00
|
|
|
Crawler bool
|
2017-12-10 23:51:04 -05:00
|
|
|
CategoryID int64
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateModification validates FeedForm fields
|
|
|
|
func (f FeedForm) ValidateModification() error {
|
|
|
|
if f.FeedURL == "" || f.SiteURL == "" || f.Title == "" || f.CategoryID == 0 {
|
2017-11-28 00:30:04 -05:00
|
|
|
return errors.NewLocalizedError("All fields are mandatory.")
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Merge updates the fields of the given feed.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (f FeedForm) Merge(feed *model.Feed) *model.Feed {
|
|
|
|
feed.Category.ID = f.CategoryID
|
|
|
|
feed.Title = f.Title
|
|
|
|
feed.SiteURL = f.SiteURL
|
|
|
|
feed.FeedURL = f.FeedURL
|
2017-12-10 23:51:04 -05:00
|
|
|
feed.ScraperRules = f.ScraperRules
|
2017-12-12 01:16:32 -05:00
|
|
|
feed.RewriteRules = f.RewriteRules
|
2017-12-12 22:19:36 -05:00
|
|
|
feed.Crawler = f.Crawler
|
2017-11-20 00:10:04 -05:00
|
|
|
feed.ParsingErrorCount = 0
|
|
|
|
feed.ParsingErrorMsg = ""
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFeedForm parses the HTTP request and returns a FeedForm
|
|
|
|
func NewFeedForm(r *http.Request) *FeedForm {
|
|
|
|
categoryID, err := strconv.Atoi(r.FormValue("category_id"))
|
|
|
|
if err != nil {
|
|
|
|
categoryID = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FeedForm{
|
2017-12-10 23:51:04 -05:00
|
|
|
FeedURL: r.FormValue("feed_url"),
|
|
|
|
SiteURL: r.FormValue("site_url"),
|
|
|
|
Title: r.FormValue("title"),
|
|
|
|
ScraperRules: r.FormValue("scraper_rules"),
|
2017-12-12 01:16:32 -05:00
|
|
|
RewriteRules: r.FormValue("rewrite_rules"),
|
2017-12-12 22:19:36 -05:00
|
|
|
Crawler: r.FormValue("crawler") == "1",
|
2017-12-10 23:51:04 -05:00
|
|
|
CategoryID: int64(categoryID),
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|