Skip to content

Commit b5b1da6

Browse files
dotansimhadarkbasic
authored andcommitted
Step 9.16: Implement chats publication
1 parent 6db29a1 commit b5b1da6

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

api/server/publications.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { User, Message } from './models';
1+
import { User, Message, Chat } from './models';
22
import { Users } from './collections/users';
33
import { Messages } from './collections/messages';
4+
import { Chats } from './collections/chats';
45

56
Meteor.publish('users', function(): Mongo.Cursor<User> {
67
if (!this.userId) {
@@ -25,3 +26,35 @@ Meteor.publish('messages', function(chatId: string): Mongo.Cursor<Message> {
2526
sort: { createdAt: -1 }
2627
});
2728
});
29+
30+
Meteor.publishComposite('chats', function(): PublishCompositeConfig<Chat> {
31+
if (!this.userId) {
32+
return;
33+
}
34+
35+
return {
36+
find: () => {
37+
return Chats.collection.find({ memberIds: this.userId });
38+
},
39+
40+
children: [
41+
<PublishCompositeConfig1<Chat, Message>> {
42+
find: (chat) => {
43+
return Messages.collection.find({ chatId: chat._id }, {
44+
sort: { createdAt: -1 },
45+
limit: 1
46+
});
47+
}
48+
},
49+
<PublishCompositeConfig1<Chat, User>> {
50+
find: (chat) => {
51+
return Users.collection.find({
52+
_id: { $in: chat.memberIds }
53+
}, {
54+
fields: { profile: 1 }
55+
});
56+
}
57+
}
58+
]
59+
};
60+
});

0 commit comments

Comments
 (0)