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

165 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-06-15 11:07:10 +09:00
import Users from './namespace.js';
import marked from 'marked';
2015-06-30 19:12:29 +09:00
//////////////////////////////////////////////////////
// Collection Hooks //
//////////////////////////////////////////////////////
/**
* @summary Generate HTML body from Markdown on user bio insert
2015-06-30 19:12:29 +09:00
*/
Users.after.insert(function (userId, user) {
// run create user async callbacks
Telescope.callbacks.runAsync("onCreateUserAsync", user);
// check if all required fields have been filled in. If so, run profile completion callbacks
if (Users.hasCompletedProfile(user)) {
Telescope.callbacks.runAsync("profileCompletedAsync", user);
}
2015-09-03 14:22:38 -06:00
2015-06-30 19:12:29 +09:00
});
/**
* @summary Generate HTML body from Markdown when user bio is updated
2015-06-30 19:12:29 +09:00
*/
Users.before.update(function (userId, doc, fieldNames, modifier) {
// if bio is being modified, update htmlBio too
if (Meteor.isServer && modifier.$set && modifier.$set["telescope.bio"]) {
modifier.$set["telescope.htmlBio"] = Telescope.utils.sanitize(marked(modifier.$set["telescope.bio"]));
}
});
/**
* @summary Disallow $rename
2015-06-30 19:12:29 +09:00
*/
Users.before.update(function (userId, doc, fieldNames, modifier) {
if (!!modifier.$rename) {
throw new Meteor.Error("illegal $rename operator detected!");
}
});
/**
* @summary If user.telescope.email has changed, check for existing emails and change user.emails and email hash if needed
2015-06-30 19:12:29 +09:00
*/
if (Meteor.isServer) {
Users.before.update(function (userId, doc, fieldNames, modifier) {
2015-06-30 19:12:29 +09:00
var user = doc;
2015-09-03 14:22:38 -06:00
2015-06-30 19:12:29 +09:00
// if email is being modified, update user.emails too
if (Meteor.isServer && modifier.$set && modifier.$set["telescope.email"]) {
2015-06-30 19:12:29 +09:00
var newEmail = modifier.$set["telescope.email"];
2015-06-30 19:12:29 +09:00
// 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 + ")");
2015-06-30 19:12:29 +09:00
}
// 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["telescope.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 || {},
telescope: {
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
2015-05-06 12:28:00 +09:00
if (options.email) {
user.telescope.email = options.email;
} else if (user.services['meteor-developer'] && user.services['meteor-developer'].emails) {
user.telescope.email = _.findWhere(user.services['meteor-developer'].emails, { primary: true }).address;
} else if (user.services.facebook && user.services.facebook.email) {
user.telescope.email = user.services.facebook.email;
} else if (user.services.github && user.services.github.email) {
user.telescope.email = user.services.github.email;
} else if (user.services.google && user.services.google.email) {
user.telescope.email = user.services.google.email;
}
// generate email hash
if (!!user.telescope.email) {
user.telescope.emailHash = Gravatar.hash(user.telescope.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.telescope.displayName = user.profile.username;
} else if (user.profile.name) {
user.telescope.displayName = user.profile.name;
} else {
user.telescope.displayName = user.username;
}
2015-05-06 12:28:00 +09:00
// create slug from display name
user.telescope.slug = Telescope.utils.slugify(user.telescope.displayName);
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 && Meteor.users.find({'profile.isDummy': {$ne: true}}).count() === 0) ? true : false;
2015-09-03 14:22:38 -06:00
Events.track('new user', {username: user.username, email: user.telescope.email});
2015-05-06 12:28:00 +09:00
return user;
}
2015-05-17 15:38:02 +09:00
Telescope.callbacks.add("onCreateUser", setupUser);
2015-05-06 12:33:48 +09:00
function hasCompletedProfile (user) {
return Users.hasCompletedProfile(user);
}
2015-05-17 15:38:02 +09:00
Telescope.callbacks.add("profileCompletedChecks", hasCompletedProfile);
2016-02-17 21:57:20 +09:00
if (Telescope.email) {
function adminUserCreationNotification (user) {
// send notifications to admins
var admins = Users.adminUsers();
admins.forEach(function(admin){
2016-04-01 12:46:01 +09:00
if (Users.getSetting(admin, "notifications_users", false)) {
2016-02-17 21:57:20 +09:00
var emailProperties = {
profileUrl: Users.getProfileUrl(user, true),
username: Users.getUserName(user)
};
var html = Telescope.email.getTemplate('newUser')(emailProperties);
2016-02-17 21:57:20 +09:00
Telescope.email.send(Users.getEmail(admin), 'New user account: '+Users.getUserName(user), Telescope.email.buildTemplate(html));
}
});
return user;
}
Telescope.callbacks.add("onCreateUser", adminUserCreationNotification);
}