<commit_before><?php
class ChatSessionManager extends DbAccessor
{
	const TBL_CHAT_SESSIONS = 'chat_sessions';
	
	const CLOSED_STATUS_NO = 0; 
	const CLOSED_STATUS_YES = 1; 
	
	const CLOSED_REASON_CLOSE = 1; 
	const CLOSED_REASON_OFFLINE = 2; 
	const CLOSED_REASON_MONEY = 3;
	const CLOSED_REASON_SYNC_UI = 4;
	
	private $sessionClearTimeout = 10;  // in minutes
	
	public function __construct(Config $config, $dbInstanceKey = null){
		parent::__construct($dbInstanceKey);
		
		if(isset($config->sessionClearTimeout)){
			$this->sessionClearTimeout = $config->sessionClearTimeout;
		}
	}
	
	public static function getInterlocutorsFromSessions($chatSessions){
		$interlocutorIds = array();
		foreach($chatSessions as $chatSession){
			array_push($interlocutorIds, $chatSession->interlocutorUser->userId);
		}
		
		return $interlocutorIds;
	}
	
	public static function fillSessionsWithMessages($chatSessions, $chatMessages){
		foreach($chatMessages as $chatMessage){
			foreach($chatSessions as &$chatSession){
				// Check if this message belongs to this session.
				if(
					(
						$chatMessage->senderUser->userId == $chatSession->interlocutorUser->userId
							and
						$chatMessage->receiverUser->userId == $chatSession->chatterUser->userId
					)
					or
					(
						$chatMessage->senderUser->userId == $chatSession->chatterUser->userId
							and
						$chatMessage->receiverUser->userId == $chatSession->interlocutorUser->userId
					)
				){
					array_push($chatSession->messages, $chatMessage);
				}
			}
		}
		return $chatSessions;
	}
	
	public function getChatSessions(ChatSessionFilter $filter, $myUserId = null){
		$chatSessions = array();
		
		if($filter == null){
			$filter = new ChatSessionFilter();
		}
		
		$sqlQuery = $filter->getSQL();
		
		$sessionRows = $this->query->exec($sqlQuery)->fetchRecords();
										
		foreach ($sessionRows as $sessionRow){
			array_push($chatSessions, $this->getChatSessionObject($sessionRow, $myUserId));
		}
		
		return $chatSessions;
	}
	
	public function getChatSession(ChatSessionFilter $filter, $myUserId = null){
		$sessions = $this->getChatSessions($filter, $myUserId);
		if(count($sessions) !== 1){
			throw new ChatSessionException("There is no such chat session or it is not unique.");
		}
		return $sessions[0];
	}
	
	protected function getChatSessionObject($sessionRow, $myUserId = null){
		if(empty($sessionRow) or !is_array($sessionRow)){<commit_msg>Set deafult filter to null for getChatSession method. New method added: getChatSessionById<commit_after><?php
class ChatSessionManager extends DbAccessor
{
	const TBL_CHAT_SESSIONS = 'chat_sessions';
	
	const CLOSED_STATUS_NO = 0; 
	const CLOSED_STATUS_YES = 1; 
	
	const CLOSED_REASON_CLOSE = 1; 
	const CLOSED_REASON_OFFLINE = 2; 
	const CLOSED_REASON_MONEY = 3;
	const CLOSED_REASON_SYNC_UI = 4;
	
	private $sessionClearTimeout = 10;  // in minutes
	
	public function __construct(Config $config, $dbInstanceKey = null){
		parent::__construct($dbInstanceKey);
		
		if(isset($config->sessionClearTimeout)){
			$this->sessionClearTimeout = $config->sessionClearTimeout;
		}
	}
	
	public static function getInterlocutorsFromSessions($chatSessions){
		$interlocutorIds = array();
		foreach($chatSessions as $chatSession){
			array_push($interlocutorIds, $chatSession->interlocutorUser->userId);
		}
		
		return $interlocutorIds;
	}
	
	public static function fillSessionsWithMessages($chatSessions, $chatMessages){
		foreach($chatMessages as $chatMessage){
			foreach($chatSessions as &$chatSession){
				// Check if this message belongs to this session.
				if(
					(
						$chatMessage->senderUser->userId == $chatSession->interlocutorUser->userId
							and
						$chatMessage->receiverUser->userId == $chatSession->chatterUser->userId
					)
					or
					(
						$chatMessage->senderUser->userId == $chatSession->chatterUser->userId
							and
						$chatMessage->receiverUser->userId == $chatSession->interlocutorUser->userId
					)
				){
					array_push($chatSession->messages, $chatMessage);
				}
			}
		}
		return $chatSessions;
	}
	
	public function getChatSessions(ChatSessionFilter $filter = null, $myUserId = null){
		$chatSessions = array();
		
		if($filter == null){
			$filter = new ChatSessionFilter();
		}
		
		$sqlQuery = $filter->getSQL();
		
		$sessionRows = $this->query->exec($sqlQuery)->fetchRecords();
										
		foreach ($sessionRows as $sessionRow){
			array_push($chatSessions, $this->getChatSessionObject($sessionRow, $myUserId));
		}
		
		return $chatSessions;
	}
	
	public function getChatSession(ChatSessionFilter $filter = null, $myUserId = null){
		$sessions = $this->getChatSessions($filter, $myUserId);
		if(count($sessions) !== 1){
			throw new ChatSessionException("There is no such chat session or it is not unique.");
		}
		return $sessions[0];
	}
	
	public function getChatSessionById($sessionId, $myUserId = null){
		$filter = new ChatSessionFilter();
		$filter->setId($sessionId);
		$session = $this->getChatSession($filter);
		return $session;
	}
	
	protected function getChatSessionObject($sessionRow, $myUserId = null){
		if(empty($sessionRow) or !is_array($sessionRow)){
{'fraction_non_alphanumeric': 0.1234928043562816, 'fraction_numerical': 0.0038895371450797353, 'mean_word_length': 4.425105485232067, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '1689583', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1602, 'n_words': 323}