2016-06-15 11:07:10 +09:00
|
|
|
|
import Users from './namespace.js';
|
2016-06-07 15:03:00 +09:00
|
|
|
|
import marked from 'marked';
|
|
|
|
|
|
2015-06-30 19:12:29 +09:00
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
// Collection Hooks //
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @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
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +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"]));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @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!");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
|
* @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-08-15 11:41:59 +09:00
|
|
|
|
|
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-08-15 11:41:59 +09:00
|
|
|
|
|
2015-06-30 19:12:29 +09:00
|
|
|
|
var newEmail = modifier.$set["telescope.email"];
|
2015-08-15 11:41:59 +09:00
|
|
|
|
|
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) {
|
2016-03-31 09:36:25 +09:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-15 11:41:59 +09:00
|
|
|
|
// 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
|
|
|
|
/**
|
2016-04-09 09:41:20 +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) {
|
2015-05-08 11:45:09 +09:00
|
|
|
|
// ------------------------------ 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);
|
|
|
|
|
|
2015-09-23 11:37:07 +09:00
|
|
|
|
// 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;
|
2015-12-31 11:30:14 -08:00
|
|
|
|
} else if (user.services['meteor-developer'] && user.services['meteor-developer'].emails) {
|
|
|
|
|
user.telescope.email = _.findWhere(user.services['meteor-developer'].emails, { primary: true }).address;
|
2015-09-23 11:37:07 +09:00
|
|
|
|
} else if (user.services.facebook && user.services.facebook.email) {
|
|
|
|
|
user.telescope.email = user.services.facebook.email;
|
2015-12-31 11:30:14 -08:00
|
|
|
|
} 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;
|
2015-09-23 11:37:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2015-05-19 10:26:31 +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-04-23 17:45:37 +09:00
|
|
|
|
|
2015-05-06 12:33:48 +09:00
|
|
|
|
|
2015-04-23 17:45:37 +09:00
|
|
|
|
function hasCompletedProfile (user) {
|
2015-05-19 12:34:27 +09:00
|
|
|
|
return Users.hasCompletedProfile(user);
|
2015-04-23 17:45:37 +09:00
|
|
|
|
}
|
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)
|
|
|
|
|
};
|
2016-02-23 21:35:54 +09:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
}
|