From 42a29e91f769317bfb1b02461280cb6827322363 Mon Sep 17 00:00:00 2001 From: Zeeshan Akram <37098720+zeeshanakram3@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:38:31 +0500 Subject: [PATCH] fix: remove duplicate CRT notifications creation (#343) * fix: remove duplicate CRT notifications creation * bump package version and update CHANGELOG --- CHANGELOG.md | 6 ++++++ package-lock.json | 4 ++-- package.json | 2 +- src/mappings/token/index.ts | 19 +++++++++++++++---- src/mappings/token/utils.ts | 38 +++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ae4ec682..87a9aa497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # 4.0.4 +## Bug Fixes: +- Fixed: avoiding IDs override/conflict while creating concurrent runtime notifications by reading/updating the `nextEntityId` from the `overlay` instead of DB - [#342](https://github.com/Joystream/orion/pull/342) +- Fixed: removed possibility of creating duplicate CRT notifications in `processAmmActivatedEvent` and `processTokenSaleInitializedEvent` mappings if the account being notified is both channel follower as well as token holder - [#343](https://github.com/Joystream/orion/pull/343) + +# 4.0.4 + ## Bug Fixes: - Fixed: improve the accuracy of `Video.orionLanguage` field by reworking the `predictVideoLanguage` function in `src/utils/language.ts` diff --git a/package-lock.json b/package-lock.json index a0650bcdd..9f82045bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "orion", - "version": "4.0.4", + "version": "4.0.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "orion", - "version": "4.0.4", + "version": "4.0.5", "hasInstallScript": true, "workspaces": [ "network-tests" diff --git a/package.json b/package.json index f2ec73eda..ef4247b79 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "orion", - "version": "4.0.4", + "version": "4.0.5", "engines": { "node": ">=16" }, 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 c75f83e6f..32c5f8a9c 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 + ) + ) + ) + ) +}