Skip to content

Node.js based forum software built for the modern web

return correct post count if its out of sync

Barış Soner Uşaklıb7eaf4191e74
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) {