Vulcan/server/notifications.js

63 lines
2.6 KiB
JavaScript
Raw Normal View History

getUnsubscribeLink = function(user){
return Meteor.absoluteUrl()+'unsubscribe/'+user.email_hash;
};
Meteor.methods({
2013-10-23 20:20:10 +09:00
createNotification: function(options){
var event = options.event,
properties = options.properties,
userToNotify = options.userToNotify,
userDoingAction = options.userDoingAction,
sendEmail = options.sendEmail;
console.log('adding new notification for:'+getDisplayName(userToNotify)+', for event:'+event);
2013-02-22 15:51:54 +09:00
// console.log(userToNotify);
// console.log(userDoingAction);
// console.log(properties);
2013-10-23 10:21:08 +08:00
// console.log(sendEmail);
2013-02-22 15:51:54 +09:00
var notification= {
timestamp: new Date().getTime(),
userId: userToNotify._id,
event: event,
properties: properties,
read: false
}
var newNotificationId=Notifications.insert(notification);
// send the notification if notifications are activated,
// the notificationsFrequency is set to 1, or if it's undefined (legacy compatibility)
2013-10-23 10:21:08 +08:00
if(sendEmail){
2013-01-19 22:09:47 +09:00
Meteor.call('sendNotificationEmail', userToNotify, newNotificationId);
}
},
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-10-23 10:21:08 +08:00
notifyUsers : function(notification, currentUser){
// send a notification to every user according to their notifications settings
_.each(Meteor.users.find().fetch(), function(user, index, list){
if(user._id !== currentUser._id && getUserSetting('notifications.posts', false, user)){
// don't send users notifications for their own posts
2013-01-19 23:01:43 +09:00
sendEmail(getEmail(user), notification.subject, notification.text, notification.html);
}
});
2013-01-19 21:37:46 +09:00
}
});