1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Add global block and keep filters

This commit is contained in:
privatmamtora 2024-07-03 04:03:49 +00:00 committed by GitHub
parent c4278821cb
commit 1a81866bb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 457 additions and 50 deletions

View file

@ -4,6 +4,9 @@
package validator // import "miniflux.app/v2/internal/validator"
import (
"slices"
"strings"
"miniflux.app/v2/internal/locale"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/storage"
@ -108,6 +111,18 @@ func ValidateUserModification(store *storage.Storage, userID int64, changes *mod
}
}
if changes.BlockFilterEntryRules != nil {
if err := isValidFilterRules(*changes.BlockFilterEntryRules, "block"); err != nil {
return err
}
}
if changes.KeepFilterEntryRules != nil {
if err := isValidFilterRules(*changes.KeepFilterEntryRules, "keep"); err != nil {
return err
}
}
return nil
}
@ -195,3 +210,35 @@ func validateMediaPlaybackRate(mediaPlaybackRate float64) *locale.LocalizedError
}
return nil
}
func isValidFilterRules(filterEntryRules string, filterType string) *locale.LocalizedError {
// Valid Format: FieldName(RegEx)~FieldName(RegEx)~...
fieldNames := []string{"EntryTitle", "EntryURL", "EntryCommentsURL", "EntryContent", "EntryAuthor", "EntryTag"}
rules := strings.Split(filterEntryRules, "\n")
for i, rule := range rules {
// Check if rule starts with a valid fieldName
idx := slices.IndexFunc(fieldNames, func(fieldName string) bool { return strings.HasPrefix(rule, fieldName) })
if idx == -1 {
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_fieldname_invalid", i+1, "'"+strings.Join(fieldNames, "', '")+"'")
}
fieldName := fieldNames[idx]
fieldRegEx, _ := strings.CutPrefix(rule, fieldName)
// Check if regex begins with a =
if !strings.HasPrefix(fieldRegEx, "=") {
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_separator_required", i+1)
}
fieldRegEx = strings.TrimPrefix(fieldRegEx, "=")
if fieldRegEx == "" {
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_regex_required", i+1)
}
// Check if provided pattern is a valid RegEx
if !IsValidRegex(fieldRegEx) {
return locale.NewLocalizedError("error.settings_"+filterType+"_rule_invalid_regex", i+1)
}
}
return nil
}