Vulcan/server/users.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

2012-10-18 17:20:20 +09:00
Accounts.onCreateUser(function(options, user){
2014-12-13 16:29:31 +09:00
// ------------------------------ Properties ------------------------------ //
var userProperties = {
profile: options.profile || {},
karma: 0,
isInvited: false,
postCount: 0,
commentCount: 0,
invitedCount: 0,
votes: {
upvotedPosts: [],
downvotedPosts: [],
upvotedComments: [],
downvotedComments: []
}
};
user = _.extend(user, userProperties);
// set email on profile
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 email is set, use it to generate email hash
if (getEmail(user))
user.email_hash = getEmailHash(user);
2012-10-01 11:18:05 +10:00
// set username on profile
2012-10-01 13:08:46 +10:00
if (!user.profile.name)
user.profile.name = user.username;
// create slug from username
user.slug = slugify(getUserName(user));
2015-01-06 14:22:39 +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;
// ------------------------------ Callbacks ------------------------------ //
// run all post submit client callbacks on properties object successively
clog('// Start userCreatedCallbacks')
user = userCreatedCallbacks.reduce(function(result, currentFunction) {
clog('// Running '+currentFunction.name+'…')
return currentFunction(result);
}, user);
clog('// Finished userCreatedCallbacks')
clog('// User object:')
clog(user)
2014-12-13 16:29:31 +09:00
// ------------------------------ Analytics ------------------------------ //
trackEvent('new user', {username: user.username, email: user.profile.email});
2012-10-01 11:18:05 +10:00
return user;
2012-10-01 13:08:46 +10:00
});
2012-10-01 13:08:46 +10:00
Meteor.methods({
2014-10-15 16:48:04 -05:00
changeEmail: function (newEmail) {
Meteor.users.update(
Meteor.userId(),
{$set: {
emails: [{address: newEmail}],
email_hash: Gravatar.hash(newEmail),
// Just in case this gets called from somewhere other than /client/views/users/user_edit.js
"profile.email": 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
},
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);
2012-10-01 13:08:46 +10:00
}
});