mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-13 07:27:43 +00:00
make watcher keywords more flexible
This commit is contained in:
parent
53a4bbee25
commit
fe8baf7989
4 changed files with 60 additions and 7 deletions
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)),
|
||||
|
|
36
internal/domain/watcher_test.go
Normal file
36
internal/domain/watcher_test.go
Normal 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))
|
||||
})
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue