From cac623a01677a50a045a5ab6805d5369397dfaee Mon Sep 17 00:00:00 2001 From: Michael Piazza Date: Tue, 24 May 2022 19:13:18 +0000 Subject: [PATCH] PR revisions --- .../20220512172226-add_notification_json_b.js | 8 +--- .../src/models/solanaNotification.js | 8 +--- .../reactionNotification.js | 44 +++++-------------- .../processNotifications/tipNotification.js | 1 - identity-service/src/routes/notifications.js | 6 ++- 5 files changed, 16 insertions(+), 51 deletions(-) diff --git a/identity-service/sequelize/migrations/20220512172226-add_notification_json_b.js b/identity-service/sequelize/migrations/20220512172226-add_notification_json_b.js index b2e172c77ee..c300c367f8f 100644 --- a/identity-service/sequelize/migrations/20220512172226-add_notification_json_b.js +++ b/identity-service/sequelize/migrations/20220512172226-add_notification_json_b.js @@ -17,17 +17,11 @@ module.exports = { ).then(() => queryInterface.addColumn('SolanaNotifications', 'metadata', { type: Sequelize.JSONB, allowNull: true - })).then(() => - queryInterface.sequelize.query( - `CREATE INDEX IF NOT EXISTS "solana_notifications_metadata_tip_tx_signature_idx" ON "SolanaNotifications"(("metadata"->>'tipTxSignature'))` - ) - ) + })) }, down: (queryInterface, Sequelize) => { return queryInterface.sequelize.transaction(async (transaction) => { - await queryInterface.sequelize.query('DROP INDEX IF EXISTS "solana_notifications_metadata_tip_tx_signature_idx";', { transaction }) - // Recreate the old enum: // Create a new enum diff --git a/identity-service/src/models/solanaNotification.js b/identity-service/src/models/solanaNotification.js index 61bc25bebe6..e3ecb62eec8 100644 --- a/identity-service/src/models/solanaNotification.js +++ b/identity-service/src/models/solanaNotification.js @@ -46,13 +46,7 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.JSONB, allowNull: true } - }, { - indexes: [{ - fields: ["((metadata->'tipTxSignature'))"], - unique: false, - name: 'solana_notifications_metadata_tip_tx_signature_idx' - }] - }) + }, {}) SolanaNotification.associate = function (models) { SolanaNotification.hasMany(models.SolanaNotificationAction, { diff --git a/identity-service/src/notifications/processNotifications/reactionNotification.js b/identity-service/src/notifications/processNotifications/reactionNotification.js index 9b3e3b49625..2feab335192 100644 --- a/identity-service/src/notifications/processNotifications/reactionNotification.js +++ b/identity-service/src/notifications/processNotifications/reactionNotification.js @@ -5,44 +5,20 @@ async function processReactionNotifications (notifications, tx) { for (const notification of notifications) { const { slot, initiator: reactorId, metadata: { reaction_value: reactionValue, reacted_to: reactedTo, reaction_type: reactionType } } = notification - // First find the originating tip receive notif - const receiveTipNotification = await models.SolanaNotification.findOne({ + await models.SolanaNotification.findOrCreate({ where: { - type: notificationTypes.TipReceive, + slot, + type: notificationTypes.Reaction, + userId: receiveTipNotification.entityId, // The user receiving the reaction is the user who sent the tip, here the entityId + entityId: reactorId, // The user who sent the reaction metadata: { - [models.Sequelize.Op.contains]: { - 'tipTxSignature': reactedTo - } + reactionType, + reactedTo, + reactionValue } - } + }, + transaction: tx }) - - if (!receiveTipNotification) { - console.error(`Received reaction to ${reactedTo} from ${reactorId}, but not corresponding tip notification`) - return notifications - } - - // Set it's reaction value - receiveTipNotification.metadata = { ...receiveTipNotification.metadata, reactionValue } - - // Save out reaction value on receive tip, and create new reaction notif - await Promise.all([ - receiveTipNotification.save({ transaction: tx }), - models.SolanaNotification.findOrCreate({ - where: { - slot, - type: notificationTypes.Reaction, - userId: receiveTipNotification.entityId, // The user receiving the reaction is the user who sent the tip, here the entityId - entityId: reactorId, // The user who sent the reaction - metadata: { - reactionType, - reactedTo, - reactionValue - } - }, - transaction: tx - }) - ]) } return notifications } diff --git a/identity-service/src/notifications/processNotifications/tipNotification.js b/identity-service/src/notifications/processNotifications/tipNotification.js index e33d18046e7..3d8f33c4670 100644 --- a/identity-service/src/notifications/processNotifications/tipNotification.js +++ b/identity-service/src/notifications/processNotifications/tipNotification.js @@ -30,7 +30,6 @@ async function processTipNotifications (notifications, tx) { metadata: { tipTxSignature, amount, - reactionValue: 0 // Initially, no reaction } }, transaction: tx diff --git a/identity-service/src/routes/notifications.js b/identity-service/src/routes/notifications.js index 9f606afb3e3..da690dab3be 100644 --- a/identity-service/src/routes/notifications.js +++ b/identity-service/src/routes/notifications.js @@ -219,7 +219,8 @@ const formatTipReceive = (notification) => ({ type: notification.type, amount: notification.metadata.amount, reactionValue: notification.metadata.reactionValue, - senderId: notification.entityId + senderId: notification.entityId, + tipTxSignature: notification.metadata.tipTxSignature }) const formatSupportingRankUp = (notification) => ({ @@ -241,7 +242,8 @@ const formatReaction = (notification) => ({ type: notification.type, reactingUser: notification.entityId, reactionType: notification.metadata.reactionType, - reactionValue: notification.metadata.reactionValue + reactionValue: notification.metadata.reactionValue, + reactedTo: notification.metadata.reactedTo }) const getCommonNotificationsFields = (notification) => ({