Node.js based forum software built for the modern web
Unread changes closes #6781 (#6783)
Barış Soner Uşaklıcf75c7961124
diff --git a/public/src/client/footer.js b/public/src/client/footer.jsindex 53b9d334c0..b8db84fe09 100644--- a/public/src/client/footer.js+++ b/public/src/client/footer.js@@ -44,16 +44,22 @@ define('forum/footer', ['notifications', 'chat', 'components', 'translator'], fuvar isNewTopic = post.isMain && parseInt(post.uid, 10) !== parseInt(app.user.uid, 10);if (isNewTopic) {- var unreadNewTopicCount = parseInt($('a[href="' + config.relative_path + '/unread/new"].navigation-link i').attr('data-content'), 10) + 1;- updateUnreadTopicCount('/unread/new', unreadNewTopicCount);+ var unreadNewTopicCount = parseInt($('a[href="' + config.relative_path + '/unread?filter=new"].navigation-link i').attr('data-content'), 10) + 1;+ updateUnreadTopicCount('/unread?filter=new', unreadNewTopicCount);+ }++ var isUnreplied = parseInt(post.topic.postcount, 10) <= 1;+ if (isUnreplied) {+ var unreadUnrepliedTopicCount = parseInt($('a[href="' + config.relative_path + '/unread?filter=unreplied"].navigation-link i').attr('data-content'), 10) + 1;+ updateUnreadTopicCount('/unread?filter=unreplied', unreadUnrepliedTopicCount);}socket.emit('topics.isFollowed', post.topic.tid, function (err, isFollowed) {if (err) {return app.alertError(err.message);}if (isFollowed) {- var unreadWatchedTopicCount = parseInt($('a[href="' + config.relative_path + '/unread/watched"].navigation-link i').attr('data-content'), 10) + 1;- updateUnreadTopicCount('/unread/watched', unreadWatchedTopicCount);+ var unreadWatchedTopicCount = parseInt($('a[href="' + config.relative_path + '/unread?filter=watched"].navigation-link i').attr('data-content'), 10) + 1;+ updateUnreadTopicCount('/unread?filter=watched', unreadWatchedTopicCount);}});}@@ -77,8 +83,9 @@ define('forum/footer', ['notifications', 'chat', 'components', 'translator'], fufunction updateUnreadCounters(data) {updateUnreadTopicCount('/unread', data.unreadTopicCount);- updateUnreadTopicCount('/unread/new', data.unreadNewTopicCount);- updateUnreadTopicCount('/unread/watched', data.unreadWatchedTopicCount);+ updateUnreadTopicCount('/unread?filter=new', data.unreadNewTopicCount);+ updateUnreadTopicCount('/unread?filter=watched', data.unreadWatchedTopicCount);+ updateUnreadTopicCount('/unread?filter=unreplied', data.unreadUnrepliedTopicCount);}socket.on('event:unread.updateCount', updateUnreadCounters);diff --git a/src/middleware/header.js b/src/middleware/header.jsindex f845d8b4bb..ed06c88ca5 100644--- a/src/middleware/header.js+++ b/src/middleware/header.js@@ -120,9 +120,7 @@ module.exports = function (middleware) {banned: async.apply(user.isBanned, req.uid),banReason: async.apply(user.getBannedReason, req.uid),- unreadTopicCount: async.apply(topics.getTotalUnread, req.uid),- unreadNewTopicCount: async.apply(topics.getTotalUnread, req.uid, 'new'),- unreadWatchedTopicCount: async.apply(topics.getTotalUnread, req.uid, 'watched'),+ unreadCounts: async.apply(topics.getUnreadTids, { uid: req.uid, count: true }),unreadChatCount: async.apply(messaging.getUnreadCount, req.uid),unreadNotificationCount: async.apply(user.notifications.getUnreadCount, req.uid),}, next);@@ -146,12 +144,14 @@ module.exports = function (middleware) {setBootswatchCSS(templateValues, res.locals.config);var unreadCount = {- topic: results.unreadTopicCount || 0,- newTopic: results.unreadNewTopicCount || 0,- watchedTopic: results.unreadWatchedTopicCount || 0,+ topic: results.unreadCounts[''] || 0,+ newTopic: results.unreadCounts.new || 0,+ watchedTopic: results.unreadCounts.watched || 0,+ unrepliedTopic: results.unreadCounts.unreplied || 0,chat: results.unreadChatCount || 0,notification: results.unreadNotificationCount || 0,};+Object.keys(unreadCount).forEach(function (key) {if (unreadCount[key] > 99) {unreadCount[key] = '99+';@@ -159,25 +159,18 @@ module.exports = function (middleware) {});results.navigation = results.navigation.map(function (item) {- if (item.originalRoute === '/unread' && results.unreadTopicCount > 0) {- return Object.assign({}, item, {- content: unreadCount.topic,- iconClass: item.iconClass + ' unread-count',- });- }- if (item.originalRoute === '/unread/new' && results.unreadNewTopicCount > 0) {- return Object.assign({}, item, {- content: unreadCount.newTopic,- iconClass: item.iconClass + ' unread-count',- });- }- if (item.originalRoute === '/unread/watched' && results.unreadWatchedTopicCount > 0) {- return Object.assign({}, item, {- content: unreadCount.watchedTopic,- iconClass: item.iconClass + ' unread-count',- });+ function modifyNavItem(item, route, count, content) {+ if (item && item.originalRoute === route) {+ item.content = content;+ if (count > 0) {+ item.iconClass += ' unread-count';+ }+ }}-+ modifyNavItem(item, '/unread', results.unreadCounts[''], unreadCount.topic);+ modifyNavItem(item, '/unread?filter=new', results.unreadCounts.new, unreadCount.newTopic);+ modifyNavItem(item, '/unread?filter=watched', results.unreadCounts.watched, unreadCount.watchedTopic);+ modifyNavItem(item, '/unread?filter=unreplied', results.unreadCounts.unreplied, unreadCount.unrepliedTopic);return item;});diff --git a/src/socket.io/helpers.js b/src/socket.io/helpers.jsindex 907d0726c1..1688d88252 100644--- a/src/socket.io/helpers.js+++ b/src/socket.io/helpers.js@@ -34,6 +34,9 @@ SocketHelpers.notifyNew = function (uid, type, result) {function (uids, next) {user.blocks.filterUids(uid, uids, next);},+ function (uids, next) {+ user.blocks.filterUids(result.posts[0].topic.uid, uids, next);+ },function (uids, next) {plugins.fireHook('filter:sockets.sendNewPostToUids', { uidsTo: uids, uidFrom: uid, type: type }, next);},diff --git a/src/socket.io/user.js b/src/socket.io/user.jsindex 81182bdd09..facb0fa0a2 100644--- a/src/socket.io/user.js+++ b/src/socket.io/user.js@@ -261,12 +261,19 @@ SocketUser.getUnreadCounts = function (socket, data, callback) {return callback(null, {});}async.parallel({- unreadTopicCount: async.apply(topics.getTotalUnread, socket.uid),- unreadNewTopicCount: async.apply(topics.getTotalUnread, socket.uid, 'new'),- unreadWatchedTopicCount: async.apply(topics.getTotalUnread, socket.uid, 'watched'),+ unreadCounts: async.apply(topics.getUnreadTids, { uid: socket.uid, count: true }),unreadChatCount: async.apply(messaging.getUnreadCount, socket.uid),unreadNotificationCount: async.apply(user.notifications.getUnreadCount, socket.uid),- }, callback);+ }, function (err, results) {+ if (err) {+ return callback(err);+ }+ results.unreadTopicCount = results.unreadCounts[''];+ results.unreadNewTopicCount = results.unreadCounts.new;+ results.unreadWatchedTopicCount = results.unreadCounts.watched;+ results.unreadUnrepliedTopicCount = results.unreadCounts.unreplied;+ callback(null, results);+ });};SocketUser.invite = function (socket, email, callback) {diff --git a/src/topics/create.js b/src/topics/create.jsindex fcd166dfde..4a0d1be942 100644--- a/src/topics/create.js+++ b/src/topics/create.js@@ -298,7 +298,7 @@ module.exports = function (Topics) {posts.getUserInfoForPosts([postData.uid], uid, next);},topicInfo: function (next) {- Topics.getTopicFields(tid, ['tid', 'title', 'slug', 'cid', 'postcount', 'mainPid'], next);+ Topics.getTopicFields(tid, ['tid', 'uid', 'title', 'slug', 'cid', 'postcount', 'mainPid'], next);},parents: function (next) {Topics.addParentPosts([postData], next);diff --git a/src/topics/teaser.js b/src/topics/teaser.jsindex da7a05b59a..2a636b0702 100644--- a/src/topics/teaser.js+++ b/src/topics/teaser.js@@ -5,6 +5,7 @@ var async = require('async');var _ = require('lodash');var winston = require('winston');+var db = require('../database');var meta = require('../meta');var user = require('../user');var posts = require('../posts');@@ -112,53 +113,54 @@ module.exports = function (Topics) {};function handleBlocks(uid, teasers, callback) {- async.mapSeries(teasers, function (postData, nextPost) {- async.waterfall([- function (next) {- user.blocks.is(postData.uid, uid, next);- },- function (isBlocked, next) {- if (!isBlocked) {- return nextPost(null, postData);- }- getPreviousNonBlockedPost(postData, uid, next);- },- ], nextPost);- }, callback);+ user.blocks.list(uid, function (err, blockedUids) {+ if (err || !blockedUids.length) {+ return callback(err, teasers);+ }+ async.mapSeries(teasers, function (postData, nextPost) {+ if (blockedUids.includes(parseInt(postData.uid, 10))) {+ getPreviousNonBlockedPost(postData, blockedUids, nextPost);+ } else {+ setImmediate(nextPost, null, postData);+ }+ }, callback);+ });}- function getPreviousNonBlockedPost(postData, uid, callback) {+ function getPreviousNonBlockedPost(postData, blockedUids, callback) {let isBlocked = false;let prevPost = postData;- Topics.getPids(postData.tid, function (err, pids) {- if (err) {- return callback(err);- }+ const postsPerIteration = 5;+ let start = 0;+ let stop = start + postsPerIteration - 1;- async.doWhilst(function (next) {- async.waterfall([- function (next) {- const index = pids.lastIndexOf(String(prevPost.pid));- if (index <= 0) {- return callback(null, null);- }+ async.doWhilst(function (next) {+ async.waterfall([+ function (next) {+ db.getSortedSetRevRange('tid:' + postData.tid + ':posts', start, stop, next);+ },+ function (pids, next) {+ if (!pids.length) {+ return callback(null, null);+ }- posts.getPostFields(pids[index - 1], ['pid', 'uid', 'timestamp', 'tid', 'content'], next);- },- function (_prevPost, next) {- prevPost = _prevPost;- user.blocks.is(prevPost.uid, uid, next);- },- function (_isBlocked, next) {- isBlocked = _isBlocked;- next();- },- ], next);- }, function () {- return isBlocked && prevPost && prevPost.pid;- }, function (err) {- callback(err, prevPost);- });+ posts.getPostsFields(pids, ['pid', 'uid', 'timestamp', 'tid', 'content'], next);+ },+ function (prevPosts, next) {+ isBlocked = prevPosts.every(function (post) {+ const isPostBlocked = blockedUids.includes(parseInt(post.uid, 10));+ prevPost = !isPostBlocked ? post : prevPost;+ return isPostBlocked;+ });+ start += postsPerIteration;+ stop = start + postsPerIteration - 1;+ next();+ },+ ], next);+ }, function () {+ return isBlocked && prevPost && prevPost.pid;+ }, function (err) {+ callback(err, prevPost);});}diff --git a/src/topics/unread.js b/src/topics/unread.jsindex 4990fc1fe7..83e2192533 100644--- a/src/topics/unread.js+++ b/src/topics/unread.js@@ -20,8 +20,8 @@ module.exports = function (Topics) {callback = filter;filter = '';}- Topics.getUnreadTids({ cid: 0, uid: uid, filter: filter }, function (err, tids) {- callback(err, Array.isArray(tids) ? tids.length : 0);+ Topics.getUnreadTids({ cid: 0, uid: uid, count: true }, function (err, counts) {+ callback(err, counts && counts[filter]);});};@@ -66,10 +66,18 @@ module.exports = function (Topics) {Topics.getUnreadTids = function (params, callback) {var uid = parseInt(params.uid, 10);+ var counts = {+ '': 0,+ new: 0,+ watched: 0,+ unreplied: 0,+ };if (uid <= 0) {- return callback(null, []);+ return callback(null, params.count ? counts : []);}+ params.filter = params.filter || '';+var cutoff = params.cutoff || Topics.unreadCutoff();if (params.cid && !Array.isArray(params.cid)) {@@ -95,148 +103,209 @@ module.exports = function (Topics) {},function (results, next) {if (results.recentTids && !results.recentTids.length && !results.tids_unread.length) {- return callback(null, []);+ return callback(null, params.count ? counts : []);}- var userRead = {};- results.userScores.forEach(function (userItem) {- userRead[userItem.value] = userItem.score;- });-- results.recentTids = results.recentTids.concat(results.tids_unread);- results.recentTids.sort(function (a, b) {- return b.score - a.score;- });-- var tids = results.recentTids.filter(function (recentTopic) {- if (results.ignoredTids.indexOf(recentTopic.value.toString()) !== -1) {- return false;- }- switch (params.filter) {- case 'new':- return !userRead[recentTopic.value];- default:- return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value];- }- }).map(function (topic) {- return topic.value;- });-- tids = _.uniq(tids);-- if (params.filter === 'watched') {- Topics.filterWatchedTids(tids, uid, next);- } else if (params.filter === 'unreplied') {- Topics.filterUnrepliedTids(tids, next);- } else {- next(null, tids);- }+ filterTopics(params, results, next);},- function (tids, next) {- tids = tids.slice(0, 200);-- filterTopics(uid, tids, params.cid, params.filter, next);- },- function (tids, next) {+ function (data, next) {plugins.fireHook('filter:topics.getUnreadTids', {uid: uid,- tids: tids,+ tids: data.tids,+ counts: data.counts,+ tidsByFilter: data.tidsByFilter,cid: params.cid,filter: params.filter,}, next);},function (results, next) {- next(null, results.tids);+ next(null, params.count ? results.counts : results.tids);},], callback);};- function filterTopics(uid, tids, cid, filter, callback) {+ function filterTopics(params, results, callback) {+ const counts = {+ '': 0,+ new: 0,+ watched: 0,+ unreplied: 0,+ };++ const tidsByFilter = {+ '': [],+ new: [],+ watched: [],+ unreplied: [],+ };++ var userRead = {};+ results.userScores.forEach(function (userItem) {+ userRead[userItem.value] = userItem.score;+ });++ results.recentTids = results.recentTids.concat(results.tids_unread);+ results.recentTids.sort(function (a, b) {+ return b.score - a.score;+ });++ var tids = results.recentTids.filter(function (recentTopic) {+ if (results.ignoredTids.includes(String(recentTopic.value))) {+ return false;+ }+ return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value];+ });++ tids = _.uniq(tids.map(topic => topic.value));++ var cid = params.cid;+ var uid = params.uid;+ var cids;+ var topicData;+ var blockedUids;++ tids = tids.slice(0, 200);+if (!tids.length) {- return callback(null, tids);+ return callback(null, { counts: counts, tids: tids });}async.waterfall([function (next) {- privileges.topics.filterTids('read', tids, uid, next);+ user.blocks.list(uid, next);},- function (tids, next) {+ function (_blockedUids, next) {+ blockedUids = _blockedUids;+ filterTidsThatHaveBlockedPosts({+ uid: uid,+ tids: tids,+ blockedUids: blockedUids,+ recentTids: results.recentTids,+ }, next);+ },+ function (_tids, next) {+ tids = _tids;+ Topics.getTopicsFields(tids, ['tid', 'cid', 'uid', 'postcount'], next);+ },+ function (_topicData, next) {+ topicData = _topicData;+ cids = _.uniq(topicData.map(topic => topic.cid)).filter(Boolean);+async.parallel({- topics: function (next) {- Topics.getTopicsFields(tids, ['tid', 'cid'], next);- },isTopicsFollowed: function (next) {- if (filter === 'watched' || filter === 'new') {- return next(null, []);- }db.sortedSetScores('uid:' + uid + ':followed_tids', tids, next);},ignoredCids: function (next) {- if (filter === 'watched') {- return next(null, []);- }user.getIgnoredCategories(uid, next);},+ readableCids: function (next) {+ privileges.categories.filterCids('read', cids, uid, next);+ },}, next);},function (results, next) {- var topics = results.topics;-cid = cid && cid.map(String);- topics = topics.filter(function (topic, index) {- return topic && topic.cid &&- (!!results.isTopicsFollowed[index] || results.ignoredCids.indexOf(topic.cid.toString()) === -1) &&- (!cid || (cid.length && cid.indexOf(String(topic.cid)) !== -1));++ topicData.forEach(function (topic, index) {+ function cidMatch(topicCid) {+ return (!cid || (cid.length && cid.includes(String(topicCid))));+ }++ if (topic && topic.cid && cidMatch(topic.cid) && !blockedUids.includes(parseInt(topic.uid, 10))) {+ if ((results.isTopicsFollowed[index] || !results.ignoredCids.includes(String(topic.cid)))) {+ counts[''] += 1;+ tidsByFilter[''].push(topic.tid);+ }++ if (results.isTopicsFollowed[index]) {+ counts.watched += 1;+ tidsByFilter.watched.push(topic.tid);+ }++ if (parseInt(topic.postcount, 10) <= 1) {+ counts.unreplied += 1;+ tidsByFilter.unreplied.push(topic.tid);+ }++ if (!userRead[topic.tid]) {+ counts.new += 1;+ tidsByFilter.new.push(topic.tid);+ }+ }});- user.blocks.filter(uid, topics, next);- },- function (filteredTopics, next) {- tids = filteredTopics.map(function (topic) {- return topic && topic.tid;+ next(null, {+ counts: counts,+ tids: tidsByFilter[params.filter],+ tidsByFilter: tidsByFilter,});- filterTidsThatHaveBlockedPosts(uid, tids, next);},], callback);}- function filterTidsThatHaveBlockedPosts(uid, tids, callback) {- async.filter(tids, function (tid, next) {- doesTidHaveUnblockedUnreadPosts(uid, tid, next);- }, callback);+ function filterTidsThatHaveBlockedPosts(params, callback) {+ if (!params.blockedUids.length) {+ return setImmediate(callback, null, params.tids);+ }+ params.topicScores = {};+ params.recentTids.forEach(function (topic) {+ params.topicScores[topic.value] = topic.score;+ });++ db.sortedSetScores('uid:' + params.uid + ':tids_read', params.tids, function (err, userScores) {+ if (err) {+ return callback(err);+ }+ params.userScores = {};+ userScores.forEach(function (score, index) {+ params.userScores[params.tids[index]] = score;+ });+ async.filter(params.tids, function (tid, next) {+ doesTidHaveUnblockedUnreadPosts(tid, params, next);+ }, callback);+ });}- function doesTidHaveUnblockedUnreadPosts(uid, tid, callback) {- var topicTimestamp;- var userLastReadTimestamp;- async.waterfall([- function (next) {- async.parallel({- topicTimestamp: async.apply(db.sortedSetScore, 'topics:recent', tid),- userLastReadTimestamp: async.apply(db.sortedSetScore, 'uid:' + uid + ':tids_read', tid),- }, next);- },- function (results, next) {- topicTimestamp = results.topicTimestamp;- userLastReadTimestamp = results.userLastReadTimestamp;- if (!userLastReadTimestamp) {- return callback(null, true);- }- db.getSortedSetRevRangeByScore('tid:' + tid + ':posts', 0, -1, '+inf', userLastReadTimestamp, next);- },- function (pidsSinceLastVisit, next) {- if (!pidsSinceLastVisit.length) {- return callback(null, topicTimestamp > userLastReadTimestamp);- }- posts.getPostsFields(pidsSinceLastVisit, ['pid', 'uid'], next);- },- function (postData, next) {- user.blocks.filter(uid, postData, next);- },- function (unreadPosts, next) {- next(null, unreadPosts.length > 0);- },- ], callback);+ function doesTidHaveUnblockedUnreadPosts(tid, params, callback) {+ var topicTimestamp = params.topicScores[tid];+ var userLastReadTimestamp = params.userScores[tid];+ if (!userLastReadTimestamp) {+ return setImmediate(callback, null, true);+ }+ var start = 0;+ var count = 5;+ var done = false;+ var hasUnblockedUnread = topicTimestamp > userLastReadTimestamp;++ async.whilst(function () {+ return !done;+ }, function (_next) {+ async.waterfall([+ function (next) {+ db.getSortedSetRangeByScore('tid:' + tid + ':posts', start, count, userLastReadTimestamp, '+inf', next);+ },+ function (pidsSinceLastVisit, next) {+ if (!pidsSinceLastVisit.length) {+ done = true;+ return _next();+ }++ posts.getPostsFields(pidsSinceLastVisit, ['pid', 'uid'], next);+ },+ function (postData, next) {+ postData = postData.filter(function (post) {+ return !params.blockedUids.includes(parseInt(post.uid, 10));+ });++ done = postData.length > 0;+ hasUnblockedUnread = postData.length > 0;+ start += count;+ next();+ },+ ], _next);+ }, function (err) {+ callback(err, hasUnblockedUnread);+ });}Topics.pushUnreadCount = function (uid, callback) {@@ -248,14 +317,15 @@ module.exports = function (Topics) {async.waterfall([function (next) {- async.parallel({- unreadTopicCount: async.apply(Topics.getTotalUnread, uid),- unreadNewTopicCount: async.apply(Topics.getTotalUnread, uid, 'new'),- unreadWatchedTopicCount: async.apply(Topics.getTotalUnread, uid, 'watched'),- }, next);+ Topics.getUnreadTids({ uid: uid, count: true }, next);},function (results, next) {- require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', results);+ require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', {+ unreadTopicCount: results[''],+ unreadNewTopicCount: results.new,+ unreadWatchedTopicCount: results.watched,+ unreadUnrepliedTopicCount: results.unreplied,+ });setImmediate(next);},], callback);@@ -378,7 +448,7 @@ module.exports = function (Topics) {async.waterfall([function (next) {async.parallel({- recentScores: function (next) {+ topicScores: function (next) {db.sortedSetScores('topics:recent', tids, next);},userScores: function (next) {@@ -387,22 +457,36 @@ module.exports = function (Topics) {tids_unread: function (next) {db.sortedSetScores('uid:' + uid + ':tids_unread', tids, next);},+ blockedUids: function (next) {+ user.blocks.list(uid, next);+ },}, next);},function (results, next) {var cutoff = Topics.unreadCutoff();var result = tids.map(function (tid, index) {var read = !results.tids_unread[index] &&- (results.recentScores[index] < cutoff ||- !!(results.userScores[index] && results.userScores[index] >= results.recentScores[index]));+ (results.topicScores[index] < cutoff ||+ !!(results.userScores[index] && results.userScores[index] >= results.topicScores[index]));return { tid: tid, read: read };});+ var topicScores = {};+ var userScores = {};+ tids.forEach(function (tid, index) {+ topicScores[tid] = results.topicScores[index];+ userScores[tid] = results.userScores[index];+ });+async.map(result, function (data, next) {if (data.read) {return next(null, true);}- doesTidHaveUnblockedUnreadPosts(uid, data.tid, function (err, hasUnblockedUnread) {+ doesTidHaveUnblockedUnreadPosts(data.tid, {+ topicScores: topicScores,+ userScores: userScores,+ blockedUids: results.blockedUids,+ }, function (err, hasUnblockedUnread) {if (err) {return next(err);}