static function Like($id, $action = 'auto', $userId = null, $byEvent = false, $withReaction = true)
{
if (!CModule::IncludeModule('pull'))
return false;
global $USER;
$userId = is_null($userId)? $USER->GetId(): intval($userId);
if ($userId <= 0)
return false;
$action = in_array($action, Array('plus', 'minus'))? $action: 'auto';
$message = self::GetById($id);
if (!$message)
return false;
$relations = self::GetRelationById($id);
$result = BitrixImModelChatTable::getList(Array(
'filter'=>Array(
'=ID' => $message['CHAT_ID']
)
));
$chat = $result->fetch();
if ($chat['ENTITY_TYPE'] != 'LIVECHAT')
{
if (!isset($relations[$userId]))
return false;
}
if (!$byEvent && $chat['ENTITY_TYPE'] == 'LINES')
{
[$connectorType, $lineId, $chatId] = explode("|", $chat['ENTITY_ID']);
if ($connectorType == "livechat")
{
foreach($message['PARAMS']['CONNECTOR_MID'] as $mid)
{
self::Like($mid, $action, $userId, true, false);
}
}
}
else if (!$byEvent && $chat['ENTITY_TYPE'] == 'LIVECHAT')
{
foreach($message['PARAMS']['CONNECTOR_MID'] as $mid)
{
self::Like($mid, $action, $userId, true, false);
}
}
$isLike = false;
if (isset($message['PARAMS']['LIKE']))
{
$isLike = in_array($userId, $message['PARAMS']['LIKE']);
}
if ($isLike && $action == 'plus')
{
return false;
}
else if (!$isLike && $action == 'minus')
{
return false;
}
$isLike = true;
if (isset($message['PARAMS']['LIKE']))
{
$like = $message['PARAMS']['LIKE'];
$selfLike = array_search($userId, $like);
if ($selfLike !== false)
{
$isLike = false;
unset($like[$selfLike]);
}
else
{
$like[] = $userId;
}
}
else
{
$like = Array($userId);
}
sort($like);
CIMMessageParam::Set($id, Array('LIKE' => $like));
if ($withReaction)
{
$messageObject = new BitrixImV2Message([
'ID' => $message['ID'],
'CHAT_ID' => $message['CHAT_ID'],
'AUTHOR_ID' => $message['AUTHOR_ID'],
'MESSAGE' => $message['MESSAGE'],
]);
$reactionService = new BitrixImV2MessageReactionReactionService($messageObject, false);
if ($isLike)
{
$reactionService->addReaction(BitrixImV2MessageReactionReactionItem::LIKE);
}
else
{
$reactionService->deleteReaction(BitrixImV2MessageReactionReactionItem::LIKE);
}
}
if ($message['AUTHOR_ID'] > 0 && $message['AUTHOR_ID'] != $userId && $isLike && $chat['ENTITY_TYPE'] != 'LIVECHAT')
{
$message['MESSAGE'] = self::PrepareParamsForPush($message);
$isChat = $chat && $chat['TITLE'] <> '';
$dot = mb_strlen($message['MESSAGE']) >= 200? '...': '';
$message['MESSAGE'] = mb_substr($message['MESSAGE'], 0, 199).$dot;
$message['MESSAGE'] = $message['MESSAGE'] <> ''? $message['MESSAGE']: '-';
/*$arMessageFields = array(
"MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
"TO_USER_ID" => $message['AUTHOR_ID'],
"FROM_USER_ID" => $userId,
"NOTIFY_TYPE" => IM_NOTIFY_FROM,
"NOTIFY_MODULE" => "im",
"NOTIFY_EVENT" => "like",
"NOTIFY_TAG" => "RATING|IM|".($isChat? 'G':'P')."|".($isChat? $chat['ID']: $userId)."|".$id,
"NOTIFY_MESSAGE" => GetMessage($isChat? 'IM_MESSAGE_LIKE': 'IM_MESSAGE_LIKE_PRIVATE', Array(
'#MESSAGE#' => $message['MESSAGE'],
'#TITLE#' => $isChat? '[CHAT='.$chat['ID'].']'.$chat['TITLE'].'[/CHAT]': $chat['TITLE']
)),
"NOTIFY_MESSAGE_OUT" => GetMessage($isChat? 'IM_MESSAGE_LIKE': 'IM_MESSAGE_LIKE_PRIVATE', Array(
'#MESSAGE#' => $message['MESSAGE'],
'#TITLE#' => $chat['TITLE']
)),
);
CIMNotify::Add($arMessageFields);*/
}
$pushUsers = $like;
$pushUsers[] = $message['AUTHOR_ID'];
$arPullMessage = Array(
'id' => (int)$id,
'dialogId' => 0,
'chatId' => (int)$chat['ID'],
'senderId' => (int)$userId,
'set' => (bool)$isLike,
'users' => $like
);
if ($chat['TYPE'] == IM_MESSAGE_PRIVATE)
{
$fromUserId = (int)$userId;
foreach ($relations as $rel)
{
if ($rel['USER_ID'] != $fromUserId)
{
$toUserId = (int)$rel['USER_ID'];
}
}
$dialogId = $toUserId;
BitrixPullEvent::add($fromUserId, Array(
'module_id' => 'im',
'command' => 'messageLike',
'params' => array_merge($arPullMessage, ['dialogId' => $toUserId]),
'extra' => BitrixImCommon::getPullExtra()
));
BitrixPullEvent::add($toUserId, Array(
'module_id' => 'im',
'command' => 'messageLike',
'params' => array_merge($arPullMessage, ['dialogId' => $fromUserId]),
'extra' => BitrixImCommon::getPullExtra()
));
}
else
{
$dialogId = 'chat'.$chat['ID'];
$arPullMessage['dialogId'] = $dialogId;
if ($chat['ENTITY_TYPE'] == 'LINES')
{
foreach ($relations as $rel)
{
if ($rel["EXTERNAL_AUTH_ID"] == 'imconnector')
{
unset($relations[$rel["USER_ID"]]);
}
}
}
BitrixPullEvent::add(array_keys($relations), Array(
'module_id' => 'im',
'command' => 'messageLike',
'params' => $arPullMessage,
'extra' => BitrixImCommon::getPullExtra()
));
if ($chat['TYPE'] == IM_MESSAGE_OPEN || $chat['TYPE'] == IM_MESSAGE_OPEN_LINE)
{
CPullWatch::AddToStack('IM_PUBLIC_'.$chat['ID'], Array(
'module_id' => 'im',
'command' => 'messageLike',
'params' => $arPullMessage,
'extra' => BitrixImCommon::getPullExtra()
));
}
}
foreach(GetModuleEvents("im", "OnAfterMessagesLike", true) as $arEvent)
{
ExecuteModuleEventEx($arEvent, array(array(
'DIALOG_ID' => $dialogId,
'CHAT' => $chat,
'MESSAGE' => $message,
'ACTION' => $action,
'USER_ID' => $userId,
'BY_EVENT' => $byEvent
)));
}
return $like;
}