From c9121a8d9e778621f9a58e51179bf670cf0932d4 Mon Sep 17 00:00:00 2001 From: Zeeshan Akram <97m.zeeshan@gmail.com> Date: Wed, 17 Jul 2024 13:31:14 +0500 Subject: [PATCH] fix: remove duplicate CRT notifications creation --- src/mappings/token/index.ts | 19 +++++++++++++++---- src/mappings/token/utils.ts | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/mappings/token/index.ts b/src/mappings/token/index.ts index 660338ad8..709428cc4 100644 --- a/src/mappings/token/index.ts +++ b/src/mappings/token/index.ts @@ -54,6 +54,7 @@ import { createAccount, getTokenAccountByMemberByToken, getTokenAccountByMemberByTokenOrFail, + notifyChannelFollowersAndTokenHolders, notifyTokenHolders, parseCreatorTokenSymbol, processTokenMetadata, @@ -248,8 +249,13 @@ export async function processAmmActivatedEvent({ channelTitle: parseChannelTitle(channel), }) - await notifyTokenHolders(overlay, tokenId.toString(), notificationData, eventEntity) - await notifyChannelFollowers(overlay, channel.id, notificationData, eventEntity) + await notifyChannelFollowersAndTokenHolders( + overlay, + channel.id, + tokenId.toString(), + notificationData, + eventEntity + ) } export async function processTokenSaleInitializedEvent({ @@ -333,8 +339,13 @@ export async function processTokenSaleInitializedEvent({ channelTitle: parseChannelTitle(channel), }) - await notifyTokenHolders(overlay, tokenId.toString(), notificationData, eventEntity) - await notifyChannelFollowers(overlay, channel.id, notificationData, eventEntity) + await notifyChannelFollowersAndTokenHolders( + overlay, + channel.id, + tokenId.toString(), + notificationData, + eventEntity + ) } export async function processPatronageRateDecreasedToEvent({ diff --git a/src/mappings/token/utils.ts b/src/mappings/token/utils.ts index a4b7d1447..422b74ad9 100644 --- a/src/mappings/token/utils.ts +++ b/src/mappings/token/utils.ts @@ -23,6 +23,7 @@ import { uniqueId } from '../../utils/crypto' import { criticalError } from '../../utils/misc' import { addNotification } from '../../utils/notification' import { EntityManagerOverlay, Flat } from '../../utils/overlay' +import { getFollowersAccountsForChannel } from '../content/utils' export const FALLBACK_TOKEN_SYMBOL = '??' @@ -366,3 +367,40 @@ export async function notifyTokenHolders( ) ) } + +export async function notifyChannelFollowersAndTokenHolders( + overlay: EntityManagerOverlay, + channelId: string, + tokenId: string, + notificationType: NotificationType, + event?: Event, + dispatchBlock?: number +) { + const [followersAccounts, holderAccounts] = await Promise.all([ + getFollowersAccountsForChannel(overlay, channelId), + getHolderAccountsForToken(overlay, tokenId), + ]) + + // Combine followers and holders, removing duplicates + const allAccounts = [...followersAccounts, ...holderAccounts] + const accounts = Array.from(new Set(allAccounts.map((a) => a.id))) + .map((id) => allAccounts.find((account) => account.id === id)) + .filter((account): account is Account => account !== undefined) + + const limit = pLimit(10) // Limit to 10 concurrent promises + + await Promise.all( + accounts.map((account) => + limit(() => + addNotification( + overlay, + account, + new MemberRecipient({ membership: account.membershipId }), + notificationType, + event, + dispatchBlock + ) + ) + ) + ) +}