From 9e8be432b32a4f0caedc9f58954a633f2e8fd3ae Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 15 Oct 2014 21:55:31 -0400 Subject: [PATCH] notification changes -only send a notification when the person you follow creates a topic -you still get a notification per post if you are following a topic -changed notifications.push so that it sends the notifications over a period of time, currently to 50 users per second -optimized topics.notifyFollowers and user.notifications.sendTopicNotification, they no longer query the database for the topic and post data instead they get it as params -you can no longer follow yourself :) -changed mongo sortedSetRemove so that it doesn't use $in if there is only a single value to remove --- public/language/en_GB/notifications.json | 1 + src/database/mongo/sorted.js | 17 +++++--- src/notifications.js | 55 +++++++++++++++++++++--- src/topics/create.js | 8 ++-- src/topics/follow.js | 41 ++++++------------ src/user/follow.js | 8 ++++ src/user/notifications.js | 54 ++++++----------------- 7 files changed, 98 insertions(+), 86 deletions(-) diff --git a/public/language/en_GB/notifications.json b/public/language/en_GB/notifications.json index 6e35eac76a..54c926a597 100644 --- a/public/language/en_GB/notifications.json +++ b/public/language/en_GB/notifications.json @@ -18,6 +18,7 @@ "favourited_your_post_in": "%1 has favourited your post in %2.", "user_flagged_post_in": "%1 flagged a post in %2", "user_posted_to" : "%1 has posted a reply to: %2", + "user_posted_topic": "%1 has posted a new topic: %2", "user_mentioned_you_in": "%1 mentioned you in %2", "user_started_following_you": "%1 started following you.", diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index 659994f878..71a30e8275 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -61,18 +61,21 @@ module.exports = function(db, module) { }; module.sortedSetRemove = function(key, value, callback) { + function done(err) { + callback(err); + } callback = callback || helpers.noop; if (!key) { return callback(); } - if (!Array.isArray(value)) { - value = [value]; - } - value = value.map(helpers.valueToString); - db.collection('objects').remove({_key: key, value: {$in: value}}, function(err) { - callback(err); - }); + if (Array.isArray(value)) { + value = value.map(helpers.valueToString); + db.collection('objects').remove({_key: key, value: {$in: value}}, done); + } else { + value = helpers.valueToString(value); + db.collection('objects').remove({_key: key, value: value}, done); + } }; module.sortedSetsRemove = function(keys, value, callback) { diff --git a/src/notifications.js b/src/notifications.js index 31663bd0e3..3ea7e0dd18 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -128,17 +128,58 @@ var async = require('async'), return callback(); } - var websockets = require('./socket.io'); if (!Array.isArray(uids)) { uids = [uids]; } + uids = uids.filter(function(uid) { + return parseInt(uid, 10); + }); + + if (!uids.length) { + return callback(); + } + + var done = false; + var start = 0; + var batchSize = 50; + + setTimeout(function() { + async.whilst( + function() { + return !done; + }, + function(next) { + var currentUids = uids.slice(start, start + batchSize); + if (!currentUids.length) { + done = true; + return next(); + } + pushToUids(currentUids, notification, function(err) { + if (err) { + return next(err); + } + start = start + batchSize; + + setTimeout(next, 1000); + }); + }, + function(err) { + if (err) { + winston.error(err.stack); + } + } + ); + }, 1000); + + callback(); + }; + + function pushToUids(uids, notification, callback) { var unreadKeys = []; var readKeys = []; - uids.filter(function(uid) { - return parseInt(uid, 10); - }).forEach(function(uid) { + uids.forEach(function(uid) { unreadKeys.push('uid:' + uid + ':notifications:unread'); readKeys.push('uid:' + uid + ':notifications:read'); }); @@ -163,13 +204,15 @@ var async = require('async'), } plugins.fireHook('action:notification.pushed', {notification: notification, uids: uids}); - callback(); + var websockets = require('./socket.io'); for(var i=0; i