<commit_before>const sqlite3 = require('sqlite3')
const crypto = require('crypto')

module.exports.db

module.exports = {

  SALT_SIZE: 64,

  connect: function(database) {
    // Use connect method to connect to the Server
    console.log(__filename + ':\tOpening SQLite3 database `' + database + '`...');
    this.db = new sqlite3.Database(database);
    tmpDb = this.db;
    /*
    this.db.serialize(function() {
      var stmt = tmpDb.prepare("INSERT INTO chat (name,api_key) VALUES (?,?)");
      for (var i = 0; i < 10; i++) {
        stmt.run("Ipsum " + i, "key" + i);
      }
      stmt.finalize();
    });
    this.db.close();
    */
  },

  generateApiKey: function(str) {
      var api = {}
      api.salt = crypto.randomBytes(this.SALT_SIZE).toString('hex')
      const input = str + api.salt
      api.key = crypto.createHash('sha256')
                            .update(input)
                            .digest('base64')
      return api
  },

  /**
   * Log chat name and generate salted API key
   */
  logChat: function(name) {
    var chatId = -1
    if(name !== undefined) {
      const apiData = this.generateApiKey(name)
      var db = this.db
      var apiKey = apiData.key
      var apiKeySalt = apiData.salt

      // Log the chat id and generate an API key
      this.db.serialize(function() {
        var stmt = db.prepare("INSERT OR IGNORE INTO chat (id, name, api_key, api_key_salt) VALUES (null,?,?,?)")
        stmt.run(name, apiKey, apiKeySalt)
        stmt.finalize()
      })

      // Retrieve the chat primary key the message was sent from
      this.db.each("SELECT rowid AS id FROM chat WHERE name = ?", name, function(err, row) {
        chatId = row.id
      })
    }
    return chatId
  },

  logUserType: function(name) {
    var typeId = -1
    if(name !== undefined) {
      var db = this.db

      this.db.serialize(function() {
        var stmt = db.prepare("INSERT OR IGNORE INTO user_type (id, name) VALUES (null,?)")
        stmt.run(name)
        stmt.finalize()
      })

      this.db.each("SELECT rowid AS id FROM user_type WHERE name = ?", name, function(err, row) {
        typeId = row.id
      })
    }
    return typeId
  },

  logMessage: function(msg) {
    const chatDbId = this.logChat(msg.chat.id)
    const userTypeDbId = this.logUserType(msg.chat.type)
    // TODO Log message
    // TODO Log user
    // TODO Log message type
  }
}<commit_msg>Fixed callback hell to an extent<commit_after>const sqlite3 = require('sqlite3')
const crypto = require('crypto')

module.exports.db

module.exports = {

  SALT_SIZE: 64,

  connect: function(database) {
    // Use connect method to connect to the Server
    console.log(__filename + ':\tOpening SQLite3 database `' + database + '`...');
    this.db = new sqlite3.Database(database);
    tmpDb = this.db;
    /*
    this.db.serialize(function() {
      var stmt = tmpDb.prepare("INSERT INTO chat (name,api_key) VALUES (?,?)");
      for (var i = 0; i < 10; i++) {
        stmt.run("Ipsum " + i, "key" + i);
      }
      stmt.finalize();
    });
    this.db.close();
    */
  },

  generateApiKey: function(str) {
      var api = {}
      api.salt = crypto.randomBytes(this.SALT_SIZE).toString('hex')
      const input = str + api.salt
      api.key = crypto.createHash('sha256')
                            .update(input)
                            .digest('base64')
      return api
  },

  /**
   * Log chat name and generate salted API key
   */
  logChat: function(name, callback) {
    var chatId = -1
    if(name !== undefined) {
      const apiData = this.generateApiKey(name)
      var db = this.db
      var apiKey = apiData.key
      var apiKeySalt = apiData.salt

      // Log the chat id and generate an API key
      this.db.serialize(function() {
        var stmt = db.prepare("INSERT OR IGNORE INTO chat (id, name, api_key, api_key_salt) VALUES (null,?,?,?)")
        stmt.run(name, apiKey, apiKeySalt)
        stmt.finalize()
      })

      // Retrieve the chat primary key the message was sent from
      this.db.each("SELECT rowid AS id FROM chat WHERE name = ?", name, function(err, row) {
        callback(row.id)
      })
    }
  },

  logUserType: function(name, callback) {
    var typeId = -1
    if(name !== undefined) {
      var db = this.db

      this.db.serialize(function() {
        var stmt = db.prepare("INSERT OR IGNORE INTO user_type (id, name) VALUES (null,?)")
        stmt.run(name)
        stmt.finalize()
      })

      this.db.each("SELECT rowid AS id FROM user_type WHERE name = ?", name, function(err, row) {
        callback(row.id)
      })
    }
  },

  logUser: function(name, firstName, lastName, userTypeId) {

  },

  logMessage: function(msg) {
    this.logChat(msg.chat.id, (chatId) => {
      this.logUserType(msg.chat.type, (typeId) => {
        console.log(chatId)
        console.log(typeId)
        // TODO Log message
        // TODO Log user
        // TODO Log message type
      })
    })
  }
}
{'fraction_non_alphanumeric': 0.13618990142828405, 'fraction_numerical': 0.0064373365520016095, 'mean_word_length': 2.176996805111821, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1301318', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1582, 'n_words': 412}