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 Oct 16, 2017
1 parent 6db29a1 commit b5b1da6
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion api/server/publications.ts
Original file line number Diff line number Diff line change
@@ -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 }
});
}
}
]
};
});

1 comment on commit b5b1da6

@sitexa
Copy link

@sitexa sitexa commented on b5b1da6 Dec 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.