From b43bfa3d5bacc5d105cb89126b8c24416a5c2d52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 14 Sep 2018 16:23:16 -0400 Subject: [PATCH] closes #6759 -moved user.blocks.filter from unread controller to getUnreadTopics so topics are filtered at other places this function is called - when a new post is made do not send it to uids that have blocked the poster - update getUnreadTids so it does not return topics that have new posts from blocked users - update hasReadTopics so it does not return false data if topic has new posts from blocked users --- src/controllers/unread.js | 6 ---- src/socket.io/helpers.js | 3 ++ src/topics/unread.js | 66 ++++++++++++++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/controllers/unread.js b/src/controllers/unread.js index 500cfe11a6..96dc4f66ce 100644 --- a/src/controllers/unread.js +++ b/src/controllers/unread.js @@ -54,12 +54,6 @@ unreadController.get = function (req, res, next) { cutoff: cutoff, }, next); }, - function (data, next) { - user.blocks.filter(req.uid, data.topics, function (err, filtered) { - data.topics = filtered; - next(err, data); - }); - }, function (data) { data.title = meta.config.homePageTitle || '[[pages:home]]'; data.pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage)); diff --git a/src/socket.io/helpers.js b/src/socket.io/helpers.js index 163e7d54d3..907d0726c1 100644 --- a/src/socket.io/helpers.js +++ b/src/socket.io/helpers.js @@ -31,6 +31,9 @@ SocketHelpers.notifyNew = function (uid, type, result) { function (uids, next) { filterTidCidIgnorers(uids, result.posts[0].topic.tid, result.posts[0].topic.cid, next); }, + function (uids, next) { + user.blocks.filterUids(uid, uids, next); + }, function (uids, next) { plugins.fireHook('filter:sockets.sendNewPostToUids', { uidsTo: uids, uidFrom: uid, type: type }, next); }, diff --git a/src/topics/unread.js b/src/topics/unread.js index 86d3cac56a..500b429cd2 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -6,6 +6,7 @@ var _ = require('lodash'); var db = require('../database'); var user = require('../user'); +var posts = require('../posts'); var notifications = require('../notifications'); var categories = require('../categories'); var privileges = require('../privileges'); @@ -150,7 +151,6 @@ module.exports = function (Topics) { ], callback); }; - function filterTopics(uid, tids, cid, filter, callback) { if (!tids.length) { return callback(null, tids); @@ -181,15 +181,53 @@ module.exports = function (Topics) { }, function (results, next) { var topics = results.topics; + cid = cid && cid.map(String); - tids = topics.filter(function (topic, index) { + 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)); - }).map(function (topic) { - return topic.tid; }); - next(null, tids); + + user.blocks.filter(uid, topics, next); + }, + function (filteredTopics, next) { + tids = filteredTopics.map(function (topic) { + return topic && topic.tid; + }); + filterTidsThatHaveBlockedPosts(uid, tids, next); + }, + ], callback); + } + + function filterTidsThatHaveBlockedPosts(uid, tids, callback) { + async.filter(tids, function (tid, next) { + doesTidHaveUnblockedUnreadPosts(uid, tid, next); + }, callback); + } + + function doesTidHaveUnblockedUnreadPosts(uid, tid, callback) { + async.waterfall([ + function (next) { + db.sortedSetScore('uid:' + uid + ':tids_read', tid, next); + }, + function (userLastReadTimestamp, next) { + 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, false); + } + posts.getPostsFields(pidsSinceLastVisit, ['pid', 'uid'], next); + }, + function (postData, next) { + user.blocks.filter(uid, postData, next); + }, + function (unreadPosts, next) { + next(null, unreadPosts.length > 0); }, ], callback); } @@ -347,12 +385,26 @@ module.exports = function (Topics) { function (results, next) { var cutoff = Topics.unreadCutoff(); var result = tids.map(function (tid, index) { - return !results.tids_unread[index] && + var read = !results.tids_unread[index] && (results.recentScores[index] < cutoff || !!(results.userScores[index] && results.userScores[index] >= results.recentScores[index])); + return { tid: tid, read: read }; }); - next(null, result); + async.map(result, function (data, next) { + if (data.read) { + return next(null, true); + } + doesTidHaveUnblockedUnreadPosts(uid, data.tid, function (err, hasUnblockedUnread) { + if (err) { + return next(err); + } + if (!hasUnblockedUnread) { + data.read = true; + } + next(null, data.read); + }); + }, next); }, ], callback); };