Skip to content

Commit

Permalink
lots of refactor for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Nov 15, 2013
1 parent 8c4f776 commit 7c1b6d6
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 269 deletions.
44 changes: 16 additions & 28 deletions src/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,26 +252,28 @@ var RDB = require('./redis.js'),

Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
function movePost(pid, callback) {
posts.getPostField(pid, 'timestamp', function(timestamp) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if(err) {
return callback(err);
}

RDB.zrem('categories:recent_posts:cid:' + oldCid, pid);
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
callback(null);
});
}

topics.getPids(tid, function(err, pids) {
if (!err) {
async.each(pids, movePost, function(err) {
if (!err) {
callback(null, 1);
} else {
winston.err(err);
callback(err, null);
}
});
} else {
winston.err(err);
callback(err, null);
if(err) {
return callback(err, null);
}

async.each(pids, movePost, function(err) {
if(err) {
return callback(err, null);
}
callback(null, 1);
});
});
};

Expand Down Expand Up @@ -372,19 +374,6 @@ var RDB = require('./redis.js'),
return callback(err, null);
}

function getPostCategory(pid, callback) {
posts.getPostField(pid, 'tid', function(tid) {

topics.getTopicField(tid, 'cid', function(err, postCid) {
if (err) {
return callback(err, null);
}

return callback(null, postCid);
});
});
}

var index = 0,
active = false;

Expand All @@ -393,7 +382,7 @@ var RDB = require('./redis.js'),
return active === false && index < pids.length;
},
function(callback) {
getPostCategory(pids[index], function(err, postCid) {
posts.getCidByPid(pids[index], function(err, postCid) {
if (err) {
return callback(err);
}
Expand All @@ -411,7 +400,6 @@ var RDB = require('./redis.js'),
return callback(err, null);
}


callback(null, active);
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/favourites.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var RDB = require('./redis.js'),
return;
}

posts.getPostFields(pid, ['uid', 'timestamp'], function (postData) {
posts.getPostFields(pid, ['uid', 'timestamp'], function (err, postData) {

Favourites.hasFavourited(pid, uid, function (hasFavourited) {
if (hasFavourited === 0) {
Expand Down Expand Up @@ -57,7 +57,7 @@ var RDB = require('./redis.js'),
return;
}

posts.getPostField(pid, 'uid', function (uid_of_poster) {
posts.getPostField(pid, 'uid', function (err, uid_of_poster) {
Favourites.hasFavourited(pid, uid, function (hasFavourited) {
if (hasFavourited === 1) {
RDB.srem('pid:' + pid + ':users_favourited', uid);
Expand Down
14 changes: 7 additions & 7 deletions src/postTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ var RDB = require('./redis.js'),
}

function getThreadPrivileges(next) {
posts.getPostField(pid, 'tid', function(tid) {
posts.getPostField(pid, 'tid', function(err, tid) {
threadTools.privileges(tid, uid, function(privileges) {
next(null, privileges);
});
});
}

function isOwnPost(next) {
posts.getPostField(pid, 'uid', function(author) {
posts.getPostField(pid, 'uid', function(err, author) {
next(null, parseInt(author, 10) === parseInt(uid, 10));
});
}
Expand Down Expand Up @@ -87,7 +87,7 @@ var RDB = require('./redis.js'),

async.parallel([
function(next) {
posts.getPostField(pid, 'tid', function(tid) {
posts.getPostField(pid, 'tid', function(err, tid) {
PostTools.isMain(pid, tid, function(isMainPost) {
if (isMainPost) {
topics.setTopicField(tid, 'title', title);
Expand Down Expand Up @@ -132,7 +132,7 @@ var RDB = require('./redis.js'),
RDB.decr('totalpostcount');
postSearch.remove(pid);

posts.getPostFields(pid, ['tid', 'uid'], function(postData) {
posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) {
RDB.hincrby('topic:' + postData.tid, 'postcount', -1);

user.decrementUserFieldBy(postData.uid, 'postcount', 1, function(err, postcount) {
Expand All @@ -150,7 +150,7 @@ var RDB = require('./redis.js'),
if (err) winston.error('Could not delete topic (tid: ' + postData.tid + ')', err.stack);
});
} else {
posts.getPostField(pid, 'timestamp', function(timestamp) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
topics.updateTimestamp(postData.tid, timestamp);
});
}
Expand All @@ -174,7 +174,7 @@ var RDB = require('./redis.js'),
posts.setPostField(pid, 'deleted', 0);
RDB.incr('totalpostcount');

posts.getPostFields(pid, ['tid', 'uid', 'content'], function(postData) {
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
RDB.hincrby('topic:' + postData.tid, 'postcount', 1);

user.incrementUserFieldBy(postData.uid, 'postcount', 1);
Expand All @@ -184,7 +184,7 @@ var RDB = require('./redis.js'),
});

threadTools.getLatestUndeletedPid(postData.tid, function(err, pid) {
posts.getPostField(pid, 'timestamp', function(timestamp) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
topics.updateTimestamp(postData.tid, timestamp);
});
});
Expand Down
Loading

0 comments on commit 7c1b6d6

Please sign in to comment.