style changes

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

@ -28,23 +28,23 @@ 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([
async.parallel({ function (next) {
posts: function (next) { async.parallel({
posts.getPostsFromSet(set, start, stop, uid, reverse, next); posts: function (next) {
posts.getPostsFromSet(set, start, stop, uid, reverse, next);
},
postCount: function (next) {
Topics.getTopicField(tid, 'postcount', next);
}
}, next);
}, },
postCount: function (next) { function (results, next) {
Topics.getTopicField(tid, 'postcount', next); Topics.calculatePostIndices(results.posts, start, stop, results.postCount, reverse);
}
}, function (err, results) {
if (err) {
return callback(err);
}
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,88 +59,82 @@ module.exports = function (Topics) {
return callback(null, []); return callback(null, []);
} }
async.parallel({ function getPostUserData(field, method, callback) {
bookmarks: function (next) { var uids = [];
posts.hasBookmarked(pids, uid, next);
},
voteData: function (next) {
posts.getVoteStatusByPostIDs(pids, uid, next);
},
userData: function (next) {
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) { async.waterfall([
if (err) { function (next) {
return next(err); 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) {
posts.hasBookmarked(pids, uid, next);
},
voteData: function (next) {
posts.getVoteStatusByPostIDs(pids, uid, next);
},
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) {
Topics.addParentPosts(postData, next);
}
}, next);
},
function (results, next) {
postData.forEach(function (postObj, i) {
if (postObj) {
postObj.deleted = parseInt(postObj.deleted, 10) === 1;
postObj.user = parseInt(postObj.uid, 10) ? results.userData[postObj.uid] : _.clone(results.userData[postObj.uid]);
postObj.editor = postObj.editor ? results.editors[postObj.editor] : null;
postObj.bookmarked = results.bookmarks[i];
postObj.upvoted = results.voteData.upvotes[i];
postObj.downvoted = results.voteData.downvotes[i];
postObj.votes = postObj.votes || 0;
postObj.replies = postObj.replies || 0;
postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10);
// Username override for guests, if enabled
if (parseInt(meta.config.allowGuestHandles, 10) === 1 && parseInt(postObj.uid, 10) === 0 && postObj.handle) {
postObj.user.username = validator.escape(String(postObj.handle));
}
} }
var editorData = {};
editors.forEach(function (editor) {
editorData[editor.uid] = editor;
});
next(null, editorData);
}); });
plugins.fireHook('filter:topics.addPostData', {
posts: postData,
uid: uid
}, next);
}, },
parents: function (next) { function (data, next) {
Topics.addParentPosts(postData, next); next(null, data.posts);
}
}, function (err, results) {
if (err) {
return callback(err);
} }
], callback);
postData.forEach(function (postObj, i) {
if (postObj) {
postObj.deleted = parseInt(postObj.deleted, 10) === 1;
postObj.user = parseInt(postObj.uid, 10) ? results.userData[postObj.uid] : _.clone(results.userData[postObj.uid]);
postObj.editor = postObj.editor ? results.editors[postObj.editor] : null;
postObj.bookmarked = results.bookmarks[i];
postObj.upvoted = results.voteData.upvotes[i];
postObj.downvoted = results.voteData.downvotes[i];
postObj.votes = postObj.votes || 0;
postObj.replies = postObj.replies || 0;
postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10);
// Username override for guests, if enabled
if (parseInt(meta.config.allowGuestHandles, 10) === 1 && parseInt(postObj.uid, 10) === 0 && postObj.handle) {
postObj.user.username = validator.escape(String(postObj.handle));
}
}
});
plugins.fireHook('filter:topics.addPostData', {
posts: postData,
uid: uid
}, function (err, data) {
callback(err, data ? data.posts : null);
});
});
}; };
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) {
if (!Array.isArray(pids) || !pids.length) { pids = _pids;
done = true; if (!Array.isArray(pids) || !pids.length) {
return next(); done = true;
} return next();
posts.getPostField(pids[0], 'deleted', function (err, deleted) {
if (err) {
return next(err);
} }
posts.getPostField(pids[0], 'deleted', _next);
},
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,22 +314,24 @@ module.exports = function (Topics) {
}; };
Topics.getPids = function (tid, callback) { Topics.getPids = function (tid, callback) {
async.parallel({ async.waterfall([
mainPid: function (next) { function (next) {
Topics.getTopicField(tid, 'mainPid', next); async.parallel({
mainPid: function (next) {
Topics.getTopicField(tid, 'mainPid', next);
},
pids: function (next) {
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, next);
}
}, next);
}, },
pids: function (next) { function (results, next) {
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, next); if (results.mainPid) {
} results.pids = [results.mainPid].concat(results.pids);
}, function (err, results) { }
if (err) { next(null, results.pids);
return callback(err);
}
if (results.mainPid) {
results.pids = [results.mainPid].concat(results.pids);
} }
callback(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