From 72a919e416d3ccc8137eb02f6e4fe148d1a3d8ff Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 15 Oct 2014 14:49:11 -0400 Subject: [PATCH] time based check --- src/messaging.js | 77 ---------------------------------------- src/notifications.js | 13 ++++--- src/socket.io/modules.js | 16 ++++----- 3 files changed, 15 insertions(+), 91 deletions(-) diff --git a/src/messaging.js b/src/messaging.js index 75c9e91896..95ff03c9aa 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -48,7 +48,6 @@ var db = require('./database'), } async.parallel([ - async.apply(addToRecent, fromuid, message), async.apply(db.sortedSetAdd, 'messages:uid:' + uids[0] + ':to:' + uids[1], timestamp, mid), async.apply(Messaging.updateChatTime, fromuid, touid), async.apply(Messaging.updateChatTime, touid, fromuid), @@ -78,16 +77,6 @@ var db = require('./database'), }); }; - function addToRecent(fromuid, message, callback) { - db.listPrepend('messages:recent:' + fromuid, message.content, function(err) { - if (err) { - return callback(err); - } - - db.listTrim('messages:recent:' + fromuid, 0, 9, callback); - }); - } - Messaging.getMessages = function(fromuid, touid, since, isNew, callback) { var uids = sortUids(fromuid, touid); @@ -277,72 +266,6 @@ var db = require('./database'), db.sortedSetAdd('uid:' + uid + ':chats:unread', Date.now(), toUid, callback); }; - /* - todo #1798 -- this utility method creates a room name given an array of uids. - - Messaging.uidsToRoom = function(uids, callback) { - uid = parseInt(uid, 10); - if (typeof uid === 'number' && Array.isArray(roomUids)) { - var room = 'chat_'; - - room = room + roomUids.map(function(uid) { - return parseInt(uid, 10); - }).sort(function(a, b) { - return a-b; - }).join('_'); - - callback(null, room); - } else { - callback(new Error('invalid-uid-or-participant-uids')); - } - };*/ - - Messaging.verifySpammer = function(uid, callback) { - var messagesToCompare = 10; - - db.getListRange('messages:recent:' + uid, 0, messagesToCompare - 1, function(err, msgs) { - var total = 0; - - for (var i = 0, ii = msgs.length - 1; i < ii; ++i) { - total += areTooSimilar(msgs[i], msgs[i+1]) ? 1 : 0; - } - - var isSpammer = total === messagesToCompare - 1; - if (isSpammer) { - db.delete('messages:recent:' + uid); - } - - callback(err, isSpammer); - }); - }; - - // modified from http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance - function areTooSimilar(a, b) { - var matrix = []; - - for(var i = 0; i <= b.length; i++){ - matrix[i] = [i]; - } - - for(var j = 0; j <= a.length; j++){ - matrix[0][j] = j; - } - - for(i = 1; i <= b.length; i++){ - for(j = 1; j <= a.length; j++){ - if(b.charAt(i-1) === a.charAt(j-1)){ - matrix[i][j] = matrix[i-1][j-1]; - } else { - matrix[i][j] = Math.min(matrix[i-1][j-1] + 1, - Math.min(matrix[i][j-1] + 1, - matrix[i-1][j] + 1)); - } - } - } - - return (matrix[b.length][a.length] / b.length < 0.1); - } - Messaging.notifyUser = function(fromuid, touid, messageObj) { var queueObj = Messaging.notifyQueue[fromuid + ':' + touid]; if (queueObj) { diff --git a/src/notifications.js b/src/notifications.js index 3d8aeb66dc..31663bd0e3 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -143,22 +143,25 @@ var async = require('async'), readKeys.push('uid:' + uid + ':notifications:read'); }); - async.parallel([ + var oneWeekAgo = Date.now() - 604800000; + async.series([ function(next) { db.sortedSetsAdd(unreadKeys, notification.datetime, notification.nid, next); }, function(next) { db.sortedSetsRemove(readKeys, notification.nid, next); + }, + function(next) { + db.sortedSetsRemoveRangeByScore(unreadKeys, 0, oneWeekAgo, next); + }, + function(next) { + db.sortedSetsRemoveRangeByScore(readKeys, 0, oneWeekAgo, next); } ], function(err) { if (err) { return callback(err); } - var oneWeekAgo = Date.now() - 604800000; - db.sortedSetsRemoveRangeByScore(unreadKeys, 0, oneWeekAgo); - db.sortedSetsRemoveRangeByScore(readKeys, 0, oneWeekAgo); - plugins.fireHook('action:notification.pushed', {notification: notification, uids: uids}); callback(); diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index b0dc4e8a7c..02d48ac959 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -179,18 +179,16 @@ SocketModules.chats.send = function(socket, data, callback) { return; } - Messaging.verifySpammer(socket.uid, function(err, isSpammer) { - if (!err && isSpammer) { + var msg = S(data.message).stripTags().s; - server.in('uid_' + socket.uid).emit('event:banned'); + var now = Date.now(); + socket.lastChatMessageTime = socket.lastChatMessageTime || 0; - // We're just logging them out, so a "temporary ban" to prevent abuse. Revisit once we implement a way to temporarily ban users - server.logoutUser(socket.uid); - return callback(); - } - }); + if (now - socket.lastChatMessageTime < 200) { + return callback(new Error('[[error:too-many-messages]]')); + } - var msg = S(data.message).stripTags().s; + socket.lastChatMessageTime = now; user.getUserField(socket.uid, 'banned', function(err, banned) { if (err) {