Almog
Almog

Reputation: 2837

Meteorjs sharing a collection across users

I need a little help to better understand how to do something or the best approach in Meteor.JS

In my app I currently have a user which has topics and topics have a list of posts, each topic has a comments and a posts / comments count.

I would like to add collaborators to each topic IE a user can invite someone to add posts and comments to a specific topic basically sharing or collaborating under that topic which is then private to the two or X amount of users.

What's the best approach for this when it comes to Meteor Pub/Sub and Mongo collections.

Thanks, Almog

Upvotes: 1

Views: 73

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

There are many ways to solve this. For example, instead of tying topic to a single user, add a param that would hold _ids of all users allowed to the topic:

sampleTopic = {
  _id: 'fpoierj9',
  title: 'Sample',
  userIds: [
    'opijo42',
    'ik03agg',
    'po32a0v',
  ],
};

Now, in your publish channel, show topics that contain your user Id in said array:

Meteor.publish('topics', function() {
  return Topics.find({userIds: this.userId});
});

Upvotes: 1

Related Questions