2012-10-24 11:04:42 +09:00
|
|
|
STATUS_PENDING=1;
|
|
|
|
STATUS_APPROVED=2;
|
|
|
|
STATUS_REJECTED=3;
|
|
|
|
|
2012-10-04 11:45:12 +10:00
|
|
|
Meteor.methods({
|
|
|
|
post: function(post){
|
2012-12-13 10:58:17 +09:00
|
|
|
var headline = cleanUp(post.headline);
|
|
|
|
var body = cleanUp(post.body);
|
2012-10-04 11:53:15 +10:00
|
|
|
var user = Meteor.user();
|
2012-10-23 12:24:38 +09:00
|
|
|
var userId = post.userId || user._id;
|
|
|
|
var submitted = parseInt(post.submitted) || new Date().getTime();
|
2012-10-30 12:01:11 +09:00
|
|
|
var defaultStatus = getSetting('requirePostsApproval') ? STATUS_PENDING : STATUS_APPROVED;
|
|
|
|
var status = post.status || defaultStatus;
|
|
|
|
var postWithSameLink = Posts.findOne({url: post.url});
|
|
|
|
var timeSinceLastPost=timeSinceLast(user, Posts);
|
|
|
|
var numberOfPostsInPast24Hours=numberOfItemsInPast24Hours(user, Posts);
|
2012-10-06 13:15:55 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// check that user can post
|
2012-10-05 13:59:40 +09:00
|
|
|
if (!user || !canPost(user))
|
2012-10-30 12:01:11 +09:00
|
|
|
throw new Meteor.Error(601, 'You need to login or be invited to post new stories.');
|
2012-10-10 08:32:49 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// check that user provided a headline
|
2012-10-11 13:21:10 +09:00
|
|
|
if(!post.headline)
|
2012-10-30 12:01:11 +09:00
|
|
|
throw new Meteor.Error(602, 'Please fill in a headline');
|
2012-10-05 13:59:40 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// check that there are no previous posts with the same link
|
|
|
|
if(post.url && postWithSameLink){
|
|
|
|
Meteor.call('upvotePost', postWithSameLink._id);
|
|
|
|
throw new Meteor.Error(603, 'This link has already been posted', postWithSameLink._id);
|
|
|
|
}
|
2012-10-06 13:15:55 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// check that user waits more than 30 seconds between posts
|
|
|
|
if(!this.isSimulation && timeSinceLastPost < 30)
|
|
|
|
throw new Meteor.Error(604, 'Please wait '+(30-timeSinceLastPost)+' seconds before posting again');
|
2012-10-24 11:04:42 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
if(!this.isSimulation && numberOfPostsInPast24Hours > 30)
|
|
|
|
throw new Meteor.Error(605, 'Sorry, you cannot submit more than 30 posts per day');
|
2012-10-23 12:24:38 +09:00
|
|
|
|
2012-10-04 11:45:12 +10:00
|
|
|
post = _.extend(post, {
|
2012-12-13 10:58:17 +09:00
|
|
|
headline: headline,
|
|
|
|
body: body,
|
2012-10-23 12:24:38 +09:00
|
|
|
userId: userId,
|
|
|
|
author: getDisplayNameById(userId),
|
|
|
|
submitted: submitted,
|
2012-10-04 11:45:12 +10:00
|
|
|
votes: 0,
|
|
|
|
comments: 0,
|
|
|
|
baseScore: 0,
|
2012-10-24 11:04:42 +09:00
|
|
|
score: 0,
|
|
|
|
status: status
|
2012-10-04 11:45:12 +10:00
|
|
|
});
|
|
|
|
|
2012-10-04 15:26:59 +09:00
|
|
|
var postId=Posts.insert(post);
|
2012-10-24 11:04:42 +09:00
|
|
|
post['postId']=postId;
|
2012-10-04 15:26:59 +09:00
|
|
|
|
|
|
|
Meteor.call('upvotePost', postId);
|
|
|
|
|
2012-10-24 11:04:42 +09:00
|
|
|
return post;
|
2012-10-04 11:45:12 +10:00
|
|
|
}
|
|
|
|
});
|