From 0674ddf730aa37d07bb6074b40cd5384b62718b8 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 13 Oct 2021 10:43:05 -0400 Subject: [PATCH] allow user notifications to be filtered by subreddit --- internal/domain/watcher.go | 13 +++++++------ internal/repository/postgres_watcher.go | 21 ++++++++++++++------- internal/worker/users.go | 24 +++++++++++++++++++++--- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/internal/domain/watcher.go b/internal/domain/watcher.go index a141473..bd1128d 100644 --- a/internal/domain/watcher.go +++ b/internal/domain/watcher.go @@ -34,12 +34,13 @@ type Watcher struct { Type WatcherType WatcheeID int64 - Author string - Upvotes int64 - Keyword string - Flair string - Domain string - Hits int64 + Author string + Subreddit string + Upvotes int64 + Keyword string + Flair string + Domain string + Hits int64 // Related models Device Device diff --git a/internal/repository/postgres_watcher.go b/internal/repository/postgres_watcher.go index efde594..dad7b2e 100644 --- a/internal/repository/postgres_watcher.go +++ b/internal/repository/postgres_watcher.go @@ -38,6 +38,7 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg &watcher.Type, &watcher.WatcheeID, &watcher.Author, + &watcher.Subreddit, &watcher.Upvotes, &watcher.Keyword, &watcher.Flair, @@ -69,6 +70,7 @@ func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (doma watchers.type, watchers.watchee_id, watchers.author, + watchers.subreddit, watchers.upvotes, watchers.keyword, watchers.flair, @@ -108,6 +110,7 @@ func (p *postgresWatcherRepository) GetByTypeAndWatcheeID(ctx context.Context, t watchers.type, watchers.watchee_id, watchers.author, + watchers.subreddit, watchers.upvotes, watchers.keyword, watchers.flair, @@ -151,6 +154,7 @@ func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx c watchers.type, watchers.watchee_id, watchers.author, + watchers.subreddit, watchers.upvotes, watchers.keyword, watchers.flair, @@ -177,8 +181,8 @@ func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain. query := ` INSERT INTO watchers - (created_at, last_notified_at, label, device_id, account_id, type, watchee_id, author, upvotes, keyword, flair, domain) - VALUES ($1, 0, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) + (created_at, last_notified_at, label, device_id, account_id, type, watchee_id, author, subreddit, upvotes, keyword, flair, domain) + VALUES ($1, 0, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING id` return p.pool.QueryRow( @@ -191,6 +195,7 @@ func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain. watcher.Type, watcher.WatcheeID, watcher.Author, + watcher.Subreddit, watcher.Upvotes, watcher.Keyword, watcher.Flair, @@ -202,11 +207,12 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain. query := ` UPDATE watchers SET author = $2, - upvotes = $3, - keyword = $4, - flair = $5, - domain = $6, - label = $7 + subreddit = $3, + upvotes = $4, + keyword = $5, + flair = $6, + domain = $7, + label = $8 WHERE id = $1` res, err := p.pool.Exec( @@ -214,6 +220,7 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain. query, watcher.ID, watcher.Author, + watcher.Subreddit, watcher.Upvotes, watcher.Keyword, watcher.Flair, diff --git a/internal/worker/users.go b/internal/worker/users.go index 50c34ab..b4abb39 100644 --- a/internal/worker/users.go +++ b/internal/worker/users.go @@ -6,6 +6,7 @@ import ( "math/rand" "os" "strconv" + "strings" "github.com/DataDog/datadog-go/statsd" "github.com/adjust/rmq/v4" @@ -222,11 +223,13 @@ func (uc *usersConsumer) Consume(delivery rmq.Delivery) { } for _, post := range posts.Children { + lowcaseSubreddit := strings.ToLower(post.Subreddit) + if post.SubredditType == "private" { continue } - payload := payloadFromUserPost(post) + notifs := []domain.Watcher{} for _, watcher := range watchers { // Make sure we only alert on activities created after the search @@ -238,6 +241,23 @@ func (uc *usersConsumer) Consume(delivery rmq.Delivery) { continue } + if watcher.Subreddit != "" && lowcaseSubreddit != watcher.Subreddit { + continue + } + + notifs = append(notifs, watcher) + } + + if len(notifs) == 0 { + continue + } + + payload := payloadFromUserPost(post) + + notification := &apns2.Notification{} + notification.Topic = "com.christianselig.Apollo" + + for _, watcher := range notifs { if err := uc.watcherRepo.IncrementHits(ctx, watcher.ID); err != nil { uc.logger.WithFields(logrus.Fields{ "user#id": user.ID, @@ -252,8 +272,6 @@ func (uc *usersConsumer) Consume(delivery rmq.Delivery) { title := fmt.Sprintf(userNotificationTitleFormat, watcher.Label) payload.AlertTitle(title) - notification := &apns2.Notification{} - notification.Topic = "com.christianselig.Apollo" notification.Payload = payload notification.DeviceToken = device.APNSToken