mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-04 21:15:55 +01:00
feat: #7743 categories
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var _ = require('lodash');
|
||||
|
||||
var db = require('../database');
|
||||
@@ -10,123 +9,89 @@ var meta = require('../meta');
|
||||
var user = require('../user');
|
||||
|
||||
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, next);
|
||||
},
|
||||
function (tids, next) {
|
||||
topics.getTopicsByTids(tids, data.uid, next);
|
||||
},
|
||||
async.apply(user.blocks.filter, data.uid),
|
||||
function (topicsData, next) {
|
||||
if (!topicsData.length) {
|
||||
return next(null, { topics: [], uid: data.uid });
|
||||
}
|
||||
topics.calculateTopicIndices(topicsData, data.start);
|
||||
Categories.getCategoryTopics = async function (data) {
|
||||
let results = await plugins.fireHook('filter:category.topics.prepare', data);
|
||||
const tids = await Categories.getTopicIds(results);
|
||||
let topicsData = await topics.getTopicsByTids(tids, data.uid);
|
||||
topicsData = await user.blocks.filter(data.uid, topicsData);
|
||||
|
||||
plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid }, next);
|
||||
},
|
||||
function (results, next) {
|
||||
next(null, { topics: results.topics, nextStart: data.stop + 1 });
|
||||
},
|
||||
], callback);
|
||||
if (!topicsData.length) {
|
||||
return { topics: [], uid: data.uid };
|
||||
}
|
||||
topics.calculateTopicIndices(topicsData, data.start);
|
||||
|
||||
results = await plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid });
|
||||
return { topics: results.topics, nextStart: data.stop + 1 };
|
||||
};
|
||||
|
||||
Categories.getTopicIds = function (data, callback) {
|
||||
var pinnedTids;
|
||||
Categories.getTopicIds = async function (data) {
|
||||
const dataForPinned = _.cloneDeep(data);
|
||||
dataForPinned.start = 0;
|
||||
dataForPinned.stop = -1;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
var dataForPinned = _.cloneDeep(data);
|
||||
dataForPinned.start = 0;
|
||||
dataForPinned.stop = -1;
|
||||
const [pinnedTids, set, direction] = await Promise.all([
|
||||
Categories.getPinnedTids(dataForPinned),
|
||||
Categories.buildTopicsSortedSet(data),
|
||||
Categories.getSortedSetRangeDirection(data.sort),
|
||||
]);
|
||||
|
||||
async.parallel({
|
||||
pinnedTids: async.apply(Categories.getPinnedTids, dataForPinned),
|
||||
set: async.apply(Categories.buildTopicsSortedSet, data),
|
||||
direction: async.apply(Categories.getSortedSetRangeDirection, data.sort),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
var totalPinnedCount = results.pinnedTids.length;
|
||||
const totalPinnedCount = pinnedTids.length;
|
||||
const pinnedTidsOnPage = pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined);
|
||||
const pinnedCountOnPage = pinnedTidsOnPage.length;
|
||||
const topicsPerPage = data.stop - data.start + 1;
|
||||
const normalTidsToGet = Math.max(0, topicsPerPage - pinnedCountOnPage);
|
||||
|
||||
pinnedTids = results.pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined);
|
||||
if (!normalTidsToGet && data.stop !== -1) {
|
||||
return pinnedTidsOnPage;
|
||||
}
|
||||
|
||||
var pinnedCount = pinnedTids.length;
|
||||
if (plugins.hasListeners('filter:categories.getTopicIds')) {
|
||||
const result = await plugins.fireHook('filter:categories.getTopicIds', {
|
||||
tids: [],
|
||||
data: data,
|
||||
pinnedTids: pinnedTidsOnPage,
|
||||
allPinnedTids: pinnedTids,
|
||||
totalPinnedCount: totalPinnedCount,
|
||||
normalTidsToGet: normalTidsToGet,
|
||||
});
|
||||
return result && result.tids;
|
||||
}
|
||||
|
||||
var topicsPerPage = data.stop - data.start + 1;
|
||||
let start = data.start;
|
||||
if (start > 0 && totalPinnedCount) {
|
||||
start -= totalPinnedCount - pinnedCountOnPage;
|
||||
}
|
||||
|
||||
var normalTidsToGet = Math.max(0, topicsPerPage - pinnedCount);
|
||||
const stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
|
||||
let normalTids;
|
||||
const reverse = direction === 'highest-to-lowest';
|
||||
if (Array.isArray(set)) {
|
||||
const weights = set.map((s, index) => (index ? 0 : 1));
|
||||
normalTids = await db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights });
|
||||
} else {
|
||||
normalTids = await db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop);
|
||||
}
|
||||
normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
|
||||
|
||||
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: results.pinnedTids,
|
||||
totalPinnedCount: totalPinnedCount,
|
||||
normalTidsToGet: normalTidsToGet,
|
||||
}, function (err, data) {
|
||||
callback(err, data && data.tids);
|
||||
});
|
||||
}
|
||||
|
||||
var set = results.set;
|
||||
var direction = results.direction;
|
||||
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)) {
|
||||
const weights = set.map((s, index) => (index ? 0 : 1));
|
||||
db[direction === 'highest-to-lowest' ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights }, next);
|
||||
} else {
|
||||
db[direction === 'highest-to-lowest' ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, next);
|
||||
}
|
||||
},
|
||||
function (normalTids, next) {
|
||||
normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
|
||||
|
||||
next(null, pinnedTids.concat(normalTids));
|
||||
},
|
||||
], callback);
|
||||
return pinnedTids.concat(normalTids);
|
||||
};
|
||||
|
||||
Categories.getTopicCount = function (data, callback) {
|
||||
Categories.getTopicCount = async function (data) {
|
||||
if (plugins.hasListeners('filter:categories.getTopicCount')) {
|
||||
return plugins.fireHook('filter:categories.getTopicCount', {
|
||||
const result = await plugins.fireHook('filter:categories.getTopicCount', {
|
||||
topicCount: data.category.topic_count,
|
||||
data: data,
|
||||
}, function (err, data) {
|
||||
callback(err, data && data.topicCount);
|
||||
});
|
||||
return result && result.topicCount;
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Categories.buildTopicsSortedSet(data, next);
|
||||
},
|
||||
function (set, next) {
|
||||
if (Array.isArray(set)) {
|
||||
db.sortedSetIntersectCard(set, next);
|
||||
} else {
|
||||
next(null, data.category.topic_count);
|
||||
}
|
||||
},
|
||||
], callback);
|
||||
const set = await Categories.buildTopicsSortedSet(data);
|
||||
if (Array.isArray(set)) {
|
||||
return await db.sortedSetIntersectCard(set);
|
||||
}
|
||||
return data.category.topic_count;
|
||||
};
|
||||
|
||||
Categories.buildTopicsSortedSet = function (data, callback) {
|
||||
Categories.buildTopicsSortedSet = async 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';
|
||||
@@ -148,40 +113,37 @@ module.exports = function (Categories) {
|
||||
set = [set, 'tag:' + data.tag + ':topics'];
|
||||
}
|
||||
}
|
||||
plugins.fireHook('filter:categories.buildTopicsSortedSet', {
|
||||
const result = await plugins.fireHook('filter:categories.buildTopicsSortedSet', {
|
||||
set: set,
|
||||
data: data,
|
||||
}, function (err, data) {
|
||||
callback(err, data && data.set);
|
||||
});
|
||||
return result && result.set;
|
||||
};
|
||||
|
||||
Categories.getSortedSetRangeDirection = function (sort, callback) {
|
||||
Categories.getSortedSetRangeDirection = async function (sort) {
|
||||
sort = sort || 'newest_to_oldest';
|
||||
var direction = sort === 'newest_to_oldest' || sort === 'most_posts' || sort === 'most_votes' ? 'highest-to-lowest' : 'lowest-to-highest';
|
||||
plugins.fireHook('filter:categories.getSortedSetRangeDirection', {
|
||||
const result = await plugins.fireHook('filter:categories.getSortedSetRangeDirection', {
|
||||
sort: sort,
|
||||
direction: direction,
|
||||
}, function (err, data) {
|
||||
callback(err, data && data.direction);
|
||||
});
|
||||
return result && result.direction;
|
||||
};
|
||||
|
||||
Categories.getAllTopicIds = function (cid, start, stop, callback) {
|
||||
db.getSortedSetRange(['cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tids'], start, stop, callback);
|
||||
Categories.getAllTopicIds = async function (cid, start, stop) {
|
||||
return await db.getSortedSetRange(['cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tids'], start, stop);
|
||||
};
|
||||
|
||||
Categories.getPinnedTids = function (data, callback) {
|
||||
Categories.getPinnedTids = async function (data) {
|
||||
if (plugins.hasListeners('filter:categories.getPinnedTids')) {
|
||||
return plugins.fireHook('filter:categories.getPinnedTids', {
|
||||
const result = await plugins.fireHook('filter:categories.getPinnedTids', {
|
||||
pinnedTids: [],
|
||||
data: data,
|
||||
}, function (err, data) {
|
||||
callback(err, data && data.pinnedTids);
|
||||
});
|
||||
return result && result.pinnedTids;
|
||||
}
|
||||
|
||||
db.getSortedSetRevRange('cid:' + data.cid + ':tids:pinned', data.start, data.stop, callback);
|
||||
return await db.getSortedSetRevRange('cid:' + data.cid + ':tids:pinned', data.start, data.stop);
|
||||
};
|
||||
|
||||
Categories.modifyTopicsByPrivilege = function (topics, privileges) {
|
||||
@@ -200,27 +162,18 @@ module.exports = function (Categories) {
|
||||
});
|
||||
};
|
||||
|
||||
Categories.onNewPostMade = function (cid, pinned, postData, callback) {
|
||||
Categories.onNewPostMade = async function (cid, pinned, postData) {
|
||||
if (!cid || !postData) {
|
||||
return setImmediate(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (pinned) {
|
||||
return setImmediate(next);
|
||||
}
|
||||
db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, err => next(err));
|
||||
},
|
||||
function (next) {
|
||||
Categories.updateRecentTid(cid, postData.tid, next);
|
||||
},
|
||||
], callback);
|
||||
const promises = [
|
||||
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid),
|
||||
db.incrObjectField('category:' + cid, 'post_count'),
|
||||
Categories.updateRecentTid(cid, postData.tid),
|
||||
];
|
||||
if (!pinned) {
|
||||
promises.push(db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid));
|
||||
}
|
||||
await Promise.all(promises);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user