posts.js refactor pt1

This commit is contained in:
barisusakli
2014-11-09 01:12:24 -05:00
parent 39def60f9b
commit 263f479b20
5 changed files with 451 additions and 405 deletions

86
src/posts/create.js Normal file
View File

@@ -0,0 +1,86 @@
'use strict';
var async = require('async'),
db = require('../database'),
plugins = require('../plugins'),
user = require('../user'),
topics = require('../topics'),
categories = require('../categories');
module.exports = function(Posts) {
Posts.create = function(data, callback) {
var uid = data.uid,
tid = data.tid,
content = data.content,
timestamp = data.timestamp || Date.now();
if (!uid && parseInt(uid, 10) !== 0) {
return callback(new Error('[[error:invalid-uid]]'));
}
var postData;
async.waterfall([
function(next) {
db.incrObjectField('global', 'nextPid', next);
},
function(pid, next) {
postData = {
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'votes': 0,
'editor': '',
'edited': 0,
'deleted': 0
};
if (data.toPid) {
postData.toPid = data.toPid;
}
plugins.fireHook('filter:post.save', postData, next);
},
function(postData, next) {
db.setObject('post:' + postData.pid, postData, next);
},
function(next) {
async.parallel([
function(next) {
user.onNewPostMade(postData, next);
},
function(next) {
topics.onNewPostMade(postData, next);
},
function(next) {
categories.onNewPostMade(postData, next);
},
function(next) {
db.sortedSetAdd('posts:pid', timestamp, postData.pid, next);
},
function(next) {
db.incrObjectField('global', 'postCount', next);
}
], function(err) {
if (err) {
return next(err);
}
plugins.fireHook('filter:post.get', postData, next);
});
},
function(postData, next) {
plugins.fireHook('action:post.save', postData);
next(null, postData);
}
], callback);
};
};