-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (57 loc) · 1.46 KB
/
index.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
/**
* Module dependencies
*/
var _ = require('lodash');
var path = require('path');
/**
* mailin.io Hook
*
* Integration with the mailin API.
*
* For a full list of available mailin options see:
* http://mailin.io/doc
*
* @param {App} sails
* @return {Object}
* @hook
*/
module.exports = function Mailin (sails) {
var self;
var mailin;
// @see https://github.com/Flolagale/mailin#events
var MAILIN_EVENTS = ["startData", "data", "dataReady", "authorizeUser", "validateSender", "senderValidationFailed", "validateRecipient", "recipientValidationFailed", "close", "startMessage", "message"];
return {
/**
* Default configuration
* @type {Object}
*/
defaults: {
__configKey__: {
enable: false,
handlerService: 'MailinService',
disableWebhook: true
}
},
/**
* @param {Function} cb
*/
initialize: function (cb) {
self = this;
if(!sails.config[self.configKey].enable) return cb();
var hServiceName = sails.config[self.configKey].handlerService;
var handler = _.find(_.values(sails.services), {
'identity': hServiceName.toLowerCase()
});
if(!handler) {
sails.log.warn("Mailin hook disabled - unable to locate handler in sails services: " + hServiceName);
return cb();
}
mailin = require('mailin');
mailin.start(sails.config[self.configKey]);
_.each(MAILIN_EVENTS, function(event) {
if(_.isFunction(handler[event])) mailin.on(event, handler[event]);
});
cb();
}
};
};