forked from nhitchins/nodebb-plugin-slack
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibrary.js
executable file
·107 lines (91 loc) · 3.91 KB
/
library.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
(function(module) {
'use strict';
var User = require.main.require('./src/user');
var Topics = require.main.require('./src/topics');
var Categories = require.main.require('./src/categories');
var meta = require.main.require('./src/meta');
var nconf = require.main.require('nconf');
var async = require.main.require('async');
var SlackClient = require('slack-node');
var slack = null;
var constants = Object.freeze({
name : 'slack',
admin: {
icon : 'fa-slack',
route : '/plugins/slack',
label : 'Slack'
}
});
var Slack = {
config: {
'webhookURL': '',
'channel': '',
'post:maxlength': '',
'slack:categories': '',
'topicsOnly': ''
}
};
Slack.init = function(params, callback) {
function render(req, res, next) {
res.render('admin/plugins/slack', {});
}
params.router.get('/admin/plugins/slack', params.middleware.admin.buildHeader, render);
params.router.get('/api/admin/plugins/slack', render);
meta.settings.get('slack', function(err, settings) {
for(var prop in Slack.config) {
if (settings.hasOwnProperty(prop)) {
Slack.config[prop] = settings[prop];
}
}
slack = new SlackClient();
slack.setWebhook(Slack.config['webhookURL']);
});
callback();
},
Slack.postSave = function(post) {
post = post.post;
var topicsOnly = Slack.config['topicsOnly'] || 'off';
if (topicsOnly === 'off' || (topicsOnly === 'on' && post.isMain)) {
var content = post.content;
async.parallel({
user: function(callback) {
User.getUserFields(post.uid, ['username', 'picture'], callback);
},
topic: function(callback) {
Topics.getTopicFields(post.tid, ['title', 'slug'], callback);
},
category: function(callback) {
Categories.getCategoryFields(post.cid, ['name'], callback);
}
}, function(err, data) {
var categories = JSON.parse(Slack.config['slack:categories']);
if (!categories || categories.indexOf(String(post.cid)) >= 0) {
// trim message based on config option
var maxContentLength = Slack.config['post:maxlength'] || false;
if (maxContentLength && content.length > maxContentLength) { content = content.substring(0, maxContentLength) + '...'; }
// message format: <username> posted [<categoryname> : <topicname>]\n <message>
var message = '<' + nconf.get('url') + '/topic/' + data.topic.slug + '|[' + data.category.name + ': ' + data.topic.title + ']>\n' + content;
slack.webhook({
'text' : message,
'channel' : (Slack.config['channel'] || '#general'),
'username' : data.user.username,
'icon_url' : data.user.picture && data.user.picture.match(/^\/\//) ? 'http:' + data.user.picture : data.user.picture
}, function(err, response) {
if (err) {
console.log(err);
}
});
}
});
}
},
Slack.adminMenu = function(headers, callback) {
headers.plugins.push({
'route' : constants.admin.route,
'icon' : constants.admin.icon,
'name' : constants.admin.label
});
callback(null, headers);
}
module.exports = Slack;
}(module));