2016-11-14 17:17:44 +09:00
|
|
|
import Posts from '../collection.js'
|
2017-01-23 15:50:55 +01:00
|
|
|
import marked from 'marked';
|
2016-11-14 17:17:44 +09:00
|
|
|
import Users from 'meteor/nova:users';
|
2017-02-02 15:15:51 +01:00
|
|
|
import { addCallback, getSetting, Utils } from 'meteor/nova:core';
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// posts.new.validate //
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Rate limiting
|
|
|
|
*/
|
|
|
|
function PostsNewRateLimit (post, user) {
|
|
|
|
|
|
|
|
if(!Users.isAdmin(user)){
|
|
|
|
|
|
|
|
var timeSinceLastPost = Users.timeSinceLast(user, Posts),
|
|
|
|
numberOfPostsInPast24Hours = Users.numberOfItemsInPast24Hours(user, Posts),
|
2016-12-12 15:00:56 +09:00
|
|
|
postInterval = Math.abs(parseInt(getSetting('postInterval', 30))),
|
2017-02-04 11:46:40 +09:00
|
|
|
maxPostsPer24Hours = Math.abs(parseInt(getSetting('maxPostsPerDay', 5)));
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
// check that user waits more than X seconds between posts
|
2017-02-04 11:46:40 +09:00
|
|
|
// if(timeSinceLastPost < postInterval)
|
|
|
|
// throw new Error(Utils.encodeIntlError({id: "posts.rate_limit_error", value: postInterval-timeSinceLastPost}));
|
|
|
|
|
|
|
|
console.log(numberOfPostsInPast24Hours)
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
// check that the user doesn't post more than Y posts per day
|
2017-02-04 11:46:40 +09:00
|
|
|
if(numberOfPostsInPast24Hours >= maxPostsPer24Hours)
|
|
|
|
throw new Error(Utils.encodeIntlError({id: "posts.max_per_day", value: maxPostsPer24Hours}));
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("posts.new.validate", PostsNewRateLimit);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// posts.new.sync //
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Check for duplicate links
|
|
|
|
*/
|
|
|
|
function PostsNewDuplicateLinksCheck (post, user) {
|
|
|
|
if(!!post.url) {
|
|
|
|
Posts.checkForSameUrl(post.url);
|
|
|
|
}
|
|
|
|
return post;
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("posts.new.sync", PostsNewDuplicateLinksCheck);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Check for necessary properties
|
|
|
|
*/
|
|
|
|
// function PostsNewRequiredPropertiesCheck (post, user) {
|
|
|
|
|
|
|
|
// // initialize default properties
|
|
|
|
// const defaultProperties = {
|
|
|
|
// createdAt: new Date(),
|
|
|
|
// author: Users.getDisplayNameById(post.userId),
|
|
|
|
// status: Posts.getDefaultStatus(user)
|
|
|
|
// };
|
|
|
|
|
|
|
|
// post = _.extend(defaultProperties, post);
|
|
|
|
|
|
|
|
// // generate slug
|
2016-12-12 11:34:28 +09:00
|
|
|
// post.slug = Utils.slugify(post.title);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
// // if post is approved but doesn't have a postedAt date, give it a default date
|
|
|
|
// // note: pending posts get their postedAt date only once theyre approved
|
|
|
|
// if (Posts.isApproved(post) && !post.postedAt) {
|
|
|
|
// post.postedAt = new Date();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return post;
|
|
|
|
// }
|
2016-12-13 11:40:24 +09:00
|
|
|
// addCallback("posts.new.sync", PostsNewRequiredPropertiesCheck);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
2016-11-30 16:54:58 +09:00
|
|
|
/**
|
|
|
|
* @summary Set the post's postedAt if it's approved
|
|
|
|
*/
|
|
|
|
function PostsSetPostedAt (post, user) {
|
|
|
|
if (!post.postedAt) post.postedAt = new Date();
|
|
|
|
return post;
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("posts.new.sync", PostsSetPostedAt);
|
2016-11-30 16:54:58 +09:00
|
|
|
|
2016-11-14 17:17:44 +09:00
|
|
|
/**
|
|
|
|
* @summary Set the post's isFuture to true if necessary
|
|
|
|
*/
|
|
|
|
function PostsNewSetFuture (post, user) {
|
|
|
|
post.isFuture = post.postedAt && new Date(post.postedAt).getTime() > new Date(post.createdAt).getTime() + 1000; // round up to the second
|
|
|
|
return post;
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("posts.new.sync", PostsNewSetFuture);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
2017-01-23 17:39:46 +01:00
|
|
|
/**
|
|
|
|
* @summary Set the post's slug based on its title
|
|
|
|
*/
|
2017-01-23 15:50:55 +01:00
|
|
|
const PostsNewSlugify = post => {
|
2017-01-23 17:39:46 +01:00
|
|
|
post.slug = Utils.slugify(post.title);
|
2017-01-23 15:50:55 +01:00
|
|
|
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
|
|
|
addCallback("posts.new.sync", PostsNewSlugify);
|
|
|
|
|
2017-01-23 17:39:46 +01:00
|
|
|
/**
|
|
|
|
* @summary Set the post's HTML content & the excerpt based on its possible body
|
|
|
|
*/
|
2017-01-23 15:50:55 +01:00
|
|
|
const PostsNewHTMLContent = post => {
|
|
|
|
if (post.body) {
|
|
|
|
// excerpt length is configurable via the settings (30 words by default, ~255 characters)
|
|
|
|
const excerptLength = getSetting('postExcerptLength', 30);
|
|
|
|
|
|
|
|
// extend the post document
|
|
|
|
post = {
|
|
|
|
...post,
|
|
|
|
htmlBody: Utils.sanitize(marked(post.body)),
|
|
|
|
excerpt: Utils.trimHTML(Utils.sanitize(marked(post.body)), excerptLength),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
addCallback("posts.new.sync", PostsNewHTMLContent);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// posts.new.async //
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Increment the user's post count
|
|
|
|
*/
|
|
|
|
function PostsNewIncrementPostCount (post) {
|
|
|
|
var userId = post.userId;
|
2017-01-18 10:18:33 +09:00
|
|
|
Users.update({_id: userId}, {$inc: {"postCount": 1}});
|
2016-11-14 17:17:44 +09:00
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("posts.new.async", PostsNewIncrementPostCount);
|