<commit_before><?php

namespace MyApp\OnMessageFilters;

use MyApp\Chain\ChainContainer;
use MyApp\Chain\ChainInterface;
use MyApp\ChatConfig;
use MyApp\Clients\UserCollection;
use MyApp\MightyLoop;
use MyApp\Response\ErrorResponse;
use MyApp\TSingleton;
use MyApp\Utils\Lang;

class FloodFilter implements ChainInterface
{
	use TSingleton;
	private $userTimers;

	public function handleRequest(ChainContainer $chain)
	{
		$user = $chain->getFrom();
		$loop = MightyLoop::get()->fetch();

		$prevRequest = isset($this->userTimers[$user->getId()]) ? $this->userTimers[$user->getId()] : false;

		if ($prevRequest) {
			$isMsgCase = isset($prevRequest['request']['msg']) && isset($chain->getRequest()['msg']);
			if ($prevRequest['request'] == $chain->getRequest() || $isMsgCase) {
				$response = (new ErrorResponse())
					->setErrors(['flood' => Lang::get()->getPhrase('FloodDetected')])
					->setChatId($user->getChatId());
				(new UserCollection())
					->setResponse($response)
					->attach($user)
					->notify(false);

				return false;
			}

		}

		$timerCallback = function() use ($user) {
			unset($this->userTimers[$user->getId()]);
		};

		$timer = $loop->addTimer(ChatConfig::get()->getConfig()->floodTimeout, $timerCallback);

		$this->userTimers[$user->getId()] = [
			'timer' => $timer,
			'request' => $chain->getRequest()
		];
	}
}<commit_msg>floodfilter fix<commit_after><?php

namespace MyApp\OnMessageFilters;

use MyApp\Chain\ChainContainer;
use MyApp\Chain\ChainInterface;
use MyApp\ChatConfig;
use MyApp\Clients\UserCollection;
use MyApp\MightyLoop;
use MyApp\Response\ErrorResponse;
use MyApp\TSingleton;
use MyApp\Utils\Lang;

class FloodFilter implements ChainInterface
{
	use TSingleton;
	private $userTimers;

	public function handleRequest(ChainContainer $chain)
	{
		$user = $chain->getFrom();
		$loop = MightyLoop::get()->fetch();

		$prevRequest = isset($this->userTimers[$user->getId()]) ? $this->userTimers[$user->getId()] : false;

		if ($prevRequest) {
			$isPing = isset($chain->getRequest()['subject']) && $chain->getRequest()['subject'] == 'Ping';
			if (!$isPing) {
				$response = (new ErrorResponse())
					->setErrors(['flood' => Lang::get()->getPhrase('FloodDetected')])
					->setChatId($user->getChatId());
				(new UserCollection())
					->setResponse($response)
					->attach($user)
					->notify(false);

				return false;
			}

		}

		$timerCallback = function() use ($user) {
			unset($this->userTimers[$user->getId()]);
		};

		$timer = $loop->addTimer(ChatConfig::get()->getConfig()->floodTimeout, $timerCallback);

		$this->userTimers[$user->getId()] = $timer;
	}
}
{'fraction_non_alphanumeric': 0.19031273836765827, 'fraction_numerical': 0.0, 'mean_word_length': 4.9343891402714934, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '7578091', 'n_tokens_mistral': 985, 'n_tokens_neox': 879, 'n_words': 145}