<commit_before>/**
 * Confirm Request message
 * @module tracker/messages/confirm-request
 */
const debug = require('debug')('bhit:message');
const moment = require('moment-timezone');
const WError = require('verror').WError;

/**
 * Confirm Request message class
 */
class ConfirmRequest {
    /**
     * Create service
     * @param {App} app                         The application
     * @param {object} config                   Configuration
     * @param {UserRepository} userRepo         User repository
     * @param {DaemonRepository} daemonRepo     Daemon repository
     */
    constructor(app, config, userRepo, daemonRepo) {
        this._app = app;
        this._config = config;
        this._userRepo = userRepo;
        this._daemonRepo = daemonRepo;
    }

    /**
     * Service name is 'modules.tracker.messages.confirmRequest'
     * @type {string}
     */
    static get provides() {
        return 'modules.tracker.messages.confirmRequest';
    }

    /**
     * Dependencies as constructor arguments
     * @type {string[]}
     */
    static get requires() {
        return [ 'app', 'config', 'repositories.user', 'repositories.daemon' ];
    }

    /**
     * Message handler
     * @param {string} id           ID of the client
     * @param {object} message      The message
     */
    onMessage(id, message) {
        let client = this.tracker.clients.get(id);
        if (!client)
            return;

        debug(`Got CONFIRM REQUEST from ${client.socket.remoteAddress}:${client.socket.remotePort}`);
        return this._daemonRepo.findByConfirm(message.confirmRequest.token)
            .then(daemons => {
                let daemon = daemons.length && daemons[0];
                if (!daemon) {
                    let response = this.tracker.ConfirmResponse.create({
                        response: this.tracker.ConfirmResponse.Result.REJECTED,
                    });
                    let message = this.tracker.ServerMessage.create({
                        type: this.tracker.ServerMessage.Type.CONFIRM_RESPONSE,
                        confirmResponse: response,
                    });
                    let data = this.tracker.ServerMessage.encode(message).finish();
                    return this.tracker.send(id, data);
                }

                daemon.token = this.tracker.generateToken();
                daemon.confirm = null;
                daemon.confirmedAt = moment();

                return this._daemonRepo.save(daemon)
                    .then(daemonId => {
                        if (!daemonId)
                            throw new Error('Could not save daemon');

                        let response = this.tracker.ConfirmResponse.create({
                            response: this.tracker.ConfirmResponse.Result.ACCEPTED,
                            token: daemon.token,
                        });
                        let message = this.tracker.ServerMessage.create({
                            type: this._tracker.ServerMessage.Type.CONFIRM_RESPONSE,
                            confirmResponse: response,
                        });
                        let data = this.tracker.ServerMessage.encode(message).finish();
                        this._tracker.send(id, data);
                    });
            })
            .catch(error => {
                this._tracker._logger.error(new WError(error, 'ConfirmRequest.onMessage()'));<commit_msg>Confirm request<commit_after>/**
 * Confirm Request message
 * @module tracker/messages/confirm-request
 */
const debug = require('debug')('bhit:message');
const moment = require('moment-timezone');
const WError = require('verror').WError;

/**
 * Confirm Request message class
 */
class ConfirmRequest {
    /**
     * Create service
     * @param {App} app                         The application
     * @param {object} config                   Configuration
     * @param {UserRepository} userRepo         User repository
     * @param {DaemonRepository} daemonRepo     Daemon repository
     */
    constructor(app, config, userRepo, daemonRepo) {
        this._app = app;
        this._config = config;
        this._userRepo = userRepo;
        this._daemonRepo = daemonRepo;
    }

    /**
     * Service name is 'modules.tracker.messages.confirmRequest'
     * @type {string}
     */
    static get provides() {
        return 'modules.tracker.messages.confirmRequest';
    }

    /**
     * Dependencies as constructor arguments
     * @type {string[]}
     */
    static get requires() {
        return [ 'app', 'config', 'repositories.user', 'repositories.daemon' ];
    }

    /**
     * Message handler
     * @param {string} id           ID of the client
     * @param {object} message      The message
     */
    onMessage(id, message) {
        let client = this.tracker.clients.get(id);
        if (!client)
            return;

        debug(`Got CONFIRM REQUEST from ${client.socket.remoteAddress}:${client.socket.remotePort}`);
        return this._daemonRepo.findByConfirm(message.confirmRequest.token)
            .then(daemons => {
                let daemon = daemons.length && daemons[0];
                if (!daemon) {
                    let response = this.tracker.ConfirmResponse.create({
                        response: this.tracker.ConfirmResponse.Result.REJECTED,
                    });
                    let reply = this.tracker.ServerMessage.create({
                        type: this.tracker.ServerMessage.Type.CONFIRM_RESPONSE,
                        messageId: message.messageId,
                        confirmResponse: response,
                    });
                    let data = this.tracker.ServerMessage.encode(reply).finish();
                    return this.tracker.send(id, data);
                }

                daemon.token = this.tracker.generateToken();
                daemon.confirm = null;
                daemon.confirmedAt = moment();

                return this._daemonRepo.save(daemon)
                    .then(daemonId => {
                        if (!daemonId)
                            throw new Error('Could not save daemon');

                        let response = this.tracker.ConfirmResponse.create({
                            response: this.tracker.ConfirmResponse.Result.ACCEPTED,
                            token: daemon.token,
                        });
                        let reply = this.tracker.ServerMessage.create({
                            type: this._tracker.ServerMessage.Type.CONFIRM_RESPONSE,
                            messageId: message.messageId,
                            confirmResponse: response,
                        });
                        let data = this.tracker.ServerMessage.encode(reply).finish();
                        this._tracker.send(id, data);
                    });
            })
            .catch(error => {
                this._tracker._logger.error(new WError(error, 'ConfirmRequest.onMessage()'));
{'fraction_non_alphanumeric': 0.10592613799026625, 'fraction_numerical': 0.000286286859433152, 'mean_word_length': 1.551862673484295, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4430332', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1732, 'n_words': 350}