Skip to content

Instantly share code, notes, and snippets.

@bitomule
Last active August 29, 2015 14:04
Show Gist options
  • Save bitomule/1908dfe578e07d04a5c4 to your computer and use it in GitHub Desktop.
Save bitomule/1908dfe578e07d04a5c4 to your computer and use it in GitHub Desktop.
Meteor.publish('userData', function() {
if(!this.userId)
{
this.ready();
return null;
}
return Meteor.users.find(this.userId, {fields: {username:1,roles:1}});
});
Meteor.publish('userDomain', function(domainName) {
var domain,users;
domain = Domains.find({name:domainName});
if(domain.count() <= 0)
{
this.ready();
return null;
}
var domainId = domain.fetch()[0]._id;
if(!domainId)
{
this.ready();
return null;
}
if(!this.userId)
{
return domain;
}
var query = {};
query['roles.' + domainId] = {$exists:true};
users = Meteor.users.find(query,{
fields:{username:1,roles:1}
});
return [domain,users];
});
Meteor.publish('projects', function() {
if(!this.userId || Domains.find().count() <= 0)
{
this.ready();
return null;
}
var projects;
if(Roles.userIsInRole(this.userId,['admin'],Domains.findOne()._id))
{
projects = Projects.find({},{ limit : 30 });
}
else
{
projects = Projects.find({$or:[{author:this.userId},{users:{$elemMatch:{$in: [this.userId]}}}]},{ limit : 30 });
}
return projects;
});
Meteor.reactivePublish('images', function() {
if(!this.userId || Domains.find().count() <= 0)
{
this.ready();
return null;
}
var projects;
if(Roles.userIsInRole(this.userId,['admin'],Domains.findOne()._id))
{
projects = Projects.find({},{reactive: true,limit : 30 });
}
else
{
projects = Projects.find({$or:[{author:this.userId},{users:{$elemMatch:{$in: [this.userId]}}}]},{reactive: true, limit : 30 });
}
var imagesIds = projects.map(function(p){return p.imageFile});
var images = Images.find({_id:{$in:imagesIds}},{reactive: true});
return images;
});
Meteor.publish('tasks', function() {
if(!this.userId)
{
this.ready();
return null;
}
if(Roles.userIsInRole(this.userId,['admin'],Domains.findOne()._id))
{
return Tasks.find({},{ limit : 30 });
}
else
{
return Tasks.find({$or:[{author:this.userId},{users:{$elemMatch:{$in: [this.userId]}}}]},{ limit : 30 });
}
});
Meteor.publish('notes', function() {
if(!this.userId)
{
this.ready();
return null;
}
return Notes.find({author:this.userId},{ limit : 50 });
});
Meteor.publish('searchprojects', function(searchText) {
if(!this.userId || searchText.length < 2)
{
return this.ready();
}
var regex = new RegExp(searchText,"i");
return Projects.find({name:{$regex:regex}},{ limit : 500 });
});
Meteor.publish('searchtasks', function(searchText) {
if(!this.userId || searchText.length < 2)
{
return this.ready();
}
var regex = new RegExp(searchText,"i");
return Tasks.find({name:{$regex:regex}},{ limit : 500 });
});
Meteor.publish('searchnotes', function(searchText) {
if(!this.userId || searchText.length < 2)
{
return this.ready();
}
var regex = new RegExp(searchText,"i");
return Notes.find({content:{$regex:regex}},{ limit : 500 });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment