Files
NodeBB/src/categories/topics.js

141 lines
3.8 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.cid, data.set, data.reverse, data.start, data.stop, next);
2016-01-27 20:36:40 +02:00
},
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.getTopicIds = function (cid, set, reverse, start, stop, callback) {
var pinnedTids;
var pinnedCount;
var totalPinnedCount;
async.waterfall([
function (next) {
Categories.getPinnedTids(cid, 0, -1, next);
},
function (_pinnedTids, next) {
totalPinnedCount = _pinnedTids.length;
pinnedTids = _pinnedTids.slice(start, stop === -1 ? undefined : stop + 1);
pinnedCount = pinnedTids.length;
var topicsPerPage = stop - start + 1;
var normalTidsToGet = Math.max(0, topicsPerPage - pinnedCount);
if (!normalTidsToGet && stop !== -1) {
return next(null, []);
}
if (start > 0 && totalPinnedCount) {
start -= totalPinnedCount - pinnedCount;
}
stop = stop === -1 ? stop : start + normalTidsToGet - 1;
if (Array.isArray(set)) {
db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({sets: set, start: start, stop: stop}, next);
} else {
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, next);
}
},
function (normalTids, next) {
normalTids = normalTids.filter(function (tid) {
return pinnedTids.indexOf(tid) === -1;
});
next(null, pinnedTids.concat(normalTids));
}
], callback);
};
Categories.getAllTopicIds = function (cid, start, stop, callback) {
db.getSortedSetRange(['cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tids'], start, stop, callback);
};
Categories.getPinnedTids = function (cid, start, stop, callback) {
db.getSortedSetRevRange('cid:' + cid + ':tids:pinned', start, stop, callback);
};
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.getTopicIndex = function (tid, callback) {
console.warn('[Categories.getTopicIndex] deprecated');
callback(null, 1);
2014-11-08 23:54:21 -05:00
};
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
};
};