subscription.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. )
  10. // SubscriptionForm represents the subscription form.
  11. type SubscriptionForm struct {
  12. URL string
  13. CategoryID int64
  14. Crawler bool
  15. FetchViaProxy bool
  16. UserAgent string
  17. Username string
  18. Password string
  19. ScraperRules string
  20. RewriteRules string
  21. BlocklistRules string
  22. KeeplistRules string
  23. }
  24. // Validate makes sure the form values are valid.
  25. func (s *SubscriptionForm) Validate() error {
  26. if s.URL == "" || s.CategoryID == 0 {
  27. return errors.NewLocalizedError("error.feed_mandatory_fields")
  28. }
  29. return nil
  30. }
  31. // NewSubscriptionForm returns a new SubscriptionForm.
  32. func NewSubscriptionForm(r *http.Request) *SubscriptionForm {
  33. categoryID, err := strconv.Atoi(r.FormValue("category_id"))
  34. if err != nil {
  35. categoryID = 0
  36. }
  37. return &SubscriptionForm{
  38. URL: r.FormValue("url"),
  39. Crawler: r.FormValue("crawler") == "1",
  40. CategoryID: int64(categoryID),
  41. UserAgent: r.FormValue("user_agent"),
  42. Username: r.FormValue("feed_username"),
  43. Password: r.FormValue("feed_password"),
  44. ScraperRules: r.FormValue("scraper_rules"),
  45. RewriteRules: r.FormValue("rewrite_rules"),
  46. BlocklistRules: r.FormValue("blocklist_rules"),
  47. KeeplistRules: r.FormValue("keeplist_rules"),
  48. }
  49. }