-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
hooks.js
172 lines (148 loc) · 4.59 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
var winston = require('winston'),
async = require('async');
module.exports = function(Plugins) {
Plugins.deprecatedHooks = {
'filter:user.custom_fields': null // remove in v1.1.0
};
/*
`data` is an object consisting of (* is required):
`data.hook`*, the name of the NodeBB hook
`data.method`*, the method called in that plugin
`data.priority`, the relative priority of the method when it is eventually called (default: 10)
*/
Plugins.registerHook = function(id, data, callback) {
function register() {
Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || [];
Plugins.loadedHooks[data.hook].push(data);
if (typeof callback === 'function') {
callback();
}
}
var method;
if (Object.keys(Plugins.deprecatedHooks).indexOf(data.hook) !== -1) {
winston.warn('[plugins/' + id + '] Hook `' + data.hook + '` is deprecated, ' +
(Plugins.deprecatedHooks[data.hook] ?
'please use `' + Plugins.deprecatedHooks[data.hook] + '` instead.' :
'there is no alternative.'
)
);
} else {
// handle hook's startsWith, i.e. action:homepage.get
var parts = data.hook.split(':');
if (parts.length > 2) {
parts.pop();
}
var hook = parts.join(':');
}
if (data.hook && data.method) {
data.id = id;
if (!data.priority) {
data.priority = 10;
}
if (typeof data.method === 'string' && data.method.length > 0) {
method = data.method.split('.').reduce(function(memo, prop) {
if (memo && 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);
}
}
};
Plugins.fireHook = function(hook, params, callback) {
callback = typeof callback === 'function' ? callback : function() {};
var hookList = Plugins.loadedHooks[hook];
var hookType = hook.split(':')[0];
switch (hookType) {
case 'filter':
fireFilterHook(hook, hookList, params, callback);
break;
case 'action':
fireActionHook(hook, hookList, params, callback);
break;
case 'static':
fireStaticHook(hook, hookList, params, callback);
break;
default:
winston.warn('[plugins] Unknown hookType: ' + hookType + ', hook : ' + hook);
break;
}
};
function fireFilterHook(hook, hookList, params, callback) {
if (!Array.isArray(hookList) || !hookList.length) {
return callback(null, params);
}
async.reduce(hookList, params, function(params, 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(null, params);
}
hookObj.method(params, next);
}, function(err, values) {
if (err) {
winston.error('[plugins] ' + hook + ', ' + err.message);
}
callback(err, values);
});
}
function fireActionHook(hook, hookList, params, callback) {
if (!Array.isArray(hookList) || !hookList.length) {
return callback();
}
async.each(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();
}
hookObj.method(params);
next();
}, callback);
}
function fireStaticHook(hook, hookList, params, callback) {
if (!Array.isArray(hookList) || !hookList.length) {
return callback();
}
async.each(hookList, function(hookObj, next) {
if (typeof hookObj.method === 'function') {
var timedOut = false;
var timeoutId = setTimeout(function() {
winston.warn('[plugins] Callback timed out, hook \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
timedOut = true;
next();
}, 5000);
try {
hookObj.method(params, function() {
clearTimeout(timeoutId);
if (!timedOut) {
next.apply(null, arguments);
}
});
} catch(err) {
winston.error('[plugins] Error executing \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
winston.error(err);
clearTimeout(timeoutId);
next();
}
} else {
next();
}
}, callback);
}
Plugins.hasListeners = function(hook) {
return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0);
};
};