subscription.go 1.3 KB

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