Files
NodeBB/src/categories/topics.js

102 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-11-08 23:54:21 -05:00
'use strict';
2016-01-27 00:05:19 +02:00
var async = require('async');
var db = require('../database');
var topics = require('../topics');
var plugins = require('../plugins');
2014-11-08 23:54:21 -05:00
module.exports = function (Categories) {
2014-11-08 23:54:21 -05:00
Categories.getCategoryTopics = function (data, callback) {
2016-01-27 20:36:40 +02:00
async.waterfall([
function (next) {
plugins.fireHook('filter:category.topics.prepare', data, next);
2014-11-08 23:54:21 -05:00
},
2016-01-27 20:36:40 +02:00
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) {
if (!Array.isArray(topics) || !topics.length) {
return next(null, {topics: [], uid: data.uid});
}
2014-11-08 23:54:21 -05:00
2016-10-13 11:42:29 +02:00
for (var i = 0; i < topics.length; ++i) {
2016-01-27 20:36:40 +02:00
topics[i].index = data.start + i;
}
2014-11-08 23:54:21 -05:00
2016-01-27 20:36:40 +02:00
plugins.fireHook('filter:category.topics.get', {topics: topics, uid: data.uid}, next);
},
function (results, next) {
next(null, {topics: results.topics, nextStart: data.stop + 1});
2014-11-08 23:54:21 -05:00
}
2016-01-27 20:36:40 +02:00
], callback);
};
2015-09-25 01:09:14 -04:00
Categories.modifyTopicsByPrivilege = function (topics, privileges) {
2016-01-27 20:36:40 +02:00
if (!Array.isArray(topics) || !topics.length || privileges.isAdminOrMod) {
return;
}
topics.forEach(function (topic) {
2016-01-27 20:36:40 +02:00
if (topic.deleted && !topic.isOwner) {
topic.title = '[[topic:topic_is_deleted]]';
topic.slug = topic.tid;
topic.teaser = null;
topic.noAnchor = true;
2016-02-24 11:35:53 +02:00
topic.tags = [];
2016-01-27 20:36:40 +02:00
}
});
2014-11-08 23:54:21 -05:00
};
Categories.getTopicIds = function (set, reverse, start, stop, callback) {
2016-09-19 13:08:31 +03:00
if (Array.isArray(set)) {
db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({sets: set, start: start, stop: stop}, callback);
2015-01-08 13:47:15 -05:00
} else {
2016-09-19 13:08:31 +03:00
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, callback);
2015-01-08 13:47:15 -05:00
}
2014-11-08 23:54:21 -05:00
};
Categories.getTopicIndex = function (tid, callback) {
topics.getTopicField(tid, 'cid', function (err, cid) {
2016-09-19 13:08:31 +03:00
if (err) {
2014-11-08 23:54:21 -05:00
return callback(err);
}
db.sortedSetRevRank('cid:' + cid + ':tids', tid, callback);
});
};
Categories.onNewPostMade = function (cid, pinned, postData, callback) {
2015-03-15 01:12:13 -04:00
if (!cid || !postData) {
return callback();
}
2014-11-08 23:54:21 -05:00
2015-03-15 01:12:13 -04:00
async.parallel([
function (next) {
2015-03-15 01:12:13 -04:00
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next);
},
function (next) {
2015-03-15 01:12:13 -04:00
db.incrObjectField('category:' + cid, 'post_count', next);
},
function (next) {
2015-03-15 01:12:13 -04:00
if (parseInt(pinned, 10) === 1) {
next();
} else {
db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
2014-11-08 23:54:21 -05:00
}
2015-03-15 01:12:13 -04:00
},
function (next) {
Categories.updateRecentTid(cid, postData.tid, next);
},
function (next) {
2015-03-15 01:12:13 -04:00
db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, next);
}
], callback);
2014-11-08 23:54:21 -05:00
};
};