From 46318fe11767134444f97a8d0198549b15aa6360 Mon Sep 17 00:00:00 2001 From: Satoshi Nakamoto Date: Sun, 5 May 2024 11:09:00 -0400 Subject: [PATCH] move `isMuted` to shared user lib, reuse in user resolver and webpush --- api/resolvers/user.js | 20 ++++---------------- lib/user.js | 12 ++++++++++++ lib/webPush.js | 12 +++--------- 3 files changed, 19 insertions(+), 25 deletions(-) create mode 100644 lib/user.js diff --git a/api/resolvers/user.js b/api/resolvers/user.js index b7503223c..65dc3ff07 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -10,6 +10,7 @@ import { viewGroup } from './growth' import { timeUnitForRange, whenRange } from '@/lib/time' import assertApiKeyNotPermitted from './apiKey' import { hashEmail } from '@/lib/crypto' +import { isMuted } from '@/lib/user' const contributors = new Set() @@ -113,19 +114,6 @@ export function viewValueGroup () { ) vv` } -const isMuted = async ({ models, me, mutedId }) => { - const mute = await models.mute.findUnique({ - where: { - muterId_mutedId: { - muterId: Number(me.id), - mutedId: Number(mutedId) - } - } - }) - - return !!mute -} - export default { Query: { me: async (parent, args, { models, me }) => { @@ -714,7 +702,7 @@ export default { subscribeUserPosts: async (parent, { id }, { me, models }) => { const lookupData = { followerId: Number(me.id), followeeId: Number(id) } const existing = await models.userSubscription.findUnique({ where: { followerId_followeeId: lookupData } }) - const muted = await isMuted({ models, me, mutedId: id }) + const muted = await isMuted({ models, muterId: me?.id, mutedId: id }) if (existing) { if (muted && !existing.postsSubscribedAt) { throw new GraphQLError("you can't subscribe to a stacker that you've muted", { extensions: { code: 'BAD_INPUT' } }) @@ -731,7 +719,7 @@ export default { subscribeUserComments: async (parent, { id }, { me, models }) => { const lookupData = { followerId: Number(me.id), followeeId: Number(id) } const existing = await models.userSubscription.findUnique({ where: { followerId_followeeId: lookupData } }) - const muted = await isMuted({ models, me, mutedId: id }) + const muted = await isMuted({ models, muterId: me?.id, mutedId: id }) if (existing) { if (muted && !existing.commentsSubscribedAt) { throw new GraphQLError("you can't subscribe to a stacker that you've muted", { extensions: { code: 'BAD_INPUT' } }) @@ -821,7 +809,7 @@ export default { if (!me) return false if (typeof user.meMute !== 'undefined') return user.meMute - return await isMuted({ models, me, mutedId: user.id }) + return await isMuted({ models, muterId: me.id, mutedId: user.id }) }, since: async (user, args, { models }) => { // get the user's first item diff --git a/lib/user.js b/lib/user.js new file mode 100644 index 000000000..b6460b1d6 --- /dev/null +++ b/lib/user.js @@ -0,0 +1,12 @@ +export const isMuted = async ({ models, muterId, mutedId }) => { + const mute = await models.mute.findUnique({ + where: { + muterId_mutedId: { + muterId: Number(muterId), + mutedId: Number(mutedId) + } + } + }) + + return !!mute +} diff --git a/lib/webPush.js b/lib/webPush.js index 91bab12eb..f8928242e 100644 --- a/lib/webPush.js +++ b/lib/webPush.js @@ -3,6 +3,7 @@ import removeMd from 'remove-markdown' import { ANON_USER_ID, COMMENT_DEPTH_LIMIT, FOUND_BLURBS, LOST_BLURBS } from './constants' import { msatsToSats, numWithUnits } from './format' import models from '@/api/models' +import { isMuted } from '@/lib/user' const webPushEnabled = process.env.NODE_ENV === 'production' || (process.env.VAPID_MAILTO && process.env.NEXT_PUBLIC_VAPID_PUBKEY && process.env.VAPID_PRIVKEY) @@ -247,15 +248,8 @@ export const notifyZapped = async ({ models, id }) => { export const notifyMention = async ({ models, userId, item }) => { try { - const mute = await models.mute.findUnique({ - where: { - muterId_mutedId: { - muterId: Number(userId), - mutedId: Number(item.userId) - } - } - }) - if (!mute) { + const muted = await isMuted({ models, muterId: userId, mutedId: item.userId }) + if (!muted) { await sendUserNotification(userId, { title: 'you were mentioned', body: item.text,