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 WatcheeID int64
Author string Author string
Subreddit string
Upvotes int64 Upvotes int64
Keyword string Keyword string
Flair string Flair string

View file

@ -38,6 +38,7 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg
&watcher.Type, &watcher.Type,
&watcher.WatcheeID, &watcher.WatcheeID,
&watcher.Author, &watcher.Author,
&watcher.Subreddit,
&watcher.Upvotes, &watcher.Upvotes,
&watcher.Keyword, &watcher.Keyword,
&watcher.Flair, &watcher.Flair,
@ -69,6 +70,7 @@ func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (doma
watchers.type, watchers.type,
watchers.watchee_id, watchers.watchee_id,
watchers.author, watchers.author,
watchers.subreddit,
watchers.upvotes, watchers.upvotes,
watchers.keyword, watchers.keyword,
watchers.flair, watchers.flair,
@ -108,6 +110,7 @@ func (p *postgresWatcherRepository) GetByTypeAndWatcheeID(ctx context.Context, t
watchers.type, watchers.type,
watchers.watchee_id, watchers.watchee_id,
watchers.author, watchers.author,
watchers.subreddit,
watchers.upvotes, watchers.upvotes,
watchers.keyword, watchers.keyword,
watchers.flair, watchers.flair,
@ -151,6 +154,7 @@ func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx c
watchers.type, watchers.type,
watchers.watchee_id, watchers.watchee_id,
watchers.author, watchers.author,
watchers.subreddit,
watchers.upvotes, watchers.upvotes,
watchers.keyword, watchers.keyword,
watchers.flair, watchers.flair,
@ -177,8 +181,8 @@ func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain.
query := ` query := `
INSERT INTO watchers INSERT INTO watchers
(created_at, last_notified_at, label, device_id, account_id, type, watchee_id, author, upvotes, keyword, flair, domain) (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) VALUES ($1, 0, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING id` RETURNING id`
return p.pool.QueryRow( return p.pool.QueryRow(
@ -191,6 +195,7 @@ func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain.
watcher.Type, watcher.Type,
watcher.WatcheeID, watcher.WatcheeID,
watcher.Author, watcher.Author,
watcher.Subreddit,
watcher.Upvotes, watcher.Upvotes,
watcher.Keyword, watcher.Keyword,
watcher.Flair, watcher.Flair,
@ -202,11 +207,12 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.
query := ` query := `
UPDATE watchers UPDATE watchers
SET author = $2, SET author = $2,
upvotes = $3, subreddit = $3,
keyword = $4, upvotes = $4,
flair = $5, keyword = $5,
domain = $6, flair = $6,
label = $7 domain = $7,
label = $8
WHERE id = $1` WHERE id = $1`
res, err := p.pool.Exec( res, err := p.pool.Exec(
@ -214,6 +220,7 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.
query, query,
watcher.ID, watcher.ID,
watcher.Author, watcher.Author,
watcher.Subreddit,
watcher.Upvotes, watcher.Upvotes,
watcher.Keyword, watcher.Keyword,
watcher.Flair, watcher.Flair,

View file

@ -6,6 +6,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"strconv" "strconv"
"strings"
"github.com/DataDog/datadog-go/statsd" "github.com/DataDog/datadog-go/statsd"
"github.com/adjust/rmq/v4" "github.com/adjust/rmq/v4"
@ -222,11 +223,13 @@ func (uc *usersConsumer) Consume(delivery rmq.Delivery) {
} }
for _, post := range posts.Children { for _, post := range posts.Children {
lowcaseSubreddit := strings.ToLower(post.Subreddit)
if post.SubredditType == "private" { if post.SubredditType == "private" {
continue continue
} }
payload := payloadFromUserPost(post) notifs := []domain.Watcher{}
for _, watcher := range watchers { for _, watcher := range watchers {
// Make sure we only alert on activities created after the search // Make sure we only alert on activities created after the search
@ -238,6 +241,23 @@ func (uc *usersConsumer) Consume(delivery rmq.Delivery) {
continue 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 { if err := uc.watcherRepo.IncrementHits(ctx, watcher.ID); err != nil {
uc.logger.WithFields(logrus.Fields{ uc.logger.WithFields(logrus.Fields{
"user#id": user.ID, "user#id": user.ID,
@ -252,8 +272,6 @@ func (uc *usersConsumer) Consume(delivery rmq.Delivery) {
title := fmt.Sprintf(userNotificationTitleFormat, watcher.Label) title := fmt.Sprintf(userNotificationTitleFormat, watcher.Label)
payload.AlertTitle(title) payload.AlertTitle(title)
notification := &apns2.Notification{}
notification.Topic = "com.christianselig.Apollo"
notification.Payload = payload notification.Payload = payload
notification.DeviceToken = device.APNSToken notification.DeviceToken = device.APNSToken