make watcher keywords more flexible

This commit is contained in:
Andre Medeiros 2022-05-07 13:22:06 -04:00
parent 53a4bbee25
commit fe8baf7989
4 changed files with 60 additions and 7 deletions

View file

@ -13,7 +13,7 @@ import (
func TestValidate(t *testing.T) { func TestValidate(t *testing.T) {
t.Parallel() t.Parallel()
tests := map[string]struct { tt := map[string]struct {
subreddit domain.Subreddit subreddit domain.Subreddit
err error 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}, "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.Run(scenario, func(t *testing.T) {
t.Parallel() t.Parallel()

View file

@ -2,6 +2,7 @@ package domain
import ( import (
"context" "context"
"strings"
"time" "time"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
@ -53,6 +54,26 @@ type Watcher struct {
Account Account 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 { func (w *Watcher) Validate() error {
return validation.ValidateStruct(w, return validation.ValidateStruct(w,
validation.Field(&w.Label, validation.Required, validation.Length(1, 64)), validation.Field(&w.Label, validation.Required, validation.Length(1, 64)),

View file

@ -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))
})
}
}

View file

@ -318,7 +318,7 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) {
continue continue
} }
matched := true matched := watcher.KeywordMatches(lowcaseTitle)
if watcher.Author != "" && lowcaseAuthor != watcher.Author { if watcher.Author != "" && lowcaseAuthor != watcher.Author {
matched = false matched = false
@ -328,10 +328,6 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) {
matched = false matched = false
} }
if watcher.Keyword != "" && !strings.Contains(lowcaseTitle, watcher.Keyword) {
matched = false
}
if watcher.Flair != "" && !strings.Contains(lowcaseFlair, watcher.Flair) { if watcher.Flair != "" && !strings.Contains(lowcaseFlair, watcher.Flair) {
matched = false matched = false
} }