|
|
|
@ -3,42 +3,45 @@
|
|
|
|
|
const winston = require('winston');
|
|
|
|
|
const async = require('async');
|
|
|
|
|
const utils = require('../utils');
|
|
|
|
|
const plugins = require('.');
|
|
|
|
|
|
|
|
|
|
module.exports = function (Plugins) {
|
|
|
|
|
Plugins.deprecatedHooks = {
|
|
|
|
|
const Hooks = {};
|
|
|
|
|
module.exports = Hooks;
|
|
|
|
|
|
|
|
|
|
Hooks.deprecatedHooks = {
|
|
|
|
|
'filter:privileges:isUserAllowedTo': 'filter:privileges:isAllowedTo', // 👋 @ 1.16.0
|
|
|
|
|
'filter:router.page': 'response:router.page', // 👋 @ 2.0.0
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.internals = {
|
|
|
|
|
Hooks.internals = {
|
|
|
|
|
_register: function (data) {
|
|
|
|
|
Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || [];
|
|
|
|
|
Plugins.loadedHooks[data.hook].push(data);
|
|
|
|
|
plugins.loadedHooks[data.hook] = plugins.loadedHooks[data.hook] || [];
|
|
|
|
|
plugins.loadedHooks[data.hook].push(data);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hookTypeToMethod = {
|
|
|
|
|
const hookTypeToMethod = {
|
|
|
|
|
filter: fireFilterHook,
|
|
|
|
|
action: fireActionHook,
|
|
|
|
|
static: fireStaticHook,
|
|
|
|
|
response: fireResponseHook,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
/*
|
|
|
|
|
`data` is an object consisting of (* is required):
|
|
|
|
|
`data.hook`*, the name of the NodeBB hook
|
|
|
|
|
`data.method`*, the method called in that plugin (can be an array of functions)
|
|
|
|
|
`data.priority`, the relative priority of the method when it is eventually called (default: 10)
|
|
|
|
|
*/
|
|
|
|
|
Plugins.registerHook = function (id, data) {
|
|
|
|
|
*/
|
|
|
|
|
Hooks.register = function (id, data) {
|
|
|
|
|
if (!data.hook || !data.method) {
|
|
|
|
|
winston.warn('[plugins/' + id + '] registerHook called with invalid data.hook/method', data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// `hasOwnProperty` needed for hooks with no alternative (set to null)
|
|
|
|
|
if (Plugins.deprecatedHooks.hasOwnProperty(data.hook)) {
|
|
|
|
|
const deprecated = Plugins.deprecatedHooks[data.hook];
|
|
|
|
|
if (Hooks.deprecatedHooks.hasOwnProperty(data.hook)) {
|
|
|
|
|
const deprecated = Hooks.deprecatedHooks[data.hook];
|
|
|
|
|
|
|
|
|
|
if (deprecated) {
|
|
|
|
|
winston.warn(`[plugins/${id}] Hook "${data.hook}" is deprecated, please use "${deprecated}" instead.`);
|
|
|
|
@ -56,7 +59,7 @@ module.exports = function (Plugins) {
|
|
|
|
|
// Go go gadget recursion!
|
|
|
|
|
data.method.forEach(function (method) {
|
|
|
|
|
const singularData = { ...data, method: method };
|
|
|
|
|
Plugins.registerHook(id, singularData);
|
|
|
|
|
Hooks.register(id, singularData);
|
|
|
|
|
});
|
|
|
|
|
} else if (typeof data.method === 'string' && data.method.length > 0) {
|
|
|
|
|
const method = data.method.split('.').reduce(function (memo, prop) {
|
|
|
|
@ -65,30 +68,30 @@ module.exports = function (Plugins) {
|
|
|
|
|
}
|
|
|
|
|
// Couldn't find method by path, aborting
|
|
|
|
|
return null;
|
|
|
|
|
}, Plugins.libraries[data.id]);
|
|
|
|
|
}, plugins.libraries[data.id]);
|
|
|
|
|
|
|
|
|
|
// Write the actual method reference to the hookObj
|
|
|
|
|
data.method = method;
|
|
|
|
|
|
|
|
|
|
Plugins.internals._register(data);
|
|
|
|
|
Hooks.internals._register(data);
|
|
|
|
|
} else if (typeof data.method === 'function') {
|
|
|
|
|
Plugins.internals._register(data);
|
|
|
|
|
Hooks.internals._register(data);
|
|
|
|
|
} else {
|
|
|
|
|
winston.warn('[plugins/' + id + '] Hook method mismatch: ' + data.hook + ' => ' + data.method);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.unregisterHook = function (id, hook, method) {
|
|
|
|
|
var hooks = Plugins.loadedHooks[hook] || [];
|
|
|
|
|
Plugins.loadedHooks[hook] = hooks.filter(function (hookData) {
|
|
|
|
|
Hooks.unregister = function (id, hook, method) {
|
|
|
|
|
var hooks = plugins.loadedHooks[hook] || [];
|
|
|
|
|
plugins.loadedHooks[hook] = hooks.filter(function (hookData) {
|
|
|
|
|
return hookData && hookData.id !== id && hookData.method !== method;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.fireHook = async function (hook, params) {
|
|
|
|
|
const hookList = Plugins.loadedHooks[hook];
|
|
|
|
|
Hooks.fire = async function (hook, params) {
|
|
|
|
|
const hookList = plugins.loadedHooks[hook];
|
|
|
|
|
const hookType = hook.split(':')[0];
|
|
|
|
|
if (global.env === 'development' && hook !== 'action:plugins.firehook') {
|
|
|
|
|
if (global.env === 'development' && hook !== 'action:plugins.fireHook') {
|
|
|
|
|
winston.verbose('[plugins/fireHook] ' + hook);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -98,15 +101,19 @@ module.exports = function (Plugins) {
|
|
|
|
|
}
|
|
|
|
|
const result = await hookTypeToMethod[hookType](hook, hookList, params);
|
|
|
|
|
|
|
|
|
|
if (hook !== 'action:plugins.firehook') {
|
|
|
|
|
Plugins.fireHook('action:plugins.firehook', { hook: hook, params: params });
|
|
|
|
|
if (hook !== 'action:plugins.fireHook') {
|
|
|
|
|
Hooks.fire('action:plugins.fireHook', { hook: hook, params: params });
|
|
|
|
|
}
|
|
|
|
|
if (result !== undefined) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Hooks.hasListeners = function (hook) {
|
|
|
|
|
return !!(plugins.loadedHooks[hook] && plugins.loadedHooks[hook].length > 0);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function fireFilterHook(hook, hookList, params) {
|
|
|
|
|
async function fireFilterHook(hook, hookList, params) {
|
|
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
@ -126,9 +133,9 @@ module.exports = function (Plugins) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fireActionHook(hook, hookList, params) {
|
|
|
|
|
async function fireActionHook(hook, hookList, params) {
|
|
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -142,9 +149,9 @@ module.exports = function (Plugins) {
|
|
|
|
|
await hookObj.method(params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fireStaticHook(hook, hookList, params) {
|
|
|
|
|
async function fireStaticHook(hook, hookList, params) {
|
|
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -184,9 +191,9 @@ module.exports = function (Plugins) {
|
|
|
|
|
callback(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fireResponseHook(hook, hookList, params) {
|
|
|
|
|
async function fireResponseHook(hook, hookList, params) {
|
|
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -205,9 +212,4 @@ module.exports = function (Plugins) {
|
|
|
|
|
|
|
|
|
|
await hookObj.method(params);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plugins.hasListeners = function (hook) {
|
|
|
|
|
return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|