mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +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:
63
src/posts.js
63
src/posts.js
@@ -19,7 +19,6 @@ var RDB = require('./redis.js'),
|
|||||||
|
|
||||||
Posts.getPostsByTid = function(tid, start, end, callback) {
|
Posts.getPostsByTid = function(tid, start, end, callback) {
|
||||||
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
|
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
|
||||||
|
|
||||||
RDB.handle(err);
|
RDB.handle(err);
|
||||||
|
|
||||||
if (pids.length) {
|
if (pids.length) {
|
||||||
@@ -154,28 +153,23 @@ var RDB = require('./redis.js'),
|
|||||||
RDB.hset('post:' + pid, field, value);
|
RDB.hset('post:' + pid, field, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* getPostsByPids using redis's multi pipeline */
|
|
||||||
Posts.getPostsByPids = function(pids, callback) {
|
Posts.getPostsByPids = function(pids, callback) {
|
||||||
var posts = []
|
var posts = [],
|
||||||
var multi = RDB.multi();
|
multi = RDB.multi();
|
||||||
|
|
||||||
for (v in pids) {
|
for(var x=0,numPids=pids.length;x<numPids;x++) {
|
||||||
var _pid = pids[v]
|
multi.hgetall("post:"+pids[x]);
|
||||||
multi.hgetall("post:"+_pid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
multi.exec(function (err, replies) {
|
multi.exec(function (err, replies) {
|
||||||
async.eachSeries(replies, function(postData, _callback) {
|
async.map(replies, function(postData, _callback) {
|
||||||
if(postData) {
|
if (postData) {
|
||||||
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
|
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
|
||||||
postData.post_rep = postData.reputation;
|
postData.post_rep = postData.reputation;
|
||||||
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
|
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
|
||||||
postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
|
postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
|
||||||
|
|
||||||
if(postData.uploadedImages) {
|
if (postData.uploadedImages) {
|
||||||
try {
|
try {
|
||||||
postData.uploadedImages = JSON.parse(postData.uploadedImages);
|
postData.uploadedImages = JSON.parse(postData.uploadedImages);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
@@ -188,15 +182,13 @@ var RDB = require('./redis.js'),
|
|||||||
|
|
||||||
postTools.parse(postData.content, function(err, content) {
|
postTools.parse(postData.content, function(err, content) {
|
||||||
postData.content = content;
|
postData.content = content;
|
||||||
posts.push(postData);
|
_callback(null, postData);
|
||||||
});
|
});
|
||||||
return _callback(null)
|
} else {
|
||||||
|
_callback(null);
|
||||||
}
|
}
|
||||||
else {
|
}, function(err, posts) {
|
||||||
return _callback(null)
|
if (!err) {
|
||||||
}
|
|
||||||
}, function(err) {
|
|
||||||
if(!err) {
|
|
||||||
return callback(null, posts);
|
return callback(null, posts);
|
||||||
} else {
|
} else {
|
||||||
return callback(err, null);
|
return callback(err, null);
|
||||||
@@ -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.get_cid_by_pid = function(pid, callback) {
|
||||||
Posts.getPostField(pid, 'tid', function(tid) {
|
Posts.getPostField(pid, 'tid', function(tid) {
|
||||||
if (tid) {
|
if (tid) {
|
||||||
|
|||||||
Reference in New Issue
Block a user