Files
NodeBB/src/posts/recent.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-09-03 15:16:41 -04:00
'use strict';
2017-02-10 15:26:17 +03:00
var async = require('async');
var _ = require('lodash');
2017-02-10 15:26:17 +03:00
var db = require('../database');
var privileges = require('../privileges');
2014-09-03 15:16:41 -04:00
module.exports = function (Posts) {
2014-09-03 15:16:41 -04:00
var terms = {
day: 86400000,
week: 604800000,
2017-02-17 19:31:21 -07:00
month: 2592000000,
2014-09-03 15:16:41 -04:00
};
Posts.getRecentPosts = function (uid, start, stop, term, callback) {
2015-03-14 20:46:28 -04:00
var min = 0;
2014-09-03 15:16:41 -04:00
if (terms[term]) {
2015-03-14 20:46:28 -04:00
min = Date.now() - terms[term];
2014-09-03 15:16:41 -04:00
}
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
2014-11-09 01:30:27 -05:00
async.waterfall([
function (next) {
2015-03-14 20:46:28 -04:00
db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', min, next);
2014-11-09 01:30:27 -05:00
},
function (pids, next) {
privileges.posts.filter('topics:read', pids, uid, next);
2014-11-09 01:30:27 -05:00
},
function (pids, next) {
2017-02-18 12:30:49 -07:00
Posts.getPostSummaryByPids(pids, uid, { stripTags: true }, next);
2017-02-17 19:31:21 -07:00
},
2014-11-09 01:30:27 -05:00
], callback);
2014-09-03 15:16:41 -04:00
};
Posts.getRecentPosterUids = function (start, stop, callback) {
2014-11-09 01:30:27 -05:00
async.waterfall([
function (next) {
db.getSortedSetRevRange('posts:pid', start, stop, next);
2014-11-09 01:30:27 -05:00
},
function (pids, next) {
2014-11-09 01:30:27 -05:00
Posts.getPostsFields(pids, ['uid'], next);
},
function (postData, next) {
var uids = _.uniq(postData.map(post => post && post.uid).filter(uid => parseInt(uid, 10)));
2017-02-10 15:26:17 +03:00
next(null, uids);
2017-02-17 19:31:21 -07:00
},
2014-11-09 01:30:27 -05:00
], callback);
};
2014-09-03 15:16:41 -04:00
};