-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathfeed.js
More file actions
70 lines (60 loc) · 2.36 KB
/
Copy pathfeed.js
File metadata and controls
70 lines (60 loc) · 2.36 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*global Posts, Comments */
var optional = Match.Optional,
loadMoreStep = 5;
Meteor.publish('feed', function(fields, limits, postIds) {
check(limits, {posts: Number});
check(postIds, Match.OneOf(null, [String]));
//console.log('Publishing Posts', fields);
//console.log("Limit:", limits);
// SECURITY NOTE
// if this was data that could not be shown to a specific set of
// users or a logged out user, you would check here to verify they're
// allowed to receive the data.
// **TRUST NOTHING FROM THE CLIENT** instead ask the server what they're
// user ID is and check their permissions to see if the role is met, never
// never pass in the user ID from the client as they could guess an admin
// ID and gain access.
//
// if (!this.userId)
// throw new Meteor.Error(401, "Access denied, please login");
// or
// if (Roles.userIsInRole(this.userId, 'admin'))
// throw new Meteor.Error(403, "Not authorized to view this data");
// -----------------------------------------------------------------------
// ensure *only* the fields we whitelist are passed in unless wrapped in
// Match.Optional it will be required. If any key does not match the
// publication will fail and throw an error
check(fields, {
posts: {
_id: Boolean, // id required for security
desc: optional(Boolean),
likeCount: optional(Boolean),
commentCount: optional(Boolean),
userName: optional(Boolean),
createdAt: optional(Boolean),
ownerId: optional(Boolean)
},
postComments: {
_id: Boolean, // id required for security
createdAt: optional(Boolean),
username: optional(Boolean),
desc: optional(Boolean),
postId: optional(Boolean)
}
});
// current 'load more' postIds
var newPostIds = Posts.find({}, {
fields: {'_id': 1},
sort: {createdAt: -1},
limit: limits.posts,
skip: limits.posts - loadMoreStep}
).map(function (post) {
return post._id;
});
postIds = _.union(postIds ? postIds : [], newPostIds);
// returns Mongo Cursors
return [
Posts.find({}, {fields: fields.posts, sort: {createdAt: -1}, limit: limits.posts}),
Comments.find({postId: {$in: postIds}}, {fields: fields.postComments})
];
});