clean up post callbacks

This commit is contained in:
SachaG 2017-09-05 15:45:14 +02:00
parent 32e90e1b9a
commit fb6287d45d
10 changed files with 191 additions and 122 deletions

View file

@ -1 +0,0 @@
export * from '../modules/categories/collection.js';

View file

@ -1 +0,0 @@
export * from '../modules/comments/collection.js';

View file

@ -1 +0,0 @@
export * from '../modules/posts/collection.js';

View file

@ -1,6 +1,6 @@
import Users from 'meteor/vulcan:users';
Users.avatar.setOptions({
"gravatarDefault": "mm",
"defaultImageUrl": "http://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&f=y"
'gravatarDefault': 'mm',
'defaultImageUrl': 'http://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm&f=y'
});

View file

@ -15,7 +15,7 @@ export const createNotification = (userIds, notificationName, variables) => {
if (to) {
VulcanEmail.buildAndSend({ to, emailName, variables });
} else {
console.log(`// Couldn't send notification: admin user ${user._id} doesn't have an email`); // eslint-disable-line
console.log(`// Couldn't send notification: user ${user._id} doesn't have an email`); // eslint-disable-line
}
});
}

View file

@ -6,10 +6,10 @@ import Events from 'meteor/vulcan:events';
// ------------------------------------- posts.remove.sync -------------------------------- //
function PostsRemoveOperations (post) {
Users.update({_id: post.userId}, {$inc: {"postCount": -1}});
Users.update({_id: post.userId}, {$inc: {'postCount': -1}});
return post;
}
addCallback("posts.remove.sync", PostsRemoveOperations);
addCallback('posts.remove.sync', PostsRemoveOperations);
// ------------------------------------- posts.approve.async -------------------------------- //
@ -25,7 +25,7 @@ function PostsSetPostedAt (modifier, post) {
}
return modifier;
}
addCallback("posts.approve.sync", PostsSetPostedAt);
addCallback('posts.approve.sync', PostsSetPostedAt);
// ------------------------------------- users.remove.async -------------------------------- //
@ -34,10 +34,10 @@ function UsersRemoveDeletePosts (user, options) {
Posts.remove({userId: user._id});
} else {
// not sure if anything should be done in that scenario yet
// Posts.update({userId: userId}, {$set: {author: "\[deleted\]"}}, {multi: true});
// Posts.update({userId: userId}, {$set: {author: '\[deleted\]'}}, {multi: true});
}
}
addCallback("users.remove.async", UsersRemoveDeletePosts);
addCallback('users.remove.async', UsersRemoveDeletePosts);
// /**

View file

@ -15,90 +15,90 @@ function PostsEditDuplicateLinksCheck (modifier, post) {
Posts.checkForSameUrl(modifier.$set.url);
}
return modifier;
};
addCallback("posts.edit.sync", PostsEditDuplicateLinksCheck);
}
addCallback('posts.edit.sync', PostsEditDuplicateLinksCheck);
/**
* @summary Force sticky to default to false when it's not specified
* (simpleSchema's defaultValue does not work on edit, so do it manually in callback)
*/
function PostsEditForceStickyToFalse (modifier, post) {
if (!modifier.$set.sticky) {
if (modifier.$unset && modifier.$unset.sticky) {
delete modifier.$unset.sticky;
}
modifier.$set.sticky = false;
}
return modifier;
}
addCallback("posts.edit.sync", PostsEditForceStickyToFalse);
// function PostsEditForceStickyToFalse (modifier, post) {
// if (!modifier.$set.sticky) {
// if (modifier.$unset && modifier.$unset.sticky) {
// delete modifier.$unset.sticky;
// }
// modifier.$set.sticky = false;
// }
// return modifier;
// }
// addCallback('posts.edit.sync', PostsEditForceStickyToFalse);
/**
* @summary Set status
*/
function PostsEditSetIsFuture (modifier, post) {
const postTime = new Date(modifier.$set.postedAt).getTime();
const currentTime = new Date().getTime() + 1000; // why "+ 1000" ??
if (modifier.$set.postedAt) {
if (postTime > currentTime) {
// if a post's postedAt date is in the future, set isFuture to true
modifier.$set.isFuture = true;
} else if (post.isFuture) {
// else if a post has isFuture to true but its date is in the past, set isFuture to false
modifier.$set.isFuture = false;
}
}
return modifier;
}
addCallback("posts.edit.sync", PostsEditSetIsFuture);
// function PostsEditSetIsFuture (modifier, post) {
// const postTime = new Date(modifier.$set.postedAt).getTime();
// const currentTime = new Date().getTime() + 1000; // why '+ 1000' ??
// if (modifier.$set.postedAt) {
// if (postTime > currentTime) {
// // if a post's postedAt date is in the future, set isFuture to true
// modifier.$set.isFuture = true;
// } else if (post.isFuture) {
// // else if a post has isFuture to true but its date is in the past, set isFuture to false
// modifier.$set.isFuture = false;
// }
// }
// return modifier;
// }
// addCallback('posts.edit.sync', PostsEditSetIsFuture);
function PostsEditRunPostApprovedSyncCallbacks (modifier, post) {
if (modifier.$set && Posts.isApproved(modifier.$set) && !Posts.isApproved(post)) {
modifier = runCallbacks("posts.approve.sync", modifier, post);
modifier = runCallbacks('posts.approve.sync', modifier, post);
}
return modifier;
}
addCallback("posts.edit.sync", PostsEditRunPostApprovedSyncCallbacks);
addCallback('posts.edit.sync', PostsEditRunPostApprovedSyncCallbacks);
/**
* @summary If title is changing, return new slug
*/
function PostsEditSlugify (modifier, post) {
if (modifier.$set && modifier.$set.title) {
modifier.$set.slug = Utils.slugify(modifier.$set.title);
}
return modifier;
}
// function PostsEditSlugify (modifier, post) {
// if (modifier.$set && modifier.$set.title) {
// modifier.$set.slug = Utils.slugify(modifier.$set.title);
// }
// return modifier;
// }
addCallback("posts.edit.sync", PostsEditSlugify);
// addCallback('posts.edit.sync', PostsEditSlugify);
/**
* @summary If body is changing, update related fields (htmlBody & excerpt)
*/
function PostsEditHTMLContent (modifier, post) {
if (modifier.$set && typeof modifier.$set.body !== 'undefined') {
// excerpt length is configurable via the settings (30 words by default, ~255 characters)
const excerptLength = getSetting('postExcerptLength', 30);
// function PostsEditHTMLContent (modifier, post) {
// if (modifier.$set && typeof modifier.$set.body !== 'undefined') {
// // excerpt length is configurable via the settings (30 words by default, ~255 characters)
// const excerptLength = getSetting('postExcerptLength', 30);
// extend the modifier
modifier.$set = {
...modifier.$set,
htmlBody: Utils.sanitize(marked(modifier.$set.body)),
excerpt: Utils.trimHTML(Utils.sanitize(marked(modifier.$set.body)), excerptLength),
};
} else if (modifier.$unset && modifier.$unset.body) {
// extend the modifier
modifier.$unset = {
...modifier.$unset,
htmlBody: true,
excerpt: true,
};
}
// // extend the modifier
// modifier.$set = {
// ...modifier.$set,
// htmlBody: Utils.sanitize(marked(modifier.$set.body)),
// excerpt: Utils.trimHTML(Utils.sanitize(marked(modifier.$set.body)), excerptLength),
// };
// } else if (modifier.$unset && modifier.$unset.body) {
// // extend the modifier
// modifier.$unset = {
// ...modifier.$unset,
// htmlBody: true,
// excerpt: true,
// };
// }
return modifier;
}
addCallback("posts.edit.sync", PostsEditHTMLContent);
// return modifier;
// }
// addCallback('posts.edit.sync', PostsEditHTMLContent);
//////////////////////////////////////////////////////
// posts.edit.async //
@ -106,7 +106,7 @@ addCallback("posts.edit.sync", PostsEditHTMLContent);
function PostsEditRunPostApprovedAsyncCallbacks (post, oldPost) {
if (Posts.isApproved(post) && !Posts.isApproved(oldPost)) {
runCallbacksAsync("posts.approve.async", post);
runCallbacksAsync('posts.approve.async', post);
}
}
addCallback("posts.edit.async", PostsEditRunPostApprovedAsyncCallbacks);
addCallback('posts.edit.async', PostsEditRunPostApprovedAsyncCallbacks);

View file

@ -35,7 +35,7 @@ function PostsNewRateLimit (post, user) {
return post;
}
addCallback("posts.new.validate", PostsNewRateLimit);
addCallback('posts.new.validate', PostsNewRateLimit);
//////////////////////////////////////////////////////
// posts.new.sync //
@ -52,66 +52,66 @@ function PostsNewDuplicateLinksCheck (post, user) {
}
return post;
}
addCallback("posts.new.sync", PostsNewDuplicateLinksCheck);
addCallback('posts.new.sync', PostsNewDuplicateLinksCheck);
/**
* @summary Set the post's postedAt if it's going to be approved
*/
function PostsSetPostedAt (post, user) {
if (!post.postedAt && Posts.getDefaultStatus(user) === Posts.config.STATUS_APPROVED) post.postedAt = new Date();
return post;
}
addCallback("posts.new.sync", PostsSetPostedAt);
// function PostsSetPostedAt (post, user) {
// if (!post.postedAt && Posts.getDefaultStatus(user) === Posts.config.STATUS_APPROVED) post.postedAt = new Date();
// return post;
// }
// addCallback('posts.new.sync', PostsSetPostedAt);
/**
* @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;
}
addCallback("posts.new.sync", PostsNewSetFuture);
// 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;
// }
// addCallback('posts.new.sync', PostsNewSetFuture);
/**
* @summary Force sticky to default to false when it's not specified
*/
function PostsNewSetStickyToFalse (post, user) {
if (!post.sticky) {
post.sticky = false;
}
return post;
}
addCallback("posts.new.sync", PostsNewSetStickyToFalse);
// function PostsNewSetStickyToFalse (post, user) {
// if (!post.sticky) {
// post.sticky = false;
// }
// return post;
// }
// addCallback('posts.new.sync', PostsNewSetStickyToFalse);
/**
* @summary Set the post's slug based on its title
*/
function PostsNewSlugify (post) {
post.slug = Utils.slugify(post.title);
return post;
}
addCallback("posts.new.sync", PostsNewSlugify);
// function PostsNewSlugify (post) {
// post.slug = Utils.slugify(post.title);
// return post;
// }
// addCallback('posts.new.sync', PostsNewSlugify);
/**
* @summary Set the post's HTML content & the excerpt based on its possible body
*/
function PostsNewHTMLContent (post) {
if (post.body) {
// excerpt length is configurable via the settings (30 words by default, ~255 characters)
const excerptLength = getSetting('postExcerptLength', 30);
// function 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),
};
}
// // 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);
// return post;
// }
// addCallback('posts.new.sync', PostsNewHTMLContent);
//////////////////////////////////////////////////////
// posts.new.async //
@ -123,6 +123,6 @@ addCallback("posts.new.sync", PostsNewHTMLContent);
*/
function PostsNewIncrementPostCount (post) {
var userId = post.userId;
Users.update({_id: userId}, {$inc: {"postCount": 1}});
Users.update({_id: userId}, {$inc: {'postCount': 1}});
}
addCallback("posts.new.async", PostsNewIncrementPostCount);
addCallback('posts.new.async', PostsNewIncrementPostCount);

View file

@ -6,8 +6,9 @@ Posts schema
import Users from 'meteor/vulcan:users';
import Posts from './collection.js';
import { Utils } from 'meteor/vulcan:core';
import { Utils, getSetting } from 'meteor/vulcan:core';
import moment from 'moment';
import marked from 'marked';
/**
* @summary Posts config namespace
@ -15,7 +16,7 @@ import moment from 'moment';
*/
const formGroups = {
admin: {
name: "admin",
name: 'admin',
order: 2
}
};
@ -40,7 +41,7 @@ const schema = {
type: Date,
optional: true,
viewableBy: ['admins'],
onInsert: (document, currentUser) => {
onInsert: () => {
return new Date();
}
},
@ -53,8 +54,14 @@ const schema = {
viewableBy: ['guests'],
insertableBy: ['admins'],
editableBy: ['admins'],
control: "datetime",
group: formGroups.admin
control: 'datetime',
group: formGroups.admin,
onInsert: (post, currentUser) => {
// Set the post's postedAt if it's going to be approved
if (!post.postedAt && Posts.getDefaultStatus(currentUser) === Posts.config.STATUS_APPROVED) {
return new Date();
}
}
},
/**
URL
@ -66,7 +73,7 @@ const schema = {
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
control: "url",
control: 'url',
order: 10,
searchable: true
},
@ -80,7 +87,7 @@ const schema = {
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
control: "text",
control: 'text',
order: 20,
searchable: true
},
@ -91,6 +98,14 @@ const schema = {
type: String,
optional: true,
viewableBy: ['guests'],
onInsert: (post) => {
return Utils.slugify(post.title);
},
onEdit: (modifier, post) => {
if (modifier.$set.title) {
return Utils.slugify(modifier.$set.title);
}
}
},
/**
Post body (markdown)
@ -102,7 +117,7 @@ const schema = {
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
control: "textarea",
control: 'textarea',
order: 30
},
/**
@ -112,6 +127,16 @@ const schema = {
type: String,
optional: true,
viewableBy: ['guests'],
onInsert: (post) => {
if (post.body) {
return Utils.sanitize(marked(post.body));
}
},
onEdit: (modifier, post) => {
if (modifier.$set.body) {
return Utils.sanitize(marked(modifier.$set.body));
}
}
},
/**
Post Excerpt
@ -120,7 +145,20 @@ const schema = {
type: String,
optional: true,
viewableBy: ['guests'],
searchable: true
searchable: true,
onInsert: (post) => {
if (post.body) {
// excerpt length is configurable via the settings (30 words by default, ~255 characters)
const excerptLength = getSetting('postExcerptLength', 30);
return Utils.trimHTML(Utils.sanitize(marked(post.body)), excerptLength);
}
},
onEdit: (modifier, post) => {
if (modifier.$set.body) {
const excerptLength = getSetting('postExcerptLength', 30);
return Utils.trimHTML(Utils.sanitize(marked(modifier.$set.body)), excerptLength);
}
}
},
/**
Count of how many times the post's page was viewed
@ -178,6 +216,28 @@ const schema = {
type: Boolean,
optional: true,
viewableBy: ['guests'],
onInsert: (post) => {
// Set the post's isFuture to true if necessary
if (post.postedAt) {
const postTime = new Date(post.postedAt).getTime();
const currentTime = new Date().getTime() + 1000;
return postTime > currentTime; // round up to the second
}
},
onEdit: (modifier, post) => {
// Set the post's isFuture to true if necessary
if (modifier.$set.postedAt) {
const postTime = new Date(modifier.$set.postedAt).getTime();
const currentTime = new Date().getTime() + 1000;
if (postTime > currentTime) {
// if a post's postedAt date is in the future, set isFuture to true
return true;
} else if (post.isFuture) {
// else if a post has isFuture to true but its date is in the past, set isFuture to false
return false;
}
}
}
},
/**
Whether the post is sticky (pinned to the top of posts lists)
@ -189,8 +249,18 @@ const schema = {
viewableBy: ['guests'],
insertableBy: ['admins'],
editableBy: ['admins'],
control: "checkbox",
group: formGroups.admin
control: 'checkbox',
group: formGroups.admin,
onInsert: (post) => {
if(!post.sticky) {
return false;
}
},
onEdit: (modifier, post) => {
if (!modifier.$set.sticky) {
return false;
}
}
},
/**
Save info for later spam checking on a post. We will use this for the akismet package
@ -230,7 +300,7 @@ const schema = {
userId: {
type: String,
optional: true,
control: "select",
control: 'select',
viewableBy: ['guests'],
insertableBy: ['members'],
hidden: true,

View file

@ -139,6 +139,8 @@ export const editMutation = async ({ collection, documentId, set, unset, current
modifier.$unset[fieldName] = true;
} else {
modifier.$set[fieldName] = autoValue;
// make sure we don't try to unset the same field at the same time
delete modifier.$unset[fieldName];
}
}
}