allow user notifications to be filtered by subreddit

This commit is contained in:
Andre Medeiros 2021-10-13 10:43:05 -04:00
parent 84c03669f1
commit 0674ddf730
3 changed files with 42 additions and 16 deletions

View file

@ -35,6 +35,7 @@ type Watcher struct {
WatcheeID int64
Author string
Subreddit string
Upvotes int64
Keyword string
Flair string

View file

@ -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,

View file

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