feed.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, feedID 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. if store.AnotherFeedURLExists(userID, feedID, *request.FeedURL) {
  41. return locale.NewLocalizedError("error.feed_already_exists")
  42. }
  43. }
  44. if request.SiteURL != nil {
  45. if *request.SiteURL == "" {
  46. return locale.NewLocalizedError("error.site_url_not_empty")
  47. }
  48. if !IsValidURL(*request.SiteURL) {
  49. return locale.NewLocalizedError("error.invalid_site_url")
  50. }
  51. }
  52. if request.Title != nil {
  53. if *request.Title == "" {
  54. return locale.NewLocalizedError("error.feed_title_not_empty")
  55. }
  56. }
  57. if request.CategoryID != nil {
  58. if !store.CategoryIDExists(userID, *request.CategoryID) {
  59. return locale.NewLocalizedError("error.feed_category_not_found")
  60. }
  61. }
  62. if request.BlocklistRules != nil {
  63. if !IsValidRegex(*request.BlocklistRules) {
  64. return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
  65. }
  66. }
  67. if request.KeeplistRules != nil {
  68. if !IsValidRegex(*request.KeeplistRules) {
  69. return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
  70. }
  71. }
  72. return nil
  73. }