mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
Replace autoValue with onInsert, onEdit, onRemove
This commit is contained in:
parent
1f9e5c17b9
commit
c9c2030752
9 changed files with 104 additions and 90 deletions
|
@ -17,9 +17,8 @@ const schema = {
|
||||||
type: Date,
|
type: Date,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
autoValue: (documentOrModifier) => {
|
onInsert: (document, currentUser) => {
|
||||||
// if this is an insert, set createdAt to current timestamp
|
return new Date();
|
||||||
if (documentOrModifier && !documentOrModifier.$set) return new Date()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
userId: {
|
userId: {
|
||||||
|
|
|
@ -19,9 +19,8 @@ const schema = {
|
||||||
type: Date,
|
type: Date,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
autoValue: (documentOrModifier) => {
|
onInsert: (document, currentUser) => {
|
||||||
// if this is an insert, set createdAt to current timestamp
|
return new Date();
|
||||||
if (documentOrModifier && !documentOrModifier.$set) return new Date()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
userId: {
|
userId: {
|
||||||
|
|
|
@ -17,8 +17,8 @@ const schema = {
|
||||||
type: Date,
|
type: Date,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
autoValue: (documentOrModifier) => {
|
onInsert: (document, currentUser) => {
|
||||||
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
|
return new Date();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
userId: {
|
userId: {
|
||||||
|
|
|
@ -46,8 +46,8 @@ const schema = {
|
||||||
type: Date,
|
type: Date,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['admins'],
|
viewableBy: ['admins'],
|
||||||
autoValue: (documentOrModifier) => {
|
onInsert: (document, currentUser) => {
|
||||||
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
|
return new Date();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
@ -57,8 +57,8 @@ const schema = {
|
||||||
type: Date,
|
type: Date,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
autoValue: (documentOrModifier) => {
|
onInsert: (document, currentUser) => {
|
||||||
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
|
return new Date();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
@ -87,10 +87,11 @@ const schema = {
|
||||||
type: String,
|
type: String,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
autoValue: (documentOrModifier) => {
|
onEdit: (modifier, document, currentUser) => {
|
||||||
// if userId is changing, change the author name too
|
// if userId is changing, change the author name too
|
||||||
const userId = documentOrModifier.userId || documentOrModifier.$set && documentOrModifier.$set.userId
|
if (modifier.$set && modifier.$set.userId) {
|
||||||
if (userId) return Users.getDisplayNameById(userId)
|
return Users.getDisplayNameById(modifier.$set.userId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -22,7 +22,10 @@ SimpleSchema.extendOptions([
|
||||||
'autoform', // legacy form placeholder; backward compatibility (not used anymore)
|
'autoform', // legacy form placeholder; backward compatibility (not used anymore)
|
||||||
'control', // SmartForm control (String or React component)
|
'control', // SmartForm control (String or React component)
|
||||||
'order', // position in the form
|
'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;
|
export default Vulcan;
|
||||||
|
|
|
@ -63,11 +63,11 @@ export const newMutation = ({ collection, document, currentUser, validate, conte
|
||||||
const userIdInSchema = Object.keys(schema).find(key => key === 'userId');
|
const userIdInSchema = Object.keys(schema).find(key => key === 'userId');
|
||||||
if (!!userIdInSchema && !newDocument.userId) newDocument.userId = currentUser._id;
|
if (!!userIdInSchema && !newDocument.userId) newDocument.userId = currentUser._id;
|
||||||
|
|
||||||
// run autoValue step
|
// run onInsert step
|
||||||
_.keys(schema).forEach(fieldName => {
|
_.keys(schema).forEach(fieldName => {
|
||||||
if (!newDocument[fieldName] && schema[fieldName].autoValue) {
|
if (!newDocument[fieldName] && schema[fieldName].onInsert) {
|
||||||
const autoValue = schema[fieldName].autoValue(newDocument);
|
const autoValue = schema[fieldName].onInsert(newDocument, currentUser);
|
||||||
if (autoValue && typeof autoValue.$setOnInsert === 'undefined') {
|
if (autoValue) {
|
||||||
newDocument[fieldName] = autoValue;
|
newDocument[fieldName] = autoValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,11 +134,11 @@ export const editMutation = ({ collection, documentId, set, unset, currentUser,
|
||||||
modifier = runCallbacks(`${collectionName}.edit.validate`, modifier, document, currentUser);
|
modifier = runCallbacks(`${collectionName}.edit.validate`, modifier, document, currentUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
// run autoValue step
|
// run onEdit step
|
||||||
_.keys(schema).forEach(fieldName => {
|
_.keys(schema).forEach(fieldName => {
|
||||||
if (!modifier.$set[fieldName] && schema[fieldName].autoValue) {
|
if (!document[fieldName] && schema[fieldName].onEdit) {
|
||||||
const autoValue = schema[fieldName].autoValue(modifier);
|
const autoValue = schema[fieldName].onEdit(modifier, document, currentUser);
|
||||||
if (autoValue && typeof autoValue.$setOnInsert === 'undefined') {
|
if (autoValue) {
|
||||||
modifier.$set[fieldName] = autoValue;
|
modifier.$set[fieldName] = autoValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,6 +183,7 @@ export const removeMutation = ({ collection, documentId, currentUser, validate,
|
||||||
// console.log(documentId)
|
// console.log(documentId)
|
||||||
|
|
||||||
const collectionName = collection._name;
|
const collectionName = collection._name;
|
||||||
|
const schema = collection.simpleSchema()._schema;
|
||||||
|
|
||||||
let document = collection.findOne(documentId);
|
let document = collection.findOne(documentId);
|
||||||
|
|
||||||
|
@ -191,6 +192,13 @@ export const removeMutation = ({ collection, documentId, currentUser, validate,
|
||||||
document = runCallbacks(`${collectionName}.remove.validate`, document, currentUser);
|
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);
|
runCallbacks(`${collectionName}.remove.sync`, document, currentUser);
|
||||||
|
|
||||||
collection.remove(documentId);
|
collection.remove(documentId);
|
||||||
|
|
|
@ -32,8 +32,8 @@ const schema = {
|
||||||
type: Date,
|
type: Date,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['admins'],
|
viewableBy: ['admins'],
|
||||||
autoValue: (documentOrModifier) => {
|
onInsert: (document, currentUser) => {
|
||||||
if (documentOrModifier && !documentOrModifier.$set) return new Date() // if this is an insert, set createdAt to current timestamp
|
return new Date();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
@ -147,10 +147,9 @@ const schema = {
|
||||||
insertableBy: ['admins'],
|
insertableBy: ['admins'],
|
||||||
editableBy: ['admins'],
|
editableBy: ['admins'],
|
||||||
control: "select",
|
control: "select",
|
||||||
autoValue(documentOrModifier) {
|
onInsert: document => {
|
||||||
// provide a default value if this is an insert operation and status field is not set in the document
|
if (document.userId && !document.status) {
|
||||||
if (documentOrModifier && !documentOrModifier.$set && documentOrModifier.userId && !documentOrModifier.status) {
|
const user = Users.findOne(document.userId);
|
||||||
const user = Users.findOne(documentOrModifier.userId);
|
|
||||||
return Posts.getDefaultStatus(user);
|
return Posts.getDefaultStatus(user);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -215,10 +214,11 @@ const schema = {
|
||||||
type: String,
|
type: String,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
autoValue: (documentOrModifier) => {
|
onEdit: (modifier, document, currentUser) => {
|
||||||
// if userId is changing, change the author name too
|
// if userId is changing, change the author name too
|
||||||
const userId = documentOrModifier.userId || documentOrModifier.$set && documentOrModifier.$set.userId
|
if (modifier.$set && modifier.$set.userId) {
|
||||||
if (userId) return Users.getDisplayNameById(userId)
|
return Users.getDisplayNameById(modifier.$set.userId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,65 +12,21 @@ import { addCallback, Utils, runCallbacksAsync } from 'meteor/vulcan:lib'; // im
|
||||||
* @param {Object} options – user options
|
* @param {Object} options – user options
|
||||||
*/
|
*/
|
||||||
function setupUser (user, options) {
|
function setupUser (user, options) {
|
||||||
|
|
||||||
|
const schema = Users.simpleSchema()._schema;
|
||||||
|
|
||||||
// ------------------------------ Properties ------------------------------ //
|
// ------------------------------ Properties ------------------------------ //
|
||||||
var userProperties = {
|
user.profile = options.profile || {};
|
||||||
profile: options.profile || {},
|
|
||||||
karma: 0,
|
|
||||||
isInvited: false,
|
|
||||||
postCount: 0,
|
|
||||||
commentCount: 0,
|
|
||||||
invitedCount: 0,
|
|
||||||
upvotedPosts: [],
|
|
||||||
downvotedPosts: [],
|
|
||||||
upvotedComments: [],
|
|
||||||
downvotedComments: []
|
|
||||||
};
|
|
||||||
user = _.extend(user, userProperties);
|
|
||||||
|
|
||||||
// look in a few places for the user email
|
// run onInsert step
|
||||||
if (options.email) {
|
_.keys(schema).forEach(fieldName => {
|
||||||
user.email = options.email;
|
if (!user[fieldName] && schema[fieldName].onInsert) {
|
||||||
} else if (user.services['meteor-developer'] && user.services['meteor-developer'].emails) {
|
const autoValue = schema[fieldName].onInsert(user, options);
|
||||||
user.email = _.findWhere(user.services['meteor-developer'].emails, { primary: true }).address;
|
if (autoValue) {
|
||||||
} else if (user.services.facebook && user.services.facebook.email) {
|
user[fieldName] = autoValue;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import SimpleSchema from 'simpl-schema';
|
import SimpleSchema from 'simpl-schema';
|
||||||
import Users from './collection.js';
|
import Users from './collection.js';
|
||||||
|
import { Utils } from 'meteor/vulcan:lib'; // import from vulcan:lib because vulcan:core isn't loaded yet
|
||||||
|
|
||||||
const adminGroup = {
|
const adminGroup = {
|
||||||
name: "admin",
|
name: "admin",
|
||||||
|
@ -56,6 +57,11 @@ const schema = {
|
||||||
editableBy: ['admins'],
|
editableBy: ['admins'],
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
group: adminGroup,
|
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: {
|
profile: {
|
||||||
type: Object,
|
type: Object,
|
||||||
|
@ -95,6 +101,17 @@ const schema = {
|
||||||
insertableBy: ['members'],
|
insertableBy: ['members'],
|
||||||
editableBy: ['members'],
|
editableBy: ['members'],
|
||||||
viewableBy: ['guests'],
|
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.
|
The user's email. Modifiable.
|
||||||
|
@ -108,6 +125,22 @@ const schema = {
|
||||||
insertableBy: ['members'],
|
insertableBy: ['members'],
|
||||||
editableBy: ['members'],
|
editableBy: ['members'],
|
||||||
viewableBy: ownsOrIsAdmin,
|
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
|
// unique: true // note: find a way to fix duplicate accounts before enabling this
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
@ -117,6 +150,11 @@ const schema = {
|
||||||
type: String,
|
type: String,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
|
onInsert: user => {
|
||||||
|
if (user.email) {
|
||||||
|
return Users.avatar.hash(user.email);
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
The HTML version of the bio field
|
The HTML version of the bio field
|
||||||
|
@ -141,6 +179,11 @@ const schema = {
|
||||||
type: String,
|
type: String,
|
||||||
optional: true,
|
optional: true,
|
||||||
viewableBy: ['guests'],
|
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
|
The user's Twitter username
|
||||||
|
@ -153,6 +196,11 @@ const schema = {
|
||||||
editableBy: ['members'],
|
editableBy: ['members'],
|
||||||
viewableBy: ['guests'],
|
viewableBy: ['guests'],
|
||||||
resolveAs: 'twitterUsername: String',
|
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
|
A link to the user's homepage
|
||||||
|
|
Loading…
Add table
Reference in a new issue