From be8e63cd5029c6b41aa1c1938e53524abc44f819 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 21 Jul 2014 10:35:57 -0400 Subject: [PATCH 1/6] special handling for filter:app.load --- src/plugins.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/plugins.js b/src/plugins.js index 1b6810bfbf..fa1e8429ce 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -319,9 +319,25 @@ var fs = require('fs'), var hookType = hook.split(':')[0]; switch (hookType) { case 'filter': + if (hook === 'filter:app.load') { + // Special case for this hook, as arguments passed in are always the same + async.each(hookList, function(hookObj, next) { + if (hookObj.method) { + hookObj.method.apply(Plugins, args.concat(next)); + } + }, function(err) { + callback(err); + }); + + return; + } + async.reduce(hookList, args, function(value, hookObj, next) { if (hookObj.method) { if (!hookObj.hasOwnProperty('callbacked') || hookObj.callbacked === true) { + // omg, after 6 months I finally realised what this does... + // It adds the callback to the arguments passed-in, since the callback + // is defined in *this* file (the async cb), and not the hooks themselves. var value = hookObj.method.apply(Plugins, value.concat(function() { next(arguments[0], Array.prototype.slice.call(arguments, 1)); })); @@ -353,7 +369,9 @@ var fs = require('fs'), } } - callback.apply(Plugins, [err].concat(values)); + if (callback) { + callback.apply(Plugins, [err].concat(values)); + } }); break; case 'action': From bee4feff127f259db39a6d525109ed67a7f97ade Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 21 Jul 2014 12:35:48 -0400 Subject: [PATCH 2/6] updated module deps for widget-essentials and markdown --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 96faea78b1..fe6db38fb4 100644 --- a/package.json +++ b/package.json @@ -35,12 +35,12 @@ "mkdirp": "~0.3.5", "nconf": "~0.6.7", "nodebb-plugin-dbsearch": "0.0.9", - "nodebb-plugin-markdown": "~0.4.2", + "nodebb-plugin-markdown": "~0.5.0", "nodebb-plugin-mentions": "~0.5.0", "nodebb-plugin-soundpack-default": "~0.1.1", "nodebb-theme-lavender": "~0.0.26", "nodebb-theme-vanilla": "~0.0.21", - "nodebb-widget-essentials": "~0.0.21", + "nodebb-widget-essentials": "~0.1.0", "npm": "^1.4.6", "passport": "~0.2.0", "passport-local": "0.1.6", From bab995626c387256748a4d596250b13fd6168351 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 21 Jul 2014 13:10:45 -0400 Subject: [PATCH 3/6] some unused vars --- src/user.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/user.js b/src/user.js index 60b7fe9394..3c62a52d21 100644 --- a/src/user.js +++ b/src/user.js @@ -3,19 +3,13 @@ var bcrypt = require('bcryptjs'), async = require('async'), nconf = require('nconf'), - winston = require('winston'), gravatar = require('gravatar'), - S = require('string'), - utils = require('./../public/src/utils'), plugins = require('./plugins'), db = require('./database'), meta = require('./meta'), groups = require('./groups'), - topics = require('./topics'), - events = require('./events'), - emitter = require('./emitter'), - Emailer = require('./emailer'); + emitter = require('./emitter'); (function(User) { From 7e82393d9f1ceaa05b111f704de8ae908fbc9cc0 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 21 Jul 2014 13:14:12 -0400 Subject: [PATCH 4/6] plugins:loaded - using emitter class instead --- src/plugins.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index fa1e8429ce..a70d762734 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -5,10 +5,10 @@ var fs = require('fs'), async = require('async'), winston = require('winston'), nconf = require('nconf'), - eventEmitter = require('events').EventEmitter, semver = require('semver'), db = require('./database'), + emitter = require('./emitter'), meta = require('./meta'), utils = require('../public/src/utils'), pkg = require('../package.json'); @@ -24,9 +24,6 @@ var fs = require('fs'), Plugins.initialized = false; - // Events - Plugins.readyEvent = new eventEmitter(); - Plugins.init = function() { if (Plugins.initialized) { return; @@ -47,14 +44,15 @@ var fs = require('fs'), if (global.env === 'development') { winston.info('[plugins] Plugins OK'); } + Plugins.initialized = true; - Plugins.readyEvent.emit('ready'); + emitter.emit('plugins:loaded'); }); }; Plugins.ready = function(callback) { if (!Plugins.initialized) { - Plugins.readyEvent.once('ready', callback); + emitter.once('plugins:loaded', callback); } else { callback(); } From e10307426d7a30a696c5adaa4ee8b9fe7f9acde1 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 21 Jul 2014 13:25:25 -0400 Subject: [PATCH 5/6] optional callback for registerHook --- src/plugins.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index a70d762734..11e22669c7 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -44,7 +44,7 @@ var fs = require('fs'), if (global.env === 'development') { winston.info('[plugins] Plugins OK'); } - + Plugins.initialized = true; emitter.emit('plugins:loaded'); }); @@ -294,7 +294,9 @@ var fs = require('fs'), Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || []; Plugins.loadedHooks[data.hook].push(data); - callback(); + if (typeof callback === 'function') { + callback(); + } } }; From 423da904dee04512ff173ab887f932d2e497b807 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 21 Jul 2014 13:42:33 -0400 Subject: [PATCH 6/6] allow core to tap into NodeBB's hook system, closes #989 --- src/plugins.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index 11e22669c7..9e62ee9e86 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -269,28 +269,34 @@ var fs = require('fs'), var method; - if (data.hook && data.method && typeof data.method === 'string' && data.method.length > 0) { + if (data.hook && data.method) { data.id = id; if (!data.priority) { data.priority = 10; } - method = data.method.split('.').reduce(function(memo, prop) { - if (memo !== null && memo[prop]) { - return memo[prop]; - } else { - // Couldn't find method by path, aborting - return null; - } - }, Plugins.libraries[data.id]); - if (method === null) { + if (typeof data.method === 'string' && data.method.length > 0) { + method = data.method.split('.').reduce(function(memo, prop) { + if (memo !== null && memo[prop]) { + return memo[prop]; + } else { + // Couldn't find method by path, aborting + return null; + } + }, Plugins.libraries[data.id]); + + // Write the actual method reference to the hookObj + data.method = method; + + register(); + } else if (typeof data.method === 'function') { + register(); + } else { winston.warn('[plugins/' + id + '] Hook method mismatch: ' + data.hook + ' => ' + data.method); - return callback(); } + } - // Write the actual method reference to the hookObj - data.method = method; - + function register() { Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || []; Plugins.loadedHooks[data.hook].push(data); @@ -680,4 +686,5 @@ var fs = require('fs'), callback(err, plugins); }); }; + }(exports));