Skip to content

Commit

Permalink
Step 9.16: Implement chats publication
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha authored and darkbasic committed Jun 13, 2017
1 parent 2bc9d19 commit 8db6bb9
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion api/server/publications.ts
@@ -1,6 +1,7 @@
import { User, Message } from './models';
import { User, Message, Chat } from './models';
import { Users } from './collections/users';
import { Messages } from './collections/messages';
import { Chats } from './collections/chats';

Meteor.publish('users', function(): Mongo.Cursor<User> {
if (!this.userId) {
Expand All @@ -25,3 +26,35 @@ Meteor.publish('messages', function(chatId: string): Mongo.Cursor<Message> {
sort: { createdAt: -1 }
});
});

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 8db6bb9

Please sign in to comment.