mirror of
https://github.com/vale981/Vulcan
synced 2025-03-07 02:21:43 -05:00
making invite stuff work
This commit is contained in:
parent
eef51ca688
commit
e74bea4555
9 changed files with 109 additions and 56 deletions
|
@ -25,13 +25,11 @@ Template.registerHelper('canPost', function() {
|
||||||
Template.registerHelper('canComment', function() {
|
Template.registerHelper('canComment', function() {
|
||||||
return Users.can.comment(Meteor.user());
|
return Users.can.comment(Meteor.user());
|
||||||
});
|
});
|
||||||
Template.registerHelper('isAdmin', function(showError) {
|
Template.registerHelper('isAdmin', function(user) {
|
||||||
|
var user = typeof user === "undefined" ? Meteor.user() : user;
|
||||||
if (Users.is.admin(Meteor.user())) {
|
if (Users.is.admin(Meteor.user())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((typeof showError === 'string') && (showError === 'true')) {
|
|
||||||
Messages.flash(i18n.t('sorry_you_do_not_have_access_to_this_page'), 'error');
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
Template.registerHelper('canEdit', function(item) {
|
Template.registerHelper('canEdit', function(item) {
|
||||||
|
|
|
@ -80,6 +80,7 @@ Router._filters = {
|
||||||
|
|
||||||
canViewRejectedPosts: function () {
|
canViewRejectedPosts: function () {
|
||||||
var post = this.data();
|
var post = this.data();
|
||||||
|
var user = Meteor.user();
|
||||||
if (!!post && post.status === Posts.config.STATUS_REJECTED && !Users.can.viewRejectedPost(user, post)) {
|
if (!!post && post.status === Posts.config.STATUS_REJECTED && !Users.can.viewRejectedPost(user, post)) {
|
||||||
this.render('no_rights');
|
this.render('no_rights');
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
InviteSchema = new SimpleSchema({
|
var InviteSchema = new SimpleSchema({
|
||||||
_id: {
|
_id: {
|
||||||
type: String,
|
type: String,
|
||||||
optional: true
|
optional: true
|
||||||
|
@ -20,6 +20,69 @@ InviteSchema = new SimpleSchema({
|
||||||
Invites = new Meteor.Collection("invites");
|
Invites = new Meteor.Collection("invites");
|
||||||
Invites.attachSchema(InviteSchema);
|
Invites.attachSchema(InviteSchema);
|
||||||
|
|
||||||
|
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,
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
// invites are managed through Meteor method
|
// invites are managed through Meteor method
|
||||||
|
|
||||||
Invites.deny({
|
Invites.deny({
|
||||||
|
|
|
@ -16,13 +16,12 @@ Meteor.methods({
|
||||||
|
|
||||||
var user = invitation.invitedUserEmail ?
|
var user = invitation.invitedUserEmail ?
|
||||||
Meteor.users.findOne({ emails : { $elemMatch: { address: invitation.invitedUserEmail } } }) :
|
Meteor.users.findOne({ emails : { $elemMatch: { address: invitation.invitedUserEmail } } }) :
|
||||||
Meteor.users.findOne({ _id : invitation.userId }),
|
Meteor.users.findOne({ _id : invitation.userId });
|
||||||
userEmail = invitation.invitedUserEmail ? invitation.invitedUserEmail :
|
|
||||||
Users.getEmail(user),
|
var userEmail = invitation.invitedUserEmail ? invitation.invitedUserEmail :Users.getEmail(user);
|
||||||
currentUser = Meteor.user(),
|
var currentUser = Meteor.user();
|
||||||
currentUserCanInvite = currentUserIsAdmin ||
|
var currentUserIsAdmin = Users.is.admin(currentUser);
|
||||||
(currentUser.inviteCount > 0 && Users.can.invite(currentUser)),
|
var currentUserCanInvite = currentUserIsAdmin || (currentUser.inviteCount > 0 && Users.can.invite(currentUser));
|
||||||
currentUserIsAdmin = Users.is.admin(currentUser);
|
|
||||||
|
|
||||||
// check if the person is already invited
|
// check if the person is already invited
|
||||||
if(user && Users.can.invite(user)){
|
if(user && Users.can.invite(user)){
|
||||||
|
@ -32,7 +31,7 @@ Meteor.methods({
|
||||||
throw new Meteor.Error(701, "You can't invite this user, sorry.");
|
throw new Meteor.Error(701, "You can't invite this user, sorry.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't allow duplicate multpile invite for the same person
|
// don't allow duplicate multiple invite for the same person
|
||||||
var existingInvite = Invites.findOne({ invitedUserEmail : userEmail });
|
var existingInvite = Invites.findOne({ invitedUserEmail : userEmail });
|
||||||
|
|
||||||
if (existingInvite) {
|
if (existingInvite) {
|
||||||
|
@ -48,15 +47,17 @@ Meteor.methods({
|
||||||
});
|
});
|
||||||
|
|
||||||
// update invinting user
|
// update invinting user
|
||||||
Meteor.users.update(Meteor.userId(), {$inc:{inviteCount: -1}});
|
Meteor.users.update(Meteor.userId(), {$inc:{"telescope.inviteCount": -1}, $inc:{"telescope.invitedCount": 1}});
|
||||||
|
|
||||||
if(user){
|
if(user){
|
||||||
// update invited user
|
// update invited user
|
||||||
Meteor.users.update(user._id, {$set: {
|
Meteor.users.update(user._id, {
|
||||||
isInvited: true,
|
$set: {
|
||||||
invitedBy: Meteor.userId(),
|
"telescope.isInvited": true,
|
||||||
invitedByName: Users.getDisplayName(currentUser)
|
"telescope.invitedBy": Meteor.userId(),
|
||||||
}});
|
"telescope.invitedByName": Users.getDisplayName(currentUser)
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var communityName = Settings.get('title','Telescope'),
|
var communityName = Settings.get('title','Telescope'),
|
||||||
|
|
|
@ -75,8 +75,6 @@ Package.onUse(function (api) {
|
||||||
|
|
||||||
// -------------------------------- 3. Variables to export --------------------------------
|
// -------------------------------- 3. Variables to export --------------------------------
|
||||||
|
|
||||||
api.export([
|
api.export("Invites");
|
||||||
//
|
|
||||||
]);
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,15 +4,20 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an additional field to a schema.
|
* Add an additional field (or an array of fields) to a schema.
|
||||||
* @param {Object} field
|
* @param {Object|Object[]} field
|
||||||
*/
|
*/
|
||||||
Mongo.Collection.prototype.addField = function (field) {
|
Mongo.Collection.prototype.addField = function (fieldOrFieldArray) {
|
||||||
|
|
||||||
var collection = this;
|
var collection = this;
|
||||||
var fieldSchema = {};
|
var fieldSchema = {};
|
||||||
|
|
||||||
fieldSchema[field.fieldName] = field.fieldSchema;
|
var fieldArray = Array.isArray(fieldOrFieldArray) ? fieldOrFieldArray : [fieldOrFieldArray];
|
||||||
|
|
||||||
|
// loop over fields and add them to schema
|
||||||
|
fieldArray.forEach(function (field) {
|
||||||
|
fieldSchema[field.fieldName] = field.fieldSchema;
|
||||||
|
});
|
||||||
|
|
||||||
// add field schema to collection schema
|
// add field schema to collection schema
|
||||||
collection.attachSchema(fieldSchema);
|
collection.attachSchema(fieldSchema);
|
||||||
|
|
|
@ -25,10 +25,14 @@
|
||||||
<a class="edit-profile-button button btn btn-primary inline" href="{{pathFor route='user_edit' slug=telescope.slug}}">{{_ "edit_profile"}}</a>
|
<a class="edit-profile-button button btn btn-primary inline" href="{{pathFor route='user_edit' slug=telescope.slug}}">{{_ "edit_profile"}}</a>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{#if canInvite}}
|
{{#if canInvite}}
|
||||||
{{#if inviteCount}}
|
{{#if isAdmin}}
|
||||||
<a class="button btn btn-primary inline invite-link" href="#">{{_ "invite"}} ({{inviteCount}} {{_ "left"}})</a>
|
<a class="button btn btn-primary inline invite-link" href="#">{{_ "invite"}}</a>
|
||||||
{{else}}
|
{{else}}
|
||||||
<a class="button btn inline disabled" href="#">{{_ "invite_none_left"}}</a>
|
{{#if inviteCount}}
|
||||||
|
<a class="button btn btn-primary inline invite-link" href="#">{{_ "invite"}} ({{inviteCount}} {{_ "left"}})</a>
|
||||||
|
{{else}}
|
||||||
|
<a class="button btn inline disabled" href="#">{{_ "invite_none_left"}}</a>
|
||||||
|
{{/if}}
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -44,7 +44,15 @@ Template.user_info.helpers({
|
||||||
|
|
||||||
Template.user_info.events({
|
Template.user_info.events({
|
||||||
'click .invite-link': function(e, instance){
|
'click .invite-link': function(e, instance){
|
||||||
Meteor.call('inviteUser', instance.data.user._id);
|
var user = this;
|
||||||
Messages.flash('Thanks, user has been invited.', "success");
|
Meteor.call('inviteUser', {userId: user._id}, function(error, success){
|
||||||
|
if (error) {
|
||||||
|
Messages.flash(error, "error");
|
||||||
|
Messages.clearSeen();
|
||||||
|
} else {
|
||||||
|
Messages.flash('Thanks, user has been invited.', "success");
|
||||||
|
Messages.clearSeen();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -90,31 +90,6 @@ Telescope.schemas.userData = new SimpleSchema({
|
||||||
},
|
},
|
||||||
template: "user_profile_bio"
|
template: "user_profile_bio"
|
||||||
},
|
},
|
||||||
/**
|
|
||||||
A count of the user's remaining invites
|
|
||||||
*/
|
|
||||||
inviteCount: {
|
|
||||||
type: Number,
|
|
||||||
optional: true
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
A count of how many users have been invited by the user
|
|
||||||
*/
|
|
||||||
invitedCount: {
|
|
||||||
type: Number,
|
|
||||||
optional: true
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
Whether the user is invited or not
|
|
||||||
*/
|
|
||||||
isInvited: {
|
|
||||||
type: Boolean,
|
|
||||||
optional: true,
|
|
||||||
editableBy: ["admin"],
|
|
||||||
autoform: {
|
|
||||||
omit: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
/**
|
/**
|
||||||
The user's karma
|
The user's karma
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue