feat: category filter on post queue (#8710)

* feat: category filter on post queue

category filter module

* feat: add spec
This commit is contained in:
Barış Soner Uşaklı
2020-10-02 16:35:20 -04:00
committed by GitHub
parent f14b49457c
commit 5d9a868142
6 changed files with 137 additions and 72 deletions

View File

@@ -214,15 +214,15 @@ helpers.buildTitle = function (pageTitle) {
helpers.getCategories = async function (set, uid, privilege, selectedCid) {
const cids = await categories.getCidsByPrivilege(set, uid, privilege);
return await getCategoryData(cids, uid, selectedCid);
return await getCategoryData(cids, uid, selectedCid, privilege);
};
helpers.getCategoriesByStates = async function (uid, selectedCid, states) {
helpers.getCategoriesByStates = async function (uid, selectedCid, states, privilege = 'topics:read') {
const cids = await categories.getAllCidsFromSet('categories:cid');
return await getCategoryData(cids, uid, selectedCid, states);
return await getCategoryData(cids, uid, selectedCid, states, privilege);
};
async function getCategoryData(cids, uid, selectedCid, states) {
async function getCategoryData(cids, uid, selectedCid, states, privilege) {
if (selectedCid && !Array.isArray(selectedCid)) {
selectedCid = [selectedCid];
}
@@ -230,7 +230,7 @@ async function getCategoryData(cids, uid, selectedCid, states) {
states = states || [categories.watchStates.watching, categories.watchStates.notwatching];
const [allowed, watchState, categoryData, isAdmin] = await Promise.all([
privileges.categories.isUserAllowedTo('topics:read', cids, uid),
privileges.categories.isUserAllowedTo(privilege, cids, uid),
categories.getWatchState(cids, uid),
categories.getCategoriesData(cids),
user.isAdministrator(uid),
@@ -246,6 +246,11 @@ async function getCategoryData(cids, uid, selectedCid, states) {
const hasVisibleChildren = checkVisibleChildren(c, cidToAllowed, cidToWatchState, states);
const isCategoryVisible = c && cidToAllowed[c.cid] && !c.link && !c.disabled && states.includes(cidToWatchState[c.cid]);
const shouldBeRemoved = !hasVisibleChildren && !isCategoryVisible;
const shouldBeDisaplayedAsDisabled = hasVisibleChildren && !isCategoryVisible;
if (shouldBeDisaplayedAsDisabled) {
c.disabledClass = true;
}
if (shouldBeRemoved && c && c.parent && c.parent.cid && cidToCategory[c.parent.cid]) {
cidToCategory[c.parent.cid].children = cidToCategory[c.parent.cid].children.filter(child => child.cid !== c.cid);
@@ -254,7 +259,7 @@ async function getCategoryData(cids, uid, selectedCid, states) {
return c && !shouldBeRemoved;
});
const categoriesData = categories.buildForSelectCategories(visibleCategories);
const categoriesData = categories.buildForSelectCategories(visibleCategories, ['disabledClass']);
let selectedCategory = [];
const selectedCids = [];

View File

@@ -202,15 +202,16 @@ modsController.postQueue = async function (req, res, next) {
if (!isPrivileged) {
return next();
}
const cid = req.query.cid;
const page = parseInt(req.query.page, 10) || 1;
const postsPerPage = 20;
const [ids, isAdminOrGlobalMod, moderatedCids, allCategories] = await Promise.all([
const [ids, isAdminOrGlobalMod, moderatedCids, allCategories, categoriesData] = await Promise.all([
db.getSortedSetRange('post:queue', 0, -1),
user.isAdminOrGlobalMod(req.uid),
user.getModeratedCids(req.uid),
categories.buildForSelect(req.uid, 'find', ['disabled', 'link', 'slug']),
helpers.getCategoriesByStates(req.uid, cid, null, 'moderate'),
]);
allCategories.forEach((c) => {
@@ -218,7 +219,9 @@ modsController.postQueue = async function (req, res, next) {
});
let postData = await getQueuedPosts(ids);
postData = postData.filter(p => p && (isAdminOrGlobalMod || moderatedCids.includes(String(p.category.cid))));
postData = postData.filter(p => p &&
(!categoriesData.selectedCids.length || categoriesData.selectedCids.includes(p.category.cid)) &&
(isAdminOrGlobalMod || moderatedCids.includes(String(p.category.cid))));
({ posts: postData } = await plugins.fireHook('filter:post-queue.get', {
posts: postData,
@@ -234,6 +237,8 @@ modsController.postQueue = async function (req, res, next) {
title: '[[pages:post-queue]]',
posts: postData,
allCategories: allCategories,
...categoriesData,
allCategoriesUrl: 'post-queue' + helpers.buildQueryString(req.query, 'cid', ''),
pagination: pagination.create(page, pageCount),
breadcrumbs: helpers.buildBreadcrumbs([{ text: '[[pages:post-queue]]' }]),
});
@@ -268,7 +273,7 @@ async function addMetaData(postData) {
}
postData.topic = { cid: 0 };
if (postData.data.cid) {
postData.topic = { cid: postData.data.cid };
postData.topic = { cid: parseInt(postData.data.cid, 10) };
} else if (postData.data.tid) {
postData.topic = await topics.getTopicFields(postData.data.tid, ['title', 'cid']);
}