From fe8baf7989d8aab98d6aa575d4975f9799a4cdda Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 7 May 2022 13:22:06 -0400 Subject: [PATCH] make watcher keywords more flexible --- internal/domain/subreddit_test.go | 4 ++-- internal/domain/watcher.go | 21 ++++++++++++++++++ internal/domain/watcher_test.go | 36 +++++++++++++++++++++++++++++++ internal/worker/subreddits.go | 6 +----- 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 internal/domain/watcher_test.go diff --git a/internal/domain/subreddit_test.go b/internal/domain/subreddit_test.go index fbebe40..cdb80be 100644 --- a/internal/domain/subreddit_test.go +++ b/internal/domain/subreddit_test.go @@ -13,7 +13,7 @@ import ( func TestValidate(t *testing.T) { t.Parallel() - tests := map[string]struct { + tt := map[string]struct { subreddit domain.Subreddit err error }{ @@ -23,7 +23,7 @@ func TestValidate(t *testing.T) { "valid subreddit with _": {domain.Subreddit{Name: "p_i_x_a_r", SubredditID: "abcd"}, nil}, } - for scenario, tc := range tests { + for scenario, tc := range tt { t.Run(scenario, func(t *testing.T) { t.Parallel() diff --git a/internal/domain/watcher.go b/internal/domain/watcher.go index e985d17..88789ed 100644 --- a/internal/domain/watcher.go +++ b/internal/domain/watcher.go @@ -2,6 +2,7 @@ package domain import ( "context" + "strings" "time" validation "github.com/go-ozzo/ozzo-validation/v4" @@ -53,6 +54,26 @@ type Watcher struct { Account Account } +func (w *Watcher) KeywordMatches(haystack string) bool { + if w.Keyword == "" { + return true + } + + keywords := strings.FieldsFunc(w.Keyword, func(r rune) bool { + return r == '+' || r == ',' + }) + + haystack = strings.ToLower(haystack) + + for _, keyword := range keywords { + if !strings.Contains(haystack, keyword) { + return false + } + } + + return true +} + func (w *Watcher) Validate() error { return validation.ValidateStruct(w, validation.Field(&w.Label, validation.Required, validation.Length(1, 64)), diff --git a/internal/domain/watcher_test.go b/internal/domain/watcher_test.go new file mode 100644 index 0000000..8aeec4e --- /dev/null +++ b/internal/domain/watcher_test.go @@ -0,0 +1,36 @@ +package domain_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/christianselig/apollo-backend/internal/domain" +) + +func TestWatcherKeywordMatches(t *testing.T) { + t.Parallel() + + tt := map[string]struct { + title string + keyword string + + want bool + }{ + "match exact": {"exact title", "exact title", true}, + "empty keyword matches all": {"exact title", "", true}, + "keywords with commas": {"exact title", "exact,title", true}, + "keywords with plus": {"exact title", "exact+title", true}, + "missing words": {"exact title", "not title", false}, + } + + for scenario, tc := range tt { + t.Run(scenario, func(t *testing.T) { + t.Parallel() + + w := &domain.Watcher{Keyword: tc.keyword} + + assert.Equal(t, tc.want, w.KeywordMatches(tc.title)) + }) + } +} diff --git a/internal/worker/subreddits.go b/internal/worker/subreddits.go index bc015cc..65fcbab 100644 --- a/internal/worker/subreddits.go +++ b/internal/worker/subreddits.go @@ -318,7 +318,7 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) { continue } - matched := true + matched := watcher.KeywordMatches(lowcaseTitle) if watcher.Author != "" && lowcaseAuthor != watcher.Author { matched = false @@ -328,10 +328,6 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) { matched = false } - if watcher.Keyword != "" && !strings.Contains(lowcaseTitle, watcher.Keyword) { - matched = false - } - if watcher.Flair != "" && !strings.Contains(lowcaseFlair, watcher.Flair) { matched = false }