Vulcan/packages/nova-users/lib/callbacks.js

198 lines
6.9 KiB
JavaScript
Raw Normal View History

2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib';
2016-06-23 15:00:58 +09:00
import Users from './collection.js';
import marked from 'marked';
2016-06-23 15:16:32 +09:00
import Events from "meteor/nova:events";
2016-06-23 16:42:06 +09:00
import NovaEmail from 'meteor/nova:email';
2015-06-30 19:12:29 +09:00
//////////////////////////////////////////////////////
// Collection Hooks //
//////////////////////////////////////////////////////
// /**
// * @summary Generate HTML body from Markdown on user bio insert
// */
// Users.after.insert(function (userId, user) {
// // run create user async callbacks
// Telescope.callbacks.runAsync("users.new.async", user);
// // check if all required fields have been filled in. If so, run profile completion callbacks
// if (Users.hasCompletedProfile(user)) {
// Telescope.callbacks.runAsync("users.profileCompleted.async", user);
// }
// });
// /**
// * @summary Generate HTML body from Markdown when user bio is updated
// */
// Users.before.update(function (userId, doc, fieldNames, modifier) {
// // if bio is being modified, update htmlBio too
// if (Meteor.isServer && modifier.$set && modifier.$set["__bio"]) {
// modifier.$set["__htmlBio"] = Telescope.utils.sanitize(marked(modifier.$set["__bio"]));
// }
// });
// /**
// * @summary Disallow $rename
// */
// Users.before.update(function (userId, doc, fieldNames, modifier) {
// if (!!modifier.$rename) {
// throw new Meteor.Error("illegal $rename operator detected!");
// }
// });
// /**
// * @summary If user.__email has changed, check for existing emails and change user.emails and email hash if needed
// */
// if (Meteor.isServer) {
// Users.before.update(function (userId, doc, fieldNames, modifier) {
// var user = doc;
// // if email is being modified, update user.emails too
// if (Meteor.isServer && modifier.$set && modifier.$set["__email"]) {
// var newEmail = modifier.$set["__email"];
// // check for existing emails and throw error if necessary
// var userWithSameEmail = Users.findByEmail(newEmail);
// if (userWithSameEmail && userWithSameEmail._id !== doc._id) {
// throw new Meteor.Error("email_taken2", "this_email_is_already_taken" + " (" + newEmail + ")");
// }
// // if user.emails exists, change it too
// if (!!user.emails) {
// user.emails[0].address = newEmail;
// modifier.$set.emails = user.emails;
// }
// // update email hash
// modifier.$set["__emailHash"] = Gravatar.hash(newEmail);
// }
// });
// }
2015-06-30 19:12:29 +09:00
//////////////////////////////////////////////////////
// Callbacks //
//////////////////////////////////////////////////////
2015-05-06 12:28:00 +09:00
/**
* @summary Set up user object on creation
2015-05-06 12:28:00 +09:00
* @param {Object} user the user object being iterated on and returned
* @param {Object} options user options
*/
function setupUser (user, options) {
// ------------------------------ Properties ------------------------------ //
2015-05-06 12:28:00 +09:00
var userProperties = {
profile: options.profile || {},
__karma: 0,
__isInvited: false,
__postCount: 0,
__commentCount: 0,
__invitedCount: 0,
__upvotedPosts: [],
__downvotedPosts: [],
__upvotedComments: [],
__downvotedComments: []
2015-05-06 12:28:00 +09:00
};
user = _.extend(user, userProperties);
// look in a few places for the user email
2015-05-06 12:28:00 +09:00
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;
}
// generate email hash
if (!!user.__email) {
user.__emailHash = Gravatar.hash(user.__email);
2015-05-06 12:28:00 +09:00
}
2015-05-16 13:13:45 +09:00
// look in a few places for the displayName
if (user.profile.username) {
user.__displayName = user.profile.username;
2015-05-16 13:13:45 +09:00
} 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;
2015-05-16 13:13:45 +09:00
} else {
user.__displayName = user.username;
}
2015-05-06 12:28:00 +09:00
2016-10-04 22:42:07 +02:00
// create a basic slug from display name and then modify it if this slugs already exists;
const basicSlug = Telescope.utils.slugify(user.__displayName);
user.__slug = Telescope.utils.getUnusedSlug(Users, basicSlug);
2015-05-06 12:28:00 +09:00
// 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;
2015-05-06 12:28:00 +09:00
Events.track('new user', {username: user.__displayName, email: user.__email});
2015-05-06 12:28:00 +09:00
return user;
}
Telescope.callbacks.add("users.new.sync", setupUser);
function hasCompletedProfile (user) {
return Users.hasCompletedProfile(user);
}
Telescope.callbacks.add("users.profileCompleted.sync", hasCompletedProfile);
2016-02-17 21:57:20 +09:00
function usersNewAdminUserCreationNotification (user) {
2016-06-23 16:42:06 +09:00
// send notifications to admins
const admins = Users.adminUsers();
admins.forEach(function(admin) {
2016-06-23 16:42:06 +09:00
if (Users.getSetting(admin, "notifications_users", false)) {
const emailProperties = Users.getNotificationProperties(user);
const html = NovaEmail.getTemplate('newUser')(emailProperties);
NovaEmail.send(Users.getEmail(admin), `New user account: ${emailProperties.displayName}`, NovaEmail.buildTemplate(html));
2016-06-23 16:42:06 +09:00
}
});
return user;
}
Telescope.callbacks.add("users.new.sync", usersNewAdminUserCreationNotification);
function usersEditGenerateHtmlBio (modifier) {
if (modifier.$set && modifier.$set.__bio) {
modifier.$set.__htmlBio = Telescope.utils.sanitize(marked(modifier.$set.__bio));
}
return modifier;
}
Telescope.callbacks.add("users.edit.sync", usersEditGenerateHtmlBio);
2016-11-17 10:32:16 +01:00
function usersEditCheckEmail (modifier, user) {
// if email is being modified, update user.emails too
if (modifier.$set && modifier.$set.__email) {
var newEmail = modifier.$set.__email;
// check for existing emails and throw error if necessary
var userWithSameEmail = Users.findByEmail(newEmail);
2016-11-17 10:32:16 +01:00
if (userWithSameEmail && userWithSameEmail._id !== user._id) {
throw new Meteor.Error("email_taken2", "this_email_is_already_taken" + " (" + newEmail + ")");
}
// if user.emails exists, change it too
if (!!user.emails) {
user.emails[0].address = newEmail;
modifier.$set.emails = user.emails;
}
// update email hash
modifier.$set.__emailHash = Gravatar.hash(newEmail);
}
return modifier;
}
Telescope.callbacks.add("users.edit.sync", usersEditCheckEmail);