Vulcan/server/notifications.js

60 lines
2.5 KiB
JavaScript
Raw Normal View History

createNotification = function(event, properties, userToNotify, userDoingAction){
// console.log('adding new notification for:'+getDisplayName(userToNotify)+', for event:'+event);
// console.log(userToNotify);
// console.log(userDoingAction);
// console.log(properties);
if(userToNotify._id!=userDoingAction._id){
// make sure we don't notify people of their own actions
var notification= {
timestamp: new Date().getTime(),
userId: userToNotify._id,
event: event,
properties: properties,
read: false
}
var newNotificationId=Notifications.insert(notification);
2013-01-29 10:51:44 +09:00
// send the notification is the notificationsFrequency is set to 1, or if it's undefined (legacy compatibility)
if(userToNotify.profile && (userToNotify.profile.notificationsFrequency === 1 || typeof userToNotify.profile.notificationsFrequency === 'undefined')){
2013-01-19 22:09:47 +09:00
Meteor.call('sendNotificationEmail', userToNotify, newNotificationId);
}
}
2013-01-19 21:37:46 +09:00
};
2013-01-19 22:09:47 +09:00
getUnsubscribeLink = function(user){
return Meteor.absoluteUrl()+'unsubscribe/'+user.email_hash;
};
2013-01-19 21:37:46 +09:00
Meteor.methods({
sendNotificationEmail : function(userToNotify, notificationId){
2013-01-19 22:09:47 +09:00
// 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, 'email');
2013-01-19 22:09:47 +09:00
var to = getEmail(userToNotify);
var text = n.text + '\n\n Unsubscribe from all notifications: '+getUnsubscribeLink(userToNotify);
var html = n.html + '<br/><br/><a href="'+getUnsubscribeLink(userToNotify)+'">Unsubscribe from all notifications</a>';
sendEmail(to, n.subject, text, html);
},
2013-01-19 21:37:46 +09:00
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
var user = Meteor.users.findOne({email_hash: hash});
if(user){
2013-01-19 21:55:10 +09:00
var update = Meteor.users.update(user._id, {
$set: {'profile.notificationsFrequency' : 0}
2013-01-19 21:37:46 +09:00
});
2013-01-19 21:55:10 +09:00
return true;
2013-01-19 21:37:46 +09:00
}
return false;
},
2013-01-19 23:01:43 +09:00
notifyAdmins : function(notification, currentUser){
2013-01-19 22:47:35 +09:00
// send a notification to every site admin
2013-01-19 23:01:43 +09:00
_.each(adminUsers(), function(user, index, list){
if(user._id !== currentUser._id){
// don't send admins notifications for their own posts
sendEmail(getEmail(user), notification.subject, notification.text, notification.html);
}
});
2013-01-19 21:37:46 +09:00
}
});