From 1441dc76006c30ff962c3d44ff2fecdd911dec4e Mon Sep 17 00:00:00 2001 From: Filipe de Luna Date: Wed, 10 Jan 2024 05:15:11 +0000 Subject: [PATCH] Update entry processor to allow blocking/keeping entries by tags --- internal/reader/processor/processor.go | 20 ++++++++++++++++++-- internal/reader/processor/processor_test.go | 8 ++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/reader/processor/processor.go b/internal/reader/processor/processor.go index bad2f134..a3c53be6 100644 --- a/internal/reader/processor/processor.go +++ b/internal/reader/processor/processor.go @@ -115,7 +115,15 @@ func ProcessFeedEntries(store *storage.Storage, feed *model.Feed, user *model.Us func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool { if feed.BlocklistRules != "" { - if matchField(feed.BlocklistRules, entry.URL) || matchField(feed.BlocklistRules, entry.Title) { + var containsBlockedTag bool = false + for _, tag := range entry.Tags { + if matchField(feed.BlocklistRules, tag) { + containsBlockedTag = true + break + } + } + + if matchField(feed.BlocklistRules, entry.URL) || matchField(feed.BlocklistRules, entry.Title) || containsBlockedTag { slog.Debug("Blocking entry based on rule", slog.Int64("entry_id", entry.ID), slog.String("entry_url", entry.URL), @@ -132,7 +140,15 @@ func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool { func isAllowedEntry(feed *model.Feed, entry *model.Entry) bool { if feed.KeeplistRules != "" { - if matchField(feed.KeeplistRules, entry.URL) || matchField(feed.KeeplistRules, entry.Title) { + var containsAllowedTag bool = false + for _, tag := range entry.Tags { + if matchField(feed.KeeplistRules, tag) { + containsAllowedTag = true + break + } + } + + if matchField(feed.KeeplistRules, entry.URL) || matchField(feed.KeeplistRules, entry.Title) || containsAllowedTag { slog.Debug("Allow entry based on rule", slog.Int64("entry_id", entry.ID), slog.String("entry_url", entry.URL), diff --git a/internal/reader/processor/processor_test.go b/internal/reader/processor/processor_test.go index bbc58c23..72469429 100644 --- a/internal/reader/processor/processor_test.go +++ b/internal/reader/processor/processor_test.go @@ -20,6 +20,10 @@ func TestBlockingEntries(t *testing.T) { {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{URL: "https://different.com"}, false}, {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Some Example"}, true}, {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different"}, false}, + {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"example", "something else"}}, true}, + {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"example", "something else"}}, true}, + {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"something different", "something else"}}, true}, + {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"something different", "something else"}}, false}, {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, false}, } @@ -42,6 +46,10 @@ func TestAllowEntries(t *testing.T) { {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Some Example"}, true}, {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different"}, false}, {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, true}, + {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"example", "something else"}}, true}, + {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"example", "something else"}}, true}, + {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"something different", "something else"}}, true}, + {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something more", Tags: []string{"something different", "something else"}}, false}, } for _, tc := range scenarios {