Vulcan/server/publications.js

172 lines
4.7 KiB
JavaScript
Raw Normal View History

2013-10-24 11:15:07 +09:00
var privacyOptions = { // false means private
secret_id: false,
isAdmin: false,
emails: false,
notifications: false,
invitesCount: false,
'profile.email': false,
'services.twitter.accessToken': false,
'services.twitter.accessTokenSecret': false,
'services.twitter.id': false,
'services.password': false,
'services.resume': false
};
2013-10-26 10:50:49 +09:00
// -------------------------------------------- Users -------------------------------------------- //
// Publish the current user
2013-07-04 12:51:26 +09:00
2012-10-18 14:24:36 +09:00
Meteor.publish('currentUser', function() {
2013-10-25 10:04:34 +09:00
var user = Meteor.users.find(this.userId);
return user;
2012-10-18 14:24:36 +09:00
});
2013-10-09 22:16:47 +09:00
2013-10-26 10:50:49 +09:00
// Publish a single user
2013-10-24 11:04:27 +09:00
Meteor.publish('singleUser', function(userIdOrSlug) {
if(canViewById(this.userId)){
var options = isAdminById(this.userId) ? {limit: 1} : {limit: 1, fields: privacyOptions};
var findById = Meteor.users.find(userIdOrSlug, options);
var findBySlug = Meteor.users.find({slug: userIdOrSlug}, options)
// if we find something when treating the argument as an ID, return that; else assume it's a slug
return findById.count() ? findById : findBySlug;
}
2013-10-09 22:16:47 +09:00
});
2013-10-26 10:50:49 +09:00
// Publish authors of the current post and its comments
Meteor.publish('postUsers', function(postId) {
if(canViewById(this.userId)){
// publish post author and post commenters
var post = Posts.findOne(postId),
users = [];
if(post) {
var comments = Comments.find({post: post._id}).fetch();
// get IDs from all commenters on the post, plus post author's ID
users = _.pluck(comments, "userId");
users.push(post.userId);
users = _.unique(users);
}
return Meteor.users.find({_id: {$in: users}}, {fields: privacyOptions});
}
});
2013-10-26 10:50:49 +09:00
// Publish author of the current comment
Meteor.publish('commentUser', function(commentId) {
if(canViewById(this.userId)){
var comment = Comments.findOne(commentId);
return Meteor.users.find({_id: comment && comment.userId}, {fields: privacyOptions});
}
});
2013-10-26 10:50:49 +09:00
// Publish all the users that have posted the currently displayed list of posts
Meteor.publish('postsListUsers', function(find, options) {
if(canViewById(this.userId)){
var posts = Posts.find(find, options);
var userIds = _.pluck(posts.fetch(), 'userId');
2013-11-06 09:29:10 +09:00
return Meteor.users.find({_id: {$in: userIds}}, {fields: privacyOptions, multi: true});
2013-10-26 10:50:49 +09:00
}
});
// Publish all users
2013-11-04 22:05:19 +09:00
Meteor.publish('allUsers', function(find, options) {
if(canViewById(this.userId)){
2013-11-04 22:05:19 +09:00
if (!isAdminById(this.userId)) // if user is not admin, filter out sensitive info
options = _.extend(options, {fields: privacyOptions});
return Meteor.users.find(find, options);
}
2012-09-06 15:28:58 +09:00
});
2012-09-19 09:03:25 +09:00
// publish all users for admins to make autocomplete work
// TODO: find a better way
Meteor.publish('allUsersAdmin', function() {
2013-11-11 16:25:47 -05:00
if (isAdminById(this.userId)) {
return Meteor.users.find();
} else {
this.stop();
}
});
2013-10-26 10:50:49 +09:00
// -------------------------------------------- Posts -------------------------------------------- //
2013-10-26 10:50:49 +09:00
// Publish a single post
2012-08-22 23:24:33 -04:00
2013-04-06 14:27:01 +09:00
Meteor.publish('singlePost', function(id) {
if(canViewById(this.userId)){
return Posts.find(id);
}
});
2013-10-26 10:50:49 +09:00
// Publish the post related to the current comment
Meteor.publish('commentPost', function(commentId) {
if(canViewById(this.userId)){
var comment = Comments.findOne(commentId);
return Posts.find({_id: comment && comment.post});
}
});
2013-10-26 10:50:49 +09:00
// Publish a list of posts
2013-10-24 20:30:05 +09:00
Meteor.publish('postsList', function(find, options) {
if(canViewById(this.userId)){
options = options || {};
var posts = Posts.find(find, options);
2013-10-24 22:22:49 +09:00
// console.log('//-------- Subscription Parameters:');
// console.log(find);
// console.log(options);
// console.log('Found '+posts.fetch().length+ ' posts:');
// posts.rewind();
// console.log(_.pluck(posts.fetch(), 'headline'));
// posts.rewind();
2013-10-24 20:30:05 +09:00
return posts;
}
});
2013-10-26 10:50:49 +09:00
// -------------------------------------------- Comments -------------------------------------------- //
2012-08-22 23:24:33 -04:00
2013-10-26 10:50:49 +09:00
// Publish comments for a specific post
Meteor.publish('postComments', function(postId) {
if(canViewById(this.userId)){
return Comments.find({post: postId});
}
2012-08-22 23:24:33 -04:00
});
2013-10-26 10:50:49 +09:00
// Publish a single comment
Meteor.publish('singleComment', function(commentId) {
if(canViewById(this.userId)){
return Comments.find(commentId);
}
});
2013-10-26 10:50:49 +09:00
// -------------------------------------------- Other -------------------------------------------- //
Meteor.publish('settings', function() {
2012-09-06 19:42:11 +09:00
return Settings.find();
2012-09-06 11:34:05 +09:00
});
Meteor.publish('notifications', function() {
// only publish notifications belonging to the current user
if(canViewById(this.userId)){
return Notifications.find({userId:this.userId});
}
});
Meteor.publish('categories', function() {
if(canViewById(this.userId)){
return Categories.find();
}
});