post status scheduled + 10-minute cron to update scheduled posts to approved status

This commit is contained in:
xavizalote 2016-07-07 15:13:19 +02:00
parent eb50a2b3a3
commit ade25cc639
5 changed files with 65 additions and 6 deletions

View file

@ -160,11 +160,11 @@ function PostsNewSubmittedPropertiesCheck (post, user) {
}
});
// note: not needed there anymore, this is already set in the next callback 'posts.new.sync' with other related properties (status, createdAt)
// if no post status has been set, set it now
if (!post.status) {
post.status = Posts.getDefaultStatus(user);
}
// if (!post.status) {
// post.status = Posts.getDefaultStatus(user);
// }
// if no userId has been set, default to current user id
if (!post.userId) {
@ -197,7 +197,7 @@ function PostsNewRequiredPropertiesCheck (post, user) {
const defaultProperties = {
createdAt: new Date(),
author: Users.getDisplayNameById(post.userId),
status: Posts.getDefaultStatus()
status: Posts.getDefaultStatus(user)
};
post = _.extend(defaultProperties, post);
@ -205,6 +205,11 @@ function PostsNewRequiredPropertiesCheck (post, user) {
// generate slug
post.slug = Telescope.utils.slugify(post.title);
// post is not pending and has been scheduled to be posted in the future by a moderator/admin
if (post.status !== Posts.config.STATUS_PENDING && post.postedAt && post.postedAt > post.createdAt) {
post.status = Posts.config.STATUS_SCHEDULED;
}
// 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) {
@ -318,6 +323,12 @@ Telescope.callbacks.add("posts.edit.sync", PostsEditForceStickyToFalse);
* @summary Set postedAt date
*/
function PostsEditSetPostedAt (post, oldPost) {
// post is not pending and has been scheduled to be posted in the future by a moderator/admin
if (post.status !== Posts.config.STATUS_PENDING && post.postedAt && post.postedAt > new Date()) {
Posts.update(post._id, {$set: {status: Posts.config.STATUS_SCHEDULED}});
}
// 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) {

View file

@ -30,6 +30,10 @@ Posts.config.postStatuses = [
{
value: 5,
label: 'deleted'
},
{
value: 6,
label: 'scheduled'
}
];
@ -38,6 +42,7 @@ Posts.config.STATUS_APPROVED = 2;
Posts.config.STATUS_REJECTED = 3;
Posts.config.STATUS_SPAM = 4;
Posts.config.STATUS_DELETED = 5;
Posts.config.STATUS_SCHEDULED = 6;
const adminGroup = {
name: "admin",

View file

@ -2,5 +2,6 @@ import Posts from './modules.js';
import './server/publications.js';
import './server/routes.js';
import './server/cron.js';
export default Posts;

View file

@ -0,0 +1,40 @@
import Posts from '../collection.js';
import moment from 'moment';
SyncedCron.options = {
log: true,
collectionName: 'cronHistory',
utc: false,
collectionTTL: 172800
};
const addJob = function () {
SyncedCron.add({
name: 'checkScheduledPosts',
schedule(parser) {
return parser.text('every 10 minutes');
},
job() {
// fetch all posts tagged as scheduled
const scheduledPosts = Posts.find({status: Posts.config.STATUS_SCHEDULED}, {fields: {_id: 1, status: 1, postedAt: 1, userId: 1, title: 1}}).fetch();
if (scheduledPosts) {
// filter the scheduled posts to retrieve only the one that should update, considering their schedule
const postsToUpdate = scheduledPosts.filter(post => post.postedAt <= new Date());
// update all posts with status approved
const postsIds = _.pluck(postsToUpdate, '_id');
Posts.update({_id: {$in: postsIds}}, {$set: {status: Posts.config.STATUS_APPROVED}}, {multi: true});
// log the action
console.log('// Scheduled posts approved:', postsIds);
}
}
});
};
Meteor.startup(function () {
addJob();
});

View file

@ -83,7 +83,9 @@ Posts.views.add("rejected", function (terms) {
*/
Posts.views.add("scheduled", function (terms) {
return {
selector: {postedAt: {$gte: new Date()}},
selector: {
status: Posts.config.STATUS_SCHEDULED
},
options: {sort: {postedAt: -1}},
showFuture: true
};