Vulcan/collections/notifications.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2013-04-13 15:05:18 +09:00
Notifications = new Meteor.Collection('notifications');
2014-05-10 16:57:17 +09:00
// Notifications = new Meteor.Collection("notifications", {
// schema: new SimpleSchema({
// properties: {
// type: Object
// },
// event: {
// type: String
// },
// read: {
// type: Boolean
// },
// createdAt: {
// type: Date
// },
// userId: {
// type: "???"
// }
// })
// });
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
});
2014-08-04 09:47:10 +09:00
createNotification = function(event, properties, userToNotify) {
// 1. Store notification in database
var notification = {
timestamp: new Date().getTime(),
userId: userToNotify._id,
event: event,
properties: properties,
read: false
};
var newNotificationId=Notifications.insert(notification);
2014-08-04 10:04:50 +09:00
// 2. Send notification by email (if on server)
2014-08-04 09:47:10 +09:00
if(Meteor.isServer && getUserSetting('notifications.replies', false, userToNotify)){
2014-08-06 12:02:17 +09:00
// put in setTimeout so it doesn't hold up the rest of the method
Meteor.setTimeout(function () {
notificationEmail = buildEmailNotification(notification);
sendEmail(getEmail(userToNotify), notificationEmail.subject, notificationEmail.html);
}, 1);
2014-08-04 09:47:10 +09:00
}
};
buildSiteNotification = function (notification) {
var event = notification.event,
comment = notification.properties.comment,
post = notification.properties.post,
2014-08-03 11:50:10 +09:00
userToNotify = Meteor.users.findOne(notification.userId),
template,
2014-08-03 11:50:10 +09:00
html
var properties = {
profileUrl: getProfileUrlById(comment.userId),
author: comment.author,
postCommentUrl: getPostCommentUrl(post._id, comment._id),
postTitle: post.title
}
switch(event){
case 'newReply':
template = 'notification_new_reply';
break;
case 'newComment':
template = 'notification_new_comment';
2014-08-03 11:50:10 +09:00
break;
default:
2014-08-03 11:50:10 +09:00
break;
}
html = Blaze.toHTML(Blaze.With(properties, function(){
return Template[getTemplate(template)]
}));
2014-08-03 11:50:10 +09:00
return html;
};
2013-03-14 09:45:57 +11:00
Meteor.methods({
markAllNotificationsAsRead: function() {
Notifications.update(
{userId: Meteor.userId()},
{
$set:{
read: true
}
},
{multi: true}
);
}
});