<commit_before>module.exports = function(db, config, sconfig, utilz, logger)
{
	const mongo = require('mongodb')
	const bcrypt = require('bcrypt')
	const mailgun = require('mailgun-js')({apiKey: sconfig.mailgun_api_key, domain: sconfig.mailgun_domain})

	const rooms_version = 40
	const users_version = 28

	function get_random_key()
	{
		return `_key_${Date.now()}_${utilz.get_random_string(12)}`
	}

	var manager = {}

	manager.get_room = function(query, fields)
	{
		return new Promise((resolve, reject) => 
		{
			if(Object.keys(fields).length > 0)
			{
				fields.version = true
			}

			if(query._id !== undefined)
			{
				if(typeof query._id === "string" && query._id !== config.main_room_id)
				{
					try
					{
						query._id = new mongo.ObjectId(query._id)
					}

					catch(err)
					{
						resolve(false)
						return
					}
				}
			}

			db.collection("rooms").findOne(query, fields)

			.then(room =>
			{
				if(!room)
				{
					if(query._id === config.main_room_id)
					{
						manager.create_room({name:config.default_main_room_name, id:config.main_room_id})

						.then(room =>
						{
							resolve(room)
							return
						})

						.catch(err =>
						{
							reject(err)
							logger.log_error(err)
							return
						})

						return
					}

					else
					{
						resolve(false)
						return
					}
				}

				if(room && room.version !== rooms_version)
				{
					db.collection('rooms').findOne({_id:room._id}, {})

					.then(room =>
					{
						if(typeof room.name !== "string")
						{
							room.name = "<NAME>"
						}
						
						if(typeof room.image_source !== "string")
						{
							room.image_source = ""
						}
						
						if(typeof room.image_setter !== "string")
						{
							room.image_setter = ""
						}
						
						if(typeof room.image_size !== "number")
						{
							room.image_size = 0
						}
						
						if(typeof room.image_date !== "number")
						{
							room.image_date = 0
						}

						if(typeof room.image_type !== "string")
						{
							room.image_type = "upload"
						}

						if(typeof room.stored_images !== "object")
						{
							room.stored_images = []
						}
						
						if(typeof room.topic !== "string")
						{
							room.topic = ""
						}
						
						if(typeof room.topic_setter !== "string")
						{
							room.topic_setter = ""
						}
						
						if(typeof room.topic_date !== "number")
						{
							room.topic_date = 0
						}
						
						if(typeof room.keys !== "object")
						{
							room.keys = {}
						}

						if(typeof room.radio_type !== "string")
						{
							room.radio_type = "radio"
						}					
						
						if(typeof room.radio_source !== "string")
						{
							room.radio_source = ""
						}

						if(typeof room.radio_title !== "string")
						{
							room.radio_title = ""
						}
						
						if(typeof room.radio_setter !== "string")
						{
							room.radio_setter = ""
						}
						
						if(typeof room.radio_date !== "number")
						{
							room.radio_date = 0
						}

						if(typeof room.tv_type !== "string")
						{
							room.tv_type = "tv"
						}					
						
						if(typeof room.tv_source !== "string")
						{
							room.tv_source = ""
						}

						if(typeof room.tv_title !== "string")
						{
							room.tv_title = ""
						}
						
						if(typeof room.tv_setter !== "string")
						{
							room.tv_setter = ""
						}
						
						if(typeof room.tv_date !== "number")
						{
							room.tv_date = 0
						}	

						if(typeof room.images_enabled !== "boolean")
						{
							room.images_enabled = true
						}	

						if(typeof room.radio_enabled !== "boolean")
						{
							room.radio_enabled = true
						}	

						if(typeof room.tv_enabled !== "boolean")
						{
							room.tv_enabled = true
						}
						
						if(typeof room.bans !== "object")
						{
							room.bans = []
						}

						if(typeof room.log !== "object")
						{
							room.log = true
						}

						if(typeof room.log_messages !== "object")
						{
							room.log_messages = []
						}

						if(typeof room.theme !== "string")
						{
							room.theme = "rgb(56, 46, 95)"
						}

						if(typeof room.background_image !== "string")
						{
							room.background_image = ""
						}

						if(typeof room.background_image_enabled !== "boolean")
						{
							room.background_image_enabled = true
						}

						if(typeof room.background_mode !== "boolean")
						{
							room.background_mode = "normal"
						}

						if(typeof room.background_tile_dimensions !== "boolean")
						{
							room.background_tile_dimensions = "100px 100px"
						}
						
						if(typeof room.modified !== "number")
						{
							room.modified = Date.now()
						}
						
						if(typeof room.public !== "boolean")
						{
							room.public = true
						}

						if(typeof room.voice1_chat_permission !== "boolean")
						{
							room.voice1_chat_permission = true
						}	

						if(typeof room.voice1_images_permission !== "boolean")
						{
							room.voice1_images_permission = true
						}	

						if(typeof room.voice1_tv_permission !== "boolean")
						{
							room.voice1_tv_permission = true
						}	

						if(typeof room.voice1_radio_permission !== "boolean")
						{
							room.voice1_radio_permission = true
						}

						if(typeof room.voice2_chat_permission !== "boolean")
						{
							room.voice2_chat_permission = true
						}	

						if(typeof room.voice2_images_permission !== "boolean")
						{
							room.voice2_images_permission = true
						}	

						if(typeof room.voice2_tv_permission !== "boolean")
						{
							room.voice2_tv_permission = true
						}	

						if(typeof room.voice2_radio_permission !== "boolean")
						{
							room.voice2_radio_permission = true
						}

						if(typeof room.voice3_chat_permission !== "boolean")
						{
							room.voice3_chat_permission = true
						}	

						if(typeof room.voice3_images_permission !== "boolean")
						{
							room.voice3_images_permission = true
						}	

						if(typeof room.voice3_tv_permission !== "boolean")
						{
							room.voice3_tv_permission = true
						}	

						if(typeof room.voice3_radio_permission !== "boolean")
						{
							room.voice3_radio_permission = true
						}

						if(typeof room.voice4_chat_permission !== "boolean")
						{
							room.voice4_chat_permission = true
						}	

						if(typeof room.voice4_images_permission !== "boolean")
						{
							room.voice4_images_permission = true
						}	

						if(typeof room.voice4_tv_permission !== "boolean")
						{
							room.voice4_tv_permission = true
						}	

						if(typeof room.voice4_radio_permission !== "boolean")
						{
							room.voice4_radio_permission = true
						}

						room.version = rooms_version

						db.collection('rooms').update({_id:room._id}, {$set:room})

						.then(ans =>
						{
							resolve(room)
							return
						})

						.catch(err =>
						{
							reject(err)
							logger.log_error(err)
							return
						})
					})

					.catch(err =>
					{
						reject(err)
						logger.log_error(err)
						return
					})
				}

				else
				{
					resolve(room)
					return
				}
			})

			.catch(err =>
			{
				reject(err)
				logger.log_error(err)
				return
			})
		})
	}

	manager.create_room = function(data)
	{
		return new Promise((resolve, reject) => 
		{
			room = 
			{
				version: rooms_version,
				image_source: '',
				image_setter: '',
				image_size: 0,
				image_date: 0,
				image_type: "upload",
				stored_images: [],
				topic: '',
				topic_setter: '',
				topic_date: 0,
				keys: {},
				radio_type: 'radio',
				radio_source: '',
				radio_title: '',
				radio_setter: '',
				radio_date: 0,
				tv_type: '',
				tv_source: '',
				tv_title: '',
				tv_setter: '',
				tv_date: 0,
				log_messages: [],
				bans: [],
				images_enabled: true,
				radio_enabled: true,
				tv_enabled: true,
				theme: "rgb(56, 46, 95)",
				background_image: "",
				background_image_enabled: true,
				background_mode: "normal",
				background_tile_dimensions: "100px 100px",
				log: true,
				voice1_chat_permission: true,
				voice1_images_permission: true,
				voice1_tv_permission: true,
				voice1_radio_permission: true,
				voice2_chat_permission: true,
				voice2_images_permission: true,
				voice2_tv_permission: true,
				voice2_radio_permission: true,
				voice3_chat_permission: true,
				voice3_images_permission: true,<commit_msg>TILES_moar<commit_after>module.exports = function(db, config, sconfig, utilz, logger)
{
	const mongo = require('mongodb')
	const bcrypt = require('bcrypt')
	const mailgun = require('mailgun-js')({apiKey: sconfig.mailgun_api_key, domain: sconfig.mailgun_domain})

	const rooms_version = 40
	const users_version = 28

	function get_random_key()
	{
		return `_key_${Date.now()}_${utilz.get_random_string(12)}`
	}

	var manager = {}

	manager.get_room = function(query, fields)
	{
		return new Promise((resolve, reject) => 
		{
			if(Object.keys(fields).length > 0)
			{
				fields.version = true
			}

			if(query._id !== undefined)
			{
				if(typeof query._id === "string" && query._id !== config.main_room_id)
				{
					try
					{
						query._id = new mongo.ObjectId(query._id)
					}

					catch(err)
					{
						resolve(false)
						return
					}
				}
			}

			db.collection("rooms").findOne(query, fields)

			.then(room =>
			{
				if(!room)
				{
					if(query._id === config.main_room_id)
					{
						manager.create_room({name:config.default_main_room_name, id:config.main_room_id})

						.then(room =>
						{
							resolve(room)
							return
						})

						.catch(err =>
						{
							reject(err)
							logger.log_error(err)
							return
						})

						return
					}

					else
					{
						resolve(false)
						return
					}
				}

				if(room && room.version !== rooms_version)
				{
					db.collection('rooms').findOne({_id:room._id}, {})

					.then(room =>
					{
						if(typeof room.name !== "string")
						{
							room.name = "<NAME>"
						}
						
						if(typeof room.image_source !== "string")
						{
							room.image_source = ""
						}
						
						if(typeof room.image_setter !== "string")
						{
							room.image_setter = ""
						}
						
						if(typeof room.image_size !== "number")
						{
							room.image_size = 0
						}
						
						if(typeof room.image_date !== "number")
						{
							room.image_date = 0
						}

						if(typeof room.image_type !== "string")
						{
							room.image_type = "upload"
						}

						if(typeof room.stored_images !== "object")
						{
							room.stored_images = []
						}
						
						if(typeof room.topic !== "string")
						{
							room.topic = ""
						}
						
						if(typeof room.topic_setter !== "string")
						{
							room.topic_setter = ""
						}
						
						if(typeof room.topic_date !== "number")
						{
							room.topic_date = 0
						}
						
						if(typeof room.keys !== "object")
						{
							room.keys = {}
						}

						if(typeof room.radio_type !== "string")
						{
							room.radio_type = "radio"
						}					
						
						if(typeof room.radio_source !== "string")
						{
							room.radio_source = ""
						}

						if(typeof room.radio_title !== "string")
						{
							room.radio_title = ""
						}
						
						if(typeof room.radio_setter !== "string")
						{
							room.radio_setter = ""
						}
						
						if(typeof room.radio_date !== "number")
						{
							room.radio_date = 0
						}

						if(typeof room.tv_type !== "string")
						{
							room.tv_type = "tv"
						}					
						
						if(typeof room.tv_source !== "string")
						{
							room.tv_source = ""
						}

						if(typeof room.tv_title !== "string")
						{
							room.tv_title = ""
						}
						
						if(typeof room.tv_setter !== "string")
						{
							room.tv_setter = ""
						}
						
						if(typeof room.tv_date !== "number")
						{
							room.tv_date = 0
						}	

						if(typeof room.images_enabled !== "boolean")
						{
							room.images_enabled = true
						}	

						if(typeof room.radio_enabled !== "boolean")
						{
							room.radio_enabled = true
						}	

						if(typeof room.tv_enabled !== "boolean")
						{
							room.tv_enabled = true
						}
						
						if(typeof room.bans !== "object")
						{
							room.bans = []
						}

						if(typeof room.log !== "object")
						{
							room.log = true
						}

						if(typeof room.log_messages !== "object")
						{
							room.log_messages = []
						}

						if(typeof room.theme !== "string")
						{
							room.theme = "rgb(56, 46, 95)"
						}

						if(typeof room.background_image !== "string")
						{
							room.background_image = ""
						}

						if(typeof room.background_image_enabled !== "boolean")
						{
							room.background_image_enabled = true
						}

						if(typeof room.background_mode !== "boolean")
						{
							room.background_mode = "normal"
						}

						if(typeof room.background_tile_dimensions !== "boolean")
						{
							room.background_tile_dimensions = "100px auto"
						}
						
						if(typeof room.modified !== "number")
						{
							room.modified = Date.now()
						}
						
						if(typeof room.public !== "boolean")
						{
							room.public = true
						}

						if(typeof room.voice1_chat_permission !== "boolean")
						{
							room.voice1_chat_permission = true
						}	

						if(typeof room.voice1_images_permission !== "boolean")
						{
							room.voice1_images_permission = true
						}	

						if(typeof room.voice1_tv_permission !== "boolean")
						{
							room.voice1_tv_permission = true
						}	

						if(typeof room.voice1_radio_permission !== "boolean")
						{
							room.voice1_radio_permission = true
						}

						if(typeof room.voice2_chat_permission !== "boolean")
						{
							room.voice2_chat_permission = true
						}	

						if(typeof room.voice2_images_permission !== "boolean")
						{
							room.voice2_images_permission = true
						}	

						if(typeof room.voice2_tv_permission !== "boolean")
						{
							room.voice2_tv_permission = true
						}	

						if(typeof room.voice2_radio_permission !== "boolean")
						{
							room.voice2_radio_permission = true
						}

						if(typeof room.voice3_chat_permission !== "boolean")
						{
							room.voice3_chat_permission = true
						}	

						if(typeof room.voice3_images_permission !== "boolean")
						{
							room.voice3_images_permission = true
						}	

						if(typeof room.voice3_tv_permission !== "boolean")
						{
							room.voice3_tv_permission = true
						}	

						if(typeof room.voice3_radio_permission !== "boolean")
						{
							room.voice3_radio_permission = true
						}

						if(typeof room.voice4_chat_permission !== "boolean")
						{
							room.voice4_chat_permission = true
						}	

						if(typeof room.voice4_images_permission !== "boolean")
						{
							room.voice4_images_permission = true
						}	

						if(typeof room.voice4_tv_permission !== "boolean")
						{
							room.voice4_tv_permission = true
						}	

						if(typeof room.voice4_radio_permission !== "boolean")
						{
							room.voice4_radio_permission = true
						}

						room.version = rooms_version

						db.collection('rooms').update({_id:room._id}, {$set:room})

						.then(ans =>
						{
							resolve(room)
							return
						})

						.catch(err =>
						{
							reject(err)
							logger.log_error(err)
							return
						})
					})

					.catch(err =>
					{
						reject(err)
						logger.log_error(err)
						return
					})
				}

				else
				{
					resolve(room)
					return
				}
			})

			.catch(err =>
			{
				reject(err)
				logger.log_error(err)
				return
			})
		})
	}

	manager.create_room = function(data)
	{
		return new Promise((resolve, reject) => 
		{
			room = 
			{
				version: rooms_version,
				image_source: '',
				image_setter: '',
				image_size: 0,
				image_date: 0,
				image_type: "upload",
				stored_images: [],
				topic: '',
				topic_setter: '',
				topic_date: 0,
				keys: {},
				radio_type: 'radio',
				radio_source: '',
				radio_title: '',
				radio_setter: '',
				radio_date: 0,
				tv_type: '',
				tv_source: '',
				tv_title: '',
				tv_setter: '',
				tv_date: 0,
				log_messages: [],
				bans: [],
				images_enabled: true,
				radio_enabled: true,
				tv_enabled: true,
				theme: "rgb(56, 46, 95)",
				background_image: "",
				background_image_enabled: true,
				background_mode: "normal",
				background_tile_dimensions: "100px auto",
				log: true,
				voice1_chat_permission: true,
				voice1_images_permission: true,
				voice1_tv_permission: true,
				voice1_radio_permission: true,
				voice2_chat_permission: true,
				voice2_images_permission: true,
				voice2_tv_permission: true,
				voice2_radio_permission: true,
				voice3_chat_permission: true,
				voice3_images_permission: true,
{'fraction_non_alphanumeric': 0.12943905775980014, 'fraction_numerical': 0.009517577776455892, 'mean_word_length': 1.8538448480733323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '1246850', 'n_tokens_mistral': 8835, 'n_tokens_neox': 5469, 'n_words': 833}