<commit_before>
import crypto from 'crypto';
import Backbone from 'backbone';
import Sifter from 'sifter';
import { ObjectID } from 'mongodb';
import ThemeModel from '../public/js/model/theme';


let options = {
    CONST: undefined,
    database: undefined,
    fileApi: undefined,
};


function setOptions(hash) {
    options = hash;
}

function addThemeInUserSession(session, theme) {
    theme._id = theme._id.toString();

    if (!session.themes) {
        session.themes = [];
    }

    session.themes.push( theme._id );
    return true;
}


class Api {
    static post(req, res) {
        if (!req.session.user) {
            res.sendStatus(401);
        }

        Api.createTheme( req.session, req.session.user._id.toString() )
        .then((result) => {
            result._id = result._id.toString();
            res.send(result);
        })
        .catch((errorCode) => {
            res.sendStatus(errorCode);
        });
    }

    static createTheme(session, userId) {
        Backbone.Relational.store.reset();

        const collection = options.database.collection('theme');
        const model = new ThemeModel({
            userId,
            owners: [ userId ],
        });

        return new Promise((resolve, reject) => {
            Api.getNewFragment()
            .then((fragment) => {
                model.set('fragment', fragment);

                collection.insertOne(
                    model.toJSON(),
                    { safe: true },
                    (err, results) => {
                        if (err) {
                            return reject(500);
                        }

                        const result = results.ops[0];

                        addThemeInUserSession(session, result);

                        return resolve(result);
                    }
                );
            });
        });
    }

    static getNewFragment() {
        const collection = options.database.collection('theme');
        const shasum = crypto.createHash('sha1');

        shasum.update([
            new Date().getTime().toString(),
        ].join('') );

        const fragment = shasum.digest('hex').substr(0, 6);

        return new Promise((resolve, reject) => {
            collection.find({
                fragment,
            })
            .toArray((err, results) => {
                if (err) {
                    return reject(500);<commit_msg>Useless code<commit_after>
import crypto from 'crypto';
import Backbone from 'backbone';
import Sifter from 'sifter';
import { ObjectID } from 'mongodb';
import ThemeModel from '../public/js/model/theme';


let options = {
    CONST: undefined,
    database: undefined,
    fileApi: undefined,
};


function setOptions(hash) {
    options = hash;
}


class Api {
    static post(req, res) {
        if (!req.session.user) {
            res.sendStatus(401);
        }

        Api.createTheme( req.session, req.session.user._id.toString() )
        .then((result) => {
            result._id = result._id.toString();
            res.send(result);
        })
        .catch((errorCode) => {
            res.sendStatus(errorCode);
        });
    }

    static createTheme(session, userId) {
        Backbone.Relational.store.reset();

        const collection = options.database.collection('theme');
        const model = new ThemeModel({
            userId,
            owners: [ userId ],
        });

        return new Promise((resolve, reject) => {
            Api.getNewFragment()
            .then((fragment) => {
                model.set('fragment', fragment);

                collection.insertOne(
                    model.toJSON(),
                    { safe: true },
                    (err, results) => {
                        if (err) {
                            return reject(500);
                        }

                        const result = results.ops[0];

                        return resolve(result);
                    }
                );
            });
        });
    }

    static getNewFragment() {
        const collection = options.database.collection('theme');
        const shasum = crypto.createHash('sha1');

        shasum.update([
            new Date().getTime().toString(),
        ].join('') );

        const fragment = shasum.digest('hex').substr(0, 6);

        return new Promise((resolve, reject) => {
            collection.find({
                fragment,
            })
            .toArray((err, results) => {
                if (err) {
                    return reject(500);
{'fraction_non_alphanumeric': 0.12799648119639323, 'fraction_numerical': 0.005718055861007257, 'mean_word_length': 1.4690553745928339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '2426084', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1147, 'n_words': 244}