feed.go 1.9 KB

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