-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (93 loc) · 3.37 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
92
93
94
95
96
97
98
99
var jwt = require('jsonwebtoken');
var _ = require('lodash');
var bodyParser = require('body-parser');
console.log('coucou')
module.exports = function chatupBlacklist(options) {
if (!options) {options = {}}
var redisSetName = options.redisSetName || 'chatup:blacklist:words';
var redisMutedSetName = options.redisMutedSetName || 'chatup:blacklist:muted';
var redisUpdateInterval = options.redisUpdateInterval || 2000;
var mutedWordsReplacement = options.mutedWordsReplacement || ' *** ';
var blacklistedWords = [];
var blacklistedRegex;
var mutedWords = [];
var mutedRegex;
var redisConnection = null;
var updaterTimer = null;
var updateWordsPromise = null;
var lastUpdated = -1;
function updateWords() {
updateWordsPromise = new Promise((resolve, reject) => {
redisConnection.multi()
.smembers(redisSetName)
.smembers(redisMutedSetName)
.exec(function(err, results) {
lastUpdated = Date.now();
blacklistedWords = results[0] || [];
blacklistedRegex = new RegExp(`(?:^|\\s)(${blacklistedWords.join('|').replace('.', '\\.')})(?:$|\\s)`, 'g')
mutedWords = results[1] || [];
mutedRegex = new RegExp(`(?:^|\\s)(${mutedWords.join('|').replace('.', '\\.')})(?:$|\\s)`, 'g')
resolve();
})
});
}
var chatupBlacklistMiddleware = function(ctx, next) {
redisConnection = ctx.redisConnection;
if (updaterTimer === null) {
updaterTimer = setInterval(updateWords, redisUpdateInterval);
updateWords();
}
var checkWords = function() {
if (blacklistedWords.length && ctx.msg.msg.match(blacklistedRegex) !== null) {
return next({status: 'error', err: 'blacklistedWord'});
}
if(mutedWords.length) {
ctx.msg.msg = ctx.msg.msg.replace(mutedRegex, mutedWordsReplacement)
}
return next();
}
if (lastUpdated === -1) {updateWordsPromise.then(checkWords)}
else {checkWords()}
}
var dispatcherPlugin = function(parent) {
parent._app.post('/plugin/blacklist', bodyParser.text(), function (req, res) {
if (!_.isString(req.body)) {
return res.status(400).send({ status: 'error', err: 'No body' });
}
jwt.verify(req.body,
parent._conf.jwt.key,
parent._conf.jwt.options,
function (err, decoded) {
if (err) {
return res.status(401).send({ status: 'error', err: 'Wrong JWT' });
}
if (!_.isObject(decoded)) {
return res.status(400).send({ status: 'error', err: 'Wrong JWT content'});
}
var redisMulti = parent._redisConnection.multi();
if (decoded.blacklistAdd) {
redisMulti.sadd(redisSetName, decoded.blacklistAdd);
}
if (decoded.blacklistRemove) {
redisMulti.srem(redisSetName, decoded.blacklistRemove);
}
if (decoded.muteAdd) {
redisMulti.sadd(redisMutedSetName, decoded.muteAdd);
}
if (decoded.muteRemove) {
redisMulti.srem(redisMutedSetName, decoded.muteRemove);
}
redisMulti.exec(function (err) {
if (err) {
logger.captureError(err);
return res.status(500).send(err);
}
res.sendStatus(200);
});
}
);
});
}
chatupBlacklistMiddleware.dispatcher = dispatcherPlugin;
return chatupBlacklistMiddleware;
}