mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-03 04:25:55 +01:00
updated getPostsByPids method to resolve premature return issue, and
refactored it to use async.map instead of eachSeries
This commit is contained in:
57
src/posts.js
57
src/posts.js
@@ -19,7 +19,6 @@ var RDB = require('./redis.js'),
|
||||
|
||||
Posts.getPostsByTid = function(tid, start, end, callback) {
|
||||
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
|
||||
|
||||
RDB.handle(err);
|
||||
|
||||
if (pids.length) {
|
||||
@@ -154,21 +153,16 @@ var RDB = require('./redis.js'),
|
||||
RDB.hset('post:' + pid, field, value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* getPostsByPids using redis's multi pipeline */
|
||||
Posts.getPostsByPids = function(pids, callback) {
|
||||
var posts = []
|
||||
var multi = RDB.multi();
|
||||
var posts = [],
|
||||
multi = RDB.multi();
|
||||
|
||||
for (v in pids) {
|
||||
var _pid = pids[v]
|
||||
multi.hgetall("post:"+_pid);
|
||||
for(var x=0,numPids=pids.length;x<numPids;x++) {
|
||||
multi.hgetall("post:"+pids[x]);
|
||||
}
|
||||
|
||||
multi.exec(function (err, replies) {
|
||||
async.eachSeries(replies, function(postData, _callback) {
|
||||
async.map(replies, function(postData, _callback) {
|
||||
if (postData) {
|
||||
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
|
||||
postData.post_rep = postData.reputation;
|
||||
@@ -188,14 +182,12 @@ var RDB = require('./redis.js'),
|
||||
|
||||
postTools.parse(postData.content, function(err, content) {
|
||||
postData.content = content;
|
||||
posts.push(postData);
|
||||
_callback(null, postData);
|
||||
});
|
||||
return _callback(null)
|
||||
} else {
|
||||
_callback(null);
|
||||
}
|
||||
else {
|
||||
return _callback(null)
|
||||
}
|
||||
}, function(err) {
|
||||
}, function(err, posts) {
|
||||
if (!err) {
|
||||
return callback(null, posts);
|
||||
} else {
|
||||
@@ -205,37 +197,6 @@ var RDB = require('./redis.js'),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Posts.getPostsByPids_original = function(pids, callback) {
|
||||
var posts = [];
|
||||
|
||||
async.eachSeries(pids, function(pid, callback) {
|
||||
Posts.getPostData(pid, function(postData) {
|
||||
if (postData) {
|
||||
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
|
||||
postData.post_rep = postData.reputation;
|
||||
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
|
||||
postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
|
||||
|
||||
postTools.parse(postData.content, function(err, content) {
|
||||
postData.content = content;
|
||||
posts.push(postData);
|
||||
callback(null);
|
||||
});
|
||||
}
|
||||
});
|
||||
}, function(err) {
|
||||
if (!err) {
|
||||
callback(null, posts);
|
||||
} else {
|
||||
callback(err, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Posts.get_cid_by_pid = function(pid, callback) {
|
||||
Posts.getPostField(pid, 'tid', function(tid) {
|
||||
if (tid) {
|
||||
|
||||
Reference in New Issue
Block a user