all files / src/posts/ recent.js

25% Statements 5/20
0% Branches 0/8
9.09% Functions 1/11
25% Lines 5/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                                                                   
'use strict';
 
var async = require('async'),
	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 min = 0;
		if (terms[term]) {
			min = Date.now() - terms[term];
		}
 
		var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
 
		async.waterfall([
			function(next) {
				db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', min, next);
			},
			function(pids, next) {
				privileges.posts.filter('read', pids, uid, next);
			},
			function(pids, next) {
				Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next);
			}
		], callback);
	};
 
	Posts.getRecentPosterUids = function(start, stop, callback) {
		async.waterfall([
			function(next) {
				db.getSortedSetRevRange('posts:pid', start, stop, next);
			},
			function(pids, next) {
				Posts.getPostsFields(pids, ['uid'], next);
			},
			function(postData, next) {
				postData = postData.map(function(post) {
					return post && post.uid;
				}).filter(function(value, index, array) {
					return value && array.indexOf(value) === index;
				});
				next(null, postData);
			}
		], callback);
 	};
};