| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-10 19:05:27 +03:00
										 |  |  | const winston = require('winston'); | 
					
						
							|  |  |  | const async = require('async'); | 
					
						
							|  |  |  | const utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											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 = { | 
					
						
							| 
									
										
										
										
											2020-07-24 10:39:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 15:41:51 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2018-04-05 14:35:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Plugins.internals = { | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		_register: function (data) { | 
					
						
							| 
									
										
										
										
											2018-04-05 14:35:49 -04:00
										 |  |  | 			Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || []; | 
					
						
							|  |  |  | 			Plugins.loadedHooks[data.hook].push(data); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 	const hookTypeToMethod = { | 
					
						
							|  |  |  | 		filter: fireFilterHook, | 
					
						
							|  |  |  | 		action: fireActionHook, | 
					
						
							|  |  |  | 		static: fireStaticHook, | 
					
						
							|  |  |  | 		response: fireResponseHook, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 	Plugins.registerHook = function (id, data) { | 
					
						
							|  |  |  | 		if (!data.hook || !data.method) { | 
					
						
							|  |  |  | 			winston.warn('[plugins/' + id + '] registerHook called with invalid data.hook/method', data); | 
					
						
							|  |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2016-07-27 22:17:02 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 12:48:26 -04:00
										 |  |  | 		if (Plugins.deprecatedHooks[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
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		data.id = id; | 
					
						
							|  |  |  | 		if (!data.priority) { | 
					
						
							|  |  |  | 			data.priority = 10; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		if (Array.isArray(data.method) && data.method.every(method => typeof method === 'function' || typeof method === 'string')) { | 
					
						
							|  |  |  | 			// Go go gadget recursion!
 | 
					
						
							|  |  |  | 			data.method.forEach(function (method) { | 
					
						
							| 
									
										
										
										
											2019-08-13 14:33:37 -04:00
										 |  |  | 				const singularData = { ...data, method: method }; | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 				Plugins.registerHook(id, singularData); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else if (typeof data.method === 'string' && data.method.length > 0) { | 
					
						
							|  |  |  | 			const method = data.method.split('.').reduce(function (memo, prop) { | 
					
						
							|  |  |  | 				if (memo && memo[prop]) { | 
					
						
							|  |  |  | 					return memo[prop]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// Couldn't find method by path, aborting
 | 
					
						
							|  |  |  | 				return null; | 
					
						
							|  |  |  | 			}, Plugins.libraries[data.id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Write the actual method reference to the hookObj
 | 
					
						
							|  |  |  | 			data.method = method; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Plugins.internals._register(data); | 
					
						
							|  |  |  | 		} else if (typeof data.method === 'function') { | 
					
						
							|  |  |  | 			Plugins.internals._register(data); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			winston.warn('[plugins/' + id + '] Hook method mismatch: ' + data.hook + ' => ' + data.method); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 	Plugins.fireHook = async function (hook, params) { | 
					
						
							|  |  |  | 		const hookList = Plugins.loadedHooks[hook]; | 
					
						
							|  |  |  | 		const hookType = hook.split(':')[0]; | 
					
						
							| 
									
										
										
										
											2019-07-04 11:30:21 -04:00
										 |  |  | 		if (hook !== 'action:plugins.firehook') { | 
					
						
							| 
									
										
										
										
											2019-06-17 17:06:47 -04:00
										 |  |  | 			winston.verbose('[plugins/fireHook] ' + hook); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!hookTypeToMethod[hookType]) { | 
					
						
							| 
									
										
										
										
											2018-02-15 14:52:49 -05:00
										 |  |  | 			winston.warn('[plugins] Unknown hookType: ' + hookType + ', hook : ' + hook); | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const result = await hookTypeToMethod[hookType](hook, hookList, params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (hook !== 'action:plugins.firehook') { | 
					
						
							|  |  |  | 			Plugins.fireHook('action:plugins.firehook', { hook: hook, params: params }); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (result !== undefined) { | 
					
						
							|  |  |  | 			return result; | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 	async function fireFilterHook(hook, hookList, params) { | 
					
						
							| 
									
										
										
										
											2015-04-28 15:09:17 -04:00
										 |  |  | 		if (!Array.isArray(hookList) || !hookList.length) { | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 			return params; | 
					
						
							| 
									
										
										
										
											2015-04-28 15:09:17 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		return await 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); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-06-10 19:05:27 +03:00
										 |  |  | 			const returned = hookObj.method(params, next); | 
					
						
							|  |  |  | 			if (utils.isPromise(returned)) { | 
					
						
							|  |  |  | 				returned.then( | 
					
						
							|  |  |  | 					payload => setImmediate(next, null, payload), | 
					
						
							|  |  |  | 					err => setImmediate(next, err) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 	async function fireActionHook(hook, hookList, params) { | 
					
						
							| 
									
										
										
										
											2015-04-28 15:09:17 -04:00
										 |  |  | 		if (!Array.isArray(hookList) || !hookList.length) { | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2015-04-28 15:09:17 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-10-10 22:03:41 -04:00
										 |  |  | 		for (const hookObj of hookList) { | 
					
						
							| 
									
										
										
										
											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.'); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-10 22:03:41 -04:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				/* eslint-disable no-await-in-loop */ | 
					
						
							|  |  |  | 				await hookObj.method(params); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-10 22:03:41 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 	async function fireStaticHook(hook, hookList, params) { | 
					
						
							| 
									
										
										
										
											2015-04-28 15:09:17 -04:00
										 |  |  | 		if (!Array.isArray(hookList) || !hookList.length) { | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2015-04-28 15:09:17 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-01-17 13:30:57 -05:00
										 |  |  | 		// don't bubble errors from these hooks, so bad plugins don't stop startup
 | 
					
						
							|  |  |  | 		const noErrorHooks = ['static:app.load', 'static:assets.prepare', 'static:app.preload']; | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		await async.each(hookList, function (hookObj, next) { | 
					
						
							| 
									
										
										
										
											2020-01-17 13:30:57 -05:00
										 |  |  | 			if (typeof hookObj.method !== 'function') { | 
					
						
							|  |  |  | 				return next(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			let timedOut = false; | 
					
						
							|  |  |  | 			const timeoutId = setTimeout(function () { | 
					
						
							|  |  |  | 				winston.warn('[plugins] Callback timed out, hook \'' + hook + '\' in plugin \'' + hookObj.id + '\''); | 
					
						
							|  |  |  | 				timedOut = true; | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			}, 5000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const callback = (err) => { | 
					
						
							|  |  |  | 				clearTimeout(timeoutId); | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2015-01-30 12:25:07 -05:00
										 |  |  | 					winston.error('[plugins] Error executing \'' + hook + '\' in plugin \'' + hookObj.id + '\''); | 
					
						
							| 
									
										
										
										
											2020-01-17 13:30:57 -05:00
										 |  |  | 					winston.error(err.stack); | 
					
						
							| 
									
										
										
										
											2015-01-30 12:25:07 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-01-17 13:30:57 -05:00
										 |  |  | 				if (!timedOut) { | 
					
						
							|  |  |  | 					next(noErrorHooks.includes(hook) ? null : err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				const returned = hookObj.method(params, callback); | 
					
						
							|  |  |  | 				if (utils.isPromise(returned)) { | 
					
						
							|  |  |  | 					returned.then( | 
					
						
							|  |  |  | 						payload => setImmediate(callback, null, payload), | 
					
						
							|  |  |  | 						err => setImmediate(callback, err) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} catch (err) { | 
					
						
							|  |  |  | 				callback(err); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-12-26 18:54:20 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 	async function fireResponseHook(hook, hookList, params) { | 
					
						
							| 
									
										
										
										
											2019-01-19 14:49:22 -05:00
										 |  |  | 		if (!Array.isArray(hookList) || !hookList.length) { | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2019-01-19 14:49:22 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		await async.eachSeries(hookList, function (hookObj, next) { | 
					
						
							| 
									
										
										
										
											2019-01-19 14:49:22 -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(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Skip remaining hooks if headers have been sent
 | 
					
						
							|  |  |  | 			if (params.res.headersSent) { | 
					
						
							|  |  |  | 				return next(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			hookObj.method(params); | 
					
						
							|  |  |  | 			next(); | 
					
						
							| 
									
										
										
										
											2019-07-22 00:30:47 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-01-19 14:49:22 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }; |