2014-12-26 18:54:20 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-02-17 20:20:42 -07:00
|
|
|
var winston = require('winston');
|
|
|
|
|
var async = require('async');
|
2014-12-26 18:54:20 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Plugins) {
|
2016-01-07 15:41:51 -05:00
|
|
|
Plugins.deprecatedHooks = {
|
2018-06-03 14:54:25 -04:00
|
|
|
|
2016-01-07 15:41:51 -05:00
|
|
|
};
|
2018-04-05 14:35:49 -04:00
|
|
|
|
|
|
|
|
Plugins.internals = {
|
|
|
|
|
_register: function (data, callback) {
|
|
|
|
|
Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || [];
|
|
|
|
|
Plugins.loadedHooks[data.hook].push(data);
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-26 18:55:06 -05:00
|
|
|
/*
|
|
|
|
|
`data` is an object consisting of (* is required):
|
|
|
|
|
`data.hook`*, the name of the NodeBB hook
|
2018-04-05 14:35:49 -04:00
|
|
|
`data.method`*, the method called in that plugin (can be an array of functions)
|
2014-12-26 18:55:06 -05:00
|
|
|
`data.priority`, the relative priority of the method when it is eventually called (default: 10)
|
|
|
|
|
*/
|
2016-10-13 11:43:39 +02:00
|
|
|
Plugins.registerHook = function (id, data, callback) {
|
|
|
|
|
callback = callback || function () {};
|
2014-12-26 18:54:20 -05:00
|
|
|
|
2016-07-27 22:17:02 +03:00
|
|
|
if (!data.hook) {
|
2016-07-28 21:28:18 +03:00
|
|
|
winston.warn('[plugins/' + id + '] registerHook called with invalid data.hook', data);
|
|
|
|
|
return callback();
|
2016-07-27 22:17:02 +03:00
|
|
|
}
|
|
|
|
|
|
2014-12-26 18:54:20 -05:00
|
|
|
var method;
|
|
|
|
|
|
2018-10-20 14:40:48 -04:00
|
|
|
if (Object.keys(Plugins.deprecatedHooks).includes(data.hook)) {
|
2018-11-28 20:29:43 -07:00
|
|
|
winston.warn('[plugins/' + id + '] Hook `' + data.hook + '` is deprecated, ' +
|
|
|
|
|
(Plugins.deprecatedHooks[data.hook] ?
|
|
|
|
|
'please use `' + Plugins.deprecatedHooks[data.hook] + '` instead.' :
|
|
|
|
|
'there is no alternative.'
|
2017-10-01 16:19:10 -06:00
|
|
|
));
|
2015-08-25 17:48:18 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-26 18:54:20 -05:00
|
|
|
if (data.hook && data.method) {
|
|
|
|
|
data.id = id;
|
|
|
|
|
if (!data.priority) {
|
|
|
|
|
data.priority = 10;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 14:35:49 -04:00
|
|
|
if (Array.isArray(data.method) && data.method.every(method => typeof method === 'function' || typeof method === 'string')) {
|
|
|
|
|
// Go go gadget recursion!
|
|
|
|
|
async.eachSeries(data.method, function (method, next) {
|
|
|
|
|
const singularData = Object.assign({}, data, { method: method });
|
|
|
|
|
Plugins.registerHook(id, singularData, next);
|
|
|
|
|
}, callback);
|
|
|
|
|
} else if (typeof data.method === 'string' && data.method.length > 0) {
|
2016-10-13 11:43:39 +02:00
|
|
|
method = data.method.split('.').reduce(function (memo, prop) {
|
2015-01-06 23:29:48 -05:00
|
|
|
if (memo && memo[prop]) {
|
2014-12-26 18:54:20 -05:00
|
|
|
return memo[prop];
|
|
|
|
|
}
|
2017-10-01 16:19:10 -06:00
|
|
|
// Couldn't find method by path, aborting
|
2017-02-18 14:27:26 -07:00
|
|
|
return null;
|
2014-12-26 18:54:20 -05:00
|
|
|
}, Plugins.libraries[data.id]);
|
|
|
|
|
|
|
|
|
|
// Write the actual method reference to the hookObj
|
|
|
|
|
data.method = method;
|
|
|
|
|
|
2018-04-05 14:35:49 -04:00
|
|
|
Plugins.internals._register(data, callback);
|
2014-12-26 18:54:20 -05:00
|
|
|
} else if (typeof data.method === 'function') {
|
2018-04-05 14:35:49 -04:00
|
|
|
Plugins.internals._register(data, callback);
|
2014-12-26 18:54:20 -05:00
|
|
|
} else {
|
|
|
|
|
winston.warn('[plugins/' + id + '] Hook method mismatch: ' + data.hook + ' => ' + data.method);
|
2016-07-28 21:28:18 +03:00
|
|
|
return callback();
|
2014-12-26 18:54:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-28 16:42:10 +03:00
|
|
|
Plugins.unregisterHook = function (id, hook, method) {
|
|
|
|
|
var hooks = Plugins.loadedHooks[hook] || [];
|
|
|
|
|
Plugins.loadedHooks[hook] = hooks.filter(function (hookData) {
|
|
|
|
|
return hookData && hookData.id !== id && hookData.method !== method;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Plugins.fireHook = function (hook, params, callback) {
|
|
|
|
|
callback = typeof callback === 'function' ? callback : function () {};
|
2019-01-15 12:55:53 -05:00
|
|
|
function done(err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
if (hook !== 'action:plugins.firehook') {
|
|
|
|
|
Plugins.fireHook('action:plugins.firehook', { hook: hook, params: params });
|
|
|
|
|
}
|
2019-01-15 13:38:42 -05:00
|
|
|
if (result !== undefined) {
|
2019-01-15 12:55:53 -05:00
|
|
|
callback(null, result);
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-26 18:54:20 -05:00
|
|
|
var hookList = Plugins.loadedHooks[hook];
|
|
|
|
|
var hookType = hook.split(':')[0];
|
2019-02-21 18:27:29 -05:00
|
|
|
winston.verbose('[plugins/fireHook] ' + hook);
|
2018-02-15 14:52:49 -05:00
|
|
|
switch (hookType) {
|
|
|
|
|
case 'filter':
|
2019-01-15 12:55:53 -05:00
|
|
|
fireFilterHook(hook, hookList, params, done);
|
2018-02-15 14:52:49 -05:00
|
|
|
break;
|
|
|
|
|
case 'action':
|
2019-01-15 12:55:53 -05:00
|
|
|
fireActionHook(hook, hookList, params, done);
|
2018-02-15 14:52:49 -05:00
|
|
|
break;
|
|
|
|
|
case 'static':
|
2019-01-15 12:55:53 -05:00
|
|
|
fireStaticHook(hook, hookList, params, done);
|
2018-02-15 14:52:49 -05:00
|
|
|
break;
|
2019-01-19 14:49:22 -05:00
|
|
|
case 'response':
|
|
|
|
|
fireResponseHook(hook, hookList, params, done);
|
|
|
|
|
break;
|
2018-02-15 14:52:49 -05:00
|
|
|
default:
|
|
|
|
|
winston.warn('[plugins] Unknown hookType: ' + hookType + ', hook : ' + hook);
|
|
|
|
|
callback();
|
|
|
|
|
break;
|
2014-12-26 18:54:20 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function fireFilterHook(hook, hookList, params, callback) {
|
2015-04-28 15:09:17 -04:00
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return callback(null, params);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
async.reduce(hookList, params, function (params, hookObj, next) {
|
2014-12-26 18:54:20 -05:00
|
|
|
if (typeof hookObj.method !== 'function') {
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
|
}
|
|
|
|
|
return next(null, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hookObj.method(params, next);
|
2017-08-14 11:10:54 -04:00
|
|
|
}, callback);
|
2014-12-26 18:54:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fireActionHook(hook, hookList, params, callback) {
|
2015-04-28 15:09:17 -04:00
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2016-10-13 11:43:39 +02:00
|
|
|
async.each(hookList, function (hookObj, next) {
|
2014-12-26 18:54:20 -05:00
|
|
|
if (typeof hookObj.method !== 'function') {
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
|
}
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hookObj.method(params);
|
|
|
|
|
next();
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fireStaticHook(hook, hookList, params, callback) {
|
2015-04-28 15:09:17 -04:00
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2016-10-13 11:43:39 +02:00
|
|
|
async.each(hookList, function (hookObj, next) {
|
2014-12-26 18:54:20 -05:00
|
|
|
if (typeof hookObj.method === 'function') {
|
|
|
|
|
var timedOut = false;
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
var timeoutId = setTimeout(function () {
|
2014-12-26 18:54:20 -05:00
|
|
|
winston.warn('[plugins] Callback timed out, hook \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
|
|
|
|
|
timedOut = true;
|
|
|
|
|
next();
|
|
|
|
|
}, 5000);
|
|
|
|
|
|
2015-01-30 12:25:07 -05:00
|
|
|
try {
|
2016-10-13 11:43:39 +02:00
|
|
|
hookObj.method(params, function () {
|
2015-01-30 12:25:07 -05:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
if (!timedOut) {
|
|
|
|
|
next.apply(null, arguments);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-02-18 01:52:56 -07:00
|
|
|
} catch (err) {
|
2015-01-30 12:25:07 -05:00
|
|
|
winston.error('[plugins] Error executing \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
|
2015-02-06 14:16:33 -05:00
|
|
|
winston.error(err);
|
2014-12-26 18:54:20 -05:00
|
|
|
clearTimeout(timeoutId);
|
2015-01-30 12:25:07 -05:00
|
|
|
next();
|
|
|
|
|
}
|
2014-12-26 18:54:20 -05:00
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 14:49:22 -05:00
|
|
|
function fireResponseHook(hook, hookList, params, callback) {
|
|
|
|
|
if (!Array.isArray(hookList) || !hookList.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
async.eachSeries(hookList, function (hookObj, next) {
|
|
|
|
|
if (typeof hookObj.method !== 'function') {
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
|
}
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip remaining hooks if headers have been sent
|
|
|
|
|
if (params.res.headersSent) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hookObj.method(params);
|
|
|
|
|
next();
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Plugins.hasListeners = function (hook) {
|
2014-12-26 18:54:20 -05:00
|
|
|
return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0);
|
|
|
|
|
};
|
2016-06-01 12:27:36 -04:00
|
|
|
};
|