subscription.go 1.2 KB

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