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 } + }); + } + } + ] + }; +});