import { Meteor } from 'meteor/meteor' ; import { Mongo } from 'meteor/mongo' ; import { Chats , Messages , Pictures , Users } from '../imports/collections' ; import { Chat , Message , Picture , User } from '../imports/models' ; Meteor . publishComposite ( 'users' , function ( pattern ) : PublishCompositeConfig < User > { if ( ! this . userId ) { return ; } let selector = { } ; if ( pattern ) { selector = { 'profile.name' : { $regex : pattern , $options : 'i' } } ; } return { find : ( ) => { return Users . collection . find ( selector , { fields : { profile : 1 } , limit : 15 } ) ; } , children : [ < PublishCompositeConfig1 < User , Picture > > { find : ( user ) => { return Pictures . collection . find ( user . profile . pictureId , { fields : { url : 1 } } ) ; } } ] } ; } ) ; Meteor . publish ( 'messages' , function ( chatId , messagesBatchCounter ) : Mongo . Cursor < Message > { if ( ! this . userId || ! chatId ) { return ; } return Messages . collection . find ( { chatId } , { sort : { createdAt : - 1 } , limit : 30 * messagesBatchCounter } ) ; } ) ; 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 } } ) ; } , children : [ < PublishCompositeConfig2 < Chat , User , Picture > > { find : ( user , chat ) => { return Pictures . collection . find ( user . profile . pictureId , { fields : { url : 1 } } ) ; } } ] } ] } ; } ) ; Meteor . publish ( 'user' , function ( ) { if ( ! this . userId ) { return ; } const profile = Users . findOne ( this . userId ) . profile || { } ; return Pictures . collection . find ( { _id : profile . pictureId } ) ; } ) ;