From bb4dc5429988023e699c112efdec97ab56a0b89a Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Mon, 12 Sep 2022 09:13:29 -0600 Subject: [PATCH] Remove magic --- functions/src/create-notification.ts | 55 +++++++++------------------- functions/src/resolve-market.ts | 3 +- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/functions/src/create-notification.ts b/functions/src/create-notification.ts index 4ac66a3f..1c251e33 100644 --- a/functions/src/create-notification.ts +++ b/functions/src/create-notification.ts @@ -45,7 +45,7 @@ export const createNotification = async ( ) => { const { contract: sourceContract, recipients, slug, title } = miscData ?? {} - const shouldGetNotification = ( + const shouldReceiveNotification = ( userId: string, userToReasonTexts: recipients_to_reason_texts ) => { @@ -126,7 +126,7 @@ export const createNotification = async ( const followerUserId = doc.ref.parent.parent?.id if ( followerUserId && - shouldGetNotification(followerUserId, userToReasonTexts) + shouldReceiveNotification(followerUserId, userToReasonTexts) ) { userToReasonTexts[followerUserId] = { reason: 'contract_from_followed_user', @@ -135,50 +135,27 @@ export const createNotification = async ( }) } - const notifyFollowedUser = ( - userToReasonTexts: recipients_to_reason_texts, - followedUserId: string - ) => { - if (shouldGetNotification(followedUserId, userToReasonTexts)) - userToReasonTexts[followedUserId] = { - reason: 'on_new_follow', - } - } - const notifyTaggedUsers = ( userToReasonTexts: recipients_to_reason_texts, userIds: (string | undefined)[] ) => { userIds.forEach((id) => { - if (id && shouldGetNotification(id, userToReasonTexts)) + if (id && shouldReceiveNotification(id, userToReasonTexts)) userToReasonTexts[id] = { reason: 'tagged_user', } }) } - const notifyContractCreator = async ( - userToReasonTexts: recipients_to_reason_texts, - sourceContract: Contract, - options?: { force: boolean } - ) => { - if ( - options?.force || - shouldGetNotification(sourceContract.creatorId, userToReasonTexts) - ) - userToReasonTexts[sourceContract.creatorId] = { - reason: - sourceType === 'liquidity' - ? 'subsidized_your_market' - : 'your_contract_closed', - } - } - // The following functions modify the userToReasonTexts object in place. const userToReasonTexts: recipients_to_reason_texts = {} if (sourceType === 'follow' && recipients?.[0]) { - notifyFollowedUser(userToReasonTexts, recipients[0]) + if (shouldReceiveNotification(recipients[0], userToReasonTexts)) + userToReasonTexts[recipients[0]] = { + reason: 'on_new_follow', + } + return await sendNotificationsIfSettingsPermit(userToReasonTexts) } else if ( sourceType === 'contract' && sourceUpdateType === 'created' && @@ -186,23 +163,27 @@ export const createNotification = async ( ) { await notifyUsersFollowers(userToReasonTexts) notifyTaggedUsers(userToReasonTexts, recipients ?? []) + return await sendNotificationsIfSettingsPermit(userToReasonTexts) } else if ( sourceType === 'contract' && sourceUpdateType === 'closed' && sourceContract ) { - await notifyContractCreator(userToReasonTexts, sourceContract, { - force: true, - }) + userToReasonTexts[sourceContract.creatorId] = { + reason: 'your_contract_closed', + } + return await sendNotificationsIfSettingsPermit(userToReasonTexts) } else if ( sourceType === 'liquidity' && sourceUpdateType === 'created' && sourceContract ) { - await notifyContractCreator(userToReasonTexts, sourceContract) + if (shouldReceiveNotification(sourceContract.creatorId, userToReasonTexts)) + userToReasonTexts[sourceContract.creatorId] = { + reason: 'subsidized_your_market', + } + return await sendNotificationsIfSettingsPermit(userToReasonTexts) } - - await sendNotificationsIfSettingsPermit(userToReasonTexts) } export const createCommentOrAnswerOrUpdatedContractNotification = async ( diff --git a/functions/src/resolve-market.ts b/functions/src/resolve-market.ts index 821063f3..015ac72f 100644 --- a/functions/src/resolve-market.ts +++ b/functions/src/resolve-market.ts @@ -178,7 +178,8 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => { } else if (contract.outcomeType === 'PSEUDO_NUMERIC') { if (resolutionText === 'MKT' && value) resolutionText = `${value}` } - console.log('resolutionText: ', resolutionText) + + // TODO: this actually may be too slow to complete with a ton of users to notify? await createCommentOrAnswerOrUpdatedContractNotification( contract.id, 'contract',