style changes

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 743f758f75
commit b4a32ee96f

@ -28,7 +28,8 @@ module.exports = function (Topics) {
}; };
Topics.getTopicPosts = function (tid, set, start, stop, uid, reverse, callback) { Topics.getTopicPosts = function (tid, set, start, stop, uid, reverse, callback) {
callback = callback || function () {}; async.waterfall([
function (next) {
async.parallel({ async.parallel({
posts: function (next) { posts: function (next) {
posts.getPostsFromSet(set, start, stop, uid, reverse, next); posts.getPostsFromSet(set, start, stop, uid, reverse, next);
@ -36,15 +37,14 @@ module.exports = function (Topics) {
postCount: function (next) { postCount: function (next) {
Topics.getTopicField(tid, 'postcount', next); Topics.getTopicField(tid, 'postcount', next);
} }
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
Topics.calculatePostIndices(results.posts, start, stop, results.postCount, reverse); Topics.calculatePostIndices(results.posts, start, stop, results.postCount, reverse);
Topics.addPostData(results.posts, uid, callback); Topics.addPostData(results.posts, uid, next);
}); }
], callback);
}; };
Topics.addPostData = function (postData, uid, callback) { Topics.addPostData = function (postData, uid, callback) {
@ -59,62 +59,55 @@ module.exports = function (Topics) {
return callback(null, []); return callback(null, []);
} }
async.parallel({ function getPostUserData(field, method, callback) {
bookmarks: function (next) {
posts.hasBookmarked(pids, uid, next);
},
voteData: function (next) {
posts.getVoteStatusByPostIDs(pids, uid, next);
},
userData: function (next) {
var uids = []; var uids = [];
for(var i = 0; i < postData.length; ++i) { postData.forEach(function (postData) {
if (postData[i] && uids.indexOf(postData[i].uid) === -1) { if (postData && postData[field] && uids.indexOf(postData[field]) === -1) {
uids.push(postData[i].uid); uids.push(postData[field]);
}
}
posts.getUserInfoForPosts(uids, uid, function (err, users) {
if (err) {
return next(err);
} }
});
async.waterfall([
function (next) {
method(uids, next);
},
function (users, next) {
var userData = {}; var userData = {};
users.forEach(function (user, index) { users.forEach(function (user, index) {
userData[uids[index]] = user; userData[uids[index]] = user;
}); });
next(null, userData); next(null, userData);
});
},
editors: function (next) {
var editors = [];
for(var i = 0; i < postData.length; ++i) {
if (postData[i] && postData[i].editor && editors.indexOf(postData[i].editor) === -1) {
editors.push(postData[i].editor);
} }
], callback);
} }
user.getUsersFields(editors, ['uid', 'username', 'userslug'], function (err, editors) { async.waterfall([
if (err) { function (next) {
return next(err); async.parallel({
} bookmarks: function (next) {
var editorData = {}; posts.hasBookmarked(pids, uid, next);
editors.forEach(function (editor) { },
editorData[editor.uid] = editor; voteData: function (next) {
}); posts.getVoteStatusByPostIDs(pids, uid, next);
next(null, editorData); },
}); userData: function (next) {
getPostUserData('uid', function (uids, next) {
posts.getUserInfoForPosts(uids, uid, next);
}, next);
},
editors: function (next) {
getPostUserData('editor', function (uids, next) {
user.getUsersFields(uids, ['uid', 'username', 'userslug'], next);
}, next);
}, },
parents: function (next) { parents: function (next) {
Topics.addParentPosts(postData, next); Topics.addParentPosts(postData, next);
} }
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
postData.forEach(function (postObj, i) { postData.forEach(function (postObj, i) {
if (postObj) { if (postObj) {
postObj.deleted = parseInt(postObj.deleted, 10) === 1; postObj.deleted = parseInt(postObj.deleted, 10) === 1;
@ -133,14 +126,15 @@ module.exports = function (Topics) {
} }
} }
}); });
plugins.fireHook('filter:topics.addPostData', { plugins.fireHook('filter:topics.addPostData', {
posts: postData, posts: postData,
uid: uid uid: uid
}, function (err, data) { }, next);
callback(err, data ? data.posts : null); },
}); function (data, next) {
}); next(null, data.posts);
}
], callback);
}; };
Topics.modifyPostsByPrivilege = function (topicData, topicPrivileges) { Topics.modifyPostsByPrivilege = function (topicData, topicPrivileges) {
@ -173,7 +167,9 @@ module.exports = function (Topics) {
async.apply(posts.getPostsFields, parentPids, ['uid']), async.apply(posts.getPostsFields, parentPids, ['uid']),
function (_parentPosts, next) { function (_parentPosts, next) {
parentPosts = _parentPosts; parentPosts = _parentPosts;
var parentUids = parentPosts.map(function (postObj) { return parseInt(postObj.uid, 10); }).filter(function (uid, idx, users) { var parentUids = parentPosts.map(function (postObj) {
return parseInt(postObj.uid, 10);
}).filter(function (uid, idx, users) {
return users.indexOf(uid) === idx; return users.indexOf(uid) === idx;
}); });
@ -232,31 +228,31 @@ module.exports = function (Topics) {
var done = false; var done = false;
var latestPid = null; var latestPid = null;
var index = 0; var index = 0;
var pids;
async.doWhilst( async.doWhilst(
function (next) { function (next) {
db.getSortedSetRevRange('tid:' + tid + ':posts', index, index, function (err, pids) { async.waterfall([
if (err) { function (_next) {
return next(err); db.getSortedSetRevRange('tid:' + tid + ':posts', index, index, _next);
} },
function (_pids, _next) {
pids = _pids;
if (!Array.isArray(pids) || !pids.length) { if (!Array.isArray(pids) || !pids.length) {
done = true; done = true;
return next(); return next();
} }
posts.getPostField(pids[0], 'deleted', function (err, deleted) { posts.getPostField(pids[0], 'deleted', _next);
if (err) { },
return next(err); function (deleted, _next) {
}
isDeleted = parseInt(deleted, 10) === 1; isDeleted = parseInt(deleted, 10) === 1;
if (!isDeleted) { if (!isDeleted) {
latestPid = pids[0]; latestPid = pids[0];
} }
++index; ++index;
next(); _next();
}); }
}); ], next);
}, },
function () { function () {
return isDeleted && !done; return isDeleted && !done;
@ -318,6 +314,8 @@ module.exports = function (Topics) {
}; };
Topics.getPids = function (tid, callback) { Topics.getPids = function (tid, callback) {
async.waterfall([
function (next) {
async.parallel({ async.parallel({
mainPid: function (next) { mainPid: function (next) {
Topics.getTopicField(tid, 'mainPid', next); Topics.getTopicField(tid, 'mainPid', next);
@ -325,15 +323,15 @@ module.exports = function (Topics) {
pids: function (next) { pids: function (next) {
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, next); db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, next);
} }
}, function (err, results) { }, next);
if (err) { },
return callback(err); function (results, next) {
}
if (results.mainPid) { if (results.mainPid) {
results.pids = [results.mainPid].concat(results.pids); results.pids = [results.mainPid].concat(results.pids);
} }
callback(null, results.pids); next(null, results.pids);
}); }
], callback);
}; };
Topics.increasePostCount = function (tid, callback) { Topics.increasePostCount = function (tid, callback) {
@ -350,12 +348,14 @@ module.exports = function (Topics) {
function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) { function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
callback = callback || function () {}; callback = callback || function () {};
db.incrObjectFieldBy('topic:' + tid, field, by, function (err, value) { async.waterfall([
if (err) { function (next) {
return callback(err); db.incrObjectFieldBy('topic:' + tid, field, by, next);
},
function (value, next) {
db.sortedSetAdd(set, value, tid, next);
} }
db.sortedSetAdd(set, value, tid, callback); ], callback);
});
} }
Topics.getTitleByPid = function (pid, callback) { Topics.getTitleByPid = function (pid, callback) {
@ -363,21 +363,25 @@ module.exports = function (Topics) {
}; };
Topics.getTopicFieldByPid = function (field, pid, callback) { Topics.getTopicFieldByPid = function (field, pid, callback) {
posts.getPostField(pid, 'tid', function (err, tid) { async.waterfall([
if (err) { function (next) {
return callback(err); posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
Topics.getTopicField(tid, field, next);
} }
Topics.getTopicField(tid, field, callback); ], callback);
});
}; };
Topics.getTopicDataByPid = function (pid, callback) { Topics.getTopicDataByPid = function (pid, callback) {
posts.getPostField(pid, 'tid', function (err, tid) { async.waterfall([
if (err) { function (next) {
return callback(err); posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
Topics.getTopicData(tid, next);
} }
Topics.getTopicData(tid, callback); ], callback);
});
}; };
Topics.getPostCount = function (tid, callback) { Topics.getPostCount = function (tid, callback) {

Loading…
Cancel
Save