Vulcan/common/post.js

95 lines
3.3 KiB
JavaScript
Raw Normal View History

2012-10-24 11:04:42 +09:00
STATUS_PENDING=1;
STATUS_APPROVED=2;
STATUS_REJECTED=3;
Meteor.methods({
post: function(post){
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();
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);
var postInterval = Math.abs(parseInt(getSetting('postInterval'))) || 30;
2012-12-22 11:06:28 +01:00
var maxPostsPer24Hours = Math.abs(parseInt(getSetting('maxPostsPerDay'))) || 30;
2013-01-13 08:52:35 +09:00
var postId = '';
// check that user can post
2012-10-05 13:59:40 +09:00
if (!user || !canPost(user))
throw new Meteor.Error(601, 'You need to login or be invited to post new stories.');
2012-10-10 08:32:49 +09:00
// check that user provided a headline
2012-10-11 13:21:10 +09:00
if(!post.headline)
throw new Meteor.Error(602, 'Please fill in a headline');
2012-10-05 13:59:40 +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);
}
if(!isAdmin(Meteor.user())){
// check that user waits more than X seconds between posts
if(!this.isSimulation && timeSinceLastPost < postInterval)
throw new Meteor.Error(604, 'Please wait '+(postInterval-timeSinceLastPost)+' seconds before posting again');
2012-10-24 11:04:42 +09:00
// check that the user doesn't post more than Y posts per day
if(!this.isSimulation && numberOfPostsInPast24Hours > maxPostsPer24Hours)
throw new Meteor.Error(605, 'Sorry, you cannot submit more than '+maxPostsPer24Hours+' posts per day');
}
2012-10-23 12:24:38 +09:00
post = _.extend(post, {
headline: headline,
body: body,
2012-10-23 12:24:38 +09:00
userId: userId,
author: getDisplayNameById(userId),
2013-01-13 08:52:35 +09:00
createdAt: new Date().getTime(),
votes: 0,
comments: 0,
baseScore: 0,
2012-10-24 11:04:42 +09:00
score: 0,
inactive: false,
2012-10-24 11:04:42 +09:00
status: status
});
2013-01-13 08:52:35 +09:00
if(status == STATUS_APPROVED){
// if post is approved, set its submitted date (if post is pending, submitted date is left blank)
post.submitted = submitted;
}
2012-10-04 15:26:59 +09:00
2013-01-13 08:52:35 +09:00
Posts.insert(post, function(error, result){
if(result){
postId = result;
}
});
2013-01-15 08:46:00 +09:00
var postAuthor = Meteor.users.findOne(post.userId);
Meteor.call('upvotePost', postId,postAuthor);
if(getSetting('newPostsNotifications')){
2013-01-19 22:47:35 +09:00
// notify admin of new posts
var properties = {
postAuthorName : getDisplayName(postAuthor),
postAuthorId : post.userId,
postHeadline : headline,
postId : postId
}
var notification = getNotification('newPost', properties);
2013-01-19 22:47:35 +09:00
// call a server method because we do not have access to admin users' info on the client
2013-01-19 23:01:43 +09:00
Meteor.call('notifyAdmins', notification, Meteor.user(), function(error, result){
2013-01-19 22:47:35 +09:00
//run asynchronously
});
}
2013-01-13 08:52:35 +09:00
// add the post's own ID to the post object and return it to the client
post.postId = postId;
2012-10-24 11:04:42 +09:00
return post;
2013-01-13 08:52:35 +09:00
},
2013-01-13 10:21:09 +09:00
post_edit: function(post){
//TO-DO: make post_edit server-side?
}
});