Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions schema/auth.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type AccountNotificationPreferences {
crtRevenueShareStarted: NotificationPreference!
crtRevenueSharePlanned: NotificationPreference!
crtRevenueShareEnded: NotificationPreference!
crtRevenueShareEndedReminder: NotificationPreference!
}

type NotificationPreference {
Expand Down
18 changes: 18 additions & 0 deletions schema/notifications.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ union NotificationType =
| CreatorTokenRevenueShareStarted
| CreatorTokenRevenueSharePlanned
| CreatorTokenRevenueShareEnded
| CreatorTokenRevenueShareEndedReminder

type ChannelSuspended @variant {
phantom: Int
Expand Down Expand Up @@ -587,3 +588,20 @@ type CreatorTokenRevenueShareEnded @variant {
"id of token"
tokenId: String!
}

type CreatorTokenRevenueShareEndedReminder @variant {
"channel title for notification text"
channelTitle: String!

"channel title for notification avatar"
channelId: String!

"symbol of the token"
tokenSymbol: String!

"id of created revenue share to verify its' viability in future"
revenueShareId: String!

"id of token"
tokenId: String!
}
27 changes: 27 additions & 0 deletions src/mappings/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
CreatorTokenMarketStarted,
CreatorTokenMarketStartedEventData,
CreatorTokenRevenueShareEnded,
CreatorTokenRevenueShareEndedReminder,
CreatorTokenRevenueSharePlanned,
CreatorTokenRevenueShareStarted,
CreatorTokenRevenueSplitIssuedEventData,
Expand All @@ -41,10 +42,12 @@ import {
VestedSale,
VestingSchedule,
} from '../../model'
import { BLOCKS_PER_DAY } from '../../server-extension/resolvers/CreatorToken'
import { getCurrentBlockHeight } from '../../utils/blockHeight'
import { EventHandlerContext } from '../../utils/events'
import { criticalError } from '../../utils/misc'
import { addNotification } from '../../utils/notification'
import { removeFutureNotification } from '../../utils/notification/helpers'
import { getChannelOwnerAccount, notifyChannelFollowers, parseChannelTitle } from '../content/utils'
import { deserializeMetadata, genericEventFields } from '../utils'
import {
Expand Down Expand Up @@ -695,6 +698,26 @@ export async function processRevenueSplitIssuedEvent({
event,
endsAt // schedule for end block
)

// This event should have dispatch block of ending block plus about 3 days
// If user closes the share before its execution, this notification will be removing during share closing mapping
const revenueSharedEndedReminderNotification = new CreatorTokenRevenueShareEndedReminder({
revenueShareId: revenueShare.id,
channelTitle: parseChannelTitle(channel),
channelId: channel.id,
tokenSymbol: parseCreatorTokenSymbol(token),
tokenId: tokenId.toString(),
})

const channelOwnerAccount = await getChannelOwnerAccount(overlay, channel)
await addNotification(
overlay,
channelOwnerAccount,
new ChannelRecipient({ channel: channel.id }),
revenueSharedEndedReminderNotification,
undefined,
endsAt + BLOCKS_PER_DAY * 3 // schedule for end block
)
}

export async function processMemberJoinedWhitelistEvent({
Expand Down Expand Up @@ -799,6 +822,10 @@ export async function processRevenueSplitFinalizedEvent({
.getByIdOrFail(token.currentRevenueShareId!)
revenueShare.finalized = true
token.currentRevenueShareId = null

await removeFutureNotification(overlay.getEm(), 'CreatorTokenRevenueShareEndedReminder', 1, {
tokenId: token.id,
})
}

export async function processUserParticipatedInSplitEvent({
Expand Down
45 changes: 44 additions & 1 deletion src/utils/notification/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Flat } from 'lodash'
import { EntityManager } from 'typeorm'
import { EntityManager, FindOptionsWhere } from 'typeorm'
import {
Account,
AccountNotificationPreferences,
Expand Down Expand Up @@ -72,6 +72,7 @@ export function defaultNotificationPreferences(): AccountNotificationPreferences
crtRevenueShareStarted: notificationPrefAllTrue(),
crtRevenueSharePlanned: notificationPrefAllTrue(),
crtRevenueShareEnded: notificationPrefAllTrue(),
crtRevenueShareEndedReminder: notificationPrefAllTrue(),
})
}

Expand Down Expand Up @@ -144,6 +145,9 @@ export function preferencesForNotification(
return preferences.crtRevenueSharePlanned
case 'CreatorTokenRevenueShareEnded':
return preferences.crtRevenueShareEnded
case 'CreatorTokenRevenueShareEndedReminder':
return preferences.crtRevenueShareEnded

default: // all the remaining notifications (v2 scope) are not enabled by default
return new NotificationPreference({ inAppEnabled: false, emailEnabled: false })
}
Expand Down Expand Up @@ -299,6 +303,45 @@ export const addNotification = async (
}
}

export async function removeFutureNotification(
em: EntityManager,
notificationType: Notification['notificationType']['isTypeOf'],
currentBlockHeight: number,
filters: Record<string, string | number>
) {
const result: { id: string }[] = await em.query(
`
SELECT
id
FROM
public.notification
WHERE
notification_type ->> 'isTypeOf'=$1
AND
dispatch_block > $2
${Object.keys(filters).map((key, idx) => {
return `
AND
notification_type ->> '${key}'=$${idx + 3}
`
})}
`,
[notificationType, currentBlockHeight, ...Object.values(filters)]
)
const notificationToRemove = result[0]

if (!notificationToRemove) {
return
}

await em.getRepository(Notification).delete({
id: notificationToRemove.id,
})
await em.getRepository(NotificationEmailDelivery).delete({
notificationId: notificationToRemove.id,
})
}

async function saveNextNotificationId(
em: EntityManager,
nextNotificationId: number,
Expand Down
10 changes: 10 additions & 0 deletions src/utils/notification/notificationsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,15 @@ export const getNotificationData = async (
subject: `🔓 ${channelTitle} ended revenue share for $${tokenSymbol} token. Unlock your locked tokens!`,
}
}
case 'CreatorTokenRevenueShareEndedReminder': {
const { tokenSymbol, channelId } = notificationType
return {
icon: await getNotificationIcon(em, 'payout'),
link: await getNotificationLink(em, 'portfolio'),
avatar: await getNotificationAvatar(em, 'channelId', channelId),
text: `🔓 Your $${tokenSymbol} revenue share is waiting to be closed. Unlock locked tokens and open your market!`,
subject: `🔓 Your $${tokenSymbol} revenue share is waiting to be closed. Unlock locked tokens and open your market!`,
}
}
}
}