refactor: move plugin hook methods to plugin.hooks.*

This commit is contained in:
Julian Lam
2020-11-20 16:06:26 -05:00
parent 3b1c03ed50
commit 6e2da9966e
136 changed files with 550 additions and 541 deletions

View File

@@ -36,7 +36,7 @@ middleware.renderHeader = async (req, res, data) => {
const results = await utils.promiseParallel({
userData: user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed']),
scripts: getAdminScripts(),
custom_header: plugins.fireHook('filter:admin.header.build', custom_header),
custom_header: plugins.hooks.fire('filter:admin.header.build', custom_header),
configs: meta.configs.list(),
latestVersion: getLatestVersion(),
privileges: privileges.admin.get(req.uid),
@@ -83,7 +83,7 @@ middleware.renderHeader = async (req, res, data) => {
};
async function getAdminScripts() {
const scripts = await plugins.fireHook('filter:admin.scripts.get', []);
const scripts = await plugins.hooks.fire('filter:admin.scripts.get', []);
return scripts.map(function (script) {
return { src: script };
});

View File

@@ -34,7 +34,7 @@ middleware.buildHeader = helpers.try(async function buildHeader(req, res, next)
const [config, isBanned] = await Promise.all([
controllers.api.loadConfig(req),
user.bans.isBanned(req.uid),
plugins.fireHook('filter:middleware.buildHeader', { req: req, locals: res.locals }),
plugins.hooks.fire('filter:middleware.buildHeader', { req: req, locals: res.locals }),
]);
if (isBanned) {
@@ -60,7 +60,7 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
'brand:logo:alt': meta.config['brand:logo:alt'] || '',
'brand:logo:display': meta.config['brand:logo'] ? '' : 'hide',
allowRegistration: registrationType === 'normal',
searchEnabled: plugins.hasListeners('filter:search.query'),
searchEnabled: plugins.hooks.hasListeners('filter:search.query'),
config: res.locals.config,
relative_path,
bodyClass: data.bodyClass,
@@ -173,7 +173,7 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
modifyTitle(templateValues);
}
const hookReturn = await plugins.fireHook('filter:middleware.renderHeader', {
const hookReturn = await plugins.hooks.fire('filter:middleware.renderHeader', {
req: req,
res: res,
templateValues: templateValues,
@@ -184,13 +184,13 @@ middleware.renderHeader = async function renderHeader(req, res, data) {
};
middleware.renderFooter = async function renderFooter(req, res, templateValues) {
const data = await plugins.fireHook('filter:middleware.renderFooter', {
const data = await plugins.hooks.fire('filter:middleware.renderFooter', {
req: req,
res: res,
templateValues: templateValues,
});
const scripts = await plugins.fireHook('filter:scripts.get', []);
const scripts = await plugins.hooks.fire('filter:scripts.get', []);
data.templateValues.scripts = scripts.map(function (script) {
return { src: script };

View File

@@ -82,7 +82,7 @@ middleware.pageView = helpers.try(async function pageView(req, res, next) {
}
next();
await analytics.pageView({ ip: req.ip, uid: req.uid });
plugins.fireHook('action:middleware.pageView', { req: req });
plugins.hooks.fire('action:middleware.pageView', { req: req });
});
middleware.pluginHooks = helpers.try(async function pluginHooks(req, res, next) {
@@ -91,7 +91,7 @@ middleware.pluginHooks = helpers.try(async function pluginHooks(req, res, next)
hookObj.method(req, res, next);
});
await plugins.fireHook('response:router.page', {
await plugins.hooks.fire('response:router.page', {
req: req,
res: res,
});
@@ -227,7 +227,7 @@ middleware.trimUploadTimestamps = function trimUploadTimestamps(req, res, next)
middleware.validateAuth = helpers.try(async function validateAuth(req, res, next) {
try {
await plugins.fireHook('static:auth.validate', {
await plugins.hooks.fire('static:auth.validate', {
user: res.locals.user,
strategy: res.locals.strategy,
});

View File

@@ -33,10 +33,10 @@ module.exports = function (middleware) {
options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
options.bodyClass = buildBodyClass(req, res, options);
const buildResult = await plugins.fireHook('filter:' + template + '.build', { req: req, res: res, templateData: options });
const buildResult = await plugins.hooks.fire('filter:' + template + '.build', { req: req, res: res, templateData: options });
const templateToRender = buildResult.templateData.templateToRender || template;
const renderResult = await plugins.fireHook('filter:middleware.render', { req: req, res: res, templateData: buildResult.templateData });
const renderResult = await plugins.hooks.fire('filter:middleware.render', { req: req, res: res, templateData: buildResult.templateData });
options = renderResult.templateData;
options._header = {
tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),

View File

@@ -69,7 +69,7 @@ module.exports = function (middleware) {
}
}
await plugins.fireHook('response:middleware.authenticate', {
await plugins.hooks.fire('response:middleware.authenticate', {
req: req,
res: res,
next: function () {}, // no-op for backwards compatibility