Replace autoValue with onInsert, onEdit, onRemove

This commit is contained in:
SachaG 2017-04-28 09:24:28 +09:00
parent 1f9e5c17b9
commit c9c2030752
9 changed files with 104 additions and 90 deletions

View file

@ -17,9 +17,8 @@ const schema = {
type: Date,
optional: true,
viewableBy: ['guests'],
autoValue: (documentOrModifier) => {
// if this is an insert, set createdAt to current timestamp
if (documentOrModifier && !documentOrModifier.$set) return new Date()
onInsert: (document, currentUser) => {
return new Date();
}
},
userId: {

View file

@ -19,9 +19,8 @@ const schema = {
type: Date,
optional: true,
viewableBy: ['guests'],
autoValue: (documentOrModifier) => {
// if this is an insert, set createdAt to current timestamp
if (documentOrModifier && !documentOrModifier.$set) return new Date()
onInsert: (document, currentUser) => {
return new Date();
}
},
userId: {

View file

@ -17,8 +17,8 @@ const schema = {
type: Date,
optional: true,
viewableBy: ['guests'],
autoValue: (documentOrModifier) => {
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
onInsert: (document, currentUser) => {
return new Date();
}
},
userId: {

View file

@ -46,8 +46,8 @@ const schema = {
type: Date,
optional: true,
viewableBy: ['admins'],
autoValue: (documentOrModifier) => {
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
onInsert: (document, currentUser) => {
return new Date();
}
},
/**
@ -57,8 +57,8 @@ const schema = {
type: Date,
optional: true,
viewableBy: ['guests'],
autoValue: (documentOrModifier) => {
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
onInsert: (document, currentUser) => {
return new Date();
}
},
/**
@ -87,10 +87,11 @@ const schema = {
type: String,
optional: true,
viewableBy: ['guests'],
autoValue: (documentOrModifier) => {
onEdit: (modifier, document, currentUser) => {
// if userId is changing, change the author name too
const userId = documentOrModifier.userId || documentOrModifier.$set && documentOrModifier.$set.userId
if (userId) return Users.getDisplayNameById(userId)
if (modifier.$set && modifier.$set.userId) {
return Users.getDisplayNameById(modifier.$set.userId)
}
}
},
/**

View file

@ -22,7 +22,10 @@ SimpleSchema.extendOptions([
'autoform', // legacy form placeholder; backward compatibility (not used anymore)
'control', // SmartForm control (String or React component)
'order', // position in the form
'group' // form fieldset group
'group', // form fieldset group
'onInsert', // field insert callback
'onEdit', // field edit callback
'onRemove', // field remove callback
]);
export default Vulcan;

View file

@ -63,11 +63,11 @@ export const newMutation = ({ collection, document, currentUser, validate, conte
const userIdInSchema = Object.keys(schema).find(key => key === 'userId');
if (!!userIdInSchema && !newDocument.userId) newDocument.userId = currentUser._id;
// run autoValue step
// run onInsert step
_.keys(schema).forEach(fieldName => {
if (!newDocument[fieldName] && schema[fieldName].autoValue) {
const autoValue = schema[fieldName].autoValue(newDocument);
if (autoValue && typeof autoValue.$setOnInsert === 'undefined') {
if (!newDocument[fieldName] && schema[fieldName].onInsert) {
const autoValue = schema[fieldName].onInsert(newDocument, currentUser);
if (autoValue) {
newDocument[fieldName] = autoValue;
}
}
@ -134,11 +134,11 @@ export const editMutation = ({ collection, documentId, set, unset, currentUser,
modifier = runCallbacks(`${collectionName}.edit.validate`, modifier, document, currentUser);
}
// run autoValue step
// run onEdit step
_.keys(schema).forEach(fieldName => {
if (!modifier.$set[fieldName] && schema[fieldName].autoValue) {
const autoValue = schema[fieldName].autoValue(modifier);
if (autoValue && typeof autoValue.$setOnInsert === 'undefined') {
if (!document[fieldName] && schema[fieldName].onEdit) {
const autoValue = schema[fieldName].onEdit(modifier, document, currentUser);
if (autoValue) {
modifier.$set[fieldName] = autoValue;
}
}
@ -183,6 +183,7 @@ export const removeMutation = ({ collection, documentId, currentUser, validate,
// console.log(documentId)
const collectionName = collection._name;
const schema = collection.simpleSchema()._schema;
let document = collection.findOne(documentId);
@ -191,6 +192,13 @@ export const removeMutation = ({ collection, documentId, currentUser, validate,
document = runCallbacks(`${collectionName}.remove.validate`, document, currentUser);
}
// run onRemove step
_.keys(schema).forEach(fieldName => {
if (!document[fieldName] && schema[fieldName].onRemove) {
schema[fieldName].onRemove(document, currentUser);
}
});
runCallbacks(`${collectionName}.remove.sync`, document, currentUser);
collection.remove(documentId);

View file

@ -32,8 +32,8 @@ const schema = {
type: Date,
optional: true,
viewableBy: ['admins'],
autoValue: (documentOrModifier) => {
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
onInsert: (document, currentUser) => {
return new Date();
}
},
/**
@ -147,10 +147,9 @@ const schema = {
insertableBy: ['admins'],
editableBy: ['admins'],
control: "select",
autoValue(documentOrModifier) {
// provide a default value if this is an insert operation and status field is not set in the document
if (documentOrModifier && !documentOrModifier.$set && documentOrModifier.userId && !documentOrModifier.status) {
const user = Users.findOne(documentOrModifier.userId);
onInsert: document => {
if (document.userId && !document.status) {
const user = Users.findOne(document.userId);
return Posts.getDefaultStatus(user);
}
},
@ -215,10 +214,11 @@ const schema = {
type: String,
optional: true,
viewableBy: ['guests'],
autoValue: (documentOrModifier) => {
onEdit: (modifier, document, currentUser) => {
// if userId is changing, change the author name too
const userId = documentOrModifier.userId || documentOrModifier.$set && documentOrModifier.$set.userId
if (userId) return Users.getDisplayNameById(userId)
if (modifier.$set && modifier.$set.userId) {
return Users.getDisplayNameById(modifier.$set.userId)
}
}
},
/**

View file

@ -12,65 +12,21 @@ import { addCallback, Utils, runCallbacksAsync } from 'meteor/vulcan:lib'; // im
* @param {Object} options user options
*/
function setupUser (user, options) {
const schema = Users.simpleSchema()._schema;
// ------------------------------ Properties ------------------------------ //
var userProperties = {
profile: options.profile || {},
karma: 0,
isInvited: false,
postCount: 0,
commentCount: 0,
invitedCount: 0,
upvotedPosts: [],
downvotedPosts: [],
upvotedComments: [],
downvotedComments: []
};
user = _.extend(user, userProperties);
user.profile = options.profile || {};
// look in a few places for the user email
if (options.email) {
user.email = options.email;
} else if (user.services['meteor-developer'] && user.services['meteor-developer'].emails) {
user.email = _.findWhere(user.services['meteor-developer'].emails, { primary: true }).address;
} else if (user.services.facebook && user.services.facebook.email) {
user.email = user.services.facebook.email;
} else if (user.services.github && user.services.github.email) {
user.email = user.services.github.email;
} else if (user.services.google && user.services.google.email) {
user.email = user.services.google.email;
} else if (user.services.linkedin && user.services.linkedin.emailAddress) {
user.email = user.services.linkedin.emailAddress;
// run onInsert step
_.keys(schema).forEach(fieldName => {
if (!user[fieldName] && schema[fieldName].onInsert) {
const autoValue = schema[fieldName].onInsert(user, options);
if (autoValue) {
user[fieldName] = autoValue;
}
// generate email hash
if (!!user.email) {
user.emailHash = Users.avatar.hash(user.email);
}
// look in a few places for the displayName
if (user.profile.username) {
user.displayName = user.profile.username;
} else if (user.profile.name) {
user.displayName = user.profile.name;
} else if (user.services.linkedin && user.services.linkedin.firstName) {
user.displayName = user.services.linkedin.firstName + " " + user.services.linkedin.lastName;
} else {
user.displayName = user.username;
}
// add Twitter username
if (user.services && user.services.twitter && user.services.twitter.screenName) {
user.twitterUsername = user.services.twitter.screenName;
}
// create a basic slug from display name and then modify it if this slugs already exists;
const basicSlug = Utils.slugify(user.displayName);
user.slug = Utils.getUnusedSlug(Users, basicSlug);
// if this is not a dummy account, and is the first user ever, make them an admin
user.isAdmin = (!user.profile.isDummy && Users.find({'profile.isDummy': {$ne: true}}).count() === 0) ? true : false;
// Events.track('new user', {username: user.displayName, email: user.email});
});
return user;
}

View file

@ -1,5 +1,6 @@
import SimpleSchema from 'simpl-schema';
import Users from './collection.js';
import { Utils } from 'meteor/vulcan:lib'; // import from vulcan:lib because vulcan:core isn't loaded yet
const adminGroup = {
name: "admin",
@ -56,6 +57,11 @@ const schema = {
editableBy: ['admins'],
viewableBy: ['guests'],
group: adminGroup,
onInsert: user => {
// if this is not a dummy account, and is the first user ever, make them an admin
const realUsersCount = Users.find({'profile.isDummy': {$ne: true}}).count();
return (!user.profile.isDummy && realUsersCount === 0) ? true : false;
}
},
profile: {
type: Object,
@ -95,6 +101,17 @@ const schema = {
insertableBy: ['members'],
editableBy: ['members'],
viewableBy: ['guests'],
onInsert: (user, options) => {
if (user.profile.username) {
return user.profile.username;
} else if (user.profile.name) {
return user.profile.name;
} else if (user.services.linkedin && user.services.linkedin.firstName) {
return user.services.linkedin.firstName + " " + user.services.linkedin.lastName;
} else {
return user.username;
}
}
},
/**
The user's email. Modifiable.
@ -108,6 +125,22 @@ const schema = {
insertableBy: ['members'],
editableBy: ['members'],
viewableBy: ownsOrIsAdmin,
onInsert: (user, options) => {
// look in a few places for the user email
if (options.email) {
return options.email;
} else if (user.services['meteor-developer'] && user.services['meteor-developer'].emails) {
return _.findWhere(user.services['meteor-developer'].emails, { primary: true }).address;
} else if (user.services.facebook && user.services.facebook.email) {
return user.services.facebook.email;
} else if (user.services.github && user.services.github.email) {
return user.services.github.email;
} else if (user.services.google && user.services.google.email) {
return user.services.google.email;
} else if (user.services.linkedin && user.services.linkedin.emailAddress) {
return user.services.linkedin.emailAddress;
}
}
// unique: true // note: find a way to fix duplicate accounts before enabling this
},
/**
@ -117,6 +150,11 @@ const schema = {
type: String,
optional: true,
viewableBy: ['guests'],
onInsert: user => {
if (user.email) {
return Users.avatar.hash(user.email);
}
}
},
/**
The HTML version of the bio field
@ -141,6 +179,11 @@ const schema = {
type: String,
optional: true,
viewableBy: ['guests'],
onInsert: user => {
// create a basic slug from display name and then modify it if this slugs already exists;
const basicSlug = Utils.slugify(user.displayName);
return Utils.getUnusedSlug(Users, basicSlug);
}
},
/**
The user's Twitter username
@ -153,6 +196,11 @@ const schema = {
editableBy: ['members'],
viewableBy: ['guests'],
resolveAs: 'twitterUsername: String',
onInsert: user => {
if (user.services && user.services.twitter && user.services.twitter.screenName) {
return user.services.twitter.screenName;
}
}
},
/**
A link to the user's homepage