<commit_before>'use strict';
const config = require('./config');
const copy = require('./copy');
const Shuffle = require('./schema').Shuffle;
const Team = require('./schema').Team;
const updateShuffleMessage = require('./util').updateShuffleMessage;


function joinShuffle(teamId, channelId, user) {
    Promise.all([
        Team.findById(teamId).exec(),
        Shuffle.findOne({ teamId, channelId, active: true }).exec(),
    ]).then((values) => {
        const team = values[0];
        const shuffle = values[1];

        if (!team || !shuffle) {
            return;
        }

        // User is already in the shuffle
        if (shuffle.people.find((p) => p.id === user.id)) {
            return;
        }

        shuffle.people.push({ _id: user.id, name: user.name });
        shuffle.save();

        updateShuffleMessage(team, shuffle);
    });
}


function leaveShuffle(teamId, channelId, user) {
    Promise.all([
        Team.findById(teamId).exec(),
        Shuffle.findOne({ teamId, channelId, active: true }).exec(),
    ]).then((values) => {
        const team = values[0];
        const shuffle = values[1];

        if (!team || !shuffle) {
            return;
        }

        shuffle.people.id(user.id).remove();
        shuffle.save();

        updateShuffleMessage(team, shuffle);
    });
}


function *route() {
    let body = this.request.body.payload;

    if (!body) {
        this.response.status = 400;
        return;
    }

    body = JSON.parse(body);

    if (body.token !== config.get('slack:verification')) {
        this.response.status = 401;
        return;
    }

    const callback = body.callback_id;
    const action = body.actions[0].name;
    const teamId = body.team.id;
    const channelId = body.channel.id;
    const user = body.user;

    if (callback === 'start' && action === 'join') {
        this.body = '';
        joinShuffle(teamId, channelId, user);
    } else if (callback === 'start' && action === 'leave') {<commit_msg>Protect against malformed JSON parsing<commit_after>'use strict';
const config = require('./config');
const copy = require('./copy');
const Shuffle = require('./schema').Shuffle;
const Team = require('./schema').Team;
const updateShuffleMessage = require('./util').updateShuffleMessage;


function joinShuffle(teamId, channelId, user) {
    Promise.all([
        Team.findById(teamId).exec(),
        Shuffle.findOne({ teamId, channelId, active: true }).exec(),
    ]).then((values) => {
        const team = values[0];
        const shuffle = values[1];

        if (!team || !shuffle) {
            return;
        }

        // User is already in the shuffle
        if (shuffle.people.find((p) => p.id === user.id)) {
            return;
        }

        shuffle.people.push({ _id: user.id, name: user.name });
        shuffle.save();

        updateShuffleMessage(team, shuffle);
    });
}


function leaveShuffle(teamId, channelId, user) {
    Promise.all([
        Team.findById(teamId).exec(),
        Shuffle.findOne({ teamId, channelId, active: true }).exec(),
    ]).then((values) => {
        const team = values[0];
        const shuffle = values[1];

        if (!team || !shuffle) {
            return;
        }

        shuffle.people.id(user.id).remove();
        shuffle.save();

        updateShuffleMessage(team, shuffle);
    });
}


function *route() {
    let body = this.request.body.payload;

    if (!body) {
        this.response.status = 400;
        return;
    }

    // Protect against malformed JSON
    try {
        body = JSON.parse(body);
    } catch (error) {
        this.response.status = 400;
        return;
    }

    if (body.token !== config.get('slack:verification')) {
        this.response.status = 401;
        return;
    }

    const callback = body.callback_id;
    const action = body.actions[0].name;
    const teamId = body.team.id;
    const channelId = body.channel.id;
    const user = body.user;

    if (callback === 'start' && action === 'join') {
        this.body = '';
        joinShuffle(teamId, channelId, user);
    } else if (callback === 'start' && action === 'leave') {
{'fraction_non_alphanumeric': 0.16024242424242424, 'fraction_numerical': 0.006060606060606061, 'mean_word_length': 2.292897047086991, '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': '6292280', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1217, 'n_words': 282}