Skip to content

Commit

Permalink
Fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Saliou Diallo committed Aug 11, 2022
1 parent e01c479 commit a5aca7f
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions identity-service/src/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,33 +450,22 @@ class NotificationProcessor {
* @returns {Promise<Object[]>} notifications - after having filtered out notifications from bad initiators
*/
async function filterOutAbusiveUsers (notifications) {
const notificationsWithAbuse = await Promise.all(
notifications.map(async notification => ({
notification,
isInitiatorAbusive: await isUserAbusive(notification.initiatior)
}))
)
const result = notificationsWithAbuse
.filter(({ isInitiatorAbusive }) => !isInitiatorAbusive)
.map(({ notification }) => notification)

const initiatorIds = notifications.map(({ initiator }) => initiator)
const users = await models.User.findAll({
where: {
blockchainUserId: { [ models.Sequelize.Op.in ]: initiatorIds }
},
attributes: ['blockchainUserId', 'isAbusive']
})
const usersAbuseMap = {}
users.forEach(user => {
usersAbuseMap[user.blockchainUserId] = user.isAbusive
})
const result = notifications.filter(notification => !usersAbuseMap[notification.initiator])
logger.info(`notifications | index.js | Filtered out ${notifications.length - result.length} bad initiators out of ${notifications.length} total.`)
return result
}

/**
* Get whether user is abusive.
* @param {number} userId
* @returns {Promise<boolean>} isAbusive
*/
async function isUserAbusive (userId) {
const user = await models.User.findOne({
where: { blockchainUserId: userId },
attributes: ['isAbusive']
})
return user.isAbusive
}

module.exports = NotificationProcessor
module.exports.NOTIFICATION_JOB_LAST_SUCCESS_KEY = NOTIFICATION_JOB_LAST_SUCCESS_KEY
module.exports.NOTIFICATION_EMAILS_JOB_LAST_SUCCESS_KEY = NOTIFICATION_EMAILS_JOB_LAST_SUCCESS_KEY
Expand Down

0 comments on commit a5aca7f

Please sign in to comment.