feed.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package form
  5. import (
  6. "net/http"
  7. "strconv"
  8. "github.com/miniflux/miniflux2/errors"
  9. "github.com/miniflux/miniflux2/model"
  10. )
  11. // FeedForm represents a feed form in the UI
  12. type FeedForm struct {
  13. FeedURL string
  14. SiteURL string
  15. Title string
  16. ScraperRules string
  17. RewriteRules string
  18. CategoryID int64
  19. }
  20. // ValidateModification validates FeedForm fields
  21. func (f FeedForm) ValidateModification() error {
  22. if f.FeedURL == "" || f.SiteURL == "" || f.Title == "" || f.CategoryID == 0 {
  23. return errors.NewLocalizedError("All fields are mandatory.")
  24. }
  25. return nil
  26. }
  27. // Merge updates the fields of the given feed.
  28. func (f FeedForm) Merge(feed *model.Feed) *model.Feed {
  29. feed.Category.ID = f.CategoryID
  30. feed.Title = f.Title
  31. feed.SiteURL = f.SiteURL
  32. feed.FeedURL = f.FeedURL
  33. feed.ScraperRules = f.ScraperRules
  34. feed.RewriteRules = f.RewriteRules
  35. feed.ParsingErrorCount = 0
  36. feed.ParsingErrorMsg = ""
  37. return feed
  38. }
  39. // NewFeedForm parses the HTTP request and returns a FeedForm
  40. func NewFeedForm(r *http.Request) *FeedForm {
  41. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  42. if err != nil {
  43. categoryID = 0
  44. }
  45. return &FeedForm{
  46. FeedURL: r.FormValue("feed_url"),
  47. SiteURL: r.FormValue("site_url"),
  48. Title: r.FormValue("title"),
  49. ScraperRules: r.FormValue("scraper_rules"),
  50. RewriteRules: r.FormValue("rewrite_rules"),
  51. CategoryID: int64(categoryID),
  52. }
  53. }