-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathposts.js
77 lines (75 loc) · 3.05 KB
/
posts.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
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
70
71
72
73
74
75
76
77
if (Meteor.isServer) {
var setUser = function() {
var token = this.query.key;
if (token) {
var encryptedToken = CryptoJS.MD5(token).toString();
var apiKey = ApiKeys.findOne({token: encryptedToken});
if (apiKey) {
var user = Meteor.users.findOne({_id: apiKey.userId});
return user && user._id;
}
}
};
HTTP.methods({
'/v1/petitions': {
auth: setUser,
get: function(data) {
if (this.userId) {
this.setContentType('application/json');
var limit = Math.min(parseInt(this.query.limit) || 500, 500);
var selector = {published: true};
var posts = Posts.find(selector, {fields: { title: 1,
votes: 1,
author: 1,
description: 1,
submitted: 1,
response: 1,
responded_at: 1,
minimumVotes: 1}, limit: limit}).fetch();
return JSON.stringify(posts);
} else {
this.setStatusCode(401);
}
}
},
'/v1/petitions/:petitionId': {
auth: setUser,
get: function(data) {
if (this.userId) {
var selector = {};
selector['_id'] = this.params.petitionId;
selector['published'] = true;
var post = Posts.findOne(selector, {fields: { title: 1,
votes: 1,
author: 1,
description: 1,
submitted: 1,
upvoters: 1,
response: 1,
responded_at: 1,
minimumVotes: 1}});
if (post) {
this.setContentType('application/json');
post.signers = Meteor.users.find({'_id': {$in: post.upvoters}}).map(function (signer) { return signer.profile.initials });
post.history = Scores.find({
postId: post._id,
created_at: { $gte: moment().startOf('day').subtract(1, 'week').valueOf() }
}, {
fields: {
created_at: 1,
votes: 1
},
limit: 7,
sort: {created_at: 1}
}).fetch();
delete post.upvoters;
return JSON.stringify(post);
} else
this.setStatusCode(404);
} else {
this.setStatusCode(401);
}
}
}
});
}