From 8db6bb91794ef4a5fb4309e2ce1509c81dcd6922 Mon Sep 17 00:00:00 2001 From: dotansimha Date: Tue, 24 Jan 2017 19:28:32 +0200 Subject: [PATCH] Step 9.16: Implement chats publication --- api/server/publications.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/api/server/publications.ts b/api/server/publications.ts index 5bae026f0..fc883f92a 100644 --- a/api/server/publications.ts +++ b/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 { if (!this.userId) { @@ -25,3 +26,35 @@ Meteor.publish('messages', function(chatId: string): Mongo.Cursor { sort: { createdAt: -1 } }); }); + +Meteor.publishComposite('chats', function(): PublishCompositeConfig { + if (!this.userId) { + return; + } + + return { + find: () => { + return Chats.collection.find({ memberIds: this.userId }); + }, + + children: [ + > { + find: (chat) => { + return Messages.collection.find({ chatId: chat._id }, { + sort: { createdAt: -1 }, + limit: 1 + }); + } + }, + > { + find: (chat) => { + return Users.collection.find({ + _id: { $in: chat.memberIds } + }, { + fields: { profile: 1 } + }); + } + } + ] + }; +});