Merge branch 'hotfix/21.56.9' into master
[gitter.git] / modules / spam-detection / lib / duplicate-chat-detector.js
blob86dc241ca21ed7247ac262bc4589def2508c3032
1 'use strict';
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'), {
12 keyPrefix: 'spam:'
13 });
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')),
20 numberOfKeys: keys
21 });
24 defineCommand('spamDetectionCountChatForUser', 'count-chat-for-user', 1);
26 function getWarnAndBanThresholds(text) {
27 // This should catch emojis etc
28 if (text.length < 10) {
29 return {
30 warn: 80,
31 ban: 100
35 if (text.length < 20) {
36 return {
37 warn: 16,
38 ban: 20
42 return {
43 warn: 8,
44 ban: 10
48 function addHash(userId, hash, text) {
49 var thresholds = getWarnAndBanThresholds(text);
51 return redisClient
52 .spamDetectionCountChatForUser('dup:' + String(userId), hash, TTL)
53 .bind({
54 thresholds: thresholds,
55 text: text,
56 userId: userId
58 .then(function(count) {
59 var thresholds = this.thresholds;
60 var userId = this.userId;
61 var text = this.text;
63 if (count > thresholds.warn) {
64 logger.warn('User sending duplicate messages', {
65 count: count,
66 text: text,
67 userId: userId
68 });
71 return count > thresholds.ban;
72 });
74 /**
75 * Super basic spam detection
77 function detect(userId, text) {
78 var hash = crypto
79 .createHash('md5')
80 .update(text)
81 .digest('hex');
82 return addHash(userId, hash, text);
85 module.exports = Promise.method(detect);