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) =>