feed.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package validator // import "miniflux.app/v2/internal/validator"
  4. import (
  5. "miniflux.app/v2/internal/locale"
  6. "miniflux.app/v2/internal/model"
  7. "miniflux.app/v2/internal/storage"
  8. )
  9. // ValidateFeedCreation validates feed creation.
  10. func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *locale.LocalizedError {
  11. if request.FeedURL == "" || request.CategoryID <= 0 {
  12. return locale.NewLocalizedError("error.feed_mandatory_fields")
  13. }
  14. if !IsValidURL(request.FeedURL) {
  15. return locale.NewLocalizedError("error.invalid_feed_url")
  16. }
  17. if store.FeedURLExists(userID, request.FeedURL) {
  18. return locale.NewLocalizedError("error.feed_already_exists")
  19. }
  20. if !store.CategoryIDExists(userID, request.CategoryID) {
  21. return locale.NewLocalizedError("error.feed_category_not_found")
  22. }
  23. if !IsValidRegex(request.BlocklistRules) {
  24. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  25. }
  26. if !IsValidRegex(request.KeeplistRules) {
  27. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  28. }
  29. return nil
  30. }
  31. // ValidateFeedModification validates feed modification.
  32. func ValidateFeedModification(store *storage.Storage, userID int64, request *model.FeedModificationRequest) *locale.LocalizedError {
  33. if request.FeedURL != nil {
  34. if *request.FeedURL == "" {
  35. return locale.NewLocalizedError("error.feed_url_not_empty")
  36. }
  37. if !IsValidURL(*request.FeedURL) {
  38. return locale.NewLocalizedError("error.invalid_feed_url")
  39. }
  40. }
  41. if request.SiteURL != nil {
  42. if *request.SiteURL == "" {
  43. return locale.NewLocalizedError("error.site_url_not_empty")
  44. }
  45. if !IsValidURL(*request.SiteURL) {
  46. return locale.NewLocalizedError("error.invalid_site_url")
  47. }
  48. }
  49. if request.Title != nil {
  50. if *request.Title == "" {
  51. return locale.NewLocalizedError("error.feed_title_not_empty")
  52. }
  53. }
  54. if request.CategoryID != nil {
  55. if !store.CategoryIDExists(userID, *request.CategoryID) {
  56. return locale.NewLocalizedError("error.feed_category_not_found")
  57. }
  58. }
  59. if request.BlocklistRules != nil {
  60. if !IsValidRegex(*request.BlocklistRules) {
  61. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  62. }
  63. }
  64. if request.KeeplistRules != nil {
  65. if !IsValidRegex(*request.KeeplistRules) {
  66. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  67. }
  68. }
  69. return nil
  70. }