forked from gothinkster/realworld-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollections.js
41 lines (31 loc) · 1.21 KB
/
collections.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Articles = new Meteor.Collection('articles');
Articles.helpers({
isFavorited() { return this.favorites?.includes(Meteor.userId()); },
author() { return Meteor.users.findOne(this.createdBy); },
isAuthor() { return this.createdBy === Meteor.userId(); },
favoriteToggle() {
const userId = Meteor.userId();
if (!userId) return;
const isFavorited = this.favorites?.includes(userId);
Articles.update(this._id, { [isFavorited ? '$pull' : '$addToSet']: { favorites: userId } });
},
});
Meteor.users.helpers({
following() { return this.profile?.followerIds?.includes(Meteor.userId()) ?? false; },
picture() { return this.profile?.picture || 'https://static.productionready.io/images/smiley-cyrus.jpg'; },
});
Comments = new Meteor.Collection('comments');
Comments.helpers({
author() { return Meteor.users.findOne(this.createdBy); },
isAuthor() { return this.createdBy === Meteor.userId(); },
});
Comments.add = (body) => {
if (!body) return;
const userId = Meteor.userId();
if (!userId) return;
const article = Articles.findOne({ slug: FlowRouter.getParam('slug') });
if (!article) return;
Comments.insert({
createdBy: userId, createdAt: new Date(), articleId: article._id, body,
});
};