diff --git a/src/controllers/mods.js b/src/controllers/mods.js index f80e9230f3..48a3bd8ff3 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -141,6 +141,7 @@ modsController.flags.detail = async function (req, res, next) { return memo; }, {}), + states: Object.fromEntries(flags._states), title: `[[pages:flag-details, ${req.params.flagId}]]`, privileges: results.privileges, breadcrumbs: helpers.buildBreadcrumbs([ diff --git a/src/flags.js b/src/flags.js index 7d8b5e43aa..8a400c9d88 100644 --- a/src/flags.js +++ b/src/flags.js @@ -20,15 +20,24 @@ const batch = require('./batch'); const Flags = module.exports; -Flags._constants = { - states: ['open', 'wip', 'resolved', 'rejected'], - state_class: { - open: 'info', - wip: 'warning', - resolved: 'success', - rejected: 'danger', - }, -}; +Flags._states = new Map([ + ['open', { + label: '[[flags:state-open]]', + class: 'danger', + }], + ['wip', { + label: '[[flags:state-wip]]', + class: 'warning', + }], + ['resolved', { + label: '[[flags:state-resolved]]', + class: 'success', + }], + ['rejected', { + label: '[[flags:state-rejected]]', + class: 'secondary', + }], +]); Flags.init = async function () { // Query plugins for custom filter strategies and merge into core filter strategies @@ -78,14 +87,15 @@ Flags.init = async function () { } }, }, + states: Flags._states, helpers: { prepareSets: prepareSets, }, }; try { - const data = await plugins.hooks.fire('filter:flags.getFilters', hookData); - Flags._filters = data.filters; + ({ filters: Flags._filters } = await plugins.hooks.fire('filter:flags.getFilters', hookData)); + ({ filters: Flags._filters, states: Flags._states } = await plugins.hooks.fire('filter:flags.init', hookData)); } catch (err) { winston.error(`[flags/init] Could not retrieve filters\n${err.stack}`); Flags._filters = {}; @@ -197,7 +207,7 @@ Flags.list = async function (data) { heat: reportCounts[idx], ...flagObj, }; - flagObj.labelClass = Flags._constants.state_class[flagObj.state]; + flagObj.labelClass = Flags._states.get(flagObj.state).class; return Object.assign(flagObj, { 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]) { delete changeset[prop]; } else if (prop === 'state') { - if (!Flags._constants.states.includes(changeset[prop])) { + if (!Flags._states.has(changeset[prop])) { delete changeset[prop]; } else { tasks.push(db.sortedSetAdd(`flags:byState:${changeset[prop]}`, now, flagId)); diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js index 4174723a70..58f8c85f96 100644 --- a/src/plugins/hooks.js +++ b/src/plugins/hooks.js @@ -33,6 +33,11 @@ Hooks._deprecated = new Map([ since: 'v2.2.0', until: 'v3.0.0', }], + ['filter:flags.getFilters', { + new: 'filter:flags.init', + since: 'v2.7.0', + until: 'v3.0.0', + }], ]); Hooks.internals = {