Node.js based forum software built for the modern web
refactor: add new hooks for notifications/websockets
Barış Soner Uşaklıd2e042d15b8b
diff --git a/src/notifications.js b/src/notifications.jsindex 612c67d95f..fdb9998248 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 notificationuids = 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.jsindex 36b3384d24..7286b79e8e 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.jsindex f3b031a582..2fc74f1424 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.jsindex d35ba3ed26..1da0bd63cb 100644--- a/src/user/notifications.js+++ b/src/user/notifications.js@@ -214,7 +214,7 @@ UserNotifications.sendTopicNotificationToFollowers = async function (uid, topicDconst 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}`,