mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
refactor(processor): massive refactoring of filters.go
- Use proper variable names for `key=value` strings parts - Explicitly assign false to the `match` boolean - Use an explicit `len(parts) == 2` assertion to help the compiler remove `isSliceInBounds` calls. - Refactor identical code into a containsRegexPattern function. - Early exit when parsing the first date fails when using the `Between` operator, instead of trying to parse the second one.
This commit is contained in:
parent
b139ac4a2c
commit
96c0ef4efd
1 changed files with 36 additions and 38 deletions
|
@ -13,35 +13,34 @@ import (
|
||||||
"miniflux.app/v2/internal/model"
|
"miniflux.app/v2/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO factorize isBlockedEntry and isAllowedEntry
|
||||||
|
|
||||||
func isBlockedEntry(feed *model.Feed, entry *model.Entry, user *model.User) bool {
|
func isBlockedEntry(feed *model.Feed, entry *model.Entry, user *model.User) bool {
|
||||||
if user.BlockFilterEntryRules != "" {
|
if user.BlockFilterEntryRules != "" {
|
||||||
rules := strings.Split(user.BlockFilterEntryRules, "\n")
|
rules := strings.Split(user.BlockFilterEntryRules, "\n")
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
|
match := false
|
||||||
parts := strings.SplitN(rule, "=", 2)
|
parts := strings.SplitN(rule, "=", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
var match bool
|
return false
|
||||||
switch parts[0] {
|
|
||||||
case "EntryDate":
|
|
||||||
datePattern := parts[1]
|
|
||||||
match = isDateMatchingPattern(entry.Date, datePattern)
|
|
||||||
case "EntryTitle":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.Title)
|
|
||||||
case "EntryURL":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.URL)
|
|
||||||
case "EntryCommentsURL":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.CommentsURL)
|
|
||||||
case "EntryContent":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.Content)
|
|
||||||
case "EntryAuthor":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.Author)
|
|
||||||
case "EntryTag":
|
|
||||||
containsTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
|
|
||||||
match, _ = regexp.MatchString(parts[1], tag)
|
|
||||||
return match
|
|
||||||
})
|
|
||||||
if containsTag {
|
|
||||||
match = true
|
|
||||||
}
|
}
|
||||||
|
part, pattern := parts[0], parts[1]
|
||||||
|
|
||||||
|
switch part {
|
||||||
|
case "EntryDate":
|
||||||
|
match = isDateMatchingPattern(pattern, entry.Date)
|
||||||
|
case "EntryTitle":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.Title)
|
||||||
|
case "EntryURL":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.URL)
|
||||||
|
case "EntryCommentsURL":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.CommentsURL)
|
||||||
|
case "EntryContent":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.Content)
|
||||||
|
case "EntryAuthor":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.Author)
|
||||||
|
case "EntryTag":
|
||||||
|
match = containsRegexPattern(pattern, entry.Tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
if match {
|
if match {
|
||||||
|
@ -90,31 +89,28 @@ func isAllowedEntry(feed *model.Feed, entry *model.Entry, user *model.User) bool
|
||||||
if user.KeepFilterEntryRules != "" {
|
if user.KeepFilterEntryRules != "" {
|
||||||
rules := strings.Split(user.KeepFilterEntryRules, "\n")
|
rules := strings.Split(user.KeepFilterEntryRules, "\n")
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
|
match := false
|
||||||
parts := strings.SplitN(rule, "=", 2)
|
parts := strings.SplitN(rule, "=", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
var match bool
|
return false
|
||||||
switch parts[0] {
|
|
||||||
case "EntryDate":
|
|
||||||
datePattern := parts[1]
|
|
||||||
match = isDateMatchingPattern(entry.Date, datePattern)
|
|
||||||
case "EntryTitle":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.Title)
|
|
||||||
case "EntryURL":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.URL)
|
|
||||||
case "EntryCommentsURL":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.CommentsURL)
|
|
||||||
case "EntryContent":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.Content)
|
|
||||||
case "EntryAuthor":
|
|
||||||
match, _ = regexp.MatchString(parts[1], entry.Author)
|
|
||||||
case "EntryTag":
|
|
||||||
containsTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
|
|
||||||
match, _ = regexp.MatchString(parts[1], tag)
|
|
||||||
return match
|
|
||||||
})
|
|
||||||
if containsTag {
|
|
||||||
match = true
|
|
||||||
}
|
}
|
||||||
|
part, pattern := parts[0], parts[1]
|
||||||
|
|
||||||
|
switch part {
|
||||||
|
case "EntryDate":
|
||||||
|
match = isDateMatchingPattern(pattern, entry.Date)
|
||||||
|
case "EntryTitle":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.Title)
|
||||||
|
case "EntryURL":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.URL)
|
||||||
|
case "EntryCommentsURL":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.CommentsURL)
|
||||||
|
case "EntryContent":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.Content)
|
||||||
|
case "EntryAuthor":
|
||||||
|
match, _ = regexp.MatchString(pattern, entry.Author)
|
||||||
|
case "EntryTag":
|
||||||
|
match = containsRegexPattern(pattern, entry.Tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
if match {
|
if match {
|
||||||
|
@ -158,7 +154,7 @@ func isAllowedEntry(feed *model.Feed, entry *model.Entry, user *model.User) bool
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isDateMatchingPattern(entryDate time.Time, pattern string) bool {
|
func isDateMatchingPattern(pattern string, entryDate time.Time) bool {
|
||||||
if pattern == "future" {
|
if pattern == "future" {
|
||||||
return entryDate.After(time.Now())
|
return entryDate.After(time.Now())
|
||||||
}
|
}
|
||||||
|
@ -168,8 +164,7 @@ func isDateMatchingPattern(entryDate time.Time, pattern string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
operator := parts[0]
|
operator, dateStr := parts[0], parts[1]
|
||||||
dateStr := parts[1]
|
|
||||||
|
|
||||||
switch operator {
|
switch operator {
|
||||||
case "before":
|
case "before":
|
||||||
|
@ -189,9 +184,12 @@ func isDateMatchingPattern(entryDate time.Time, pattern string) bool {
|
||||||
if len(dates) != 2 {
|
if len(dates) != 2 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
startDate, err1 := time.Parse("2006-01-02", dates[0])
|
startDate, err := time.Parse("2006-01-02", dates[0])
|
||||||
endDate, err2 := time.Parse("2006-01-02", dates[1])
|
if err != nil {
|
||||||
if err1 != nil || err2 != nil {
|
return false
|
||||||
|
}
|
||||||
|
endDate, err := time.Parse("2006-01-02", dates[1])
|
||||||
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return entryDate.After(startDate) && entryDate.Before(endDate)
|
return entryDate.After(startDate) && entryDate.Before(endDate)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue