-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlibrary.js
executable file
·127 lines (111 loc) · 4.38 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(function(module) {
'use strict';
var User = module.parent.require('./user'),
Topics = module.parent.require('./topics'),
Categories = module.parent.require('./categories'),
meta = module.parent.require('./meta'),
db = module.parent.require('../src/database'),
fs = module.parent.require('fs'),
path = module.parent.require('path'),
nconf = module.parent.require('nconf'),
winston = module.parent.require('winston'),
async = module.parent.require('async'),
SlackClient = require('node-slack'),
slack = null,
constants = Object.freeze({
name : 'slack',
admin: {
icon : 'fa-slack',
route : '/plugins/slack',
label : 'Slack'
}
});
var Slack = {
config: {
domain: '',
token: '',
channel: '',
'post:maxlength': ''
}
};
Slack.init = function(app, middleware, controllers, callback) {
function render(req, res, next) {
res.render('admin/plugins/slack', {});
}
var router;
if(app.router) {
callback = middleware;
controllers = app.controllers;
middleware = app.middleware;
router = app.router;
} else {
router = app;
}
router.get('/admin/plugins/slack', middleware.admin.buildHeader, render);
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.config['domain'], Slack.config['token'])
console.log('init', Slack.config);
});
callback();
},
Slack.postSave = function(post) {
var content = post.content;
async.parallel({
user: function(callback) {
// retrieve user
User.getUserData(post.uid, function(err, user) {
if (err) { return callback(err); }
var data = {
username : user.username,
email : user.email,
image : user.picture.match(/^\/\//) ? 'http:' + user.picture : user.picture
};
return callback(null, data);
})
},
topic: function(callback) {
// retrieve topic and category
Topics.getTopicData(post.tid, function(err, topic) {
if (err) { return callback(err); }
Categories.getCategoryData(topic.cid, function(err, category) {
if (err) { return callback(err); }
var data = {
title : topic.title,
category : category.name,
link : nconf.get('url') + '/topic/' + topic.slug,
timestamp : topic.timestamp
};
return callback(null, data);
})
})
}
}, function(err, data) {
// 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 = '<' + data.topic.link + '|[' + data.topic.category + ': ' + data.topic.title + ']>\n' + content;
slack.send({
'text' : message,
'channel' : (Slack.config['channel'] || '#general'),
'username' : data.user.username,
'icon_url' : data.user.image
});
});
},
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));