Skip to content

Commit

Permalink
fix: remove duplicate CRT notifications creation (#343)
Browse files Browse the repository at this point in the history
* fix: remove duplicate CRT notifications creation

* bump package version and update CHANGELOG
  • Loading branch information
zeeshanakram3 authored Jul 17, 2024
1 parent 3373b62 commit 42a29e9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orion",
"version": "4.0.4",
"version": "4.0.5",
"engines": {
"node": ">=16"
},
Expand Down
19 changes: 15 additions & 4 deletions src/mappings/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
createAccount,
getTokenAccountByMemberByToken,
getTokenAccountByMemberByTokenOrFail,
notifyChannelFollowersAndTokenHolders,
notifyTokenHolders,
parseCreatorTokenSymbol,
processTokenMetadata,
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
38 changes: 38 additions & 0 deletions src/mappings/token/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '??'

Expand Down Expand Up @@ -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
)
)
)
)
}

0 comments on commit 42a29e9

Please sign in to comment.