2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-01-04 22:49:28 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package validator // import "miniflux.app/v2/internal/validator"
|
2021-01-04 22:49:28 +01:00
|
|
|
|
|
|
|
import (
|
2023-10-22 04:50:29 +02:00
|
|
|
"miniflux.app/v2/internal/locale"
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
2021-01-04 22:49:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateFeedCreation validates feed creation.
|
2023-10-22 04:50:29 +02:00
|
|
|
func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *locale.LocalizedError {
|
2021-01-04 22:49:28 +01:00
|
|
|
if request.FeedURL == "" || request.CategoryID <= 0 {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_mandatory_fields")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 03:38:45 +01:00
|
|
|
if !IsValidURL(request.FeedURL) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_feed_url")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if store.FeedURLExists(userID, request.FeedURL) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_already_exists")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !store.CategoryIDExists(userID, request.CategoryID) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_category_not_found")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 03:38:45 +01:00
|
|
|
if !IsValidRegex(request.BlocklistRules) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
|
2021-02-08 03:38:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidRegex(request.KeeplistRules) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
|
2021-02-08 03:38:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 22:49:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateFeedModification validates feed modification.
|
2024-03-16 02:17:13 +01:00
|
|
|
func ValidateFeedModification(store *storage.Storage, userID, feedID int64, request *model.FeedModificationRequest) *locale.LocalizedError {
|
2021-01-04 22:49:28 +01:00
|
|
|
if request.FeedURL != nil {
|
|
|
|
if *request.FeedURL == "" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_url_not_empty")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 03:38:45 +01:00
|
|
|
if !IsValidURL(*request.FeedURL) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_feed_url")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
2024-03-16 02:17:13 +01:00
|
|
|
|
|
|
|
if store.AnotherFeedURLExists(userID, feedID, *request.FeedURL) {
|
|
|
|
return locale.NewLocalizedError("error.feed_already_exists")
|
|
|
|
}
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.SiteURL != nil {
|
|
|
|
if *request.SiteURL == "" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.site_url_not_empty")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 03:38:45 +01:00
|
|
|
if !IsValidURL(*request.SiteURL) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.invalid_site_url")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Title != nil {
|
|
|
|
if *request.Title == "" {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_title_not_empty")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.CategoryID != nil {
|
|
|
|
if !store.CategoryIDExists(userID, *request.CategoryID) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_category_not_found")
|
2021-01-04 22:49:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 03:38:45 +01:00
|
|
|
if request.BlocklistRules != nil {
|
|
|
|
if !IsValidRegex(*request.BlocklistRules) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
|
2021-02-08 03:38:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.KeeplistRules != nil {
|
|
|
|
if !IsValidRegex(*request.KeeplistRules) {
|
2023-10-22 04:50:29 +02:00
|
|
|
return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
|
2021-02-08 03:38:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 22:49:28 +01:00
|
|
|
return nil
|
|
|
|
}
|