• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/lib/sync/googleapipush.php
  • Класс: BitrixCalendarSyncGoogleApiPush
  • Вызов: GoogleApiPush::checkPushSectionChannel
static function checkPushSectionChannel(array $connectionIds, array $connections): void
{
	$existedSectionChannels = [];
	$sections = [];
	$sectionIds = [];

	$sectionsDb = InternalsSectionTable::getList(
		[
			'filter' => [
				'CAL_DAV_CON' => $connectionIds,
			],
			'order' => [
				'ID' => 'ASC',
			],
		]
	);

	while ($section = $sectionsDb->fetch())
	{
		$sections[$section['ID']] = $section;
		$sectionIds[] = $section['ID'];
	}

	if (!empty($sectionIds))
	{
		$pushSectionChannelsDb = PushTable::getList(
			[
				'filter' => [
					'=ENTITY_TYPE' => self::TYPE_SECTION,
					'=ENTITY_ID' => $sectionIds
				]
			]
		);

		while ($row = $pushSectionChannelsDb->fetch())
		{
			$channelInfo = null;
			if (!empty($sections[$row['ENTITY_ID']]))
			{
				$section = $sections[$row['ENTITY_ID']];

				if (self::isVirtualCalendar($section['GAPI_CALENDAR_ID'], $section['EXTERNAL_TYPE']))
				{
					continue;
				}

				if (!self::isConnectionError($connections[$section['CAL_DAV_CON']]['LAST_RESULT']))
				{
					$existedSectionChannels[] = $row['ENTITY_ID'];
					continue;
				}

				$googleApiConnection = new GoogleApiSync($section['OWNER_ID'], $section['CAL_DAV_CON']);
				if ($googleApiConnection->stopChannel($row['CHANNEL_ID'], $row['RESOURCE_ID']))
				{
					self::deletePushChannel($row);
					$channelInfo = $googleApiConnection->startWatchEventsChannel($section['GAPI_CALENDAR_ID']);
				}
				if (is_string($googleApiConnection->getTransportConnectionError()))
				{
					self::deletePushChannel($row);
				}

				if ($channelInfo && isset($channelInfo['id'], $channelInfo['resourceId']))
				{
					$existedSectionChannels[] = $row['ENTITY_ID'];
					$googleApiConnection->updateSuccessLastResultConnection();
					PushTable::add([
						'ENTITY_TYPE' => $row['ENTITY_TYPE'],
						'ENTITY_ID' => $row['ENTITY_ID'],
						'CHANNEL_ID' => $channelInfo['id'],
						'RESOURCE_ID' => $channelInfo['resourceId'],
						'EXPIRES' => $channelInfo['expiration'],
						'NOT_PROCESSED' => 'N'
					]);
				}
			}
		}

		//create new channel for sections
		$missedChannelSections = array_diff($sectionIds, $existedSectionChannels);
		if (!empty($missedChannelSections))
		{
			foreach ($missedChannelSections as $missedSection)
			{
				$channelInfo = null;
				$connectionData = $connections[$sections[$missedSection]['CAL_DAV_CON']];
				$section = $sections[$missedSection];
				if (
					self::isAuthError($connectionData['LAST_RESULT'])
					|| self::isVirtualCalendar($section['GAPI_CALENDAR_ID'], $section['EXTERNAL_TYPE'])
				)
				{
					continue;
				}

				$googleApiConnection = new GoogleApiSync($connectionData['ENTITY_ID'], $connectionData['ID']);
				$channelInfo = $googleApiConnection->startWatchEventsChannel($section['GAPI_CALENDAR_ID']);
				if ($channelInfo && isset($channelInfo['id'], $channelInfo['resourceId']))
				{
					$googleApiConnection->updateSuccessLastResultConnection();
					$row = [
						'ENTITY_TYPE' => self::TYPE_SECTION,
						'ENTITY_ID' => $missedSection,
						'CHANNEL_ID' => $channelInfo['id'],
						'RESOURCE_ID' => $channelInfo['resourceId'],
						'EXPIRES' => $channelInfo['expiration'],
						'NOT_PROCESSED' => 'N'
					];
					self::deletePushChannel($row);
					PushTable::add($row);
				}
				else
				{
					$error = $googleApiConnection->getTransportConnectionError();
					if (is_string($error))
					{
						$googleApiConnection->updateLastResultConnection($error);
					}
				}
			}
		}
	}
}