/**
 * Adopted from TelescopeJS
 *
 * See: https://github.com/TelescopeJS/Telescope/blob/ba38e2a75b1de3e5a5e5332341a74d5f4424498c/lib/permissions.js
 */


// canView = function (user) {
//   if(getSetting('requireViewInvite', false)){

//     if(Meteor.isClient){
//       // on client only, default to the current user
//       var user=(typeof user === 'undefined') ? Meteor.user() : user;
//     }

//     if(user && (isAdmin(user) || isInvited(user))){
//       // if logged in AND either admin or invited
//       return true;
//     }else{
//       return false;
//     }

//   }
//   return true;
// };
// canViewById = function(userId, returnError){
//   // if an invite is required to view, run permission check, else return true
//   if(getSetting('requireViewInvite', false)){
//     // if user is logged in, then run canView, else return false
//     return userId ? canView(Meteor.users.findOne(userId), returnError) : false;
//   }
//   return true;
// };

// can post topics
canPost = function (user, returnError) {
  var user = (typeof user === 'undefined') ? Meteor.user() : user;
  return !!user;
};
canPostById = function (userId, returnError) {
  var user = Meteor.users.findOne(userId);
  return canPost(user, returnError);
};

// can post comments
canComment = function (user, returnError) {
  return canPost(user, returnError);
};
canCommentById = function (userId, returnError) {
  var user = Meteor.users.findOne(userId);
  return canComment(user, returnError);
};

// can upvote comments
canUpvote = function (user, collection, returnError) {
  return canPost(user, returnError);
};
canUpvoteById = function (userId, returnError) {
  var user = Meteor.users.findOne(userId);
  return canUpvote(user, returnError);
};

// can follow users
canFollow = function (user, returnError) {
  return canPost(user, returnError);
};
canFollowById = function (userId, returnError) {
  var user = Meteor.users.findOne(userId);
  return canFollow(user, returnError);
};

// can edit documents
canEdit = function (user, item, returnError) {
  var user = (typeof user === 'undefined') ? Meteor.user() : user;
  
  if (!user || !item) return false;

  return isAdmin(user) || user._id === item.userId;
};
canEditById = function (userId, item){
  var user = Meteor.users.findOne(userId);
  return canEdit(user, item);
};

canInvite = function (user) {
  if (!user) return false;

  return isAdmin(user) || user.invites && user.invites.inviteCount > 0; 
};











/**
 * Adopted from TelescopeJS
 *
 * See: https://github.com/TelescopeJS/Telescope/blob/ba38e2a75b1de3e5a5e5332341a74d5f4424498c/lib/permissions.js
 */

canView = function (user) {
  if (Meteor.isClient) {
    var user = (typeof user === 'undefined') ? Meteor.user() : user;
  }
  return !!user;
};
canViewById = function (userId) {
  return userId && canView(Meteor.users.findOne(userId));
};

// can post topics
canPost = function (user) {
  var user = (typeof user === 'undefined') ? Meteor.user() : user;
  return !!user;
};
canPostById = function (userId) {
  return userId && canPost(Meteor.users.findOne(userId));
};

// can post comments
canComment = function (user) {
  return canPost(user);
};
canCommentById = function (userId) {
  return userId && canComment(Meteor.users.findOne(userId));
};

// can upvote comments
canUpvote = function (user, collection) {
  return canPost(user);
};
canUpvoteById = function (userId) {
  return userId && canUpvote(Meteor.users.findOne(userId));
};

// can follow users
canFollow = function (user) {
  return canPost(user);
};
canFollowById = function (userId) {
  return userId && canFollow(Meteor.users.findOne(userId));
};

// can edit documents
canEdit = function (user, item) {
  var user = (typeof user === 'undefined') ? Meteor.user() : user;
  
  if (!user || !item) return false;

  return isAdmin(user) || user._id === item.userId;
};
canEditById = function (userId, item){
  if (!userId || !item) return false;

  return canEdit(Meteor.users.findOne(userId), item);
};

canInvite = function (user) {
  if (!user) return false;

  return isAdmin(user) || user.invites && user.invites.inviteCount > 0; 
};











Add canView helpers