2015-05-18 18:32:54 +09:00
|
|
|
var InviteSchema = new SimpleSchema({
|
2015-04-22 07:50:11 +09:00
|
|
|
_id: {
|
|
|
|
type: String,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
invitingUserId: {
|
|
|
|
type: String,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
invitedUserEmail: {
|
|
|
|
type: String,
|
|
|
|
regEx: SimpleSchema.RegEx.Email
|
|
|
|
},
|
|
|
|
accepted: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Invites = new Meteor.Collection("invites");
|
|
|
|
Invites.attachSchema(InviteSchema);
|
|
|
|
|
2015-05-18 18:32:54 +09:00
|
|
|
Users.addField([
|
|
|
|
/**
|
|
|
|
A count of the user's remaining invites
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: "telescope.inviteCount",
|
|
|
|
fieldSchema: {
|
|
|
|
type: Number,
|
|
|
|
optional: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
A count of how many users have been invited by the user
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: "telescope.invitedCount",
|
|
|
|
fieldSchema: {
|
|
|
|
type: Number,
|
|
|
|
optional: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
Whether the user is invited or not
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: "telescope.isInvited",
|
|
|
|
fieldSchema: {
|
|
|
|
type: Boolean,
|
2015-06-05 10:55:52 +09:00
|
|
|
public: true,
|
2015-05-18 18:32:54 +09:00
|
|
|
optional: true,
|
|
|
|
editableBy: ["admin"],
|
|
|
|
autoform: {
|
|
|
|
omit: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
The _id of the user who invited the current user
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: "telescope.invitedBy",
|
|
|
|
fieldSchema: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
|
|
|
autoform: {
|
|
|
|
omit: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
The name of the user who invited the current user
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: "telescope.invitedByName",
|
|
|
|
fieldSchema: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
|
|
|
autoform: {
|
|
|
|
omit: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
// invites are managed through Meteor method
|
|
|
|
|
|
|
|
Invites.deny({
|
|
|
|
insert: function(){ return true; },
|
|
|
|
update: function(){ return true; },
|
|
|
|
remove: function(){ return true; }
|
|
|
|
});
|
|
|
|
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.modules.add("profileEdit", {
|
2015-05-18 10:12:48 +09:00
|
|
|
template: 'user_invites',
|
2015-04-22 07:50:11 +09:00
|
|
|
order: 2
|
|
|
|
});
|
|
|
|
|
|
|
|
function setStartingInvites (user) {
|
|
|
|
// give new users a few invites (default to 3)
|
2015-05-19 12:34:27 +09:00
|
|
|
user.telescope.inviteCount = Settings.get('startInvitesCount', 3);
|
2015-04-22 07:50:11 +09:00
|
|
|
return user;
|
|
|
|
}
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.callbacks.add("onCreateUser", setStartingInvites);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-05-19 12:34:27 +09:00
|
|
|
// on profile completion, check if the new user has been invited
|
|
|
|
// if so set her status accordingly and update invitation info
|
2015-04-22 07:50:11 +09:00
|
|
|
function checkIfInvited (user) {
|
|
|
|
|
2015-05-19 12:34:27 +09:00
|
|
|
var invite = Invites.findOne({ invitedUserEmail : Users.getEmail(user) });
|
|
|
|
|
|
|
|
if(invite){
|
|
|
|
|
|
|
|
var invitedBy = Meteor.users.findOne({ _id : invite.invitingUserId });
|
|
|
|
|
|
|
|
Users.update(user._id, { $set: {
|
|
|
|
"telescope.isInvited": true,
|
|
|
|
"telescope.invitedBy": invitedBy._id,
|
|
|
|
"telescope.invitedByName": Users.getDisplayName(invitedBy)
|
|
|
|
}});
|
|
|
|
|
|
|
|
Invites.update(invite._id, {$set : {
|
|
|
|
accepted : true
|
|
|
|
}});
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 12:34:27 +09:00
|
|
|
Telescope.callbacks.add("profileCompletedAsync", checkIfInvited);
|