8000 [pull] main from miniflux:main by pull[bot] · Pull Request #315 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 51 additions & 24 deletions internal/reader/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"slices"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"miniflux.app/v2/internal/model"
Expand All @@ -41,6 +43,34 @@ type filterRule struct {

type filterRules []filterRule

const maxCachedRegexes = 1024

var (
compiledRegexesCache sync.Map
compiledRegexesCacheSize atomic.Int64
)

func cachedRegex(pattern string) *regexp.Regexp {
if v, ok := compiledRegexesCache.Load(pattern); ok {
return v.(*regexp.Regexp)
}

re, err := regexp.Compile(pattern)
if err != nil {
slog.Warn("Failed on regexp compilation",
slog.String("regex_pattern", pattern),
slog.Any("error", err),
)
}

compiledRegexesCache.Store(pattern, re)
if compiledRegexesCacheSize.Add(1) >= maxCachedRegexes {
compiledRegexesCache.Clear()
compiledRegexesCacheSize.Store(0)
}
return re
}

func ParseRules(userRules, feedRules string) filterRules {
rules := make(filterRules, 0)
for line := range strings.SplitSeq(strings.TrimSpace(userRules), "\n") {
Expand Down Expand Up @@ -103,12 +133,8 @@ func matchesEntryRegexRules(regexPattern string, feed *model.Feed, entry *model.
return false, true // No pattern means rule is valid but doesn't match
}

compiledRegex, err := regexp.Compile(regexPattern)
if err != nil {
slog.Warn("Failed on regexp compilation",
slog.String("regex_pattern", regexPattern),
slog.Any("error", err),
)
compiledRegex := cachedRegex(regexPattern)
if compiledRegex == nil {
return false, false // Invalid regex pattern
}

Expand Down Expand Up @@ -151,26 +177,28 @@ func matchesEntryFilterRules(rules filterRules, feed *model.Feed, entry *model.E
}

func matchesRule(rule filterRule, entry *model.Entry) bool {
switch rule.Type {
case "EntryDate":
if rule.Type == "EntryDate" {
return isDateMatchingPattern(rule.Value, entry.Date)
}

re := cachedRegex(rule.Value)
if re == nil {
return false
}

switch rule.Type {
case "EntryTitle":
match, _ := regexp.MatchString(rule.Value, entry.Title)
return match
return re.MatchString(entry.Title)
case "EntryURL":
match, _ := regexp.MatchString(rule.Value, entry.URL)
return match
return re.MatchString(entry.URL)
case "EntryCommentsURL":
match, _ := regexp.MatchString(rule.Value, entry.CommentsURL)
return match
return re.MatchString(entry.CommentsURL)
case "EntryContent":
match, _ := regexp.MatchString(rule.Value, entry.Content)
return match
return re.MatchString(entry.Content)
case "EntryAuthor":
match, _ := regexp.MatchString(rule.Value, entry.Author)
return match
return re.MatchString(entry.Author)
case "EntryTag":
return containsRegexPattern(rule.Value, entry.Tags)
return slices.ContainsFunc(entry.Tags, re.MatchString)
}

return false
Expand Down Expand Up @@ -227,12 +255,11 @@ func isDateMatchingPattern(pattern string, entryDate time.Time) bool {
}

func containsRegexPattern(pattern string, items []string) bool {
for _, item := range items {
if matched, _ := regexp.MatchString(pattern, item); matched {
return true
}
re := cachedRegex(pattern)
if re == nil {
return false
}
return false
return slices.ContainsFunc(items, re.MatchString)
}

func parseDuration(duration string) (time.Duration, error) {
Expand Down
24 changes: 17 additions & 7 deletions internal/storage/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,24 @@ func (s *Storage) CategoriesWithFeedCount(userID int64, sortOrder string) (model
c.user_id,
c.title,
c.hide_globally,
(SELECT count(*) FROM feeds WHERE feeds.category_id=c.id) AS count,
(SELECT count(*)
FROM feeds
JOIN entries ON (feeds.id = entries.feed_id)
WHERE feeds.category_id = c.id AND entries.status = $1) AS count_unread
coalesce(fc.feed_count, 0),
coalesce(uc.unread_count, 0)
FROM categories c
LEFT JOIN (
SELECT category_id, count(*) AS feed_count
FROM feeds
WHERE user_id = $2
GROUP BY category_id
) fc ON fc.category_id = c.id
LEFT JOIN (
SELECT f.category_id, count(*) AS unread_count
FROM entries e
INNER JOIN feeds f ON f.id = e.feed_id
WHERE e.user_id = $2 AND e.status = $1
GROUP BY f.category_id
) uc ON uc.category_id = c.id
WHERE
user_id=$2
c.user_id=$2
`

if sortOrder == "alphabetical" {
Expand All @@ -135,7 +145,7 @@ func (s *Storage) CategoriesWithFeedCount(userID int64, sortOrder string) (model
} else {
query += `
ORDER BY
count_unread DESC,
coalesce(uc.unread_count, 0) DESC,
c.title ASC
`
}
Expand Down
Loading
0