feed.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "miniflux.app/v2/internal/urllib"
  9. )
  10. // ValidateFeedCreation validates feed creation.
  11. func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *locale.LocalizedError {
  12. if request.FeedURL == "" || request.CategoryID <= 0 {
  13. return locale.NewLocalizedError("error.feed_mandatory_fields")
  14. }
  15. if !urllib.IsAbsoluteURL(request.FeedURL) {
  16. return locale.NewLocalizedError("error.invalid_feed_url")
  17. }
  18. if store.FeedURLExists(userID, request.FeedURL) {
  19. return locale.NewLocalizedError("error.feed_already_exists")
  20. }
  21. if !store.CategoryIDExists(userID, request.CategoryID) {
  22. return locale.NewLocalizedError("error.feed_category_not_found")
  23. }
  24. if !IsValidRegex(request.BlocklistRules) {
  25. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  26. }
  27. if !IsValidRegex(request.KeeplistRules) {
  28. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  29. }
  30. if request.BlockFilterEntryRules != "" {
  31. if err := isValidFilterRules(request.BlockFilterEntryRules, "block"); err != nil {
  32. return err
  33. }
  34. }
  35. if request.KeepFilterEntryRules != "" {
  36. if err := isValidFilterRules(request.KeepFilterEntryRules, "keep"); err != nil {
  37. return err
  38. }
  39. }
  40. if request.ProxyURL != "" && !urllib.IsAbsoluteURL(request.ProxyURL) {
  41. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  42. }
  43. return nil
  44. }
  45. // ValidateFeedModification validates feed modification.
  46. func ValidateFeedModification(store *storage.Storage, userID, feedID int64, request *model.FeedModificationRequest) *locale.LocalizedError {
  47. if request.FeedURL != nil {
  48. if *request.FeedURL == "" {
  49. return locale.NewLocalizedError("error.feed_url_not_empty")
  50. }
  51. if !urllib.IsAbsoluteURL(*request.FeedURL) {
  52. return locale.NewLocalizedError("error.invalid_feed_url")
  53. }
  54. if store.AnotherFeedURLExists(userID, feedID, *request.FeedURL) {
  55. return locale.NewLocalizedError("error.feed_already_exists")
  56. }
  57. }
  58. if request.SiteURL != nil {
  59. if *request.SiteURL == "" {
  60. return locale.NewLocalizedError("error.site_url_not_empty")
  61. }
  62. if !urllib.IsAbsoluteURL(*request.SiteURL) {
  63. return locale.NewLocalizedError("error.invalid_site_url")
  64. }
  65. }
  66. if request.Title != nil {
  67. if *request.Title == "" {
  68. return locale.NewLocalizedError("error.feed_title_not_empty")
  69. }
  70. }
  71. if request.CategoryID != nil {
  72. if !store.CategoryIDExists(userID, *request.CategoryID) {
  73. return locale.NewLocalizedError("error.feed_category_not_found")
  74. }
  75. }
  76. if request.BlocklistRules != nil {
  77. if !IsValidRegex(*request.BlocklistRules) {
  78. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  79. }
  80. }
  81. if request.KeeplistRules != nil {
  82. if !IsValidRegex(*request.KeeplistRules) {
  83. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  84. }
  85. }
  86. if request.BlockFilterEntryRules != nil && *request.BlockFilterEntryRules != "" {
  87. if err := isValidFilterRules(*request.BlockFilterEntryRules, "block"); err != nil {
  88. return err
  89. }
  90. }
  91. if request.KeepFilterEntryRules != nil && *request.KeepFilterEntryRules != "" {
  92. if err := isValidFilterRules(*request.KeepFilterEntryRules, "keep"); err != nil {
  93. return err
  94. }
  95. }
  96. if request.ProxyURL != nil {
  97. if *request.ProxyURL == "" {
  98. return locale.NewLocalizedError("error.proxy_url_not_empty")
  99. }
  100. if !urllib.IsAbsoluteURL(*request.ProxyURL) {
  101. return locale.NewLocalizedError("error.invalid_feed_proxy_url")
  102. }
  103. }
  104. return nil
  105. }