Vulcan/server/invites.js

86 lines
2.9 KiB
JavaScript
Raw Normal View History

2013-10-23 19:43:42 +09:00
Meteor.methods({
inviteUser: function(invitation){
// invite user returns the following hash
// { newUser : true|false }
// newUser is true if the person being invited is not on the site yet
// invitation can either contain userId or an email address :
2014-08-12 16:02:05 +02:00
// { invitedUserEmail : 'bob@gmail.com' } or { userId : 'user-id' }
check(invitation, Match.OneOf(
{ invitedUserEmail : String },
{ userId : String }
));
var user = invitation.invitedUserEmail ?
2014-08-12 16:02:05 +02:00
Meteor.users.findOne({ emails : { $elemMatch: { address: invitation.invitedUserEmail } } }) :
Meteor.users.findOne({ _id : invitation.userId }),
userEmail = invitation.invitedUserEmail ? invitation.invitedUserEmail :
getEmail(user),
currentUser = Meteor.user(),
2014-08-12 16:02:05 +02:00
currentUserCanInvite = currentUserIsAdmin ||
(currentUser.inviteCount > 0 && canInvite(currentUser)),
currentUserIsAdmin = isAdmin(currentUser);
// check if the person is already invited
if(user && (isInvited(user) || isAdmin(user))){
throw new Meteor.Error(403, "This person is already invited.");
} else {
2014-08-12 16:02:05 +02:00
if(!currentUserCanInvite){
throw new Meteor.Error(701, "You can't invite this user, sorry.");
}
2014-08-12 16:02:05 +02:00
// don't allow duplicate multpile invite for the same person
var existingInvite = Invites.findOne({ invitedUserEmail : userEmail });
2014-08-12 16:02:05 +02:00
if(existingInvite){
throw new Meteor.Error(403, "Somebody has already invited this person.");
2014-08-03 11:50:10 +09:00
}
2014-08-12 16:02:05 +02:00
// create an invite
// consider invite accepted if the invited person has an account already
Invites.insert({
invitingUserId: Meteor.userId(),
invitedUserEmail: userEmail,
accepted: typeof user !== "undefined"
});
// update invinting user
Meteor.users.update(Meteor.userId(), {$inc:{inviteCount: -1}, $inc:{invitedCount: 1}});
2014-08-02 14:05:51 +02:00
2014-08-12 16:02:05 +02:00
if(user){
// update invited user
Meteor.users.update(user._id, {$set: {
isInvited: true,
invitedBy: Meteor.userId(),
invitedByName: getDisplayName(currentUser)
}});
}
2014-08-02 14:05:51 +02:00
2014-08-12 16:02:05 +02:00
var emailTemplate = getTemplate('emailInvite'),
communityName = getSetting('title','Telescope'),
emailSubject = 'You are invited to try '+communityName,
emailProperties = {
newUser : typeof user === 'undefined',
communityName : communityName,
actionLink : user ? getSigninUrl() : getSignupUrl(),
invitedBy : getDisplayName(currentUser),
profileUrl : getProfileUrl(currentUser)
},
notificationHtml = Handlebars.templates[emailTemplate](emailProperties),
html = buildEmailTemplate(notificationHtml);
2014-08-02 14:05:51 +02:00
2014-08-12 16:02:05 +02:00
Meteor.setTimeout(function () {
sendEmail(userEmail, emailSubject, html);
}, 1);
}
2014-08-12 16:02:05 +02:00
return {
2014-08-12 16:02:05 +02:00
newUser : typeof user === 'undefined'
};
2013-10-23 19:43:42 +09:00
}
2013-12-01 11:25:17 +09:00
});