diff --git a/server/email.js b/server/email.js index f8334e3cb..067899484 100644 --- a/server/email.js +++ b/server/email.js @@ -20,14 +20,4 @@ sendEmail = function(to, subject, text, html){ text: text, html: html }); -}; - -Meteor.methods({ - sendNotificationEmail: function(to, notificationId){ - // Note: we query the DB instead of simply passing arguments from the client - // to make sure our email method cannot be used for spam - var notification = Notifications.findOne(notificationId); - var n = getNotification(notification.event, notification.properties); - sendEmail(to, n.subject, n.text, n.html); - } -}) \ No newline at end of file +}; \ No newline at end of file diff --git a/server/notifications.js b/server/notifications.js index 871f68d21..dd453258a 100644 --- a/server/notifications.js +++ b/server/notifications.js @@ -15,12 +15,26 @@ createNotification = function(event, properties, userToNotify, userDoingAction){ var newNotificationId=Notifications.insert(notification); if(userToNotify.profile && userToNotify.profile.notificationsFrequency === 1){ - Meteor.call('sendNotificationEmail', getEmail(userToNotify), newNotificationId); + Meteor.call('sendNotificationEmail', userToNotify, newNotificationId); } } }; +getUnsubscribeLink = function(user){ + return Meteor.absoluteUrl()+'unsubscribe/'+user.email_hash; +}; + Meteor.methods({ + sendNotificationEmail: function(userToNotify, notificationId){ + // Note: we query the DB instead of simply passing arguments from the client + // to make sure our email method cannot be used for spam + var notification = Notifications.findOne(notificationId); + var n = getNotification(notification.event, notification.properties); + var to = getEmail(userToNotify); + var text = n.text + '\n\n Unsubscribe from all notifications: '+getUnsubscribeLink(userToNotify); + var html = n.html + '

Unsubscribe from all notifications'; + sendEmail(to, n.subject, text, html); + }, unsubscribeUser : function(hash){ // TO-DO: currently, if you have somebody's email you can unsubscribe them // A site-specific salt should be added to the hashing method to prevent this diff --git a/server/users.js b/server/users.js index 75d157e2f..7412ccd1c 100644 --- a/server/users.js +++ b/server/users.js @@ -48,6 +48,10 @@ Meteor.methods({ var ageInHours = (new Date().getTime() - object.submitted) / (60 * 60 * 1000); var newScore = baseScore / Math.pow(ageInHours + 2, 1.3); return Math.abs(object.score - newScore); + }, + generateEmailHash: function(){ + var email_hash = CryptoJS.MD5(getEmail(Meteor.user()).trim().toLowerCase()).toString(); + Meteor.users.update(Meteor.userId(), {$set : {email_hash : email_hash}}); } });