<commit_before>'use strict';

// Modules
let _ = require('lodash');
let async = require('async');
let chalk = require('chalk');
let config = require('../config');
let fs = require('fs');
let stringify = require('csv-stringify');
let Organization = require('../Organization');
let path = require('path');
let Submissions = require('../Submissions');

// Settings
let cordiality = +config.get('cordiality');

// Modifications
Organization.prototype.requestSubmission = function requestSubmission(params) {
    let eligible = params.free ? this.eligible.free : this.eligible[params.from.source];
    let lostIndexes = [];
    let submission = _.find(eligible, (submission, index) => {
        if (!this.hashes[submission.hash] && submission.givenCount < cordiality) {
            return true;
        } else {
            lostIndexes.push(index);
            return false;
        }
    });

    if (lostIndexes.length > 0) {
        _.pullAt(eligible, lostIndexes);
    }

    return submission;
}

Organization.prototype.giveSubmission = function giveSubmission(params) {
    let submission = params.submission;
    params.to.hashes[submission.hash] = true;
    params.to.eligible[this.source].shift();
    params.to.received.push(submission);
    submission.givenCount++;
    this.givenCount++;
}

Organization.prototype.takeFreeSubmission = function takeFreeSubmission(params) {
    let submission = params.submission;
    submission.givenCount++;
    this.eligible.free.shift();
    this.freeCount++;
    this.hashes[submission.hash] = true;
    this.received.push(submission);
}

// Class
module.exports = class YouGetWhatYouSource {

    constructor(params) {
        this.callback = params.callback;
        this.organizations = params.organizations;
        this.submissions = params.submissions;
        this.start();
    }

    start(next) {
        async.series([
            // Modifying organization and submissions objects
            (next) => {
                _.each(this.organizations, (organization) => {
                    organization.eligible = {};
                    organization.eligibleCount = 0;
                    organization.freeCount = 0;
                    organization.givenCount = 0;
                    organization.received = [];
                    organization.source = organization.sources[0];
                    organization.sourced = [];
                    organization.swappableSourced = [];
                });

                _.each(this.submissions.hashes, (submission) => {
                    // Used to determine rarity of submission
                    submission.eligible = [];
                    submission.givenCount = 0;
                });

                next();
            },

            // Assigning submissions to organizations
            (next) => {
                let sourceMap = {};

                _.each(this.organizations, (organization) => {
                    _.each(organization.sources, (source) => {
                        sourceMap[source] = organization;
                    });
                });

                _.each(this.submissions.hashes, (submission) => {
                    submission.sourceObjects = [];

                    _.each(_.keys(submission.sources), (source) => {
                        let organization = sourceMap[source];
                        if (organization) {
                            organization.hashes[submission.hash] = true;<commit_msg>Allowing for states not to be defined in config<commit_after>'use strict';

// Modules
let _ = require('lodash');
let async = require('async');
let chalk = require('chalk');
let config = require('../config');
let fs = require('fs');
let stringify = require('csv-stringify');
let Organization = require('../Organization');
let path = require('path');
let Submissions = require('../Submissions');

// Settings
let cordiality = +config.get('cordiality');

// Modifications
Organization.prototype.requestSubmission = function requestSubmission(params) {
    let eligible = params.free ? this.eligible.free : this.eligible[params.from.source];
    let lostIndexes = [];
    let submission = _.find(eligible, (submission, index) => {
        if (!this.hashes[submission.hash] && submission.givenCount < cordiality) {
            return true;
        } else {
            lostIndexes.push(index);
            return false;
        }
    });

    if (lostIndexes.length > 0) {
        _.pullAt(eligible, lostIndexes);
    }

    return submission;
}

Organization.prototype.giveSubmission = function giveSubmission(params) {
    let submission = params.submission;
    params.to.hashes[submission.hash] = true;
    params.to.eligible[this.source].shift();
    params.to.received.push(submission);
    submission.givenCount++;
    this.givenCount++;
}

Organization.prototype.takeFreeSubmission = function takeFreeSubmission(params) {
    let submission = params.submission;
    submission.givenCount++;
    this.eligible.free.shift();
    this.freeCount++;
    this.hashes[submission.hash] = true;
    this.received.push(submission);
}

// Class
module.exports = class YouGetWhatYouSource {

    constructor(params) {
        this.callback = params.callback;
        this.organizations = params.organizations;
        this.submissions = params.submissions;
        this.start();
    }

    start(next) {
        async.series([
            // Modifying organization and submissions objects
            (next) => {
                _.each(this.organizations, (organization) => {
                    organization.eligible = {};
                    organization.eligibleCount = 0;
                    organization.freeCount = 0;
                    organization.givenCount = 0;
                    organization.received = [];
                    organization.source = organization.sources[0];
                    organization.sourced = [];
                    organization.states = organization.states || [];
                    organization.swappableSourced = [];
                });

                _.each(this.submissions.hashes, (submission) => {
                    // Used to determine rarity of submission
                    submission.eligible = [];
                    submission.givenCount = 0;
                });

                next();
            },

            // Assigning submissions to organizations
            (next) => {
                let sourceMap = {};

                _.each(this.organizations, (organization) => {
                    _.each(organization.sources, (source) => {
                        sourceMap[source] = organization;
                    });
                });

                _.each(this.submissions.hashes, (submission) => {
                    submission.sourceObjects = [];

                    _.each(_.keys(submission.sources), (source) => {
                        let organization = sourceMap[source];
                        if (organization) {
                            organization.hashes[submission.hash] = true;
{'fraction_non_alphanumeric': 0.12071174377224199, 'fraction_numerical': 0.001708185053380783, 'mean_word_length': 1.9275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '3383390', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1711, 'n_words': 331}