Vulcan/server/notifications.js

79 lines
2.3 KiB
JavaScript
Raw Normal View History

getUnsubscribeLink = function(user){
return Meteor.absoluteUrl()+'unsubscribe/'+user.email_hash;
};
2014-08-04 10:04:50 +09:00
// given a notification, return the correct subject and html to send an email
2014-08-03 11:50:10 +09:00
buildEmailNotification = function (notification) {
2014-08-06 10:44:19 +09:00
2014-08-04 09:47:10 +09:00
var subject, template;
var post = notification.properties.post;
var comment = notification.properties.comment;
2014-08-04 09:47:10 +09:00
switch(notification.event){
2014-08-03 11:50:10 +09:00
case 'newReply':
subject = 'Someone replied to your comment on "'+post.title+'"';
2014-08-04 09:47:10 +09:00
template = 'emailNewReply';
2014-08-03 11:50:10 +09:00
break;
case 'newComment':
subject = 'A new comment on your post "'+post.title+'"';
2014-08-04 09:47:10 +09:00
template = 'emailNewComment';
2014-08-03 11:50:10 +09:00
break;
default:
break;
}
var emailProperties = _.extend(notification.properties, {
body: marked(comment.body),
profileUrl: getProfileUrlById(comment.userId),
postCommentUrl: getPostCommentUrl(post._id, comment._id),
postLink: getPostLink(post)
2014-08-04 09:47:10 +09:00
});
2014-08-03 11:50:10 +09:00
// console.log(emailProperties)
2014-08-03 11:50:10 +09:00
var notificationHtml = getEmailTemplate(template)(emailProperties);
2014-08-04 09:47:10 +09:00
var html = buildEmailTemplate(notificationHtml);
return {
subject: subject,
html: html
}
};
2014-08-03 11:50:10 +09:00
2014-08-06 12:02:17 +09:00
newPostNotification = function(post, excludedIDs){
var excludedIDs = typeof excludedIDs == 'undefined' ? [] : excludedIDs;
var p = getPostProperties(post);
var subject = p.postAuthorName+' has created a new post: '+p.postTitle;
var html = buildEmailTemplate(getEmailTemplate('emailNewPost')(p));
2014-08-06 12:02:17 +09:00
// send a notification to every user according to their notifications settings
Meteor.users.find({'profile.notifications.posts': 1}).forEach(function(user) {
// don't send user a notification if their ID is in excludedIDs
if(excludedIDs.indexOf(user._id) == -1)
sendEmail(getEmail(user), subject, html);
});
};
2014-08-06 12:02:17 +09:00
Meteor.methods({
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;
}
2013-11-11 23:19:32 +02:00
});