Skip to content

Commit ae1942a

Browse files
Soxasorahuumn
andauthored
fix: duplicate push notification on subscribed user and territory (#1820)
* fix: duplicate notification on subscribed user and territory * fix comments not showing up, adjust query * use and tagged template helpers --------- Co-authored-by: k00b <[email protected]>
1 parent a92215c commit ae1942a

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

lib/webPush.js

+31-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { COMMENT_DEPTH_LIMIT, FOUND_BLURBS, LOST_BLURBS } from './constants'
44
import { msatsToSats, numWithUnits } from './format'
55
import models from '@/api/models'
66
import { isMuted } from '@/lib/user'
7+
import { Prisma } from '@prisma/client'
78

89
const webPushEnabled = process.env.NODE_ENV === 'production' ||
910
(process.env.VAPID_MAILTO && process.env.NEXT_PUBLIC_VAPID_PUBKEY && process.env.VAPID_PRIVKEY)
@@ -123,21 +124,36 @@ export async function replyToSubscription (subscriptionId, notification) {
123124
export const notifyUserSubscribers = async ({ models, item }) => {
124125
try {
125126
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}`
141157
const subType = isPost ? 'POST' : 'COMMENT'
142158
const tag = `FOLLOW-${item.userId}-${subType}`
143159
await Promise.allSettled(userSubsExcludingMutes.map(({ followerId, followeeName }) => sendUserNotification(followerId, {

0 commit comments

Comments
 (0)