Vulcan/common/users.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2012-10-05 13:59:40 +09:00
isAdminById=function(userId){
2012-10-08 10:44:13 +09:00
var user = Meteor.users.findOne(userId);
return user && isAdmin(user);
2012-10-05 13:59:40 +09:00
}
isAdmin=function(user){
2012-10-18 12:34:13 +09:00
if(!user || typeof user === 'undefined')
2012-10-08 10:44:13 +09:00
return false;
2012-10-18 12:34:13 +09:00
return !!user.isAdmin;
2012-10-05 13:59:40 +09:00
}
2012-10-01 14:52:32 +09:00
getDisplayName = function(user){
2012-10-17 16:04:47 +09:00
return (user.profile && user.profile.name) ? user.profile.name : user.username;
2012-10-01 14:52:32 +09:00
}
2012-10-23 12:24:38 +09:00
getDisplayNameById = function(userId){
return getDisplayName(Meteor.users.findOne(userId));
}
2012-10-03 16:33:28 +09:00
getSignupMethod = function(user){
2012-10-08 10:44:13 +09:00
if(user.services && user.services.twitter){
return 'twitter';
}else{
2012-10-17 16:04:47 +09:00
return 'regular';
2012-10-08 10:44:13 +09:00
}
2012-10-03 16:33:28 +09:00
}
getEmail = function(user){
2012-10-08 10:44:13 +09:00
if(getSignupMethod(user)=='twitter'){
return user.profile.email;
}else if(user.emails){
return user.emails[0].address || user.emails[0].email;
2012-10-18 12:34:13 +09:00
}else if(user.profile && user.profile.email){
2012-10-18 11:46:35 +09:00
return user.profile.email;
2012-10-08 10:44:13 +09:00
}else{
return '';
}
2012-10-03 16:33:28 +09:00
}
getAvatarUrl = function(user){
2012-10-08 10:44:13 +09:00
if(getSignupMethod(user)=='twitter'){
return 'https://api.twitter.com/1/users/profile_image?screen_name='+user.services.twitter.screenName;
}else{
return Gravatar.getGravatar(user, {
d: 'http://telesc.pe/img/default_avatar.png',
s: 30
});
}
2012-10-02 10:54:04 +09:00
}
2012-10-03 16:33:28 +09:00
getCurrentUserEmail = function(){
2012-10-08 10:44:13 +09:00
return Meteor.user() ? getEmail(Meteor.user()) : '';
2012-10-03 16:33:28 +09:00
}
userProfileComplete = function(user) {
2012-10-08 10:44:13 +09:00
return !!getEmail(user);
2012-10-03 16:33:28 +09:00
}
2012-10-05 13:59:40 +09:00
findLast = function(user, collection){
return collection.findOne({userId: user._id}, {sort: {submitted: -1}});
}
timeSinceLast = function(user, collection){
2012-10-08 10:44:13 +09:00
var now = new Date().getTime();
var last = findLast(user, collection);
if(!last)
return 999; // if this is the user's first post or comment ever, stop here
return Math.abs(Math.floor((now-last.submitted)/1000));
}
numberOfItemsInPast24Hours = function(user, collection){
var mDate = moment(new Date());
var items=collection.find({
userId: user._id,
submitted: {
$gte: mDate.subtract('hours',24).valueOf()
}
});
return items.count();
2012-10-05 13:59:40 +09:00
}