-
Notifications
You must be signed in to change notification settings - Fork 721
/
feed.go
87 lines (69 loc) · 2.26 KB
/
feed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package validator // import "miniflux.app/validator"
import (
"miniflux.app/model"
"miniflux.app/storage"
)
// ValidateFeedCreation validates feed creation.
func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *ValidationError {
if request.FeedURL == "" || request.CategoryID <= 0 {
return NewValidationError("error.feed_mandatory_fields")
}
if !IsValidURL(request.FeedURL) {
return NewValidationError("error.invalid_feed_url")
}
if store.FeedURLExists(userID, request.FeedURL) {
return NewValidationError("error.feed_already_exists")
}
if !store.CategoryIDExists(userID, request.CategoryID) {
return NewValidationError("error.feed_category_not_found")
}
if !IsValidRegex(request.BlocklistRules) {
return NewValidationError("error.feed_invalid_blocklist_rule")
}
if !IsValidRegex(request.KeeplistRules) {
return NewValidationError("error.feed_invalid_keeplist_rule")
}
return nil
}
// ValidateFeedModification validates feed modification.
func ValidateFeedModification(store *storage.Storage, userID int64, request *model.FeedModificationRequest) *ValidationError {
if request.FeedURL != nil {
if *request.FeedURL == "" {
return NewValidationError("error.feed_url_not_empty")
}
if !IsValidURL(*request.FeedURL) {
return NewValidationError("error.invalid_feed_url")
}
}
if request.SiteURL != nil {
if *request.SiteURL == "" {
return NewValidationError("error.site_url_not_empty")
}
if !IsValidURL(*request.SiteURL) {
return NewValidationError("error.invalid_site_url")
}
}
if request.Title != nil {
if *request.Title == "" {
return NewValidationError("error.feed_title_not_empty")
}
}
if request.CategoryID != nil {
if !store.CategoryIDExists(userID, *request.CategoryID) {
return NewValidationError("error.feed_category_not_found")
}
}
if request.BlocklistRules != nil {
if !IsValidRegex(*request.BlocklistRules) {
return NewValidationError("error.feed_invalid_blocklist_rule")
}
}
if request.KeeplistRules != nil {
if !IsValidRegex(*request.KeeplistRules) {
return NewValidationError("error.feed_invalid_keeplist_rule")
}
}
return nil
}