Files
NodeBB/src/categories/topics.js

223 lines
6.1 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');
2017-06-26 16:51:45 -04:00
var _ = require('lodash');
2016-01-27 00:05:19 +02:00
var db = require('../database');
var topics = require('../topics');
var plugins = require('../plugins');
var meta = require('../meta');
2014-11-08 23:54:21 -05:00
module.exports = function (Categories) {
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) {
2017-06-06 12:55:43 -04:00
Categories.getTopicIds(data, next);
2016-01-27 20:36:40 +02:00
},
function (tids, next) {
topics.getTopicsByTids(tids, data.uid, next);
},
function (topics, next) {
2017-05-27 00:30:07 -04:00
if (!topics.length) {
2017-02-18 12:30:49 -07:00
return next(null, { topics: [], uid: data.uid });
2016-01-27 20:36:40 +02:00
}
2014-11-08 23:54:21 -05:00
for (var i = 0; i < topics.length; i += 1) {
2016-01-27 20:36:40 +02:00
topics[i].index = data.start + i;
}
2014-11-08 23:54:21 -05:00
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topics, uid: data.uid }, next);
2016-01-27 20:36:40 +02:00
},
function (results, next) {
2017-02-18 12:30:49 -07:00
next(null, { topics: results.topics, nextStart: data.stop + 1 });
2017-02-17 19:31:21 -07:00
},
2016-01-27 20:36:40 +02:00
], callback);
};
2015-09-25 01:09:14 -04:00
2017-06-06 12:55:43 -04:00
Categories.getTopicIds = function (data, callback) {
var pinnedTids;
2017-06-06 12:55:43 -04:00
async.waterfall([
function (next) {
2017-06-26 16:51:45 -04:00
var dataForPinned = _.cloneDeep(data);
dataForPinned.start = 0;
dataForPinned.stop = -1;
Categories.getPinnedTids(dataForPinned, next);
},
function (_pinnedTids, next) {
var totalPinnedCount = _pinnedTids.length;
pinnedTids = _pinnedTids.slice(data.start, data.stop === -1 ? undefined : data.stop + 1);
var pinnedCount = pinnedTids.length;
var topicsPerPage = data.stop - data.start + 1;
var normalTidsToGet = Math.max(0, topicsPerPage - pinnedCount);
if (!normalTidsToGet && data.stop !== -1) {
return next(null, []);
}
if (plugins.hasListeners('filter:categories.getTopicIds')) {
return plugins.fireHook('filter:categories.getTopicIds', {
tids: [],
data: data,
pinnedTids: pinnedTids,
allPinnedTids: _pinnedTids,
totalPinnedCount: totalPinnedCount,
normalTidsToGet: normalTidsToGet,
}, function (err, data) {
callback(err, data && data.tids);
});
}
var set = Categories.buildTopicsSortedSet(data);
var reverse = Categories.getSortedSetRangeDirection(data.sort);
var start = data.start;
if (start > 0 && totalPinnedCount) {
start -= totalPinnedCount - pinnedCount;
}
var stop = data.stop === -1 ? data.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));
2017-02-17 19:31:21 -07:00
},
], callback);
};
Categories.getTopicCount = function (data, callback) {
if (plugins.hasListeners('filter:categories.getTopicCount')) {
return plugins.fireHook('filter:categories.getTopicCount', {
topicCount: data.category.topic_count,
data: data,
}, function (err, data) {
callback(err, data && data.topicCount);
});
}
var set = Categories.buildTopicsSortedSet(data);
if (Array.isArray(set)) {
db.sortedSetIntersectCard(set, callback);
} else {
callback(null, data.category.topic_count);
}
};
Categories.buildTopicsSortedSet = function (data) {
var cid = data.cid;
var set = 'cid:' + cid + ':tids';
var sort = data.sort || (data.settings && data.settings.categoryTopicSort) || meta.config.categoryTopicSort || 'newest_to_oldest';
if (sort === 'most_posts') {
set = 'cid:' + cid + ':tids:posts';
}
if (data.targetUid) {
set = 'cid:' + cid + ':uid:' + data.targetUid + ':tids';
}
if (data.tag) {
if (Array.isArray(data.tag)) {
set = [set].concat(data.tag.map(function (tag) {
return 'tag:' + tag + ':topics';
}));
} else {
set = [set, 'tag:' + data.tag + ':topics'];
}
}
return set;
};
Categories.getSortedSetRangeDirection = function (sort) {
sort = sort || 'newest_to_oldest';
var reverse = sort === 'newest_to_oldest' || sort === 'most_posts';
return reverse;
};
Categories.getAllTopicIds = function (cid, start, stop, callback) {
db.getSortedSetRange(['cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tids'], start, stop, callback);
};
2017-06-26 16:51:45 -04:00
Categories.getPinnedTids = function (data, callback) {
if (plugins.hasListeners('filter:categories.getPinnedTids')) {
return plugins.fireHook('filter:categories.getPinnedTids', {
pinnedTids: [],
data: data,
}, function (err, data) {
callback(err, data && data.pinnedTids);
});
}
db.getSortedSetRevRange('cid:' + data.cid + ':tids:pinned', data.start, data.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 setImmediate(callback);
2015-03-15 01:12:13 -04:00
}
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) {
return setImmediate(next);
2014-11-08 23:54:21 -05:00
}
2017-01-05 21:14:12 +03:00
async.parallel([
function (next) {
db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
},
function (next) {
db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
next(err);
2017-01-05 21:14:12 +03:00
});
2015-03-15 01:12:13 -04:00
},
function (next) {
Categories.updateRecentTid(cid, postData.tid, next);
2017-02-17 19:31:21 -07:00
},
2015-03-15 01:12:13 -04:00
], callback);
2014-11-08 23:54:21 -05:00
};
};