From b0a2f1fcaad45101c966cc6a42cf25fa5f32d601 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Mon, 2 Feb 2015 16:50:14 +0100 Subject: [PATCH] check for subscription preferences when sending party invitations --- src/controllers/user.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/controllers/user.js b/src/controllers/user.js index 8557dd22a8..5f70d13780 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -437,19 +437,34 @@ api.cast = function(req, res, next) { api.inviteFriends = function(req, res, next) { Group.findOne({type:'party', members:{'$in': [res.locals.user._id]}}).select('_id name').exec(function(err,party){ if (err) return next(err); - var link = nconf.get('BASE_URL')+'?partyInvite='+ utils.encrypt(JSON.stringify({id:party._id, inviter:res.locals.user._id, name:party.name})); + _.each(req.body.emails, function(invite){ if (invite.email) { - var variables = [ - {name: 'LINK', content: link}, - {name: 'INVITER', content: req.body.inviter || res.locals.user.profile.name}, - {name: 'INVITEE', content: invite.name} - ]; - invite.canSend = true; + User.findOne({$or: [ + {'auth.local.email': invite.email}, + {'auth.facebook.emails.value': invite.email} + ]}).select({_id: true, 'preferences.emailNotifications': true}) + .exec(function(err, userToContact){ + if(err) return next(err); + + var link = nconf.get('BASE_URL')+'?partyInvite='+ utils.encrypt(JSON.stringify({id:party._id, inviter:res.locals.user._id, name:party.name})); + + var variables = [ + {name: 'LINK', content: link}, + {name: 'INVITER', content: req.body.inviter || utils.getUserInfo(res.locals.user, ['name']).name} + ]; + + invite.canSend = true; + + // We check for unsubscribeFromAll here because don't pass through utils.getUserInfo + if(userToContact.preferences.emailNotifications.invitedParty !== false && + userToContact.preferences.emailNotifications.unsubscribeFromAll !== true){ + // TODO implement "users can only be invited once" + utils.txnEmail(invite, 'invite-friend', variables); + } + }); - // TODO implement "users can only be invited once" - utils.txnEmail(invite, 'invite-friend', variables); } }); res.send(200);