4
0

feed.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 // import "miniflux.app/ui/form"
  5. import (
  6. "net/http"
  7. "strconv"
  8. "miniflux.app/errors"
  9. "miniflux.app/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. BlocklistRules string
  19. KeeplistRules string
  20. Crawler bool
  21. UserAgent string
  22. CategoryID int64
  23. Username string
  24. Password string
  25. IgnoreHTTPCache bool
  26. FetchViaProxy bool
  27. Disabled bool
  28. }
  29. // ValidateModification validates FeedForm fields
  30. func (f FeedForm) ValidateModification() error {
  31. if f.FeedURL == "" || f.SiteURL == "" || f.Title == "" || f.CategoryID == 0 {
  32. return errors.NewLocalizedError("error.fields_mandatory")
  33. }
  34. return nil
  35. }
  36. // Merge updates the fields of the given feed.
  37. func (f FeedForm) Merge(feed *model.Feed) *model.Feed {
  38. feed.Category.ID = f.CategoryID
  39. feed.Title = f.Title
  40. feed.SiteURL = f.SiteURL
  41. feed.FeedURL = f.FeedURL
  42. feed.ScraperRules = f.ScraperRules
  43. feed.RewriteRules = f.RewriteRules
  44. feed.BlocklistRules = f.BlocklistRules
  45. feed.KeeplistRules = f.KeeplistRules
  46. feed.Crawler = f.Crawler
  47. feed.UserAgent = f.UserAgent
  48. feed.ParsingErrorCount = 0
  49. feed.ParsingErrorMsg = ""
  50. feed.Username = f.Username
  51. feed.Password = f.Password
  52. feed.IgnoreHTTPCache = f.IgnoreHTTPCache
  53. feed.FetchViaProxy = f.FetchViaProxy
  54. feed.Disabled = f.Disabled
  55. return feed
  56. }
  57. // NewFeedForm parses the HTTP request and returns a FeedForm
  58. func NewFeedForm(r *http.Request) *FeedForm {
  59. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  60. if err != nil {
  61. categoryID = 0
  62. }
  63. return &FeedForm{
  64. FeedURL: r.FormValue("feed_url"),
  65. SiteURL: r.FormValue("site_url"),
  66. Title: r.FormValue("title"),
  67. ScraperRules: r.FormValue("scraper_rules"),
  68. UserAgent: r.FormValue("user_agent"),
  69. RewriteRules: r.FormValue("rewrite_rules"),
  70. BlocklistRules: r.FormValue("blocklist_rules"),
  71. KeeplistRules: r.FormValue("keeplist_rules"),
  72. Crawler: r.FormValue("crawler") == "1",
  73. CategoryID: int64(categoryID),
  74. Username: r.FormValue("feed_username"),
  75. Password: r.FormValue("feed_password"),
  76. IgnoreHTTPCache: r.FormValue("ignore_http_cache") == "1",
  77. FetchViaProxy: r.FormValue("fetch_via_proxy") == "1",
  78. Disabled: r.FormValue("disabled") == "1",
  79. }
  80. }