3 var env
= require('gitter-web-env');
4 var config
= env
.config
;
5 var fs
= require('fs');
6 var path
= require('path');
7 var Promise
= require('bluebird');
8 var crypto
= require('crypto');
9 var logger
= env
.logger
.get('spam-detection');
11 var redisClient
= env
.ioredis
.createClient(config
.get('redis_nopersist'), {
15 var TTL
= 12 * 60 * 60;
17 function defineCommand(name
, script
, keys
) {
18 redisClient
.defineCommand(name
, {
19 lua
: fs
.readFileSync(path
.join(__dirname
, '..', 'redis-lua', script
+ '.lua')),
24 defineCommand('spamDetectionCountChatForUser', 'count-chat-for-user', 1);
26 function getWarnAndBanThresholds(text
) {
27 // This should catch emojis etc
28 if (text
.length
< 10) {
35 if (text
.length
< 20) {
48 function addHash(userId
, hash
, text
) {
49 var thresholds
= getWarnAndBanThresholds(text
);
52 .spamDetectionCountChatForUser('dup:' + String(userId
), hash
, TTL
)
54 thresholds
: thresholds
,
58 .then(function(count
) {
59 var thresholds
= this.thresholds
;
60 var userId
= this.userId
;
63 if (count
> thresholds
.warn
) {
64 logger
.warn('User sending duplicate messages', {
71 return count
> thresholds
.ban
;
75 * Super basic spam detection
77 function detect(userId
, text
) {
82 return addHash(userId
, hash
, text
);
85 module
.exports
= Promise
.method(detect
);