2013-04-13 15:05:18 +09:00
|
|
|
Posts = new Meteor.Collection('posts');
|
|
|
|
|
2012-10-24 11:04:42 +09:00
|
|
|
STATUS_PENDING=1;
|
|
|
|
STATUS_APPROVED=2;
|
|
|
|
STATUS_REJECTED=3;
|
|
|
|
|
2013-07-04 12:51:26 +09:00
|
|
|
Posts.deny({
|
|
|
|
update: function(userId, post, fieldNames) {
|
|
|
|
if(isAdminById(userId))
|
2013-07-05 07:09:15 +09:00
|
|
|
return false;
|
2013-11-05 09:32:21 +09:00
|
|
|
// deny the update if it contains something other than the following fields
|
2013-07-04 12:51:26 +09:00
|
|
|
return (_.without(fieldNames, 'headline', 'url', 'body', 'shortUrl', 'shortTitle', 'categories').length > 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-05 09:32:21 +09:00
|
|
|
Posts.allow({
|
|
|
|
insert: canPostById
|
|
|
|
, update: canEditById
|
|
|
|
, remove: canEditById
|
|
|
|
});
|
|
|
|
|
2013-11-06 10:11:35 +09:00
|
|
|
clickedPosts = [];
|
|
|
|
|
2012-10-04 11:45:12 +10:00
|
|
|
Meteor.methods({
|
|
|
|
post: function(post){
|
2013-04-26 17:28:09 +09:00
|
|
|
var headline = cleanUp(post.headline),
|
|
|
|
body = cleanUp(post.body),
|
|
|
|
user = Meteor.user(),
|
2013-11-05 18:46:25 -05:00
|
|
|
userId = user._id,
|
2013-04-26 17:28:09 +09:00
|
|
|
submitted = parseInt(post.submitted) || new Date().getTime(),
|
|
|
|
defaultStatus = getSetting('requirePostsApproval') ? STATUS_PENDING : STATUS_APPROVED,
|
|
|
|
status = post.status || defaultStatus,
|
2013-11-08 09:47:23 +09:00
|
|
|
postWithSameLink = Posts.findOne({url: post.url}), // TODO: limit scope of search to past month or something
|
2013-04-26 17:28:09 +09:00
|
|
|
timeSinceLastPost=timeSinceLast(user, Posts),
|
|
|
|
numberOfPostsInPast24Hours=numberOfItemsInPast24Hours(user, Posts),
|
|
|
|
postInterval = Math.abs(parseInt(getSetting('postInterval', 30))),
|
|
|
|
maxPostsPer24Hours = Math.abs(parseInt(getSetting('maxPostsPerDay', 30))),
|
|
|
|
postId = '';
|
2013-11-06 09:33:56 +09:00
|
|
|
|
|
|
|
// only let admins post as another user
|
|
|
|
if(isAdmin(Meteor.user()))
|
|
|
|
userId = post.userId || user._id
|
2013-11-05 18:46:25 -05: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
|
2013-10-29 17:54:45 +09:00
|
|
|
if(post.url && (typeof postWithSameLink !== 'undefined')){
|
|
|
|
Meteor.call('upvotePost', postWithSameLink);
|
2012-10-30 12:01:11 +09:00
|
|
|
throw new Meteor.Error(603, 'This link has already been posted', postWithSameLink._id);
|
|
|
|
}
|
2012-10-06 13:15:55 +09:00
|
|
|
|
2013-01-13 19:18:01 +09:00
|
|
|
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
|
|
|
|
2013-01-13 19:18:01 +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
|
|
|
|
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),
|
2013-01-13 08:52:35 +09:00
|
|
|
createdAt: new Date().getTime(),
|
2012-10-04 11:45:12 +10:00
|
|
|
votes: 0,
|
|
|
|
comments: 0,
|
|
|
|
baseScore: 0,
|
2012-10-24 11:04:42 +09:00
|
|
|
score: 0,
|
2012-12-24 10:59:13 +01:00
|
|
|
inactive: false,
|
2012-10-24 11:04:42 +09:00
|
|
|
status: status
|
2012-10-04 11:45:12 +10:00
|
|
|
});
|
2013-07-19 14:30:39 +03:00
|
|
|
|
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-09-06 16:05:38 +09:00
|
|
|
postId = Posts.insert(post);
|
2013-01-15 08:46:00 +09:00
|
|
|
|
2013-11-08 11:10:23 +09:00
|
|
|
// increment posts count
|
|
|
|
Meteor.users.update({_id: userId}, {$inc: {postCount: 1}});
|
|
|
|
|
2013-10-29 17:54:45 +09:00
|
|
|
post = _.extend(post, {_id: postId});
|
|
|
|
|
2013-01-19 22:42:59 +09:00
|
|
|
var postAuthor = Meteor.users.findOne(post.userId);
|
2013-10-29 17:54:45 +09:00
|
|
|
|
|
|
|
Meteor.call('upvotePost', post, postAuthor);
|
2013-01-19 22:42:59 +09:00
|
|
|
|
2013-10-23 10:21:08 +08:00
|
|
|
if(getSetting('emailNotifications', false)){
|
|
|
|
// notify users of new posts
|
2013-01-19 22:42:59 +09:00
|
|
|
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-10-23 10:21:08 +08:00
|
|
|
Meteor.call('notifyUsers', notification, Meteor.user(), function(error, result){
|
2013-07-19 14:30:39 +03:00
|
|
|
//run asynchronously
|
2013-01-19 22:42:59 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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){
|
2013-11-06 09:29:10 +09:00
|
|
|
// TODO: make post_edit server-side?
|
2013-07-04 12:51:26 +09:00
|
|
|
},
|
2013-11-06 10:11:35 +09:00
|
|
|
clickedPost: function(post, sessionId){
|
|
|
|
// only let clients increment a post's click counter once per session
|
|
|
|
var click = {_id: post._id, sessionId: sessionId};
|
|
|
|
if(_.where(clickedPosts, click).length == 0){
|
|
|
|
clickedPosts.push(click);
|
|
|
|
Posts.update(post._id, { $inc: { clicks: 1 }});
|
|
|
|
}
|
2013-07-19 14:30:39 +03:00
|
|
|
},
|
|
|
|
deletePostById: function(postId) {
|
|
|
|
// remove post comments
|
2013-11-08 11:10:23 +09:00
|
|
|
// if(!this.isSimulation) {
|
|
|
|
// Comments.remove({post: postId});
|
|
|
|
// }
|
|
|
|
// NOTE: actually, keep comments afer all
|
|
|
|
|
|
|
|
// decrement post count
|
|
|
|
Meteor.users.update({_id: post.userId}, {$inc: {postCount: -1}});
|
2013-07-19 14:30:39 +03:00
|
|
|
Posts.remove(postId);
|
2012-10-04 11:45:12 +10:00
|
|
|
}
|
2013-07-19 14:30:39 +03:00
|
|
|
});
|