moved postTools and threadTools into posts/ and topics/

v1.18.x
barisusakli 10 years ago
parent 19a0e1cf54
commit 0c6495de72

@ -22,6 +22,7 @@ var async = require('async'),
require('./posts/summary')(Posts); require('./posts/summary')(Posts);
require('./posts/recent')(Posts); require('./posts/recent')(Posts);
require('./posts/flags')(Posts); require('./posts/flags')(Posts);
require('./posts/tools')(Posts);
Posts.exists = function(pid, callback) { Posts.exists = function(pid, callback) {
db.isSortedSetMember('posts:pid', pid, callback); db.isSortedSetMember('posts:pid', pid, callback);

@ -38,7 +38,7 @@ module.exports = function(Posts) {
topics.updateTeaser(postData.tid, next); topics.updateTeaser(postData.tid, next);
} }
], function(err) { ], function(err) {
callback(err, postData); next(err, postData);
}); });
} }
], callback); ], callback);

@ -2,27 +2,27 @@
var async = require('async'), var async = require('async'),
posts = require('./posts'), privileges = require('../privileges'),
privileges = require('./privileges'), cache = require('./cache');
cache = require('./posts/cache');
(function(PostTools) { module.exports = function(Posts) {
Posts.tools = {};
PostTools.delete = function(uid, pid, callback) { Posts.tools.delete = function(uid, pid, callback) {
togglePostDelete(uid, pid, true, callback); togglePostDelete(uid, pid, true, callback);
}; };
PostTools.restore = function(uid, pid, callback) { Posts.tools.restore = function(uid, pid, callback) {
togglePostDelete(uid, pid, false, callback); togglePostDelete(uid, pid, false, callback);
}; };
function togglePostDelete(uid, pid, isDelete, callback) { function togglePostDelete(uid, pid, isDelete, callback) {
async.waterfall([ async.waterfall([
function(next) { function(next) {
posts.getPostField(pid, 'deleted', next); Posts.getPostField(pid, 'deleted', next);
}, },
function(deleted, next) { function(deleted, next) {
if(parseInt(deleted, 10) === 1 && isDelete) { if (parseInt(deleted, 10) === 1 && isDelete) {
return next(new Error('[[error:post-already-deleted]]')); return next(new Error('[[error:post-already-deleted]]'));
} else if(parseInt(deleted, 10) !== 1 && !isDelete) { } else if(parseInt(deleted, 10) !== 1 && !isDelete) {
return next(new Error('[[error:post-already-restored]]')); return next(new Error('[[error:post-already-restored]]'));
@ -43,19 +43,19 @@ var async = require('async'),
if (isDelete) { if (isDelete) {
cache.del(pid); cache.del(pid);
posts.delete(pid, callback); Posts.delete(pid, callback);
} else { } else {
posts.restore(pid, function(err, postData) { Posts.restore(pid, function(err, postData) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
posts.parsePost(postData, callback); Posts.parsePost(postData, callback);
}); });
} }
}); });
} }
PostTools.purge = function(uid, pid, callback) { Posts.tools.purge = function(uid, pid, callback) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
privileges.posts.canPurge(pid, uid, next); privileges.posts.canPurge(pid, uid, next);
@ -65,10 +65,10 @@ var async = require('async'),
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
cache.del(pid); cache.del(pid);
posts.purge(pid, next); Posts.purge(pid, next);
} }
], callback); ], callback);
}; };
};
}(exports));

@ -11,7 +11,6 @@ var async = require('async'),
meta = require('../meta'), meta = require('../meta'),
topics = require('../topics'), topics = require('../topics'),
favourites = require('../favourites'), favourites = require('../favourites'),
postTools = require('../postTools'),
notifications = require('../notifications'), notifications = require('../notifications'),
groups = require('../groups'), groups = require('../groups'),
user = require('../user'), user = require('../user'),
@ -346,28 +345,27 @@ SocketPosts.edit = function(socket, data, callback) {
}; };
SocketPosts.delete = function(socket, data, callback) { SocketPosts.delete = function(socket, data, callback) {
deleteOrRestore('delete', socket, data, callback); doPostAction('delete', 'event:post_deleted', socket, data, callback);
}; };
SocketPosts.restore = function(socket, data, callback) { SocketPosts.restore = function(socket, data, callback) {
deleteOrRestore('restore', socket, data, callback); doPostAction('restore', 'event:post_restored', socket, data, callback);
}; };
function deleteOrRestore(command, socket, data, callback) { function doPostAction(command, eventName, socket, data, callback) {
if (!data) { if (!data) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
postTools[command](socket.uid, data.pid, function(err, postData) { posts.tools[command](socket.uid, data.pid, function(err, postData) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var eventName = command === 'delete' ? 'event:post_deleted' : 'event:post_restored';
websockets.in('topic_' + data.tid).emit(eventName, postData); websockets.in('topic_' + data.tid).emit(eventName, postData);
events.log({ events.log({
type: command === 'delete' ? 'post-delete' : 'post-restore', type: 'post-' + command,
uid: socket.uid, uid: socket.uid,
pid: data.pid, pid: data.pid,
ip: socket.ip ip: socket.ip
@ -379,7 +377,7 @@ function deleteOrRestore(command, socket, data, callback) {
SocketPosts.purge = function(socket, data, callback) { SocketPosts.purge = function(socket, data, callback) {
function purgePost() { function purgePost() {
postTools.purge(socket.uid, data.pid, function(err) { posts.tools.purge(socket.uid, data.pid, function(err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }

@ -10,7 +10,6 @@ var nconf = require('nconf'),
privileges = require('../privileges'), privileges = require('../privileges'),
plugins = require('../plugins'), plugins = require('../plugins'),
notifications = require('../notifications'), notifications = require('../notifications'),
threadTools = require('../threadTools'),
websockets = require('./index'), websockets = require('./index'),
user = require('../user'), user = require('../user'),
db = require('../database'), db = require('../database'),
@ -239,12 +238,12 @@ SocketTopics.doTopicAction = function(action, event, socket, data, callback) {
return callback(new Error('[[error:invalid-tid]]')); return callback(new Error('[[error:invalid-tid]]'));
} }
if (typeof threadTools[action] !== 'function') { if (typeof topics.tools[action] !== 'function') {
return callback(); return callback();
} }
async.each(data.tids, function(tid, next) { async.each(data.tids, function(tid, next) {
threadTools[action](tid, socket.uid, function(err, data) { topics.tools[action](tid, socket.uid, function(err, data) {
if (err) { if (err) {
return next(err); return next(err);
} }
@ -330,7 +329,7 @@ SocketTopics.move = function(socket, data, callback) {
function(_topicData, next) { function(_topicData, next) {
topicData = _topicData; topicData = _topicData;
topicData.tid = tid; topicData.tid = tid;
threadTools.move(tid, data.cid, socket.uid, next); topics.tools.move(tid, data.cid, socket.uid, next);
} }
], function(err) { ], function(err) {
if(err) { if(err) {
@ -392,7 +391,7 @@ SocketTopics.moveAll = function(socket, data, callback) {
} }
async.eachLimit(tids, 10, function(tid, next) { async.eachLimit(tids, 10, function(tid, next) {
threadTools.move(tid, data.cid, socket.uid, next); topics.tools.move(tid, data.cid, socket.uid, next);
}, callback); }, callback);
}); });
}); });

@ -2,22 +2,23 @@
var async = require('async'), var async = require('async'),
db = require('./database'), db = require('../database'),
topics = require('./topics'), categories = require('../categories'),
categories = require('./categories'), plugins = require('../plugins'),
posts = require('./posts'), privileges = require('../privileges');
plugins = require('./plugins'),
batch = require('./batch'),
privileges = require('./privileges');
(function(ThreadTools) { module.exports = function(Topics) {
ThreadTools.delete = function(tid, uid, callback) { var topicTools = {};
Topics.tools = topicTools;
topicTools.delete = function(tid, uid, callback) {
toggleDelete(tid, uid, true, callback); toggleDelete(tid, uid, true, callback);
}; };
ThreadTools.restore = function(tid, uid, callback) { topicTools.restore = function(tid, uid, callback) {
toggleDelete(tid, uid, false, callback); toggleDelete(tid, uid, false, callback);
}; };
@ -31,7 +32,7 @@ var async = require('async'),
if (!isOwnerOrAdminOrMod) { if (!isOwnerOrAdminOrMod) {
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
topics.getTopicFields(tid, ['tid', 'cid', 'uid', 'deleted', 'title', 'mainPid'], next); Topics.getTopicFields(tid, ['tid', 'cid', 'uid', 'deleted', 'title', 'mainPid'], next);
}, },
function (_topicData, next) { function (_topicData, next) {
topicData = _topicData; topicData = _topicData;
@ -42,7 +43,7 @@ var async = require('async'),
return callback(new Error('[[error:topic-already-restored]]')); return callback(new Error('[[error:topic-already-restored]]'));
} }
topics[isDelete ? 'delete' : 'restore'](tid, next); Topics[isDelete ? 'delete' : 'restore'](tid, next);
}, },
function (next) { function (next) {
topicData.deleted = isDelete ? 1 : 0; topicData.deleted = isDelete ? 1 : 0;
@ -65,11 +66,11 @@ var async = require('async'),
], callback); ], callback);
} }
ThreadTools.purge = function(tid, uid, callback) { topicTools.purge = function(tid, uid, callback) {
var cid; var cid;
async.waterfall([ async.waterfall([
function(next) { function(next) {
topics.exists(tid, next); Topics.exists(tid, next);
}, },
function(exists, next) { function(exists, next) {
if (!exists) { if (!exists) {
@ -82,12 +83,12 @@ var async = require('async'),
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
topics.getTopicField(tid, 'cid', next); Topics.getTopicField(tid, 'cid', next);
}, },
function (_cid, next) { function (_cid, next) {
cid = _cid; cid = _cid;
topics.purgePostsAndTopic(tid, next); Topics.purgePostsAndTopic(tid, next);
}, },
function (next) { function (next) {
next(null, {tid: tid, cid: cid, uid: uid}); next(null, {tid: tid, cid: cid, uid: uid});
@ -95,11 +96,11 @@ var async = require('async'),
], callback); ], callback);
}; };
ThreadTools.lock = function(tid, uid, callback) { topicTools.lock = function(tid, uid, callback) {
toggleLock(tid, uid, true, callback); toggleLock(tid, uid, true, callback);
}; };
ThreadTools.unlock = function(tid, uid, callback) { topicTools.unlock = function(tid, uid, callback) {
toggleLock(tid, uid, false, callback); toggleLock(tid, uid, false, callback);
}; };
@ -110,7 +111,7 @@ var async = require('async'),
async.waterfall([ async.waterfall([
function (next) { function (next) {
topics.getTopicField(tid, 'cid', next); Topics.getTopicField(tid, 'cid', next);
}, },
function (_cid, next) { function (_cid, next) {
cid = _cid; cid = _cid;
@ -124,7 +125,7 @@ var async = require('async'),
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
topics.setTopicField(tid, 'locked', lock ? 1 : 0, next); Topics.setTopicField(tid, 'locked', lock ? 1 : 0, next);
}, },
function (next) { function (next) {
var data = { var data = {
@ -141,11 +142,11 @@ var async = require('async'),
], callback); ], callback);
} }
ThreadTools.pin = function(tid, uid, callback) { topicTools.pin = function(tid, uid, callback) {
togglePin(tid, uid, true, callback); togglePin(tid, uid, true, callback);
}; };
ThreadTools.unpin = function(tid, uid, callback) { topicTools.unpin = function(tid, uid, callback) {
togglePin(tid, uid, false, callback); togglePin(tid, uid, false, callback);
}; };
@ -153,13 +154,13 @@ var async = require('async'),
var topicData; var topicData;
async.waterfall([ async.waterfall([
function (next) { function (next) {
topics.exists(tid, next); Topics.exists(tid, next);
}, },
function (exists, next) { function (exists, next) {
if (!exists) { if (!exists) {
return callback(); return callback();
} }
topics.getTopicFields(tid, ['cid', 'lastposttime'], next); Topics.getTopicFields(tid, ['cid', 'lastposttime'], next);
}, },
function (_topicData, next) { function (_topicData, next) {
topicData = _topicData; topicData = _topicData;
@ -170,7 +171,7 @@ var async = require('async'),
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
async.parallel([ async.parallel([
async.apply(topics.setTopicField, tid, 'pinned', pin ? 1 : 0), async.apply(Topics.setTopicField, tid, 'pinned', pin ? 1 : 0),
async.apply(db.sortedSetAdd, 'cid:' + topicData.cid + ':tids', pin ? Math.pow(2, 53) : topicData.lastposttime, tid) async.apply(db.sortedSetAdd, 'cid:' + topicData.cid + ':tids', pin ? Math.pow(2, 53) : topicData.lastposttime, tid)
], next); ], next);
}, },
@ -189,11 +190,11 @@ var async = require('async'),
], callback); ], callback);
} }
ThreadTools.move = function(tid, cid, uid, callback) { topicTools.move = function(tid, cid, uid, callback) {
var topic; var topic;
async.waterfall([ async.waterfall([
function(next) { function(next) {
topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next); Topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next);
}, },
function(topicData, next) { function(topicData, next) {
topic = topicData; topic = topicData;
@ -229,7 +230,7 @@ var async = require('async'),
categories.incrementCategoryFieldBy(cid, 'topic_count', 1, next); categories.incrementCategoryFieldBy(cid, 'topic_count', 1, next);
}, },
function (next) { function (next) {
topics.setTopicField(tid, 'cid', cid, next); Topics.setTopicField(tid, 'cid', cid, next);
} }
], function(err) { ], function(err) {
if (err) { if (err) {
@ -247,4 +248,4 @@ var async = require('async'),
}; };
}(exports)); };
Loading…
Cancel
Save