all files / src/topics/ recent.js

62.96% Statements 17/27
25% Branches 2/8
57.14% Functions 8/14
62.96% Lines 17/27
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                                                    32×   32×   32×     32×     32×         32×     32×       32× 32×      
 
 
'use strict';
 
var async = require('async');
var db = require('../database');
 
module.exports = function(Topics) {
	var terms = {
		day: 86400000,
		week: 604800000,
		month: 2592000000,
		year: 31104000000
	};
 
	Topics.getLatestTopics = function(uid, start, stop, term, callback) {
		async.waterfall([
			function (next) {
				Topics.getLatestTidsFromSet('topics:recent', start, stop, term, next);
			},
			function(tids, next) {
				Topics.getTopics(tids, uid, next);
			},
			function(topics, next) {
				next(null, {topics: topics, nextStart: stop + 1});
			}
		], callback);
	};
 
	Topics.getLatestTidsFromSet = function(set, 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(set, start, count, '+inf', Date.now() - since, callback);
	};
 
	Topics.updateTimestamp = function(tid, timestamp, callback) {
		async.parallel([
			function(next) {
				async.waterfall([
					function (next) {
						Topics.getTopicField(tid, 'deleted', next);
					},
					function (deleted, next) {
						Iif (parseInt(deleted, 10) === 1) {
							return next();
						}
						Topics.updateRecent(tid, timestamp, next);
					}
				], next);
			},
			function(next) {
				Topics.setTopicField(tid, 'lastposttime', timestamp, next);
			}
		], function(err) {
			callback(err);
		});
	};
 
	Topics.updateRecent = function(tid, timestamp, callback) {
		callback = callback || function() {};
		db.sortedSetAdd('topics:recent', timestamp, tid, callback);
	};
};