From b7eaf4191e74f14bd5a77cc4af0ef2a13d9d86f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 30 Sep 2018 16:37:10 -0400 Subject: [PATCH] return correct post count if its out of sync --- src/socket.io/topics.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 89fca006c5..4468b4fb53 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -2,12 +2,14 @@ var async = require('async'); +var db = require('../database'); var topics = require('../topics'); var posts = require('../posts'); var websockets = require('./index'); var user = require('../user'); var meta = require('../meta'); var apiController = require('../controllers/api'); +var privileges = require('../privileges'); var socketHelpers = require('./helpers'); var SocketTopics = module.exports; @@ -62,7 +64,33 @@ function postTopic(socket, data, callback) { } SocketTopics.postcount = function (socket, tid, callback) { - topics.getTopicField(tid, 'postcount', callback); + async.waterfall([ + function (next) { + privileges.topics.can('read', tid, socket.uid, next); + }, + function (canRead, next) { + if (!canRead) { + return next(new Error('[[no-privileges]]')); + } + + async.parallel({ + replyCount: function (next) { + db.sortedSetCard('tid:' + tid + ':posts', next); + }, + topicData: function (next) { + topics.getTopicFields(tid, ['mainPid', 'postcount'], next); + }, + }, next); + }, + function (results, next) { + if (results.topicData.mainPid && parseInt(results.topicData.postcount, 10) === parseInt(results.replyCount, 10) + 1) { + return next(null, results.topicData.postcount); + } + var postcount = results.replyCount + (results.topicData.mainPid ? 1 : 0); + topics.setTopicField(tid, 'postcount', postcount); + next(null, postcount); + }, + ], callback); }; SocketTopics.bookmark = function (socket, data, callback) {