Vulcan/server/users.js

148 lines
4.4 KiB
JavaScript
Raw Normal View History

2012-10-18 17:20:20 +09:00
Accounts.onCreateUser(function(options, user){
var userProperties = {
profile: options.profile || {},
karma: 0,
isInvited: false,
isAdmin: false,
postCount: 0,
commentCount: 0,
invitedCount: 0
};
user = _.extend(user, userProperties);
2012-10-01 11:18:05 +10:00
if (options.email)
2012-10-01 13:08:46 +10:00
user.profile.email = options.email;
if (getEmail(user))
user.email_hash = getEmailHash(user);
2012-10-01 11:18:05 +10:00
2012-10-01 13:08:46 +10:00
if (!user.profile.name)
user.profile.name = user.username;
2012-10-01 11:18:05 +10:00
// set notifications default preferences
user.profile.notifications = {
users: false,
posts: false,
comments: true,
replies: true
};
// create slug from username
user.slug = slugify(getUserName(user));
2012-10-19 12:42:28 +09:00
// if this is the first user ever, make them an admin
2013-10-23 19:43:42 +09:00
if (!Meteor.users.find().count() )
2012-10-19 12:42:28 +09:00
user.isAdmin = true;
2013-10-23 19:43:42 +09:00
// give new users a few invites (default to 3)
user.inviteCount = getSetting('startInvitesCount', 3);
2013-10-23 19:43:42 +09:00
trackEvent('new user', {username: user.username, email: user.profile.email});
// if user has already filled in their email, add them to MailChimp list
if(user.profile.email)
addToMailChimpList(user, false);
// send notifications to admins
var admins = Meteor.users.find({isAdmin: true});
admins.forEach(function(admin){
if(getUserSetting('notifications.users', false, admin)){
2014-08-03 11:50:10 +09:00
var emailProperties = {
profileUrl: getProfileUrl(user),
username: getUserName(user)
}
2014-08-04 19:02:21 +09:00
var html = Handlebars.templates[getTemplate('emailNewUser')](emailProperties);
sendEmail(getEmail(admin), 'New user account: '+getUserName(user), buildEmailTemplate(html));
}
});
2012-10-01 11:18:05 +10:00
return user;
2012-10-01 13:08:46 +10:00
});
getEmailHash = function(user){
// todo: add some kind of salt in here
return CryptoJS.MD5(getEmail(user).trim().toLowerCase() + user.createdAt).toString();
};
addToMailChimpList = function(userOrEmail, confirm){
var returnValue;
if(typeof userOrEmail == "string"){
var user = null;
var email = userOrEmail;
}else if(typeof userOrEmail == "object"){
var user = userOrEmail;
var email = getEmail(user);
if (!email)
throw 'User must have an email address';
}
2014-08-04 19:02:21 +09:00
MailChimpOptions.apiKey = getSetting('mailChimpAPIKey');
MailChimpOptions.listId = getSetting('mailChimpListId');
// add a user to a MailChimp list.
// called when a new user is created, or when an existing user fills in their email
2014-08-04 19:02:21 +09:00
if(!!MailChimpOptions.apiKey && !!MailChimpOptions.listId){
console.log('adding "'+email+'" to MailChimp list…');
2014-08-04 19:02:21 +09:00
try {
var api = new MailChimp();
} catch ( error ) {
console.log( error.message );
}
api.call( 'lists', 'subscribe', {
id: MailChimpOptions.listId,
email: {"email": email},
double_optin: confirm
}, Meteor.bindEnvironment(function ( error, result ) {
2014-08-04 19:02:21 +09:00
if ( error ) {
console.log( error.message );
throw new Meteor.error(500, error.message);
2014-08-04 19:02:21 +09:00
} else {
console.log( JSON.stringify( result ) );
if(!!user)
setUserSetting('subscribedToNewsletter', true);
return result;
2014-08-04 19:02:21 +09:00
}
}));
}
return returnValue;
};
2012-10-01 13:08:46 +10:00
Meteor.methods({
changeEmail: function(newEmail) {
Meteor.users.update(Meteor.userId(), {$set: {emails: [{address: newEmail}]}});
},
numberOfPostsToday: function(){
console.log(numberOfItemsInPast24Hours(Meteor.user(), Posts));
},
numberOfCommentsToday: function(){
console.log(numberOfItemsInPast24Hours(Meteor.user(), Comments));
2012-11-21 12:30:45 +09:00
},
testEmail: function(){
2013-01-19 23:14:59 +09:00
Email.send({from: 'test@test.com', to: getEmail(Meteor.user()), subject: 'Telescope email test', text: 'lorem ipsum dolor sit amet.'});
},
testBuffer: function(){
// TODO
},
getScoreDiff: function(id){
var object = Posts.findOne(id);
var baseScore = object.baseScore;
var ageInHours = (new Date().getTime() - object.submitted) / (60 * 60 * 1000);
var newScore = baseScore / Math.pow(ageInHours + 2, 1.3);
return Math.abs(object.score - newScore);
2013-01-19 22:09:47 +09:00
},
setEmailHash: function(user){
var email_hash = CryptoJS.MD5(getEmail(user).trim().toLowerCase()).toString();
Meteor.users.update(user._id, {$set : {email_hash : email_hash}});
},
addCurrentUserToMailChimpList: function(){
var currentUser = Meteor.users.findOne(this.userId);
return addToMailChimpList(currentUser, false);
},
addEmailToMailChimpList: function (email) {
return addToMailChimpList(email, true);
2012-10-01 13:08:46 +10:00
}
});