<commit_before>// generate slug on insert
Categories.before.insert(function (userId, doc) {
  // if no slug has been provided, generate one
  var slug = !!doc.slug ? doc.slug : Telescope.utils.slugify(doc.name);
  doc.slug = Telescope.utils.getUnusedSlug(Categories, slug);
});

// generate slug on edit
Categories.before.update(function (userId, doc, fieldNames, modifier) {
  if (modifier.$set && modifier.$set.slug) {
    modifier.$set.slug = Telescope.utils.getUnusedSlug(Categories, modifier.$set.slug);
  }  
});

// add callback that adds categories CSS classes
function addCategoryClass (postClass, post) {
  var classArray = _.map(Posts.getCategories(post), function (category){return "category-"+category.slug;});
  return postClass + " " + classArray.join(' ');
}
Telescope.callbacks.add("postClass", addCategoryClass);

// ------- Categories Check -------- //

// make sure all categories in the post.categories array exist in the db
var checkCategories = function (post) {

  // if there are not categories, stop here
  if (!post.categories || post.categories.length === 0) {
    return;
  }

  // check how many of the categories given also exist in the db
  var categoryCount = Categories.find({_id: {$in: post.categories}}).count();

  if (post.categories.length !== categoryCount) {
    throw new Meteor.Error('invalid_category', i18n.t('invalid_category'));
  }
};

function postSubmitCheckCategories (post) {
  checkCategories(post);
  return post;
}
Telescope.callbacks.add("postSubmit", postSubmitCheckCategories);

function postEditCheckCategories (post) {
  checkCategories(post);
  return post;
}
Telescope.callbacks.add("postEdit", postEditCheckCategories);

function addParentCategoriesOnSubmit (post) {
  var categories = post.categories;
  var newCategories = [];
  if (categories) {
    categories.forEach(function (categoryId) {
      var category = Categories.findOne(categoryId);
      newCategories = newCategories.concat(_.pluck(category.getParents().reverse(), "_id"));
      newCategories.push(category._id);
    });
  }
  post.categories = _.unique(newCategories);
  return post;
}
Telescope.callbacks.add("postSubmit", addParentCategoriesOnSubmit);

function addParentCategoriesOnEdit (modifier, post) {
  var categories = modifier.$set.categories;
  var newCategories = [];
  if (categories) {
    categories.forEach(function (categoryId) {
      var category = Categories.findOne(categoryId);
      newCategories = newCategories.concat(_.pluck(category.getParents().reverse(), "_id"));
      newCategories.push(category._id);
    });
  }
  modifier.$set.categories = _.unique(newCategories);<commit_msg>Fix bug that $set and $unset categories same time.<commit_after>// generate slug on insert
Categories.before.insert(function (userId, doc) {
  // if no slug has been provided, generate one
  var slug = !!doc.slug ? doc.slug : Telescope.utils.slugify(doc.name);
  doc.slug = Telescope.utils.getUnusedSlug(Categories, slug);
});

// generate slug on edit
Categories.before.update(function (userId, doc, fieldNames, modifier) {
  if (modifier.$set && modifier.$set.slug) {
    modifier.$set.slug = Telescope.utils.getUnusedSlug(Categories, modifier.$set.slug);
  }
});

// add callback that adds categories CSS classes
function addCategoryClass (postClass, post) {
  var classArray = _.map(Posts.getCategories(post), function (category){return "category-"+category.slug;});
  return postClass + " " + classArray.join(' ');
}
Telescope.callbacks.add("postClass", addCategoryClass);

// ------- Categories Check -------- //

// make sure all categories in the post.categories array exist in the db
var checkCategories = function (post) {

  // if there are not categories, stop here
  if (!post.categories || post.categories.length === 0) {
    return;
  }

  // check how many of the categories given also exist in the db
  var categoryCount = Categories.find({_id: {$in: post.categories}}).count();

  if (post.categories.length !== categoryCount) {
    throw new Meteor.Error('invalid_category', i18n.t('invalid_category'));
  }
};

function postSubmitCheckCategories (post) {
  checkCategories(post);
  return post;
}
Telescope.callbacks.add("postSubmit", postSubmitCheckCategories);

function postEditCheckCategories (post) {
  checkCategories(post);
  return post;
}
Telescope.callbacks.add("postEdit", postEditCheckCategories);

function addParentCategoriesOnSubmit (post) {
  var categories = post.categories;
  var newCategories = [];
  if (categories) {
    categories.forEach(function (categoryId) {
      var category = Categories.findOne(categoryId);
      newCategories = newCategories.concat(_.pluck(category.getParents().reverse(), "_id"));
      newCategories.push(category._id);
    });
  }
  post.categories = _.unique(newCategories);
  return post;
}
Telescope.callbacks.add("postSubmit", addParentCategoriesOnSubmit);

function addParentCategoriesOnEdit (modifier, post) {
  if (modifier.$unset && modifier.$unset.categories !== undefined) {
    return modifier;
  }

  var categories = modifier.$set.categories;
  var newCategories = [];
  if (categories) {
    categories.forEach(function (categoryId) {
      var category = Categories.findOne(categoryId);
      newCategories = newCategories.concat(_.pluck(category.getParents().reverse(), "_id"));
      newCategories.push(category._id);
    });
  }
  modifier.$set.categories = _.unique(newCategories);
{'fraction_non_alphanumeric': 0.13700147710487445, 'fraction_numerical': 0.0011078286558345643, 'mean_word_length': 4.449698189134809, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6875115', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1558, 'n_words': 369}