diff --git a/src/notifications.js b/src/notifications.js index 612c67d95f87..fdb9998248a8 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -197,9 +197,13 @@ async function pushToUids(uids, notification) { await db.sortedSetsRemoveRangeByScore(unreadKeys.concat(readKeys), '-inf', cutoff); const websockets = require('./socket.io'); if (websockets.server) { - uids.forEach((uid) => { + await Promise.all(uids.map(async (uid) => { + await plugins.hooks.fire('filter:sockets.sendNewNoticationToUid', { + uid, + notification, + }); websockets.in(`uid_${uid}`).emit('event:new_notification', notification); - }); + })); } } @@ -223,7 +227,10 @@ async function pushToUids(uids, notification) { // Remove uid from recipients list if they have blocked the user triggering the notification uids = await User.blocks.filterUids(notification.from, uids); - const data = await plugins.hooks.fire('filter:notification.push', { notification: notification, uids: uids }); + const data = await plugins.hooks.fire('filter:notification.push', { + notification, + uids, + }); if (!data || !data.notification || !data.uids || !data.uids.length) { return; } diff --git a/src/socket.io/helpers.js b/src/socket.io/helpers.js index 36b3384d2451..7286b79e8e75 100644 --- a/src/socket.io/helpers.js +++ b/src/socket.io/helpers.js @@ -28,8 +28,7 @@ SocketHelpers.notifyNew = async function (uid, type, result) { async function notifyUids(uid, uids, type, result) { const post = result.posts[0]; - const { tid } = post.topic; - const { cid } = post.topic; + const { tid, cid } = post.topic; uids = await privileges.topics.filterUids('topics:read', tid, uids); const watchStateUids = uids; @@ -49,14 +48,28 @@ async function notifyUids(uid, uids, type, result) { post.ip = undefined; - data.uidsTo.forEach((toUid) => { - post.categoryWatchState = categoryWatchStates[toUid]; - post.topic.isFollowing = topicFollowState[toUid]; - websockets.in(`uid_${toUid}`).emit('event:new_post', result); - if (result.topic && type === 'newTopic') { - websockets.in(`uid_${toUid}`).emit('event:new_topic', result.topic); + await Promise.all(data.uidsTo.map(async (toUid) => { + const copyResult = _.cloneDeep(result); + const postToUid = copyResult.posts[0]; + postToUid.categoryWatchState = categoryWatchStates[toUid]; + postToUid.topic.isFollowing = topicFollowState[toUid]; + + await plugins.hooks.fire('filter:sockets.sendNewPostToUid', { + uid: toUid, + uidFrom: uid, + post: postToUid, + }); + + websockets.in(`uid_${toUid}`).emit('event:new_post', copyResult); + if (copyResult.topic && type === 'newTopic') { + await plugins.hooks.fire('filter:sockets.sendNewTopicToUid', { + uid: toUid, + uidFrom: uid, + topic: copyResult.topic, + }); + websockets.in(`uid_${toUid}`).emit('event:new_topic', copyResult.topic); } - }); + })); } async function getWatchStates(uids, tid, cid) { diff --git a/src/user/follow.js b/src/user/follow.js index f3b031a582db..2fc74f14245b 100644 --- a/src/user/follow.js +++ b/src/user/follow.js @@ -21,31 +21,37 @@ module.exports = function (User) { if (parseInt(uid, 10) === parseInt(theiruid, 10)) { throw new Error('[[error:you-cant-follow-yourself]]'); } - const exists = await User.exists(theiruid); + const [exists, isFollowing] = await Promise.all([ + User.exists(theiruid), + User.isFollowing(uid, theiruid), + ]); if (!exists) { throw new Error('[[error:no-user]]'); } - const isFollowing = await User.isFollowing(uid, theiruid); + + await plugins.hooks.fire('filter:user.toggleFollow', { + type, + uid, + theiruid, + isFollowing, + }); + if (type === 'follow') { if (isFollowing) { throw new Error('[[error:already-following]]'); } const now = Date.now(); - await Promise.all([ - db.sortedSetAddBulk([ - [`following:${uid}`, now, theiruid], - [`followers:${theiruid}`, now, uid], - ]), + await db.sortedSetAddBulk([ + [`following:${uid}`, now, theiruid], + [`followers:${theiruid}`, now, uid], ]); } else { if (!isFollowing) { throw new Error('[[error:not-following]]'); } - await Promise.all([ - db.sortedSetRemoveBulk([ - [`following:${uid}`, theiruid], - [`followers:${theiruid}`, uid], - ]), + await db.sortedSetRemoveBulk([ + [`following:${uid}`, theiruid], + [`followers:${theiruid}`, uid], ]); } diff --git a/src/user/notifications.js b/src/user/notifications.js index d35ba3ed269d..1da0bd63cb77 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -214,7 +214,7 @@ UserNotifications.sendTopicNotificationToFollowers = async function (uid, topicD const notifObj = await notifications.create({ type: 'new-topic', - bodyShort: `[[notifications:user-posted-topic, ${postData.user.displayname}, ${title}]]`, + bodyShort: translator.compile('notifications:user-posted-topic', postData.user.displayname, title), bodyLong: postData.content, pid: postData.pid, path: `/post/${postData.pid}`,