mirror of
https://github.com/miniflux/v2.git
synced 2025-07-22 17:18:37 +00:00
refactor(processor): move FilterEntryMaxAgeDays filter to filter package
This commit is contained in:
parent
e6b814199b
commit
db49e41acf
4 changed files with 92 additions and 36 deletions
|
@ -11,6 +11,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"miniflux.app/v2/internal/config"
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
|
@ -21,7 +22,31 @@ const (
|
|||
filterActionAllow filterActionType = "allow"
|
||||
)
|
||||
|
||||
func isBlockedGlobally(entry *model.Entry) bool {
|
||||
if config.Opts == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if config.Opts.FilterEntryMaxAgeDays() > 0 {
|
||||
maxAge := time.Duration(config.Opts.FilterEntryMaxAgeDays()) * 24 * time.Hour
|
||||
if entry.Date.Before(time.Now().Add(-maxAge)) {
|
||||
slog.Debug("Entry is blocked globally due to max age",
|
||||
slog.String("entry_url", entry.URL),
|
||||
slog.Time("entry_date", entry.Date),
|
||||
slog.Duration("max_age", maxAge),
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func IsBlockedEntry(feed *model.Feed, entry *model.Entry, user *model.User) bool {
|
||||
if isBlockedGlobally(entry) {
|
||||
return true
|
||||
}
|
||||
|
||||
combinedRules := combineFilterRules(user.BlockFilterEntryRules, feed.BlockFilterEntryRules)
|
||||
if combinedRules != "" {
|
||||
if matchesEntryFilterRules(combinedRules, entry, feed, filterActionBlock) {
|
||||
|
|
|
@ -4,9 +4,11 @@
|
|||
package filter // import "miniflux.app/v2/internal/reader/filter"
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"miniflux.app/v2/internal/config"
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
|
@ -171,3 +173,66 @@ func TestMaxAgeFilter(t *testing.T) {
|
|||
t.Error("Expected new entry to not be blocked with max-age:1d")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsBlockedGlobally(t *testing.T) {
|
||||
var err error
|
||||
config.Opts, err = config.NewParser().ParseEnvironmentVariables()
|
||||
if err != nil {
|
||||
t.Fatalf(`Parsing failure: %v`, err)
|
||||
}
|
||||
|
||||
if isBlockedGlobally(&model.Entry{Title: "Test Entry", Date: time.Date(2020, 5, 1, 05, 05, 05, 05, time.UTC)}) {
|
||||
t.Error("Expected no entries to be blocked globally when max-age is not set")
|
||||
}
|
||||
|
||||
os.Setenv("FILTER_ENTRY_MAX_AGE_DAYS", "30")
|
||||
defer os.Clearenv()
|
||||
|
||||
config.Opts, err = config.NewParser().ParseEnvironmentVariables()
|
||||
if err != nil {
|
||||
t.Fatalf(`Parsing failure: %v`, err)
|
||||
}
|
||||
|
||||
if !isBlockedGlobally(&model.Entry{Title: "Test Entry", Date: time.Date(2020, 5, 1, 05, 05, 05, 05, time.UTC)}) {
|
||||
t.Error("Expected entries to be blocked globally when max-age is set")
|
||||
}
|
||||
|
||||
if isBlockedGlobally(&model.Entry{Title: "Test Entry", Date: time.Now().Add(-2 * time.Hour)}) {
|
||||
t.Error("Expected entries not to be blocked globally when they are within the max-age limit")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsBlockedEntryWithGlobalMaxAge(t *testing.T) {
|
||||
os.Setenv("FILTER_ENTRY_MAX_AGE_DAYS", "30")
|
||||
defer os.Clearenv()
|
||||
|
||||
var err error
|
||||
config.Opts, err = config.NewParser().ParseEnvironmentVariables()
|
||||
if err != nil {
|
||||
t.Fatalf(`Parsing failure: %v`, err)
|
||||
}
|
||||
|
||||
entry := &model.Entry{Title: "Test Entry", Date: time.Now().Add(-31 * 24 * time.Hour)} // 31 days old
|
||||
feed := &model.Feed{ID: 1}
|
||||
user := &model.User{}
|
||||
|
||||
if !IsBlockedEntry(feed, entry, user) {
|
||||
t.Error("Expected entry to be blocked due to global max-age rule")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsBlockedEntryWithDefaultGlobalMaxAge(t *testing.T) {
|
||||
var err error
|
||||
config.Opts, err = config.NewParser().ParseEnvironmentVariables()
|
||||
if err != nil {
|
||||
t.Fatalf(`Parsing failure: %v`, err)
|
||||
}
|
||||
|
||||
entry := &model.Entry{Title: "Test Entry", Date: time.Now().Add(-31 * 24 * time.Hour)} // 31 days old
|
||||
feed := &model.Feed{ID: 1}
|
||||
user := &model.User{}
|
||||
|
||||
if IsBlockedEntry(feed, entry, user) {
|
||||
t.Error("Expected entry not to be blocked due to default global max-age rule")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue