Vulcan/packages/telescope-invites/lib/server/invites.js
Maxime Quandalle 94c6121d91 Improve jsHint consistency
This commit touch a lot of lines of code with the goal to be more
rigorous about JavaScript code conventions defined in the `.jshintrc`.

Some modification:

* Add a list of used global symbols in the corresponding section of
  `.jshintrc`
* Use local variables instead of global in a lot of places where the
  keyword `var` was mistakenly forgotten
* Add missing semi-colons after instructions
* Add new lines at the end of files
* Remove trailing whitespaces
* Use newer name of some Meteor APIs, eg `addFiles` instead of
  `add_files`
* Add missing `break` statements in `switch` blocks
* Use `===` instead of `==` and `!==` instead of `!=`
* Remove unused variables

This commit should also fix a few bugs due to this lack of rigor. One
example of that was the test `typeof navElements === "array"` that was
never true because in JavaScript, `typeof [] === "object"`, we
replaced this test by the `_.isArray` method provided by underscore.
It might also fix some potential collision related to global
variables.

There is still plenty of work until Telescope code base passes jsHint
validation, but at least this commit is a step in the right direction.
2015-05-01 18:38:27 +02:00

82 lines
2.8 KiB
JavaScript

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 :
// { invitedUserEmail : 'bob@gmail.com' } or { userId : 'user-id' }
check(invitation, Match.OneOf(
{ invitedUserEmail : String },
{ userId : String }
));
var user = invitation.invitedUserEmail ?
Meteor.users.findOne({ emails : { $elemMatch: { address: invitation.invitedUserEmail } } }) :
Meteor.users.findOne({ _id : invitation.userId }),
userEmail = invitation.invitedUserEmail ? invitation.invitedUserEmail :
Users.getEmail(user),
currentUser = Meteor.user(),
currentUserCanInvite = currentUserIsAdmin ||
(currentUser.inviteCount > 0 && Users.can.invite(currentUser)),
currentUserIsAdmin = Users.is.admin(currentUser);
// check if the person is already invited
if(user && Users.can.invite(user)){
throw new Meteor.Error(403, "This person is already invited.");
} else {
if (!currentUserCanInvite){
throw new Meteor.Error(701, "You can't invite this user, sorry.");
}
// don't allow duplicate multpile invite for the same person
var existingInvite = Invites.findOne({ invitedUserEmail : userEmail });
if (existingInvite) {
throw new Meteor.Error(403, "Somebody has already invited this person.");
}
// 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}});
if(user){
// update invited user
Meteor.users.update(user._id, {$set: {
isInvited: true,
invitedBy: Meteor.userId(),
invitedByName: Users.getDisplayName(currentUser)
}});
}
var communityName = Settings.get('title','Telescope'),
emailSubject = 'You are invited to try '+communityName,
emailProperties = {
newUser : typeof user === 'undefined',
communityName : communityName,
actionLink : user ? Telescope.utils.getSigninUrl() : Telescope.utils.getSignupUrl(),
invitedBy : Users.getDisplayName(currentUser),
profileUrl : Users.getProfileUrl(currentUser)
};
Meteor.setTimeout(function () {
buildAndSendEmail(userEmail, emailSubject, 'emailInvite', emailProperties);
}, 1);
}
return {
newUser : typeof user === 'undefined'
};
}
});