Permalink
Browse files

Step 9.16: Implement chats publication

  • Loading branch information...
dotansimha authored and darkbasic committed Jan 24, 2017
1 parent 6db29a1 commit b5b1da65bb403849303bfe2ee9bdbcb39b89bd2a
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 }
});
}
}
]
};
});

1 comment on commit b5b1da6

@sitexa

This comment has been minimized.

Show comment
Hide comment
@sitexa

sitexa Dec 1, 2017

server/publications.ts (28, 8): Property 'publishComposite' does not exist on type 'typeof Meteor'.

sitexa commented on b5b1da6 Dec 1, 2017

server/publications.ts (28, 8): Property 'publishComposite' does not exist on type 'typeof Meteor'.

Please sign in to comment.