@@ -4,6 +4,7 @@ import { COMMENT_DEPTH_LIMIT, FOUND_BLURBS, LOST_BLURBS } from './constants'
4
4
import { msatsToSats , numWithUnits } from './format'
5
5
import models from '@/api/models'
6
6
import { isMuted } from '@/lib/user'
7
+ import { Prisma } from '@prisma/client'
7
8
8
9
const webPushEnabled = process . env . NODE_ENV === 'production' ||
9
10
( process . env . VAPID_MAILTO && process . env . NEXT_PUBLIC_VAPID_PUBKEY && process . env . VAPID_PRIVKEY )
@@ -123,21 +124,36 @@ export async function replyToSubscription (subscriptionId, notification) {
123
124
export const notifyUserSubscribers = async ( { models, item } ) => {
124
125
try {
125
126
const isPost = ! ! item . title
126
- const userSubsExcludingMutes = await models . $queryRawUnsafe ( `
127
- SELECT "UserSubscription"."followerId", "UserSubscription"."followeeId", users.name as "followeeName"
128
- FROM "UserSubscription"
129
- INNER JOIN users ON users.id = "UserSubscription"."followeeId"
130
- WHERE "followeeId" = $1 AND ${ isPost ? '"postsSubscribedAt"' : '"commentsSubscribedAt"' } IS NOT NULL
131
- AND NOT EXISTS (SELECT 1 FROM "Mute" WHERE "Mute"."muterId" = "UserSubscription"."followerId" AND "Mute"."mutedId" = $1)
132
- -- ignore subscription if user was already notified of item as a reply
133
- AND NOT EXISTS (
134
- SELECT 1 FROM "Reply"
135
- INNER JOIN users follower ON follower.id = "UserSubscription"."followerId"
136
- WHERE "Reply"."itemId" = $2
137
- AND "Reply"."ancestorUserId" = follower.id
138
- AND follower."noteAllDescendants"
139
- )
140
- ` , Number ( item . userId ) , Number ( item . id ) )
127
+
128
+ const userSubsExcludingMutes = await models . $queryRaw `
129
+ SELECT "UserSubscription"."followerId", "UserSubscription"."followeeId", users.name as "followeeName"
130
+ FROM "UserSubscription"
131
+ INNER JOIN users ON users.id = "UserSubscription"."followeeId"
132
+ WHERE "followeeId" = ${ Number ( item . userId ) } ::INTEGER
133
+ AND ${ isPost ? Prisma . sql `"postsSubscribedAt"` : Prisma . sql `"commentsSubscribedAt"` } IS NOT NULL
134
+ -- ignore muted users
135
+ AND NOT EXISTS (
136
+ SELECT 1
137
+ FROM "Mute"
138
+ WHERE "Mute"."muterId" = "UserSubscription"."followerId"
139
+ AND "Mute"."mutedId" = ${ Number ( item . userId ) } ::INTEGER)
140
+ -- ignore subscription if user was already notified of item as a reply
141
+ AND NOT EXISTS (
142
+ SELECT 1 FROM "Reply"
143
+ INNER JOIN users follower ON follower.id = "UserSubscription"."followerId"
144
+ WHERE "Reply"."itemId" = ${ Number ( item . id ) } ::INTEGER
145
+ AND "Reply"."ancestorUserId" = follower.id
146
+ AND follower."noteAllDescendants"
147
+ )
148
+ -- ignore subscription if user has posted to a territory the recipient is subscribed to
149
+ ${ isPost
150
+ ? Prisma . sql `AND NOT EXISTS (
151
+ SELECT 1
152
+ FROM "SubSubscription"
153
+ WHERE "SubSubscription"."userId" = "UserSubscription"."followerId"
154
+ AND "SubSubscription"."subName" = ${ item . subName }
155
+ )`
156
+ : Prisma . empty } `
141
157
const subType = isPost ? 'POST' : 'COMMENT'
142
158
const tag = `FOLLOW-${ item . userId } -${ subType } `
143
159
await Promise . allSettled ( userSubsExcludingMutes . map ( ( { followerId, followeeName } ) => sendUserNotification ( followerId , {
0 commit comments