Vulcan/collections/notifications.js

91 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-04-13 15:05:18 +09:00
Notifications = new Meteor.Collection('notifications');
2013-07-04 12:51:26 +09:00
Notifications.allow({
insert: function(userId, doc){
// new notifications can only be created via a Meteor method
return false;
}
, update: canEditById
, remove: canEditById
});
getNotificationContents = function(notification, context){
// the same notifications can be displayed in multiple contexts: on-site in the sidebar, sent by email, etc.
var event = notification.event,
p = notification.properties,
context = typeof context === 'undefined' ? 'sidebar' : context,
userToNotify = Meteor.users.findOne(notification.userId);
switch(event){
case 'newReply':
var n = {
subject: 'Someone replied to your comment on "'+p.postHeadline+'"',
text: p.commentAuthorName+' has replied to your comment on "'+p.postHeadline+'": '+getPostCommentUrl(p.postId, p.commentId),
html: '<p><a href="'+getProfileUrlById(p.commentAuthorId)+'">'+p.commentAuthorName+'</a> has replied to your comment on "<a href="'+getPostCommentUrl(p.postId, p.commentId)+'" class="action-link">'+p.postHeadline+'</a>"</p>'
}
if(context == 'email')
n.html += '<p>'+p.commentExcerpt+'</p><a href="'+getPostCommentUrl(p.postId, p.commentId)+'" class="action-link">Read more</a>';
break;
case 'newComment':
var n = {
subject: 'A new comment on your post "'+p.postHeadline+'"',
text: 'You have a new comment by '+p.commentAuthorName+' on your post "'+p.postHeadline+'": '+getPostCommentUrl(p.postId, p.commentId),
html: '<p><a href="'+getProfileUrlById(p.commentAuthorId)+'">'+p.commentAuthorName+'</a> left a new comment on your post "<a href="'+getPostCommentUrl(p.postId, p.commentId)+'" class="action-link">'+p.postHeadline+'</a>"</p>'
}
if(context == 'email')
n.html += '<p>'+p.commentExcerpt+'</p><a href="'+getPostCommentUrl(p.postId, p.commentId)+'" class="action-link">Read more</a>';
break;
case 'newPost':
var n = {
subject: p.postAuthorName+' has created a new post: "'+p.postHeadline+'"',
text: p.postAuthorName+' has created a new post: "'+p.postHeadline+'" '+getPostUrl(p.postId),
html: '<a href="'+getProfileUrlById(p.postAuthorId)+'">'+p.postAuthorName+'</a> has created a new post: "<a href="'+getPostUrl(p.postId)+'" class="action-link">'+p.postHeadline+'</a>".'
}
break;
case 'accountApproved':
var n = {
subject: 'Your account has been approved.',
text: 'Welcome to '+getSetting('title')+'! Your account has just been approved.',
html: 'Welcome to '+getSetting('title')+'!<br/> Your account has just been approved. <a href="'+Meteor.absoluteUrl()+'">Start posting.</a>'
}
break;
case 'newUser':
var n = {
subject: 'New user: '+p.username,
text: 'A new user account has been created: '+p.profileUrl,
html: 'A new user account has been created: <a href="'+p.profileUrl+'">'+p.username+'</a>'
}
break;
default:
break;
}
// if context is email, append unsubscribe link to all outgoing notifications
if(context == 'email'){
n.to = getEmail(userToNotify);
n.text = n.text + '\n\n Unsubscribe from all notifications: '+getUnsubscribeLink(userToNotify);
n.html = n.html + '<br/><br/><a href="'+getUnsubscribeLink(userToNotify)+'">Unsubscribe from all notifications</a>';
}
return n;
2013-03-14 09:45:57 +11:00
}
Meteor.methods({
markAllNotificationsAsRead: function() {
Notifications.update(
{userId: Meteor.userId()},
{
$set:{
read: true
}
},
{multi: true}
);
}
});