Skip to content

Commit

Permalink
Step 7.13: Add publication for Chats
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored and dotansimha committed Nov 27, 2016
1 parent b3b797b commit 011cc2f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions server/imports/publications/chats.publication.ts
@@ -0,0 +1,38 @@
import { Meteor } from 'meteor/meteor';

import { Chats } from '../../../both/collections/chats.collection';
import { Chat } from '../../../both/models/chat.model';
import { Messages } from '../../../both/collections/messages.collection';
import { Message } from '../../../both/models/message.model';
import { Users } from '../../../both/collections/users.collection';
import { User } from '../../../both/models/user.model';

Meteor.publishComposite('chats', function(): PublishCompositeConfig<Chat> {
if (!this.userId) return;

return {
find: () => {
return Chats.collection.find({memberIds: this.userId});
},

children: [
<PublishCompositeConfig1<Chat, Message>> {
find: (chat) => {
return Messages.collection.find({chatId: chat._id}, {
sort: {createdAt: -1},
limit: 1
});
}
},
<PublishCompositeConfig1<Chat, User>> {
find: (chat) => {
return Users.collection.find({
_id: {$in: chat.memberIds}
}, {
fields: {profile: 1}
});
}
}
]
};
});

0 comments on commit 011cc2f

Please sign in to comment.