1
0
Fork 0

Filter feed entries based on url or title

This commit is contained in:
Tianfeng Wang 2023-10-26 03:38:08 +01:00 committed by GitHub
parent eeaab72a9f
commit a1537f4b0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View file

@ -115,8 +115,7 @@ func ProcessFeedEntries(store *storage.Storage, feed *model.Feed, user *model.Us
func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool { func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool {
if feed.BlocklistRules != "" { if feed.BlocklistRules != "" {
match, _ := regexp.MatchString(feed.BlocklistRules, entry.Title) if matchField(feed.BlocklistRules, entry.URL) || matchField(feed.BlocklistRules, entry.Title) {
if match {
slog.Debug("Blocking entry based on rule", slog.Debug("Blocking entry based on rule",
slog.Int64("entry_id", entry.ID), slog.Int64("entry_id", entry.ID),
slog.String("entry_url", entry.URL), slog.String("entry_url", entry.URL),
@ -127,13 +126,13 @@ func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool {
return true return true
} }
} }
return false return false
} }
func isAllowedEntry(feed *model.Feed, entry *model.Entry) bool { func isAllowedEntry(feed *model.Feed, entry *model.Entry) bool {
if feed.KeeplistRules != "" { if feed.KeeplistRules != "" {
match, _ := regexp.MatchString(feed.KeeplistRules, entry.Title) if matchField(feed.KeeplistRules, entry.URL) || matchField(feed.KeeplistRules, entry.Title) {
if match {
slog.Debug("Allow entry based on rule", slog.Debug("Allow entry based on rule",
slog.Int64("entry_id", entry.ID), slog.Int64("entry_id", entry.ID),
slog.String("entry_url", entry.URL), slog.String("entry_url", entry.URL),
@ -148,6 +147,19 @@ func isAllowedEntry(feed *model.Feed, entry *model.Entry) bool {
return true return true
} }
func matchField(pattern, value string) bool {
match, err := regexp.MatchString(pattern, value)
if err != nil {
slog.Debug("Failed on regexp match",
slog.String("pattern", pattern),
slog.String("value", value),
slog.Bool("match", match),
slog.Any("error", err),
)
}
return match
}
// ProcessEntryWebPage downloads the entry web page and apply rewrite rules. // ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
func ProcessEntryWebPage(feed *model.Feed, entry *model.Entry, user *model.User) error { func ProcessEntryWebPage(feed *model.Feed, entry *model.Entry, user *model.User) error {
startTime := time.Now() startTime := time.Now()

View file

@ -16,6 +16,8 @@ func TestBlockingEntries(t *testing.T) {
entry *model.Entry entry *model.Entry
expected bool expected bool
}{ }{
{&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{URL: "https://example.com"}, true},
{&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: "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"}, false},
{&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, false}, {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, false},
@ -35,6 +37,8 @@ func TestAllowEntries(t *testing.T) {
entry *model.Entry entry *model.Entry
expected bool expected bool
}{ }{
{&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "https://example.com"}, true},
{&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "https://different.com"}, false},
{&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Some Example"}, true}, {&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, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different"}, false},
{&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, true}, {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, true},