<commit_before><?php

	require_once __DIR__ . '/avatar.php';
	require_once __DIR__ . '/bbcode.php';

	require_once __DIR__ . '/../config/chat.php';
	require_once __DIR__ . '/../config/bbcode.php';
	require_once __DIR__ . '/../config/user.php';
	require_once __DIR__ . '/../config/misc.php';


	function getNumChatMessages()
	{
		global $database;

		return $database->count('chat_messages', [
			'deleted' => 0
		]);
	}


	function getRecentChatMessages($lastId = null, $returnRefreshTime = false)
	{
		global $database;

		$where = [
			'AND'   => [
				'deleted' => 0
			],
			'ORDER' => 'chat_messages.id DESC',
			'LIMIT' => INIT_CHAT_MESSAGES,
		];
		if ($lastId !== null)
		{
			$where['AND']['chat_messages.id[>]'] = $lastId;
		}

		$messages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id',
			'chat_messages.author(author_id)',
			'users.name(author_name)',
			'chat_messages.post_time',
			'chat_messages.content',
		], $where);

		$messages = array_reverse($messages);

		$messages = processMessages($messages);

		if ($returnRefreshTime)
		{
			return [
				'refreshTime'    => date(DEFAULT_DATE_FORMAT),
				'unreadMessages' => $messages
			];
		}

		return $messages;
	}


	function getDeletedMessages($firstId, $lastId)
	{
		global $database;

		$deletedMessages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id'
		], [
			'AND' => [
				'deleted'              => 1,
				'chat_messages.id[<>]' => [$firstId, $lastId]
			]
		]);

		return $deletedMessages;
	}


	function getLatestChatMessage()
	{
		global $database;

		$messages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id',
			'chat_messages.author(author_id)',
			'users.name(author_name)',
			'chat_messages.post_time',
			'chat_messages.content',
		], [
			'deleted' => 0,
			'ORDER'   => 'chat_messages.id DESC',
			'LIMIT'   => 1
		]);

		processMessages($messages);

		return $messages[0];
	}


	function processMessages($messages)
	{
		foreach ($messages as $key => $message)
		{
			$messages[$key]['content'] = linkifyMentions(parseBBCode($message['content']));
			$messages[$key]['avatar_url'] = getAvatarUrlFromMessage($message);
			$messages[$key]['post_time'] = date(DEFAULT_DATE_FORMAT, $message['post_time']);
			$messages[$key]['can_delete'] = isLoggedIn()
				&& (isAdmin() || $message['author_id'] === $_SESSION['userId']);

			if ($messages[$key]['author_id'] * 1 === CHAT_BOT_USER_ID)
			{
				$messages[$key]['author_name'] = CHAT_BOT_USER_NAME;
			}
		}

		return $messages;
	}


	function getChatMessagesForArchive($page)
	{
		global $database;

		$messages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id',
			'chat_messages.author(author_id)',
			'users.name(author_name)',
			'chat_messages.post_time',
			'chat_messages.content',
		], [
			'deleted' => 0,
			'ORDER'   => 'post_time ASC',
			'LIMIT'   => [($page - 1) * INIT_CHAT_MESSAGES, INIT_CHAT_MESSAGES]
		]);

		$messages = processMessages($messages);

		return $messages;
	}


	function createMessage($content)
	{
		global $database;

		if (!isLoggedIn() || isBanned())
		{
			return;
		}

		$postTime = time();
		$content = delimitSmileys(htmlspecialchars($content));

		$database->insert('chat_messages', [
			'id'        => null,
			'author'    => $_SESSION['userId'],
			'post_time' => $postTime,
			'content'   => $content,
			'deleted'   => 0
		]);
	}


	function deleteMessage($id)
	{
		global $database;
<commit_msg>made messages to bot not show up<commit_after><?php

	require_once __DIR__ . '/avatar.php';
	require_once __DIR__ . '/bbcode.php';

	require_once __DIR__ . '/../config/chat.php';
	require_once __DIR__ . '/../config/bbcode.php';
	require_once __DIR__ . '/../config/user.php';
	require_once __DIR__ . '/../config/misc.php';


	function getNumChatMessages()
	{
		global $database;

		return $database->count('chat_messages', [
			'deleted' => 0
		]);
	}


	function getRecentChatMessages($lastId = null, $returnRefreshTime = false)
	{
		global $database;

		$where = [
			'AND'   => [
				'deleted' => 0
			],
			'ORDER' => 'chat_messages.id DESC',
			'LIMIT' => INIT_CHAT_MESSAGES,
		];
		if ($lastId !== null)
		{
			$where['AND']['chat_messages.id[>]'] = $lastId;
		}

		$messages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id',
			'chat_messages.author(author_id)',
			'users.name(author_name)',
			'chat_messages.post_time',
			'chat_messages.content',
		], $where);

		$messages = array_reverse($messages);

		$messages = processMessages($messages);

		if ($returnRefreshTime)
		{
			return [
				'refreshTime'    => date(DEFAULT_DATE_FORMAT),
				'unreadMessages' => $messages
			];
		}

		return $messages;
	}


	function getDeletedMessages($firstId, $lastId)
	{
		global $database;

		$deletedMessages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id'
		], [
			'AND' => [
				'deleted'              => 1,
				'chat_messages.id[<>]' => [$firstId, $lastId]
			]
		]);

		return $deletedMessages;
	}


	function getLatestChatMessage()
	{
		global $database;

		$messages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id',
			'chat_messages.author(author_id)',
			'users.name(author_name)',
			'chat_messages.post_time',
			'chat_messages.content',
		], [
			'deleted' => 0,
			'ORDER'   => 'chat_messages.id DESC',
			'LIMIT'   => 1
		]);

		processMessages($messages);

		return $messages[0];
	}


	function processMessages($messages)
	{
		foreach ($messages as $key => $message)
		{
			$messages[$key]['content'] = linkifyMentions(parseBBCode($message['content']));
			$messages[$key]['avatar_url'] = getAvatarUrlFromMessage($message);
			$messages[$key]['post_time'] = date(DEFAULT_DATE_FORMAT, $message['post_time']);
			$messages[$key]['can_delete'] = isLoggedIn()
				&& (isAdmin() || $message['author_id'] === $_SESSION['userId']);

			if ($messages[$key]['author_id'] * 1 === CHAT_BOT_USER_ID)
			{
				$messages[$key]['author_name'] = CHAT_BOT_USER_NAME;
			}
		}

		return $messages;
	}


	function getChatMessagesForArchive($page)
	{
		global $database;

		$messages = $database->select('chat_messages', [
			'[>]users' => ['author' => 'id']
		], [
			'chat_messages.id',
			'chat_messages.author(author_id)',
			'users.name(author_name)',
			'chat_messages.post_time',
			'chat_messages.content',
		], [
			'deleted' => 0,
			'ORDER'   => 'post_time ASC',
			'LIMIT'   => [($page - 1) * INIT_CHAT_MESSAGES, INIT_CHAT_MESSAGES]
		]);

		$messages = processMessages($messages);

		return $messages;
	}


	function createMessage($content)
	{
		global $database;

		if (!isLoggedIn() || isBanned())
		{
			return;
		}

		$postTime = time();
		$content = delimitSmileys(htmlspecialchars($content));

		$database->insert('chat_messages', [
			'id'        => null,
			'author'    => $_SESSION['userId'],
			'post_time' => $postTime,
			'content'   => $content,
			'deleted'   => (trim($content) === '@Bot history') ? 1 : 0
		]);
	}


	function deleteMessage($id)
	{
		global $database;

{'fraction_non_alphanumeric': 0.19382835101253615, 'fraction_numerical': 0.0028929604628736743, 'mean_word_length': 3.708171206225681, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 87, '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': '773989', 'n_tokens_mistral': 3021, 'n_tokens_neox': 2587, 'n_words': 404}