Node.js based forum software built for the modern web
closes #2515
Barış Soner Uşaklıcda38d6ed63c
diff --git a/src/posts.js b/src/posts.jsindex c8cfb8f085..2c7ca82737 100644--- a/src/posts.js+++ b/src/posts.js@@ -16,6 +16,7 @@ var async = require('async'),require('./posts/create')(Posts);require('./posts/delete')(Posts);require('./posts/user')(Posts);+ require('./posts/topics')(Posts);require('./posts/category')(Posts);require('./posts/summary')(Posts);require('./posts/recent')(Posts);@@ -25,20 +26,6 @@ var async = require('async'),db.isSortedSetMember('posts:pid', pid, callback);};- Posts.getPostsByTid = function(tid, set, start, end, uid, reverse, callback) {- Posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {- if(err) {- return callback(err);- }-- if(!Array.isArray(pids) || !pids.length) {- return callback(null, []);- }-- Posts.getPostsByPids(pids, uid, callback);- });- };-Posts.getPidsFromSet = function(set, start, end, reverse, callback) {if (isNaN(start) || isNaN(end)) {return callback(null, []);@@ -243,17 +230,6 @@ var async = require('async'),});};- Posts.isMain = function(pid, callback) {- Posts.getPostField(pid, 'tid', function(err, tid) {- if (err) {- return callback(err);- }- topics.getTopicField(tid, 'mainPid', function(err, mainPid) {- callback(err, parseInt(pid, 10) === parseInt(mainPid, 10));- });- });- };-Posts.updatePostVoteCount = function(pid, voteCount, callback) {async.parallel([function(next) {diff --git a/src/posts/topics.js b/src/posts/topics.jsnew file mode 100644index 0000000000..75577d2925--- /dev/null+++ b/src/posts/topics.js@@ -0,0 +1,44 @@++'use strict';++var async = require('async'),+ topics = require('../topics');++module.exports = function(Posts) {++ Posts.getPostsByTid = function(tid, set, start, end, uid, reverse, callback) {+ Posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {+ if (err) {+ return callback(err);+ }++ if (!Array.isArray(pids) || !pids.length) {+ return callback(null, []);+ }++ Posts.getPostsByPids(pids, uid, callback);+ });+ };++ Posts.isMain = function(pid, callback) {+ Posts.getPostField(pid, 'tid', function(err, tid) {+ if (err) {+ return callback(err);+ }+ topics.getTopicField(tid, 'mainPid', function(err, mainPid) {+ callback(err, parseInt(pid, 10) === parseInt(mainPid, 10));+ });+ });+ };++ Posts.getTopicFields = function(pid, fields, callback) {+ Posts.getPostField(pid, 'tid', function(err, tid) {+ if (err) {+ return callback(err);+ }++ topics.getTopicFields(tid, fields, callback);+ });+ };++};\ No newline at end of filediff --git a/src/socket.io/posts.js b/src/socket.io/posts.jsindex 0b5ab9b418..e8f5ef9a63 100644--- a/src/socket.io/posts.js+++ b/src/socket.io/posts.js@@ -16,6 +16,7 @@ var async = require('async'),groups = require('../groups'),user = require('../user'),websockets = require('./index'),+ socketTopics = require('./topics'),events = require('../events'),utils = require('../../public/src/utils'),@@ -345,27 +346,64 @@ function deleteOrRestore(command, socket, data, callback) {}SocketPosts.purge = function(socket, data, callback) {- if(!data || !parseInt(data.pid, 10)) {+ function purgePost() {+ postTools.purge(socket.uid, data.pid, function(err) {+ if (err) {+ return callback(err);+ }++ websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);++ events.log({+ type: 'post-purge',+ uid: socket.uid,+ pid: data.pid,+ ip: socket.ip+ });++ callback();+ });+ }++ if (!data || !parseInt(data.pid, 10)) {return callback(new Error('[[error:invalid-data]]'));}- postTools.purge(socket.uid, data.pid, function(err) {- if(err) {++ isMainAndLastPost(data.pid, function(err, results) {+ if (err) {return callback(err);}- websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);+ if (!results.isMain) {+ return purgePost();+ }- events.log({- type: 'post-purge',- uid: socket.uid,- pid: data.pid,- ip: socket.ip- });+ if (!results.isLast) {+ return callback(new Error('[[error:cant-purge-main-post]]'));+ }- callback();+ posts.getTopicFields(data.pid, ['tid', 'cid'], function(err, topic) {+ if (err) {+ return callback(err);+ }+ socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, callback);+ });});};+function isMainAndLastPost(pid, callback) {+ async.parallel({+ isMain: function(next) {+ posts.isMain(pid, next);+ },+ isLast: function(next) {+ posts.getTopicFields(pid, ['postcount'], function(err, topic) {+ next(err, topic ? parseInt(topic.postcount, 10) === 1 : false);+ });+ }+ }, callback);+}+SocketPosts.getPrivileges = function(socket, pids, callback) {privileges.posts.get(pids, socket.uid, function(err, privileges) {if (err) {diff --git a/src/socket.io/topics.js b/src/socket.io/topics.jsindex a4ee937429..cd42d1fde4 100644--- a/src/socket.io/topics.js+++ b/src/socket.io/topics.js@@ -205,34 +205,34 @@ SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {};SocketTopics.delete = function(socket, data, callback) {- doTopicAction('delete', 'event:topic_deleted', socket, data, callback);+ SocketTopics.doTopicAction('delete', 'event:topic_deleted', socket, data, callback);};SocketTopics.restore = function(socket, data, callback) {- doTopicAction('restore', 'event:topic_restored', socket, data, callback);+ SocketTopics.doTopicAction('restore', 'event:topic_restored', socket, data, callback);};SocketTopics.purge = function(socket, data, callback) {- doTopicAction('purge', 'event:topic_purged', socket, data, callback);+ SocketTopics.doTopicAction('purge', 'event:topic_purged', socket, data, callback);};SocketTopics.lock = function(socket, data, callback) {- doTopicAction('lock', 'event:topic_locked', socket, data, callback);+ SocketTopics.doTopicAction('lock', 'event:topic_locked', socket, data, callback);};SocketTopics.unlock = function(socket, data, callback) {- doTopicAction('unlock', 'event:topic_unlocked', socket, data, callback);+ SocketTopics.doTopicAction('unlock', 'event:topic_unlocked', socket, data, callback);};SocketTopics.pin = function(socket, data, callback) {- doTopicAction('pin', 'event:topic_pinned', socket, data, callback);+ SocketTopics.doTopicAction('pin', 'event:topic_pinned', socket, data, callback);};SocketTopics.unpin = function(socket, data, callback) {- doTopicAction('unpin', 'event:topic_unpinned', socket, data, callback);+ SocketTopics.doTopicAction('unpin', 'event:topic_unpinned', socket, data, callback);};-function doTopicAction(action, event, socket, data, callback) {+SocketTopics.doTopicAction = function(action, event, socket, data, callback) {if (!socket.uid) {return;}@@ -274,7 +274,7 @@ function doTopicAction(action, event, socket, data, callback) {});});}, callback);-}+};function emitToTopicAndCategory(event, data) {websockets.in('topic_' + data.tid).emit(event, data);