making invite stuff work

This commit is contained in:
Sacha Greif 2015-05-18 18:32:54 +09:00
parent eef51ca688
commit e74bea4555
9 changed files with 109 additions and 56 deletions

View file

@ -25,13 +25,11 @@ Template.registerHelper('canPost', function() {
Template.registerHelper('canComment', function() {
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())) {
return true;
}
if ((typeof showError === 'string') && (showError === 'true')) {
Messages.flash(i18n.t('sorry_you_do_not_have_access_to_this_page'), 'error');
}
return false;
});
Template.registerHelper('canEdit', function(item) {

View file

@ -80,6 +80,7 @@ Router._filters = {
canViewRejectedPosts: function () {
var post = this.data();
var user = Meteor.user();
if (!!post && post.status === Posts.config.STATUS_REJECTED && !Users.can.viewRejectedPost(user, post)) {
this.render('no_rights');
} else {

View file

@ -1,4 +1,4 @@
InviteSchema = new SimpleSchema({
var InviteSchema = new SimpleSchema({
_id: {
type: String,
optional: true
@ -20,6 +20,69 @@ InviteSchema = new SimpleSchema({
Invites = new Meteor.Collection("invites");
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.deny({

View file

@ -16,13 +16,12 @@ Meteor.methods({
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);
Meteor.users.findOne({ _id : invitation.userId });
var userEmail = invitation.invitedUserEmail ? invitation.invitedUserEmail :Users.getEmail(user);
var currentUser = Meteor.user();
var currentUserIsAdmin = Users.is.admin(currentUser);
var currentUserCanInvite = currentUserIsAdmin || (currentUser.inviteCount > 0 && Users.can.invite(currentUser));
// check if the person is already invited
if(user && Users.can.invite(user)){
@ -32,7 +31,7 @@ Meteor.methods({
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 });
if (existingInvite) {
@ -48,15 +47,17 @@ Meteor.methods({
});
// 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){
// update invited user
Meteor.users.update(user._id, {$set: {
isInvited: true,
invitedBy: Meteor.userId(),
invitedByName: Users.getDisplayName(currentUser)
}});
Meteor.users.update(user._id, {
$set: {
"telescope.isInvited": true,
"telescope.invitedBy": Meteor.userId(),
"telescope.invitedByName": Users.getDisplayName(currentUser)
}
});
}
var communityName = Settings.get('title','Telescope'),

View file

@ -75,8 +75,6 @@ Package.onUse(function (api) {
// -------------------------------- 3. Variables to export --------------------------------
api.export([
//
]);
api.export("Invites");
});

View file

@ -4,15 +4,20 @@
*/
/**
* Add an additional field to a schema.
* @param {Object} field
* Add an additional field (or an array of fields) to a schema.
* @param {Object|Object[]} field
*/
Mongo.Collection.prototype.addField = function (field) {
Mongo.Collection.prototype.addField = function (fieldOrFieldArray) {
var collection = this;
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
collection.attachSchema(fieldSchema);

View file

@ -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>
{{/if}}
{{#if canInvite}}
{{#if inviteCount}}
<a class="button btn btn-primary inline invite-link" href="#">{{_ "invite"}} ({{inviteCount}} {{_ "left"}})</a>
{{#if isAdmin}}
<a class="button btn btn-primary inline invite-link" href="#">{{_ "invite"}}</a>
{{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}}
</div>

View file

@ -44,7 +44,15 @@ Template.user_info.helpers({
Template.user_info.events({
'click .invite-link': function(e, instance){
Meteor.call('inviteUser', instance.data.user._id);
Messages.flash('Thanks, user has been invited.', "success");
var user = this;
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();
}
});
}
});

View file

@ -90,31 +90,6 @@ Telescope.schemas.userData = new SimpleSchema({
},
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
*/