feed.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/miniflux/errors"
  9. "github.com/miniflux/miniflux/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. Crawler bool
  19. CategoryID int64
  20. Username string
  21. Password string
  22. }
  23. // ValidateModification validates FeedForm fields
  24. func (f FeedForm) ValidateModification() error {
  25. if f.FeedURL == "" || f.SiteURL == "" || f.Title == "" || f.CategoryID == 0 {
  26. return errors.NewLocalizedError("All fields are mandatory.")
  27. }
  28. return nil
  29. }
  30. // Merge updates the fields of the given feed.
  31. func (f FeedForm) Merge(feed *model.Feed) *model.Feed {
  32. feed.Category.ID = f.CategoryID
  33. feed.Title = f.Title
  34. feed.SiteURL = f.SiteURL
  35. feed.FeedURL = f.FeedURL
  36. feed.ScraperRules = f.ScraperRules
  37. feed.RewriteRules = f.RewriteRules
  38. feed.Crawler = f.Crawler
  39. feed.ParsingErrorCount = 0
  40. feed.ParsingErrorMsg = ""
  41. feed.Username = f.Username
  42. feed.Password = f.Password
  43. return feed
  44. }
  45. // NewFeedForm parses the HTTP request and returns a FeedForm
  46. func NewFeedForm(r *http.Request) *FeedForm {
  47. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  48. if err != nil {
  49. categoryID = 0
  50. }
  51. return &FeedForm{
  52. FeedURL: r.FormValue("feed_url"),
  53. SiteURL: r.FormValue("site_url"),
  54. Title: r.FormValue("title"),
  55. ScraperRules: r.FormValue("scraper_rules"),
  56. RewriteRules: r.FormValue("rewrite_rules"),
  57. Crawler: r.FormValue("crawler") == "1",
  58. CategoryID: int64(categoryID),
  59. Username: r.FormValue("username"),
  60. Password: r.FormValue("password"),
  61. }
  62. }