2 new hooks for changing direction and set name

v1.18.x
Baris Usakli 8 years ago
parent f1f8a587d4
commit 72cafd55cf

@ -45,12 +45,17 @@ module.exports = function (Categories) {
var dataForPinned = _.cloneDeep(data); var dataForPinned = _.cloneDeep(data);
dataForPinned.start = 0; dataForPinned.start = 0;
dataForPinned.stop = -1; dataForPinned.stop = -1;
Categories.getPinnedTids(dataForPinned, next);
async.parallel({
pinnedTids: async.apply(Categories.getPinnedTids, dataForPinned),
set: async.apply(Categories.buildTopicsSortedSet, data),
direction: async.apply(Categories.getSortedSetRangeDirection, data.sort),
}, next);
}, },
function (_pinnedTids, next) { function (results, next) {
var totalPinnedCount = _pinnedTids.length; var totalPinnedCount = results.pinnedTids.length;
pinnedTids = _pinnedTids.slice(data.start, data.stop === -1 ? undefined : data.stop + 1); pinnedTids = results.pinnedTids.slice(data.start, data.stop === -1 ? undefined : data.stop + 1);
var pinnedCount = pinnedTids.length; var pinnedCount = pinnedTids.length;
@ -67,7 +72,7 @@ module.exports = function (Categories) {
tids: [], tids: [],
data: data, data: data,
pinnedTids: pinnedTids, pinnedTids: pinnedTids,
allPinnedTids: _pinnedTids, allPinnedTids: results.pinnedTids,
totalPinnedCount: totalPinnedCount, totalPinnedCount: totalPinnedCount,
normalTidsToGet: normalTidsToGet, normalTidsToGet: normalTidsToGet,
}, function (err, data) { }, function (err, data) {
@ -75,8 +80,8 @@ module.exports = function (Categories) {
}); });
} }
var set = Categories.buildTopicsSortedSet(data); var set = results.set;
var reverse = Categories.getSortedSetRangeDirection(data.sort); var direction = results.direction;
var start = data.start; var start = data.start;
if (start > 0 && totalPinnedCount) { if (start > 0 && totalPinnedCount) {
start -= totalPinnedCount - pinnedCount; start -= totalPinnedCount - pinnedCount;
@ -85,9 +90,9 @@ module.exports = function (Categories) {
var stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1; var stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
if (Array.isArray(set)) { if (Array.isArray(set)) {
db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop }, next); db[direction === 'highest-to-lowest' ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop }, next);
} else { } else {
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, next); db[direction === 'highest-to-lowest' ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, next);
} }
}, },
function (normalTids, next) { function (normalTids, next) {
@ -109,15 +114,21 @@ module.exports = function (Categories) {
callback(err, data && data.topicCount); callback(err, data && data.topicCount);
}); });
} }
var set = Categories.buildTopicsSortedSet(data); async.waterfall([
if (Array.isArray(set)) { function (next) {
db.sortedSetIntersectCard(set, callback); Categories.buildTopicsSortedSet(data, next);
} else { },
callback(null, data.category.topic_count); function (set, next) {
} if (Array.isArray(set)) {
db.sortedSetIntersectCard(set, next);
} else {
next(null, data.category.topic_count);
}
},
], callback);
}; };
Categories.buildTopicsSortedSet = function (data) { Categories.buildTopicsSortedSet = function (data, callback) {
var cid = data.cid; var cid = data.cid;
var set = 'cid:' + cid + ':tids'; var set = 'cid:' + cid + ':tids';
var sort = data.sort || (data.settings && data.settings.categoryTopicSort) || meta.config.categoryTopicSort || 'newest_to_oldest'; var sort = data.sort || (data.settings && data.settings.categoryTopicSort) || meta.config.categoryTopicSort || 'newest_to_oldest';
@ -139,13 +150,23 @@ module.exports = function (Categories) {
set = [set, 'tag:' + data.tag + ':topics']; set = [set, 'tag:' + data.tag + ':topics'];
} }
} }
return set; plugins.fireHook('filter:categories.buildTopicsSortedSet', {
set: set,
data: data,
}, function (err, data) {
callback(err, data && data.set);
});
}; };
Categories.getSortedSetRangeDirection = function (sort) { Categories.getSortedSetRangeDirection = function (sort, callback) {
sort = sort || 'newest_to_oldest'; sort = sort || 'newest_to_oldest';
var reverse = sort === 'newest_to_oldest' || sort === 'most_posts'; var direction = sort === 'newest_to_oldest' || sort === 'most_posts' ? 'highest-to-lowest' : 'lowest-to-highest';
return reverse; plugins.fireHook('filter:categories.getSortedSetRangeDirection', {
sort: sort,
direction: direction,
}, function (err, data) {
callback(err, data && data.direction);
});
}; };
Categories.getAllTopicIds = function (cid, start, stop, callback) { Categories.getAllTopicIds = function (cid, start, stop, callback) {

Loading…
Cancel
Save