<commit_before>Petitions = new Meteor.Collection('petitions');

if (Meteor.isServer)
  Petitions._ensureIndex({title: 1}, {unique: 1});

PetitionsIndex = new EasySearch.Index({
  collection: Petitions,
  fields: ['title', 'description', 'author'],
  engine: new EasySearch.Minimongo()
}, {
  limit: 50
})

var validatePetitionOnCreate = function validatePetitionOnCreate (petitionAttributes) {

  // ensure title is unique
  if (Petitions.findOne({title: petitionAttributes.title}))
    throw new Meteor.Error(422, 'This title has already been used. Write a different one.');

};

var validatePetition = function validatePetition (petitionAttributes) {

  // ensure the user is logged in
  if (!Meteor.user())
    throw new Meteor.Error(401, "You need to login to do that.");

  // ensure the petition has a title
  if (!petitionAttributes.title || !petitionAttributes.title.trim())
    throw new Meteor.Error(422, 'Please fill in a \n title.');

  var titleLength = petitionAttributes.title.length;
  if (titleLength > 70)
    throw new Meteor.Error(422, 'Title must not exceed 70 characters. Currently: ' + titleLength );

  // ensure the petition has at least one tag
  if (!petitionAttributes.tag_ids || petitionAttributes.tag_ids.length == 0)
    throw new Meteor.Error(422, 'Please add at least one tag to the petition.');

  if (petitionAttributes.tag_ids.length > 3)
    throw new Meteor.Error(422, 'Petitions are limited to at most 3 tags.');

  // ensure the petition has a description
  if (!petitionAttributes.description || !petitionAttributes.description.trim())
    throw new Meteor.Error(422, 'Please fill in a description.');

  var descriptionLength = petitionAttributes.title.length;
  if (descriptionLength > 10000)
    throw new Meteor.Error(422, 'Description must not exceed 10000 characters. Currently: ' + descriptionLength );
};

Meteor.methods({
  petition: function(petitionAttributes) {

    validatePetition(petitionAttributes);
    validatePetitionOnCreate(petitionAttributes);

    var user = Meteor.user();
    var publishByDefault = !(Singleton.findOne().moderation);

    // pick out the whitelisted keys
    var petition = _.extend(_.pick(petitionAttributes, 'title', 'description', 'tag_ids'), {
      userId: user._id,
      author: user.profile.name,
      submitted: new Date().getTime(),
      upvoters: [user._id],
      subscribers: [user._id],
      votes: 1,
      minimumVotes: Singleton.findOne().minimumThreshold,
      published: publishByDefault,
      pending: Singleton.findOne().moderation,
      lastSignedAt: new Date().getTime()
    });

    var petitionId = Petitions.insert(petition);

    Singleton.update({}, {$inc: {petitionsCount: 1}});

    return petitionId;
  },

  sign: function(petitionId) {
    var user = Meteor.user();

    if (!user)
      throw new Meteor.Error(401, "You need to login to sign a petition.");

    var petition = Petitions.findOne(petitionId);

    if (!petition.published)
      throw new Meteor.Error(401, "This petition is not published.");

    if (moment(petition.submitted).isBefore(moment().subtract(1, 'month')))
      throw new Meteor.Error(401, "This petition has expired.");

    Petitions.update({
      _id: petitionId,
      upvoters: {$ne: user._id},
      subscribers: {$ne: user._id}
    }, {
      $addToSet: {upvoters: user._id},
      $addToSet: {subscribers: user._id},
      $inc: {votes: 1},
      $set: {lastSignedAt: new Date().getTime()}
    });
    if(Meteor.isServer){
      if (petition.votes === petition.minimumVotes && Meteor.isServer) {
        var users = Meteor.users.find({roles: {$in: ['notify-threshold-reached']}});
        var emails = users.map(function (user) { return user.profile.mail || user.username + '@' + Meteor.settings.MAIL.default_domain; });

        if (!_.isEmpty(emails)) {
          Mailer.sendTemplatedEmail(
            "petition_threshold_reached",
            {
              to: emails
            },
            {
              petition: petition
            }
          );
        }<commit_msg>resolves issue when signing petitions<commit_after>Petitions = new Meteor.Collection('petitions');

if (Meteor.isServer)
  Petitions._ensureIndex({title: 1}, {unique: 1});

PetitionsIndex = new EasySearch.Index({
  collection: Petitions,
  fields: ['title', 'description', 'author'],
  engine: new EasySearch.Minimongo()
}, {
  limit: 50
})

var validatePetitionOnCreate = function validatePetitionOnCreate (petitionAttributes) {

  // ensure title is unique
  if (Petitions.findOne({title: petitionAttributes.title}))
    throw new Meteor.Error(422, 'This title has already been used. Write a different one.');

};

var validatePetition = function validatePetition (petitionAttributes) {

  // ensure the user is logged in
  if (!Meteor.user())
    throw new Meteor.Error(401, "You need to login to do that.");

  // ensure the petition has a title
  if (!petitionAttributes.title || !petitionAttributes.title.trim())
    throw new Meteor.Error(422, 'Please fill in a \n title.');

  var titleLength = petitionAttributes.title.length;
  if (titleLength > 70)
    throw new Meteor.Error(422, 'Title must not exceed 70 characters. Currently: ' + titleLength );

  // ensure the petition has at least one tag
  if (!petitionAttributes.tag_ids || petitionAttributes.tag_ids.length == 0)
    throw new Meteor.Error(422, 'Please add at least one tag to the petition.');

  if (petitionAttributes.tag_ids.length > 3)
    throw new Meteor.Error(422, 'Petitions are limited to at most 3 tags.');

  // ensure the petition has a description
  if (!petitionAttributes.description || !petitionAttributes.description.trim())
    throw new Meteor.Error(422, 'Please fill in a description.');

  var descriptionLength = petitionAttributes.title.length;
  if (descriptionLength > 10000)
    throw new Meteor.Error(422, 'Description must not exceed 10000 characters. Currently: ' + descriptionLength );
};

Meteor.methods({
  petition: function(petitionAttributes) {

    validatePetition(petitionAttributes);
    validatePetitionOnCreate(petitionAttributes);

    var user = Meteor.user();
    var publishByDefault = !(Singleton.findOne().moderation);

    // pick out the whitelisted keys
    var petition = _.extend(_.pick(petitionAttributes, 'title', 'description', 'tag_ids'), {
      userId: user._id,
      author: user.profile.name,
      submitted: new Date().getTime(),
      upvoters: [user._id],
      subscribers: [user._id],
      votes: 1,
      minimumVotes: Singleton.findOne().minimumThreshold,
      published: publishByDefault,
      pending: Singleton.findOne().moderation,
      lastSignedAt: new Date().getTime()
    });

    var petitionId = Petitions.insert(petition);

    Singleton.update({}, {$inc: {petitionsCount: 1}});

    return petitionId;
  },

  sign: function(petitionId) {
    var user = Meteor.user();

    if (!user)
      throw new Meteor.Error(401, "You need to login to sign a petition.");

    var petition = Petitions.findOne(petitionId);

    if (!petition.published)
      throw new Meteor.Error(401, "This petition is not published.");

    if (moment(petition.submitted).isBefore(moment().subtract(1, 'month')))
      throw new Meteor.Error(401, "This petition has expired.");

    Petitions.update({
      _id: petitionId,
      upvoters: {$ne: user._id}
    }, {
      $addToSet: {upvoters: user._id},
      $inc: {votes: 1},
      $set: {lastSignedAt: new Date().getTime()}
    });
    Petitions.update({
      _id: petitionId,
      subscribers: {$ne: user._id}
    }, {
      $addToSet: {subscribers: user._id}
    });
    if(Meteor.isServer){
      if (petition.votes === petition.minimumVotes && Meteor.isServer) {
        var users = Meteor.users.find({roles: {$in: ['notify-threshold-reached']}});
        var emails = users.map(function (user) { return user.profile.mail || user.username + '@' + Meteor.settings.MAIL.default_domain; });

        if (!_.isEmpty(emails)) {
          Mailer.sendTemplatedEmail(
            "petition_threshold_reached",
            {
              to: emails
            },
            {
              petition: petition
            }
          );
        }
{'fraction_non_alphanumeric': 0.128531246178305, 'fraction_numerical': 0.014186131833190656, 'mean_word_length': 3.2951680672268906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2938631', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2346, 'n_words': 647}