30 $this->message = $message;
39 $authorId = $this->message->getAuthorId();
49 ->setChatId($this->message->getChatId())
50 ->setMessageId($this->message->getMessageId())
52 ->setFromUserId($authorId)
56 $anchorCollection->add($anchorItem);
59 return $this->saveAnchors($anchorCollection);
64 $fromUserId ??= $this->getContext()->getUserId();
65 if ($this->message->getAuthorId() === $fromUserId)
70 $authorId = $this->message->getAuthorId();
77 ->setChatId($this->message->getChatId())
78 ->setMessageId($this->message->getMessageId())
79 ->setUserId($authorId)
80 ->setFromUserId($fromUserId)
82 ->setSubType($reaction)
85 return $this->saveAnchor($anchorItem);
90 $fromUserId ??= $this->getContext()->getUserId();
91 if ($this->message->getAuthorId() === $fromUserId)
96 return $this->deleteAnchors(
98 fromUserId: $fromUserId,
99 messageId: $this->message->getId()
105 $fromUserId ??= $this->getContext()->getUserId();
107 return $this->deleteAnchors(
109 fromUserId: $fromUserId,
110 messageId: $this->message->getId()
121 return $this->deleteAnchors(
123 fromUserId: $this->getContext()->getUserId(),
125 messageId: $this->message->getId(),
129 private function deleteAnchors(
130 ?
string $type =
null,
131 ?
int $fromUserId =
null,
132 ?
array $userIds =
null,
141 'FROM_USER_ID' => $fromUserId,
142 'USER_ID' => $userIds,
144 'CHAT_ID' => $chatId,
150 if ($anchorCollection->isEmpty())
155 $deleteResult = $anchorCollection->delete();
156 if (!$deleteResult->isSuccess())
158 return $result->addErrors($deleteResult->getErrors());
161 catch (SystemException $exception)
163 $this->exceptionHandler->writeToLog($exception);
168 $this->anchorProvider->cleanUsersCache((
array)$anchorCollection->getUserIdList());
170 $this->pushService->deleteMulti($anchorCollection);
175 private function saveAnchors(AnchorCollection $anchorCollection): Result
179 if ($anchorCollection->isEmpty())
184 if (!$anchorCollection->getCommonMessageId())
191 $saveResult = $anchorCollection->save(
true);
192 if (!$saveResult->isSuccess())
194 return $result->addErrors($saveResult->getErrors());
197 catch (SystemException $exception)
199 $this->exceptionHandler->writeToLog($exception);
204 $chat = $this->message->getChat();
205 $anchorCollection->setParentChatId($chat->getParentChatId());
206 $anchorCollection->setParentMessageId($chat->getParentMessageId());
208 $this->anchorProvider->cleanUsersCache((
array)$anchorCollection->getUserIdList());
210 $this->pushService->addMulti($anchorCollection);
215 private function saveAnchor(AnchorItem $anchorItem): Result
221 $saveResult = $anchorItem->save();
222 if (!$saveResult->isSuccess())
224 return $result->addErrors($saveResult->getErrors());
227 catch (DuplicateEntryException)
231 catch (SystemException $exception)
233 $this->exceptionHandler->writeToLog($exception);
238 $chat = $this->message->getChat();
239 $anchorItem->setParentChatId($chat->getParentChatId());
240 $anchorItem->setParentMessageId($chat->getParentMessageId());
242 $this->anchorProvider->cleanCache($anchorItem->getUserId());
244 $this->pushService->add($anchorItem);
249 private function init(): void
252 $this->pushService = AnchorContainer::getInstance()->getPushService();
253 $this->anchorProvider = AnchorContainer::getInstance()->getAnchorProvider();