dont create term object on each call

This commit is contained in:
barisusakli
2014-09-03 15:16:41 -04:00
parent 9f0fff4434
commit 4736a68534
4 changed files with 84 additions and 76 deletions

70
src/posts/recent.js Normal file
View File

@@ -0,0 +1,70 @@
'use strict';
var db = require('../database'),
privileges = require('../privileges');
module.exports = function(Posts) {
var terms = {
day: 86400000,
week: 604800000,
month: 2592000000
};
Posts.getRecentPosts = function(uid, start, stop, term, callback) {
var since = terms.day;
if (terms[term]) {
since = terms[term];
}
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
db.getSortedSetRevRangeByScore('posts:pid', start, count, Infinity, Date.now() - since, function(err, pids) {
if (err) {
return callback(err);
}
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
privileges.posts.filter('read', pids, uid, function(err, pids) {
if (err) {
return callback(err);
}
Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);
});
});
};
Posts.getRecentPosterUids = function(start, end, callback) {
db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) {
if (err) {
return callback(err);
}
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
pids = pids.map(function(pid) {
return 'post:' + pid;
});
db.getObjectsFields(pids, ['uid'], function(err, postData) {
if (err) {
return callback(err);
}
postData = postData.map(function(post) {
return post && post.uid;
}).filter(function(value, index, array) {
return value && array.indexOf(value) === index;
});
callback(null, postData);
});
});
};
};