Node.js based forum software built for the modern web
refactor: new topic notification
Barış Soner Uşaklı708ccddc75e3
diff --git a/src/categories/topics.js b/src/categories/topics.jsindex 1ac5469d7b..e2d49e0d4d 100644--- a/src/categories/topics.js+++ b/src/categories/topics.js@@ -6,9 +6,6 @@ const plugins = require('../plugins');const meta = require('../meta');const privileges = require('../privileges');const user = require('../user');-const notifications = require('../notifications');-const tx = require('../translator');-const batch = require('../batch');const utils = require('../utils');module.exports = function (Categories) {@@ -216,51 +213,6 @@ module.exports = function (Categories) {return tids.filter((tid, index) => tid && (!scores[index] || scores[index] <= now));}- Categories.notifyCategoryFollowers = async (postData, exceptUid) => {- const { cid, title } = postData.topic;- const followers = [];- await batch.processSortedSet(`cid:${cid}:uid:watch:state`, async (uids) => {- followers.push(- ...await privileges.categories.filterUids('topics:read', cid, uids)- );- }, {- batch: 500,- min: Categories.watchStates.watching,- max: Categories.watchStates.watching,- });- const index = followers.indexOf(String(exceptUid));- if (index !== -1) {- followers.splice(index, 1);- }- if (!followers.length) {- return;- }-- const [displayname, categoryName] = await Promise.all([- user.getNotificationDisplayname(postData.user.uid),- Categories.getCategoryField(cid, 'name'),- ]);-- const bodyShort = tx.compile(- 'notifications:user-posted-topic-in-category',- displayname,- tx.escape(title),- categoryName- );-- const notification = await notifications.create({- type: 'new-topic-in-category',- nid: `new_topic:tid:${postData.topic.tid}:uid:${exceptUid}`,- bodyShort: bodyShort,- bodyLong: postData.content,- pid: postData.pid,- path: `/post/${encodeURIComponent(postData.pid)}`,- tid: postData.topic.tid,- from: exceptUid,- });- notifications.push(notification, followers);- };-Categories.sortTidsBySet = async (tids, sort) => {let cids = await topics.getTopicsFields(tids, ['cid']);cids = cids.map(({ cid }) => cid);diff --git a/src/topics/create.js b/src/topics/create.jsindex c28a8f6720..6db78e8309 100644--- a/src/topics/create.js+++ b/src/topics/create.js@@ -182,18 +182,9 @@ module.exports = function (Topics) {plugins.hooks.fire('action:topic.post', { topic: topicData, post: postData, data: data });if (!topicData.scheduled && !topicData.deleted) {- setImmediate(async () => {- try {- if (utils.isNumber(uid)) {- // New topic notifications only sent for local-to-local follows only- await user.notifications.sendTopicNotificationToFollowers(uid, topicData, postData);- }-- await Topics.notifyTagFollowers(postData, uid);- await categories.notifyCategoryFollowers(postData, uid);- } catch (err) {- winston.error(err.stack);- }+ setImmediate(() => {+ user.notifications.sendTopicNotificationToFollowers(uid, topicData, postData)+ .catch(err => winston.error(err.stack));});}diff --git a/src/topics/scheduled.js b/src/topics/scheduled.jsindex 1b570ed0f9..a17fa5d43c 100644--- a/src/topics/scheduled.js+++ b/src/topics/scheduled.js@@ -7,7 +7,6 @@ const db = require('../database');const posts = require('../posts');const socketHelpers = require('../socket.io/helpers');const topics = require('./index');-const categories = require('../categories');const groups = require('../groups');const user = require('../user');const activitypub = require('../activitypub');@@ -130,10 +129,6 @@ async function sendNotifications(uids, topicsData) {await Promise.all(topicsData.map((t, idx) => user.notifications.sendTopicNotificationToFollowers(t.uid, t, postsData[idx])- ).concat(- postsData.map(p => topics.notifyTagFollowers(p, p.uid))- ).concat(- postsData.map(p => categories.notifyCategoryFollowers(p, p.uid))).concat(topicsData.map((t, idx) => socketHelpers.notifyNew(t.uid, 'newTopic', { posts: [postsData[idx]], topic: t })diff --git a/src/topics/tags.js b/src/topics/tags.jsindex 388e2818ed..e5c08384e8 100644--- a/src/topics/tags.js+++ b/src/topics/tags.js@@ -9,9 +9,6 @@ const meta = require('../meta');const user = require('../user');const categories = require('../categories');const plugins = require('../plugins');-const privileges = require('../privileges');-const notifications = require('../notifications');-const tx = require('../translator');const utils = require('../utils');const batch = require('../batch');const cache = require('../cache');@@ -606,49 +603,4 @@ module.exports = function (Topics) {]);plugins.hooks.fire('action:tags.unfollow', { tag, uid });};-- Topics.notifyTagFollowers = async function (postData, exceptUid) {- const { tags, title } = postData.topic;- if (!tags.length) {- return;- }-- const [followersOfPoster, allFollowers, displayname] = await Promise.all([- db.getSortedSetRange(`followers:${exceptUid}`, 0, -1),- db.getSortedSetRange(tags.map(tag => `tag:${tag.value}:followers`), 0, -1),- user.getNotificationDisplayname(exceptUid),- ]);- const followerSet = new Set(followersOfPoster);- // filter out followers of the poster since they get a notification already- let followers = _.uniq(allFollowers).filter(uid => !followerSet.has(uid) && uid !== String(exceptUid));- followers = await privileges.topics.filterUids('topics:read', postData.topic.tid, followers);- if (!followers.length) {- return;- }-- const notifBase = 'notifications:user-posted-topic-with-tag';- let suffix = '';- let tagArgs = tags.map(tag => tx.escape(tag.value));- if (tagArgs.length === 2) {- suffix = '-dual';- } else if (tagArgs.length === 3) {- suffix = '-triple';- } else if (tagArgs.length > 3) {- suffix = '-multiple';- tagArgs = [tagArgs.join(', ')];- }- const bodyShort = tx.compile(`${notifBase}${suffix}`, displayname, tx.escape(title), ...tagArgs);-- const notification = await notifications.create({- type: 'new-topic-with-tag',- nid: `new_topic:tags:${tagArgs.join('.')}:tid:${postData.topic.tid}:uid:${exceptUid}`,- bodyShort: bodyShort,- bodyLong: postData.content,- pid: postData.pid,- path: `/post/${encodeURIComponent(postData.pid)}`,- tid: postData.topic.tid,- from: exceptUid,- });- notifications.push(notification, followers);- };};diff --git a/src/user/notifications.js b/src/user/notifications.jsindex 3dc84ed99e..41cc54a6e4 100644--- a/src/user/notifications.js+++ b/src/user/notifications.js@@ -11,6 +11,8 @@ const privileges = require('../privileges');const plugins = require('../plugins');const tx = require('../translator');const posts = require('../posts');+const categories = require('../categories');+const utils = require('../utils');const user = require('./index');const UserNotifications = module.exports;@@ -222,27 +224,87 @@ UserNotifications.deleteAll = async function (uid) {UserNotifications.sendTopicNotificationToFollowers = async function (uid, topicData, postData) {try {- const [displayname, allFollowers] = await Promise.all([+ const { tid, cid, title, tags } = topicData;+ let [displayname, userFollowers, tagFollowers, categoryFollowers] = await Promise.all([user.getNotificationDisplayname(uid),- db.getSortedSetRange(`followers:${uid}`, 0, -1),+ // New topic notifications only sent for local-to-local follows only+ utils.isNumber(uid) ? db.getSortedSetRange(`followers:${uid}`, 0, -1) : [],+ db.getSortedSetRange(tags.map(tag => `tag:${tag.value}:followers`), 0, -1),+ db.getSortedSetRangeByScore(+ `cid:${topicData.cid}:uid:watch:state`, 0, -1,+ categories.watchStates.watching,+ categories.watchStates.watching+ ),]);- const followers = await privileges.categories.filterUids('topics:read', topicData.cid, allFollowers);- if (!followers.length) {- return;++ const userFollowersSet = new Set(userFollowers);+ tagFollowers = _.uniq(tagFollowers).filter(_uid => !userFollowersSet.has(_uid) && _uid !== String(uid));+ categoryFollowers = categoryFollowers.filter(_uid => !userFollowersSet.has(_uid) && _uid !== String(uid));++ [userFollowers, tagFollowers, categoryFollowers] = await Promise.all([+ privileges.categories.filterUids('topics:read', cid, userFollowers),+ privileges.categories.filterUids('topics:read', cid, tagFollowers),+ privileges.categories.filterUids('topics:read', cid, categoryFollowers),+ ]);++ function createNotification(data) {+ return notifications.create({+ bodyLong: postData.content,+ pid: postData.pid,+ path: `/post/${encodeURIComponent(postData.pid)}`,+ tid: tid,+ from: uid,+ ...data,+ });+ }++ async function sendUserNotification() {+ const notifObj = await createNotification({+ type: 'new-topic',+ nid: `tid:${tid}:uid:${uid}`,+ bodyShort: tx.compile('notifications:user-posted-topic', displayname, tx.escape(title)),+ });++ await notifications.push(notifObj, userFollowers);}- const notifObj = await notifications.create({- type: 'new-topic',- bodyShort: tx.compile('notifications:user-posted-topic', displayname, tx.escape(topicData.title)),- bodyLong: postData.content,- pid: postData.pid,- path: `/post/${postData.pid}`,- nid: `tid:${postData.tid}:uid:${uid}`,- tid: postData.tid,- from: uid,- });-- await notifications.push(notifObj, followers);+ async function sendTagNotification() {+ const notifBase = 'notifications:user-posted-topic-with-tag';+ let suffix = '';+ let tagArgs = tags.map(tag => tx.escape(tag.value));+ if (tagArgs.length === 2) {+ suffix = '-dual';+ } else if (tagArgs.length === 3) {+ suffix = '-triple';+ } else if (tagArgs.length > 3) {+ suffix = '-multiple';+ tagArgs = [tagArgs.join(', ')];+ }++ const notification = await createNotification({+ type: 'new-topic-with-tag',+ nid: `new_topic:tags:${tagArgs.join('.')}:tid:${tid}:uid:${uid}`,+ bodyShort: tx.compile(`${notifBase}${suffix}`, displayname, tx.escape(title), ...tagArgs),+ });+ await notifications.push(notification, tagFollowers);+ }++ async function sendCategoryNotification() {+ const categoryName = await categories.getCategoryField(cid, 'name');+ const notifBase = 'notifications:user-posted-topic-in-category';+ const notification = await createNotification({+ type: 'new-topic-in-category',+ nid: `new_topic:tid:${tid}:uid:${uid}`,+ bodyShort: tx.compile(notifBase, displayname, tx.escape(title), categoryName),+ });+ await notifications.push(notification, categoryFollowers);+ }++ await Promise.all([+ userFollowers.length && sendUserNotification(),+ tagFollowers.length && sendTagNotification(),+ categoryFollowers.length && sendCategoryNotification(),+ ]);} catch (err) {winston.error(err.stack);}