<commit_before>const pgp = require('pg-promise')();
const log = require('./logger.js');
const config = require('./config.js');
const request = require('request-promise-native');
const makeMessageResponse = require('./makeMessageResponse.js');

const db = pgp(config.postgres);

module.exports = function (message, room, event, client, language) {
    log.log(`Recieved message [${message}]`);
    return request.post({
            method: 'POST',
            uri: `http://api.polite.ai/api/${config.api.version}/classify`,
            body: {
                text: message
            },
            json: true
        })
        .then(res => {
            log.log(`Got following results for [${message}]: `, res.results);
            const positiveResults = Object.keys(res.results).filter(key => Number(res.results[key]));
            if (positiveResults.length) {
                const messageResponse = makeMessageResponse(message, res.results, language);
                client.sendTextMessage(room.roomId, `${event.getSender()}: ${messageResponse}`);

                return db.none('INSERT INTO messages(message, classifier, derived, room_provider, room_id, event_id, time) VALUES(${message}, ${classifier}, ${derived}, ${room_provider}, ${room_id}, ${event_id}, ${time})', {
                    message: message,
                    classifier: config.api.version,
                    derived: JSON.stringify(res.results),
                    room_provider: 'matrix',
                    room_id: room.roomId,
                    event_id: event.event.event_id,
                    time: event._date
                });
            }
        })
        .catch(err => {
            log.warn(`Failed to get data from api (${message})`, err);
        });
};<commit_msg>Always store message in database row and include crypto key<commit_after>const pgp = require('pg-promise')();
const log = require('./logger.js');
const config = require('./config.js');
const request = require('request-promise-native');
const makeMessageResponse = require('./makeMessageResponse.js');
const crypto = require('crypto');


const db = pgp(config.postgres);
const salt = config.global.secretsalt;

module.exports = function (message, room, event, client, language) {
    log.log(`Recieved message [${message}]`);
    return request.post({
            method: 'POST',
            uri: `http://api.polite.ai/api/${config.api.version}/classify`,
            body: {
                text: message
            },
            json: true
        })
        .then(res => {
            log.log(`Got following results for [${message}]: `, res.results);
            const hash = crypto.createHash('sha256');

            hash.update(salt+'matrix'+room.roomId);
            console.log(hash.digest('hex'));

            const positiveResults = Object.keys(res.results).filter(key => Number(res.results[key]));
            return db.none('INSERT INTO messages(message, classifier, derived, room_provider, room_id, room_key, event_id, time) VALUES(${message}, ${classifier}, ${derived}, ${room_provider}, ${room_id}, ${event_id}, ${time})', {
                message: message,
                classifier: config.api.version,
                derived: JSON.stringify(res.results),
                room_provider: 'matrix',
                room_id: room.roomId,
                room_key: hash.digest('hex'),
                event_id: event.event.event_id,
                time: event._date
            });
            if (positiveResults.length) {
                const messageResponse = makeMessageResponse(message, res.results, language);
                client.sendTextMessage(room.roomId, `${event.getSender()}: ${messageResponse}`);
            }

        })
        .catch(err => {
            log.warn(`Failed to get data from api (${message})`, err);
        });
};
{'fraction_non_alphanumeric': 0.14865920333246552, 'fraction_numerical': 0.0007810466024472793, 'mean_word_length': 2.250423011844332, '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': '4270586', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1006, 'n_words': 223}