forked from pvnick/nodebb-plugin-post-notification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (83 loc) · 3.48 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var winston = module.parent.require('winston'),
Meta = module.parent.require('./meta'),
User = module.parent.require('./user'),
Plugins = module.parent.require('./plugins'),
Topics = module.parent.require('./topics'),
PostNotification = {};
PostNotification.init = function(args,callback) {
var app = args.router,
middleware = args.middleware,
controllers = args.controllers;
function renderAdminPage(req, res, next) {
res.render('admin/post-notification/config', {});
}
app.get('/admin/post-notification/config', middleware.admin.buildHeader,[], renderAdminPage);
app.get('/api/admin/post-notification/config', renderAdminPage);
callback();
};
PostNotification.admin = {
menu: function(custom_header, callback) {
custom_header.plugins.push({
"route": '/post-notification/config',
"icon": 'fa-envelope-o',
"name": 'Post Notification'
});
callback(null, custom_header);
}
};
PostNotification.postSaved = function(postData) {
function getEmails(commaSeparatedList) {
if (typeof(commaSeparatedList) != "string")
return null;
var emailListSplit = commaSeparatedList.split(","),
emails = [];
validEmailRE = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
for (var i = 0; i != emailListSplit.length; ++i) {
var email = emailListSplit[i].trim();
if ( ! validEmailRE.test(email))
return null;
emails.push(email);
}
return emails;
}
var userID = postData.uid,
content = postData.content,
topicID = postData.tid,
urlPrefix = Meta.config['postnotification:urlPrefix'],
recipientList = Meta.config['postnotification:emails'],
recipients = getEmails(recipientList);
winston.log("[PostNotification] post saved");
if ( ! recipients || recipients.length == 0) {
winston.warn('[PostNotification] No recipients configured or invalid recipient list!');
return;
}
if ( ! Plugins.hasListeners('action:email.send')) {
winston.warn('[PostNotification] No active email plugin found!');
return;
}
User.getUserData(userID, function(err, userData) {
if (err) {
winston.error('[PostNotification] Could not look up user data!');
return;
}
var username = userData.username;
Topics.getTopicField(topicID, 'slug', function(err, slug) {
if (err || ! slug || slug === topicID + '/') {
winston.error('[PostNotification] Could not get topic slug!');
return;
}
for (var i = 0; i != recipients.length; ++i) {
var recipient = recipients[i];
Plugins.fireHook('action:email.send', {
to: recipient,
from: Meta.config['email:from'] || 'no-reply@localhost.lan',
subject: "[Forum] Post saved",
html: '<p><a href="' + urlPrefix + '/topic/' + encodeURI(slug) + '">A post has been made or edited by <b>' + username + '</a>:</p>\n\n<p>' + content + '</p>',
plaintext: 'A post has been made or edited by <b>' + username + ' (' + urlPrefix + '/topic/' + encodeURI(slug) + '):\n\n' + content,
template: "post-notification"
});
}
});
});
}
module.exports = PostNotification;