• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/rest.php
  • Класс: CSocNetLogRestService
  • Вызов: CSocNetLogRestService::getUserBlogComment
static function getUserBlogComment($arParams, $offset, CRestServer $server): array
{
	$arParams = array_change_key_case($arParams, CASE_UPPER);

	$result = Array(
		'COMMENTS' => array(),
		'FILES' => array(),
	);

	if (!Loader::includeModule("blog"))
	{
		return $result;
	}

	$userId = (int)(
		isset($arParams["USER_ID"])
		&& (int)$arParams['USER_ID'] > 0
		&& self::isAdmin()
			? $arParams["USER_ID"]
			: self::getCurrentUserId()
	);

	$otherUserMode = ($userId !== self::getCurrentUserId());

	if ($userId <= 0)
	{
		throw new BitrixRestRestException("User ID can't be empty", "ID_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
	}

	if (isset($arParams['FIRST_ID']))
	{
		$options['FIRST_ID'] = (int)$arParams['FIRST_ID'];
	}
	else
	{
		$options['LAST_ID'] = (
			isset($arParams['LAST_ID']) && (int)$arParams['LAST_ID'] > 0
				? (int)$arParams['LAST_ID']
				: 0
		);
	}

	$options['LIMIT'] = (
		isset($arParams['LIMIT'])
			? (
				(int)$arParams['LIMIT'] > 1000
					? 1000
					: (int)$arParams['LIMIT'])
			: 100
	);

	$filter = [
		'=USER_ID' => $userId,
		'@EVENT_ID' => self::getBlogCommentEventId(),
	];

	if (isset($options['FIRST_ID']))
	{
		$order = array();

		if ((int)$options['FIRST_ID'] > 0)
		{
			$filter['>ID'] = $options['FIRST_ID'];
		}
	}
	else
	{
		$order = Array('ID' => 'DESC');

		if (isset($options['LAST_ID']) && (int)$options['LAST_ID'] > 0)
		{
			$filter[' $filter,
		'select' => array(
			'ID', 'SOURCE_ID'
		),
		'order' => $order,
		'limit' => $options['LIMIT']
	));

	$commentIdList = [];
	while ($logCommentFields = $res->fetch())
	{
		if ((int)$logCommentFields['SOURCE_ID'] > 0)
		{
			$commentIdList[] = $logCommentFields['SOURCE_ID'];
			$logCommentIdList[$logCommentFields['SOURCE_ID']] = $logCommentFields['ID'];
		}
	}

	$commentIdList = array_unique($commentIdList);
	if (empty($commentIdList))
	{
		return $result;
	}

	$res = BitrixBlogCommentTable::getList(array(
		'filter' => array(
			'@ID' => $commentIdList
		),
		'select' => array(
			'ID', 'AUTHOR_ID', 'POST_ID', 'DATE_CREATE', 'POST_TEXT', 'SHARE_DEST', 'UF_BLOG_COMMENT_FILE'
		),
		'order' => array('ID' => 'DESC')
	));

	$attachedIdList = array();
	$commentAttachedList = array();

	$loadedSocialnetwork = Loader::includeModule('socialnetwork');

	while ($commentFields = $res->fetch())
	{
		$result['COMMENTS'][$commentFields['ID']] = array(
			'ID' => (int)$logCommentIdList[$commentFields['ID']],
			'COMMENT_ID' => (int)$commentFields['ID'],
			'POST_ID' => (int)$commentFields['POST_ID'],
			'DATE' => $commentFields['DATE_CREATE'],
			'TEXT' => ($otherUserMode ? '' : (string)$commentFields['POST_TEXT']),
			'ATTACH' => array()
		);

		if (
			$loadedSocialnetwork
			&& ($commentAuxProvider = BitrixSocialnetworkCommentAuxBase::findProvider(
				$commentFields,
				array(
					"mobile" => false,
					"bPublicPage" => true,
					"cache" => true
				)
			)
		))
		{
			$result['COMMENTS'][$commentFields['ID']]['TEXT'] = $commentAuxProvider->getText();
		}

		if (!empty($commentFields['UF_BLOG_COMMENT_FILE']))
		{
			if (is_array($commentFields['UF_BLOG_COMMENT_FILE']))
			{
				$attached = $commentFields['UF_BLOG_COMMENT_FILE'];
			}
			elseif ((int)$commentFields['UF_BLOG_COMMENT_FILE'] > 0)
			{
				$attached = [ (int)$commentFields['UF_BLOG_COMMENT_FILE'] ];
			}
			else
			{
				$attached = [];
			}

			if (!empty($attached))
			{
				$attachedIdList = array_merge($attachedIdList, $attached);
			}

			$commentAttachedList[$commentFields['ID']] = $attached;
		}
	}

	$attachedObjectList = array();

	if (
		!empty($attachedIdList)
		&& Loader::includeModule('disk')
	)
	{
		$res = BitrixDiskAttachedObject::getList(array(
			'filter' => array(
				'@ID' => array_unique($attachedIdList)
			),
			'select' => array('ID', 'OBJECT_ID')
		));
		while ($attachedObjectFields = $res->fetch())
		{
			$diskObjectId = $attachedObjectFields['OBJECT_ID'];
			if ($fileData = self::getFileData($diskObjectId))
			{
				$attachedObjectList[$attachedObjectFields['ID']] = $diskObjectId;
				$result['FILES'][$diskObjectId] = $fileData;
			}
		}
	}

	foreach ($result['COMMENTS'] as $key => $value)
	{
		if ($value['DATE'] instanceof BitrixMainTypeDateTime)
		{
			$result['COMMENTS'][$key]['DATE'] = date('c', $value['DATE']->getTimestamp());
		}

		if (!empty($commentAttachedList[$key]))
		{
			foreach ($commentAttachedList[$key] as $attachedId)
			{
				if (!empty($attachedObjectList[$attachedId]))
				{
					$result['COMMENTS'][$key]['ATTACH'][] = $attachedObjectList[$attachedId];
				}
			}
		}

		$result['COMMENTS'][$key] = array_change_key_case($result['COMMENTS'][$key], CASE_LOWER);
	}

	$result['COMMENTS'] = array_values($result['COMMENTS']);
	$result['FILES'] = self::convertFileData($result['FILES']);

	return $result;
}