Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAY-370] Dedupe reaction notifications #3306

Merged
merged 1 commit into from
Jun 23, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,39 @@ const models = require('../../models')
const { notificationTypes } = require('../constants')

async function processReactionNotifications (notifications, tx) {
const notifsToReturn = []

for (const notification of notifications) {
const { slot, initiator: reactorId, metadata: { reaction_value: reactionValue, reacted_to_entity: reactedToEntity, reaction_type: reactionType } } = notification

// TODO: unhardcode assumptions about the userId receiving the notification, when
// we have additional reaction types.

await models.SolanaNotification.findOrCreate({
const existingNotification = await models.SolanaNotification.findOne({
where: {
type: notificationTypes.Reaction,
userId: reactedToEntity.tip_sender_id, // The user receiving the reaction is the user who sent the tip
entityId: reactorId, // The user who sent the reaction
metadata: {
reactionType,
reactedToEntity
}
},
transaction: tx
})

// In the case that the notification already exists, avoid returning it to prevent
// sending it a second time. Just update the original reaction value.
if (existingNotification) {
// Have to recreate the metadata object for save to work properly
existingNotification.metadata = {
...existingNotification.metadata,
reactionValue
}
await existingNotification.save({ transaction: tx })
} else {
notifsToReturn.push(notification)
await models.SolanaNotification.create({
slot,
type: notificationTypes.Reaction,
userId: reactedToEntity.tip_sender_id, // The user receiving the reaction is the user who sent the tip
Expand All @@ -20,10 +45,14 @@ async function processReactionNotifications (notifications, tx) {
reactionValue
}
},
transaction: tx
})
{
transaction: tx
}
)
}
}
return notifications

return notifsToReturn
}

module.exports = processReactionNotifications