From add4e6ee5b056ade4d0584844de03c35b0ca7eb4 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 27 Jan 2016 20:03:28 +0200 Subject: [PATCH] notifications.create can return null --- src/groups/membership.js | 51 ++++++++++++++++++++++----------------- src/notifications.js | 4 +-- src/socket.io/user.js | 11 ++++++--- src/topics/follow.js | 16 ++++-------- src/user/approval.js | 9 +++---- src/user/notifications.js | 9 +++---- 6 files changed, 49 insertions(+), 51 deletions(-) diff --git a/src/groups/membership.js b/src/groups/membership.js index 663b3d7b14..0d40af19c1 100644 --- a/src/groups/membership.js +++ b/src/groups/membership.js @@ -74,26 +74,30 @@ module.exports = function(Groups) { Groups.requestMembership = function(groupName, uid, callback) { async.waterfall([ async.apply(inviteOrRequestMembership, groupName, uid, 'request'), - function(next) { - user.getUserField(uid, 'username', function(err, username) { - if (err) { - return next(err); - } - next(null, { - bodyShort: '[[groups:request.notification_title, ' + username + ']]', - bodyLong: '[[groups:request.notification_text, ' + username + ', ' + groupName + ']]', - nid: 'group:' + groupName + ':uid:' + uid + ':request', - path: '/groups/' + utils.slugify(groupName) - }); - }); + function (next) { + user.getUserField(uid, 'username', next); }, - async.apply(notifications.create), - function(notification, next) { - Groups.getOwners(groupName, function(err, ownerUids) { - next(null, notification, ownerUids); - }); + function (username, next) { + async.parallel({ + notification: function(next) { + notifications.create({ + bodyShort: '[[groups:request.notification_title, ' + username + ']]', + bodyLong: '[[groups:request.notification_text, ' + username + ', ' + groupName + ']]', + nid: 'group:' + groupName + ':uid:' + uid + ':request', + path: '/groups/' + utils.slugify(groupName) + }, next); + }, + owners: function(next) { + Groups.getOwners(groupName, next); + } + }, next); }, - async.apply(notifications.push) + function (results, next) { + if (!results.notification || !results.owners.length) { + return next(); + } + notifications.push(results.notification, results.owners, next); + } ], callback); }; @@ -123,10 +127,13 @@ module.exports = function(Groups) { nid: 'group:' + groupName + ':uid:' + uid + ':invite', path: '/groups/' + utils.slugify(groupName) }), - function(notification, next) { - next(null, notification, [uid]); - }, - async.apply(notifications.push) + function (notification, next) { + if (!notification) { + return next(); + } + + notifications.push(notification, [uid]); + } ], callback); }; diff --git a/src/notifications.js b/src/notifications.js index f0e72ceb97..0cce0693a9 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -90,7 +90,7 @@ var async = require('async'), if (oldNotification) { if (parseInt(oldNotification.pid, 10) === parseInt(data.pid, 10) && parseInt(oldNotification.importance, 10) > parseInt(data.importance, 10)) { - return callback(); + return callback(null, null); } } @@ -367,7 +367,7 @@ var async = require('async'), return cur; }, []); - + differentiators.forEach(function(differentiator) { set = isolated.filter(function(notifObj) { return notifObj.mergeId === (mergeId + '|' + differentiator); diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 67068fea95..6c4c492f38 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -137,13 +137,13 @@ SocketUser.follow = function(socket, data, callback) { } var userData; async.waterfall([ - function(next) { + function (next) { toggleFollow('follow', socket.uid, data.uid, next); }, - function(next) { + function (next) { user.getUserFields(socket.uid, ['username', 'userslug'], next); }, - function(_userData, next) { + function (_userData, next) { userData = _userData; notifications.create({ bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]', @@ -153,7 +153,10 @@ SocketUser.follow = function(socket, data, callback) { mergeId: 'notifications:user_started_following_you' }, next); }, - function(notification, next) { + function (notification, next) { + if (!notification) { + return next(); + } notification.user = userData; notifications.push(notification, [data.uid], next); } diff --git a/src/topics/follow.js b/src/topics/follow.js index 671a540496..dc441b1ca4 100644 --- a/src/topics/follow.js +++ b/src/topics/follow.js @@ -139,18 +139,12 @@ module.exports = function(Topics) { from: exceptUid, mergeId: 'notifications:user_posted_to|' + postData.topic.tid, topicTitle: title - }, function(err, notification) { - if (err) { - return next(err); - } - - if (notification) { - notifications.push(notification, followers); - } - next(); - }); + }, next); }, - function (next) { + function (notification, next) { + if (notification) { + notifications.push(notification, followers); + } if (parseInt(meta.config.disableEmailSubscriptions, 10) === 1) { return next(); diff --git a/src/user/approval.js b/src/user/approval.js index 07c93ce0a8..0639286a67 100644 --- a/src/user/approval.js +++ b/src/user/approval.js @@ -50,14 +50,11 @@ module.exports = function(User) { nid: 'new_register:' + username, path: '/admin/manage/registration' }, function(err, notification) { - if (err) { + if (err || !notification) { return callback(err); } - if (notification) { - notifications.pushGroup(notification, 'administrators', callback); - } else { - callback(); - } + + notifications.pushGroup(notification, 'administrators', callback); }); } diff --git a/src/user/notifications.js b/src/user/notifications.js index f40143a28d..4e66afd606 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -295,14 +295,11 @@ var async = require('async'), path: path, nid: 'welcome_' + uid }, function(err, notification) { - if (err) { + if (err || !notification) { return callback(err); } - if (notification) { - notifications.push(notification, [uid], callback); - } else { - callback(); - } + + notifications.push(notification, [uid], callback); }); };