19 private bool $withLegacy;
24 $this->message = $message;
25 $message->setContext($this->context);
26 $this->withLegacy = $withLegacy;
34 ->setMessageId($this->message->getMessageId())
35 ->setChatId($this->message->getChatId())
36 ->setUserId($this->getContext()->getUserId())
37 ->setContext($this->getContext())
38 ->setReaction($reaction)
45 $saveResult = $reactionItem->save();
46 if (!$saveResult->isSuccess())
48 return $result->addErrors($saveResult->getErrors());
56 if (!$byEvent && $this->isMessageLiveChat())
58 $this->processAddForLiveChat($reaction);
61 if ($this->withLegacy)
70 $this->addAnchors($reaction);
80 if ($reactionItem ===
null)
85 $deleteResult = $reactionItem->delete();
87 if (!$deleteResult->isSuccess())
89 return $result->addErrors($deleteResult->getErrors());
92 if (!$byEvent && $this->isMessageLiveChat())
94 $this->processDeleteForLiveChat($reaction);
97 if ($this->withLegacy)
99 $this->deleteLegacy();
104 $this->deleteAnchor();
109 private function processAddForLiveChat(
string $reaction): void
111 $connectorMid = $this->message->getParams()->get(MessageParameter::CONNECTOR_MID)->getValue();
116 $service->setContext($this->getContext());
117 $service->addReaction($reaction,
true);
121 private function processDeleteForLiveChat(
string $reaction): void
123 $connectorMid = $this->message->getParams()->get(MessageParameter::CONNECTOR_MID)->getValue();
128 $service->setContext($this->getContext());
129 $service->deleteReaction($reaction,
true);
133 private function isMessageLiveChat(): bool
135 $chat = $this->message->getChat();
136 $isLiveChat = $chat->getEntityType() ===
'LIVECHAT';
137 $isToLiveChat =
false;
138 if ($chat->getEntityType() ===
'LINES')
140 [$connectorType] = explode(
'|', $chat->getEntityId());
141 $isToLiveChat = $connectorType ===
'livechat';
144 return $isLiveChat || $isToLiveChat;
147 private function hasAnyReaction(): bool
149 $result = ReactionTable::query()
150 ->setSelect([
'MESSAGE_ID'])
151 ->where(
'MESSAGE_ID', $this->message->getMessageId())
152 ->where(
'USER_ID', $this->
getContext()->getUserId())
162 ReactionTable::deleteByFilter([
'=MESSAGE_ID' => $this->message->getMessageId(),
'=USER_ID' => $this->getContext()->getUserId()]);
165 private function addLegacy(): void
167 \CIMMessenger::Like($this->message->getMessageId(),
'plus', $this->getContext()->getUserId(),
false,
false);
170 private function deleteLegacy(): void
172 if (!$this->hasAnyReaction())
174 \CIMMessenger::Like($this->message->getMessageId(),
'minus', $this->getContext()->getUserId(),
false,
false);
178 private function addAnchors(
string $reaction): void
185 $anchorService = AnchorContainer::getInstance()
186 ->getAnchorService($this->message)
187 ->setContext($this->getContext());
189 $anchorService->addReactionAnchor($reaction);
192 private function deleteAnchor(): void
194 $anchorService = AnchorContainer::getInstance()
195 ->getAnchorService($this->message)
196 ->setContext($this->getContext());
198 $anchorService->deleteReactionAnchors();