<commit_before>const loki = require("lokijs");

let db = new loki("data.json");
let messages = db.addCollection("messages");
let sessions = db.addCollection("sessions");

let _exports = module.exports = {};

/**
 * Clone an object
 * This is rather hacky
 * 
 * @private
 * @param {object} obj - The object to clone
 * @returns {obj} Cloned object
 */
function clone(obj) {
	return JSON.parse( JSON.stringify(obj) );
}

/**
 * Prepare a message for storage
 * 
 * @private
 * @param {object} msg - Message to be cleaned
 * @returns {object} Cleaned message
 */
function clean_message(msg) {
	msg = clone(msg);

	delete msg.secret;
	msg.timestamp = Math.floor( (new Date()).getTime() / 1000 );
	return msg;
}

/**
 * Clean a list of messages
 * 
 * @private
 * @param {array} msg - Messages to be cleaned
 * @returns {array} Cleaned list of messages
 */
function clean_messages(msg) {
	let new_messages = [];

	msg.forEach( (item) => {
		item = clone(item);

		delete item.token;

		new_messages.push( _exports.clean_object(item) );
	});
	
	return new_messages;
}

/**
 * Strip database information from an object
 * 
 * @public
 * @param {object} obj - Object to be cleaned
 * @returns {object} The cleaned object
 */
_exports.clean_object = function (obj) {
	obj = clone(obj);
	
	delete obj.meta;
	delete obj["$loki"];
	
	return obj;
}

/**
 * Save a message to the database
 *
 * @public
 * @param {object} msg - Message to be sent
 */
_exports.store_message = function (msg) {
	let session = _exports.get_session(msg.token);

	if (session === false) {
		return false;
	}

	console.log(session);

	msg = clone(msg);

	msg = Object.assign(session, msg);

	messages.insert( clean_message(msg) );
}

/**
 * Get messages since a timestamp
 * 
 * @public
 * @param {number} timestamp - Timestamp from which to select
 * @returns {array} All messages sent since the timestamp
 */
_exports.get_messages = function (timestamp) {
	return clean_messages( clone( messages.find( {"timestamp" : {"$gt" : timestamp} } ) ) );
}

/**
 * Find a user by its token
 *
 * @public
 * @param {string} token - Token to select by
 * @returns {object} First matching session
 */
_exports.get_session = function (token) {
	return _exports.find_session( { token : token } );
}

/**
 * Find a user with an arbitrary query
 *
 * @public
 * @param {object} obj - Query to select by
 * @returns {object} First matching session
 */
_exports.find_session = function (obj) {
	let session = sessions.find( obj );

	if ( session.length === 0 ) {
		return false;
	}

	session = clone(session[0])

	delete session.token;

	return _exports.clean_object(session);
}

/**
 * Register a user in the database
 *
 * @public
 * @param {object} session - Session to be registered
 * @returns {string} Token for the new user
 */
_exports.generate_session = function (session) {
	token = require('crypto').randomBytes(64).toString('hex');
	
	if ( _exports.get_session(token).length > 0 ) {
		return _exports.generate_session(session);	
	}
	
	id = require('crypto').randomBytes(16).toString('hex');

	session.token = token;<commit_msg>Shrink uid value<commit_after>const loki = require("lokijs");

let db = new loki("data.json");
let messages = db.addCollection("messages");
let sessions = db.addCollection("sessions");

let _exports = module.exports = {};

/**
 * Clone an object
 * This is rather hacky
 * 
 * @private
 * @param {object} obj - The object to clone
 * @returns {obj} Cloned object
 */
function clone(obj) {
	return JSON.parse( JSON.stringify(obj) );
}

/**
 * Prepare a message for storage
 * 
 * @private
 * @param {object} msg - Message to be cleaned
 * @returns {object} Cleaned message
 */
function clean_message(msg) {
	msg = clone(msg);

	delete msg.secret;
	msg.timestamp = Math.floor( (new Date()).getTime() / 1000 );
	return msg;
}

/**
 * Clean a list of messages
 * 
 * @private
 * @param {array} msg - Messages to be cleaned
 * @returns {array} Cleaned list of messages
 */
function clean_messages(msg) {
	let new_messages = [];

	msg.forEach( (item) => {
		item = clone(item);

		delete item.token;

		new_messages.push( _exports.clean_object(item) );
	});
	
	return new_messages;
}

/**
 * Strip database information from an object
 * 
 * @public
 * @param {object} obj - Object to be cleaned
 * @returns {object} The cleaned object
 */
_exports.clean_object = function (obj) {
	obj = clone(obj);
	
	delete obj.meta;
	delete obj["$loki"];
	
	return obj;
}

/**
 * Save a message to the database
 *
 * @public
 * @param {object} msg - Message to be sent
 */
_exports.store_message = function (msg) {
	let session = _exports.get_session(msg.token);

	if (session === false) {
		return false;
	}

	console.log(session);

	msg = clone(msg);

	msg = Object.assign(session, msg);

	messages.insert( clean_message(msg) );
}

/**
 * Get messages since a timestamp
 * 
 * @public
 * @param {number} timestamp - Timestamp from which to select
 * @returns {array} All messages sent since the timestamp
 */
_exports.get_messages = function (timestamp) {
	return clean_messages( clone( messages.find( {"timestamp" : {"$gt" : timestamp} } ) ) );
}

/**
 * Find a user by its token
 *
 * @public
 * @param {string} token - Token to select by
 * @returns {object} First matching session
 */
_exports.get_session = function (token) {
	return _exports.find_session( { token : token } );
}

/**
 * Find a user with an arbitrary query
 *
 * @public
 * @param {object} obj - Query to select by
 * @returns {object} First matching session
 */
_exports.find_session = function (obj) {
	let session = sessions.find( obj );

	if ( session.length === 0 ) {
		return false;
	}

	session = clone(session[0])

	delete session.token;

	return _exports.clean_object(session);
}

/**
 * Register a user in the database
 *
 * @public
 * @param {object} session - Session to be registered
 * @returns {string} Token for the new user
 */
_exports.generate_session = function (session) {
	token = require('crypto').randomBytes(64).toString('hex');
	
	if ( _exports.get_session(token).length > 0 ) {
		return _exports.generate_session(session);	
	}
	
	id = require('crypto').randomBytes(2).toString('hex');

	session.token = token;
{'fraction_non_alphanumeric': 0.1424409193913888, 'fraction_numerical': 0.003399158303658142, 'mean_word_length': 3.727620504973221, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '306624', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1933, 'n_words': 595}