all files / src/categories/ topics.js

57.78% Statements 26/45
34.78% Branches 8/23
76.47% Functions 13/17
57.78% Lines 26/45
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                                                                                            22×       22×   22×     22×     22×     22×       22×            
'use strict';
 
var async = require('async');
 
var db = require('../database');
var topics = require('../topics');
var plugins = require('../plugins');
 
module.exports = function(Categories) {
 
	Categories.getCategoryTopics = function(data, callback) {
		async.waterfall([
			function (next) {
				plugins.fireHook('filter:category.topics.prepare', data, next);
			},
			function (data, next) {
				Categories.getTopicIds(data.set, data.reverse, data.start, data.stop, next);
			},
			function (tids, next) {
				topics.getTopicsByTids(tids, data.uid, next);
			},
			function (topics, next) {
				Eif (!Array.isArray(topics) || !topics.length) {
					return next(null, {topics: [], uid: data.uid});
				}
 
				for (var i=0; i<topics.length; ++i) {
					topics[i].index = data.start + i;
				}
 
				plugins.fireHook('filter:category.topics.get', {topics: topics, uid: data.uid}, next);
			},
			function (results, next) {
				next(null, {topics: results.topics, nextStart: data.stop + 1});
			}
		], callback);
	};
 
	Categories.modifyTopicsByPrivilege = function(topics, privileges) {
		if (!Array.isArray(topics) || !topics.length || privileges.isAdminOrMod) {
			return;
		}
 
		topics.forEach(function(topic) {
			if (topic.deleted && !topic.isOwner) {
				topic.title = '[[topic:topic_is_deleted]]';
				topic.slug = topic.tid;
				topic.teaser = null;
				topic.noAnchor = true;
				topic.tags = [];
			}
		});
	};
 
	Categories.getTopicIds = function(set, reverse, start, stop, callback) {
		Eif (reverse) {
			db.getSortedSetRevRange(set, start, stop, callback);
		} else {
			db.getSortedSetRange(set, start, stop, callback);
		}
	};
 
	Categories.getTopicIndex = function(tid, callback) {
		topics.getTopicField(tid, 'cid', function(err, cid) {
			if(err) {
				return callback(err);
			}
 
			db.sortedSetRevRank('cid:' + cid + ':tids', tid, callback);
		});
	};
 
	Categories.onNewPostMade = function(cid, pinned, postData, callback) {
		Iif (!cid || !postData) {
			return callback();
		}
 
		async.parallel([
			function(next) {
				db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next);
			},
			function(next) {
				db.incrObjectField('category:' + cid, 'post_count', next);
			},
			function(next) {
				Iif (parseInt(pinned, 10) === 1) {
					next();
				} else {
					db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
				}
			},
			function(next) {
				db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, next);
			}
		], callback);
	};
 
};