18 private bool $withLegacy;
23 $this->message = $message;
24 $message->setContext($this->context);
25 $this->withLegacy = $withLegacy;
33 ->setMessageId($this->message->getMessageId())
34 ->setChatId($this->message->getChatId())
35 ->setUserId($this->getContext()->getUserId())
36 ->setContext($this->getContext())
37 ->setReaction($reaction)
44 $saveResult = $reactionItem->save();
45 if (!$saveResult->isSuccess())
47 return $result->addErrors($saveResult->getErrors());
55 if (!$byEvent && $this->isMessageLiveChat())
57 $this->processAddForLiveChat($reaction);
60 if ($this->withLegacy)
65 $this->sendNotification($reactionItem);
77 if ($reactionItem ===
null)
82 $deleteResult = $reactionItem->delete();
84 if (!$deleteResult->isSuccess())
86 return $result->addErrors($deleteResult->getErrors());
89 if (!$byEvent && $this->isMessageLiveChat())
91 $this->processDeleteForLiveChat($reaction);
94 if ($this->withLegacy)
96 $this->deleteLegacy();
104 private function sendNotification(
ReactionItem $reaction): void
106 $authorId = $this->message->getAuthorId();
109 || $authorId === $this->getContext()->getUserId()
118 'MESSAGE_TYPE' => IM_MESSAGE_SYSTEM,
119 'TO_USER_ID' => $this->message->getAuthorId(),
120 'FROM_USER_ID' => $this->getContext()->getUserId(),
121 'NOTIFY_TYPE' => IM_NOTIFY_FROM,
122 'NOTIFY_MODULE' =>
'im',
123 'NOTIFY_EVENT' =>
'like',
124 'NOTIFY_TAG' => $this->getNotifyTag($reaction),
125 'NOTIFY_MESSAGE' => $this->getTextNotification($reaction),
127 \CIMNotify::Add($arMessageFields);
130 private function getNotifyTag(ReactionItem $reaction): string
136 $id = $this->getContext()->getUserId();
141 $id = $chat->getChatId();
144 return "RATING|IM|{$type}|{$id}|{$reaction->getMessageId()}|{$reaction->getReaction()}";
147 private function getTextNotification(ReactionItem $reaction): callable
149 $genderModifier =
"_{$this->getContext()->getUser()->getGender()}";
151 $code =
"IM_MESSAGE_REACTION{$genderModifier}";
153 if ($chat instanceof Chat\PrivateChat)
158 '#DIALOG_ID#' => $chat->getDialogContextId(),
159 '#MESSAGE_ID#' => $this->message->getMessageId(),
160 '#QOUTED_MESSAGE#' => $this->message->getForPush(),
161 '#REACTION_NAME#' => $reaction->getLocName($languageId),
169 '#DIALOG_ID#' => $chat->getDialogContextId(),
170 '#MESSAGE_ID#' => $this->message->getMessageId(),
171 '#QOUTED_MESSAGE#' => $this->message->getForPush(),
172 '#CHAT_ID#' => $this->message->getChatId(),
173 '#CHAT_TITLE#' => $chat->getTitle(),
174 '#REACTION_NAME#' => $reaction->getLocName($languageId),
179 private function processAddForLiveChat(
string $reaction): void
181 $connectorMid = $this->message->getParams()->get(MessageParameter::CONNECTOR_MID)->getValue();
183 foreach ($connectorMid as $messageId)
185 $service =
new static(
new Message((
int)$messageId),
false);
187 $service->addReaction($reaction,
true);
191 private function processDeleteForLiveChat(
string $reaction): void
193 $connectorMid = $this->message->getParams()->get(MessageParameter::CONNECTOR_MID)->getValue();
195 foreach ($connectorMid as $messageId)
197 $service =
new static(
new Message((
int)$messageId),
false);
199 $service->deleteReaction($reaction,
true);
203 private function isMessageLiveChat(): bool
205 $chat = $this->message->getChat();
206 $isLiveChat = $chat->getEntityType() ===
'LIVECHAT';
207 $isToLiveChat =
false;
208 if ($chat->getEntityType() ===
'LINES')
210 [$connectorType] = explode(
'|', $chat->getEntityId());
211 $isToLiveChat = $connectorType ===
'livechat';
214 return $isLiveChat || $isToLiveChat;
217 private function hasAnyReaction(): bool
219 $result = ReactionTable::query()
220 ->setSelect([
'MESSAGE_ID'])
221 ->where(
'MESSAGE_ID', $this->message->getMessageId())
222 ->where(
'USER_ID', $this->
getContext()->getUserId())
227 return $result !==
false;
232 ReactionTable::deleteByFilter([
'=MESSAGE_ID' => $this->message->getMessageId(),
'=USER_ID' => $this->getContext()->getUserId()]);
235 private function addLegacy(): void
237 \CIMMessenger::Like($this->message->getMessageId(),
'plus', $this->getContext()->getUserId(),
false,
false);
240 private function deleteLegacy(): void
242 if (!$this->hasAnyReaction())
244 \CIMMessenger::Like($this->message->getMessageId(),
'minus', $this->getContext()->getUserId(),
false,
false);
static getMessage($code, $replace=null, $language=null)