Skip to content

Commit

Permalink
move isMuted to shared user lib, reuse in user resolver and webpush
Browse files Browse the repository at this point in the history
  • Loading branch information
SatsAllDay committed May 5, 2024
1 parent 5caa828 commit 46318fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
20 changes: 4 additions & 16 deletions api/resolvers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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' } })
Expand All @@ -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' } })
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/user.js
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 3 additions & 9 deletions lib/webPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 46318fe

Please sign in to comment.