Permalink
Browse files

Step 9.16: Implement chats publication

  • Loading branch information...
dotansimha authored and darkbasic committed Jan 24, 2017
1 parent 2bc9d19 commit 8db6bb91794ef4a5fb4309e2ce1509c81dcd6922
Showing with 34 additions and 1 deletion.
  1. +34 −1 api/server/publications.ts
View
@@ -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) {
@@ -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.