lots of refactor for error handling

This commit is contained in:
Baris Usakli
2013-11-15 14:57:50 -05:00
parent 8c4f776122
commit 7c1b6d6ad2
8 changed files with 319 additions and 271 deletions

View File

@@ -18,6 +18,176 @@ var RDB = require('./redis.js'),
(function(Posts) {
var customUserInfo = {};
Posts.create = function(uid, tid, content, callback) {
if (uid === null) {
callback(new Error('invalid-user'), null);
return;
}
topics.isLocked(tid, function(err, locked) {
if(err) {
return callback(err, null);
} else if(locked) {
callback(new Error('topic-locked'), null);
}
RDB.incr('global:next_post_id', function(err, pid) {
if(err) {
return callback(err, null);
}
plugins.fireHook('filter:post.save', content, function(err, newContent) {
if(err) {
return callback(err, null);
}
content = newContent;
var timestamp = Date.now(),
postData = {
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'editor': '',
'edited': 0,
'deleted': 0,
'fav_button_class': '',
'fav_star_class': 'icon-star-empty',
'show_banned': 'hide',
'relativeTime': new Date(timestamp).toISOString(),
'post_rep': '0',
'edited-class': 'none',
'relativeEditTime': ''
};
RDB.hmset('post:' + pid, postData);
topics.addPostToTopic(tid, pid);
topics.increasePostCount(tid);
topics.updateTimestamp(tid, timestamp);
RDB.incr('totalpostcount');
topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) {
RDB.handle(err);
var cid = topicData.cid;
feed.updateTopic(tid);
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
if(topicData.pinned === '0')
RDB.zadd('categories:' + cid + ':tid', timestamp, tid);
RDB.scard('cid:' + cid + ':active_users', function(err, amount) {
if (amount > 10) {
RDB.spop('cid:' + cid + ':active_users');
}
categories.addActiveUser(cid, uid);
});
});
user.onNewPostMade(uid, tid, pid, timestamp);
plugins.fireHook('filter:post.get', postData, function(err, newPostData) {
if(err) {
return callback(err, null);
}
postData = newPostData;
postTools.parse(postData.content, function(err, content) {
if(err) {
return callback(err, null);
}
postData.content = content;
plugins.fireHook('action:post.save', postData);
postSearch.index(content, pid);
callback(null, postData);
});
});
});
});
});
};
Posts.reply = function(tid, uid, content, callback) {
if(content) {
content = content.trim();
}
if (!content || content.length < meta.config.minimumPostLength) {
callback(new Error('content-too-short'), null);
return;
}
Posts.create(uid, tid, content, function(err, postData) {
if(err) {
return callback(err, null);
} else if(!postData) {
callback(new Error('reply-error'), null);
}
async.parallel([
function(next) {
topics.markUnRead(tid, function(err) {
if(err) {
return next(err);
}
topics.markAsRead(tid, uid);
next();
});
},
function(next) {
Posts.getCidByPid(postData.pid, function(err, cid) {
if(err) {
return next(err);
}
RDB.del('cid:' + cid + ':read_by_uid');
next();
});
},
function(next) {
threadTools.notifyFollowers(tid, uid);
next();
},
function(next) {
Posts.addUserInfoToPost(postData, function(err) {
if(err) {
return next(err);
}
var socketData = {
posts: [postData]
};
io.sockets.in('topic_' + tid).emit('event:new_post', socketData);
io.sockets.in('recent_posts').emit('event:new_post', socketData);
io.sockets.in('user/' + uid).emit('event:new_post', socketData);
next();
});
}
], function(err, results) {
if(err) {
return callback(err, null);
}
callback(null, 'Reply successful');
});
});
}
Posts.getPostsByTid = function(tid, start, end, callback) {
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
RDB.handle(err);
@@ -86,7 +256,7 @@ var RDB = require('./redis.js'),
function getPostSummary(pid, callback) {
async.waterfall([
function(next) {
Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(postData) {
Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(err, postData) {
if (postData.deleted === '1') return callback(null);
else {
postData.relativeTime = new Date(parseInt(postData.timestamp || 0, 10)).toISOString();
@@ -148,35 +318,31 @@ var RDB = require('./redis.js'),
Posts.getPostFields = function(pid, fields, callback) {
RDB.hmgetObject('post:' + pid, fields, function(err, data) {
if (err === null) {
// TODO: I think the plugins system needs an optional 'parameters' paramter so I don't have to do this:
data = data || {};
data.pid = pid;
data.fields = fields;
plugins.fireHook('filter:post.getFields', data, function(err, data) {
callback(data);
});
} else {
console.log(err);
if(err) {
return callback(err, null);
}
// TODO: I think the plugins system needs an optional 'parameters' paramter so I don't have to do this:
data = data || {};
data.pid = pid;
data.fields = fields;
plugins.fireHook('filter:post.getFields', data, function(err, data) {
if(err) {
return callback(err, null);
}
callback(null, data);
});
});
}
Posts.getPostField = function(pid, field, callback) {
RDB.hget('post:' + pid, field, function(err, data) {
if (err === null) {
// TODO: I think the plugins system needs an optional 'parameters' paramter so I don't have to do this:
data = data || {};
data.pid = pid;
data.field = field;
plugins.fireHook('filter:post.getField', data, function(err, data) {
callback(data);
});
} else {
console.log(err);
Posts.getPostFields(pid, [field], function(err, data) {
if(err) {
return callback(err, null);
}
callback(null, data[field]);
});
}
@@ -193,8 +359,8 @@ var RDB = require('./redis.js'),
var posts = [],
multi = RDB.multi();
for(var x=0,numPids=pids.length;x<numPids;x++) {
multi.hgetall("post:"+pids[x]);
for(var x=0, numPids=pids.length; x<numPids; x++) {
multi.hgetall("post:" + pids[x]);
}
multi.exec(function (err, replies) {
@@ -205,12 +371,11 @@ var RDB = require('./redis.js'),
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
try {
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
postData.relativeEditTime = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
} catch(e) {
winston.err('invalid time value');
}
if (postData.uploadedImages) {
try {
postData.uploadedImages = JSON.parse(postData.uploadedImages);
@@ -239,17 +404,23 @@ var RDB = require('./redis.js'),
})
}
Posts.get_cid_by_pid = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(tid) {
if (tid) {
topics.getTopicField(tid, 'cid', function(err, cid) {
if (cid) {
callback(cid);
} else {
callback(false);
}
});
Posts.getCidByPid = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(err, tid) {
if(err) {
return callback(err, null);
}
topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return callback(err, null);
}
if (cid) {
callback(null, cid);
} else {
callback(new Error('invalid-category-id'), null);
}
});
});
}
@@ -272,138 +443,6 @@ var RDB = require('./redis.js'),
});
}
Posts.reply = function(tid, uid, content, callback) {
if(content) {
content = content.trim();
}
if (!content || content.length < meta.config.minimumPostLength) {
callback(new Error('content-too-short'), null);
return;
}
Posts.create(uid, tid, content, function(postData) {
if (postData) {
topics.markUnRead(tid);
Posts.get_cid_by_pid(postData.pid, function(cid) {
RDB.del('cid:' + cid + ':read_by_uid', function(err, data) {
topics.markAsRead(tid, uid);
});
});
threadTools.notifyFollowers(tid, uid);
Posts.addUserInfoToPost(postData, function() {
var socketData = {
posts: [postData]
};
io.sockets.in('topic_' + tid).emit('event:new_post', socketData);
io.sockets.in('recent_posts').emit('event:new_post', socketData);
io.sockets.in('user/' + uid).emit('event:new_post', socketData);
});
callback(null, 'Reply successful');
} else {
callback(new Error('reply-error'), null);
}
});
}
Posts.create = function(uid, tid, content, callback) {
if (uid === null) {
callback(null);
return;
}
topics.isLocked(tid, function(locked) {
if (!locked || locked === '0') {
RDB.incr('global:next_post_id', function(err, pid) {
RDB.handle(err);
plugins.fireHook('filter:post.save', content, function(err, newContent) {
if (!err) content = newContent;
var timestamp = Date.now(),
postData = {
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'editor': '',
'edited': 0,
'deleted': 0,
'fav_button_class': '',
'fav_star_class': 'icon-star-empty',
'show_banned': 'hide',
'relativeTime': new Date(timestamp).toISOString(),
'post_rep': '0',
'edited-class': 'none',
'relativeEditTime': ''
};
RDB.hmset('post:' + pid, postData);
topics.addPostToTopic(tid, pid);
topics.increasePostCount(tid);
topics.updateTimestamp(tid, timestamp);
RDB.incr('totalpostcount');
topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) {
RDB.handle(err);
var cid = topicData.cid;
feed.updateTopic(tid);
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
if(topicData.pinned === '0')
RDB.zadd('categories:' + cid + ':tid', timestamp, tid);
RDB.scard('cid:' + cid + ':active_users', function(err, amount) {
if (amount > 10) {
RDB.spop('cid:' + cid + ':active_users');
}
categories.addActiveUser(cid, uid);
});
});
user.onNewPostMade(uid, tid, pid, timestamp);
async.parallel({
content: function(next) {
plugins.fireHook('filter:post.get', postData, function(err, newPostData) {
if (!err) postData = newPostData;
postTools.parse(postData.content, function(err, content) {
next(null, content);
});
});
}
}, function(err, results) {
postData.content = results.content;
callback(postData);
});
plugins.fireHook('action:post.save', postData);
postSearch.index(content, pid);
});
});
} else {
callback(null);
}
});
}
Posts.uploadPostImage = function(image, callback) {
var imgur = require('./imgur');
imgur.setClientID(meta.config.imgurClientID);
@@ -463,7 +502,7 @@ var RDB = require('./redis.js'),
function reIndex(pid, callback) {
Posts.getPostField(pid, 'content', function(content) {
Posts.getPostField(pid, 'content', function(err, content) {
postSearch.remove(pid, function() {
if (content && content.length) {