From 74b6df2e4430d42b24fe741d6c22d990ebc62e0c Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 31 Aug 2022 16:18:48 -0600 Subject: [PATCH] Unwatch applies to email comment notifs too --- functions/src/create-notification.ts | 14 ++++++++++++++ functions/src/on-create-comment-on-contract.ts | 16 +++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/functions/src/create-notification.ts b/functions/src/create-notification.ts index 9c5d98c1..8ed14704 100644 --- a/functions/src/create-notification.ts +++ b/functions/src/create-notification.ts @@ -723,3 +723,17 @@ export const createLikeNotification = async ( } return await notificationRef.set(removeUndefinedProps(notification)) } + +export async function filterUserIdsForOnlyFollowerIds( + userIds: string[], + contractId: string +) { + // get contract follower documents and check here if they're a follower + const contractFollowersSnap = await firestore + .collection(`contracts/${contractId}/follows`) + .get() + const contractFollowersIds = contractFollowersSnap.docs.map( + (doc) => doc.data().id + ) + return userIds.filter((id) => contractFollowersIds.includes(id)) +} diff --git a/functions/src/on-create-comment-on-contract.ts b/functions/src/on-create-comment-on-contract.ts index 8651bde0..663a7977 100644 --- a/functions/src/on-create-comment-on-contract.ts +++ b/functions/src/on-create-comment-on-contract.ts @@ -6,7 +6,10 @@ import { ContractComment } from '../../common/comment' import { sendNewCommentEmail } from './emails' import { Bet } from '../../common/bet' import { Answer } from '../../common/answer' -import { createCommentOrAnswerOrUpdatedContractNotification } from './create-notification' +import { + createCommentOrAnswerOrUpdatedContractNotification, + filterUserIdsForOnlyFollowerIds, +} from './create-notification' import { parseMentions, richTextToString } from '../../common/util/parse' import { addUserToContractFollowers } from './follow-market' @@ -95,10 +98,13 @@ export const onCreateCommentOnContract = functions } ) - const recipientUserIds = uniq([ - contract.creatorId, - ...comments.map((comment) => comment.userId), - ]).filter((id) => id !== comment.userId) + const recipientUserIds = await filterUserIdsForOnlyFollowerIds( + uniq([ + contract.creatorId, + ...comments.map((comment) => comment.userId), + ]).filter((id) => id !== comment.userId), + contractId + ) await Promise.all( recipientUserIds.map((userId) =>