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
DAB0mB committed Feb 26, 2017
1 parent 1f207b4 commit 835b399
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions server/publications.ts
@@ -1,7 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { Messages, Users } from '../imports/collections';
import { Message, User } from '../imports/models';
import { Chats, Messages, Users } from '../imports/collections';
import { Chat, Message, User } from '../imports/models';

Meteor.publish('users', function(): Mongo.Cursor<User> {
if (!this.userId) {
Expand All @@ -25,4 +25,36 @@ 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 835b399

Please sign in to comment.