refactor: flag states so that they are not hardcoded, allow plugins to add additional states, deprecated filter:flags.getFilters hook, closes #11065

isekai-main
Julian Lam 2 years ago
parent 8b209f16d1
commit 9f531f957e

@ -141,6 +141,7 @@ modsController.flags.detail = async function (req, res, next) {
return memo; return memo;
}, {}), }, {}),
states: Object.fromEntries(flags._states),
title: `[[pages:flag-details, ${req.params.flagId}]]`, title: `[[pages:flag-details, ${req.params.flagId}]]`,
privileges: results.privileges, privileges: results.privileges,
breadcrumbs: helpers.buildBreadcrumbs([ breadcrumbs: helpers.buildBreadcrumbs([

@ -20,15 +20,24 @@ const batch = require('./batch');
const Flags = module.exports; const Flags = module.exports;
Flags._constants = { Flags._states = new Map([
states: ['open', 'wip', 'resolved', 'rejected'], ['open', {
state_class: { label: '[[flags:state-open]]',
open: 'info', class: 'danger',
wip: 'warning', }],
resolved: 'success', ['wip', {
rejected: 'danger', label: '[[flags:state-wip]]',
}, class: 'warning',
}; }],
['resolved', {
label: '[[flags:state-resolved]]',
class: 'success',
}],
['rejected', {
label: '[[flags:state-rejected]]',
class: 'secondary',
}],
]);
Flags.init = async function () { Flags.init = async function () {
// Query plugins for custom filter strategies and merge into core filter strategies // Query plugins for custom filter strategies and merge into core filter strategies
@ -78,14 +87,15 @@ Flags.init = async function () {
} }
}, },
}, },
states: Flags._states,
helpers: { helpers: {
prepareSets: prepareSets, prepareSets: prepareSets,
}, },
}; };
try { try {
const data = await plugins.hooks.fire('filter:flags.getFilters', hookData); ({ filters: Flags._filters } = await plugins.hooks.fire('filter:flags.getFilters', hookData));
Flags._filters = data.filters; ({ filters: Flags._filters, states: Flags._states } = await plugins.hooks.fire('filter:flags.init', hookData));
} catch (err) { } catch (err) {
winston.error(`[flags/init] Could not retrieve filters\n${err.stack}`); winston.error(`[flags/init] Could not retrieve filters\n${err.stack}`);
Flags._filters = {}; Flags._filters = {};
@ -197,7 +207,7 @@ Flags.list = async function (data) {
heat: reportCounts[idx], heat: reportCounts[idx],
...flagObj, ...flagObj,
}; };
flagObj.labelClass = Flags._constants.state_class[flagObj.state]; flagObj.labelClass = Flags._states.get(flagObj.state).class;
return Object.assign(flagObj, { return Object.assign(flagObj, {
target_readable: `${flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1)} ${flagObj.targetId}`, target_readable: `${flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1)} ${flagObj.targetId}`,
@ -674,7 +684,7 @@ Flags.update = async function (flagId, uid, changeset) {
if (current[prop] === changeset[prop]) { if (current[prop] === changeset[prop]) {
delete changeset[prop]; delete changeset[prop];
} else if (prop === 'state') { } else if (prop === 'state') {
if (!Flags._constants.states.includes(changeset[prop])) { if (!Flags._states.has(changeset[prop])) {
delete changeset[prop]; delete changeset[prop];
} else { } else {
tasks.push(db.sortedSetAdd(`flags:byState:${changeset[prop]}`, now, flagId)); tasks.push(db.sortedSetAdd(`flags:byState:${changeset[prop]}`, now, flagId));

@ -33,6 +33,11 @@ Hooks._deprecated = new Map([
since: 'v2.2.0', since: 'v2.2.0',
until: 'v3.0.0', until: 'v3.0.0',
}], }],
['filter:flags.getFilters', {
new: 'filter:flags.init',
since: 'v2.7.0',
until: 'v3.0.0',
}],
]); ]);
Hooks.internals = { Hooks.internals = {

Loading…
Cancel
Save