Vulcan/server/notifications.js

70 lines
2.5 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;
2013-10-23 20:27:19 +09:00
// 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);
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){
// get specific notification content for "email" context
var contents = getNotificationContents(notification, 'email');
sendNotification(contents);
}
},
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 user-specific salt should be added to the hashing method to prevent this
2013-01-19 21:37:46 +09:00
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.notifications.users' : 0,
'profile.notifications.posts' : 0,
'profile.notifications.comments' : 0,
'profile.notifications.replies' : 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;
},
newPostNotify : function(properties){
var currentUser = Meteor.users.findOne(this.userId);
console.log('newPostNotify')
2013-10-23 10:21:08 +08:00
// send a notification to every user according to their notifications settings
2013-11-11 23:19:32 +02:00
Meteor.users.find().forEach(function(user) {
// don't send users notifications for their own posts
2013-10-23 10:21:08 +08:00
if(user._id !== currentUser._id && getUserSetting('notifications.posts', false, user)){
properties.userId = user._id;
var notification = getNotificationContents(properties, 'email');
sendNotification(notification, user);
2013-01-19 23:01:43 +09:00
}
});
2013-01-19 21:37:46 +09:00
}
2013-11-11 23:19:32 +02:00
});
sendNotification = function (notification) {
// console.log('send notification:')
// console.log(notification)
sendEmail(notification.to, notification.subject, notification.text, notification.html);
}