17 private readonly
int $voteId,
23 if ($this->voteId <= 0 || $this->messageId <= 0)
25 return (
new Result())->addError(
new Error(
'Incorrect input params'));
28 if (!$this->obtainLock())
30 return (
new Result())->addError(
new Error(
'Another process already handling stop event'));
33 if ($this->isVoteStopEventsAlreadyHandled())
35 return (
new Result())->addError(
new Error(
'Event already handled'));
38 $this->setStopEventHandled(
true);
44 $this->setStopEventHandled(
false);
50 private function releaseLock(): void
52 Application::getConnection()->unlock($this->getLockName());
55 private function obtainLock(): bool
57 return Application::getConnection()->lock($this->getLockName());
60 private function getLockName(): string
62 return "vote_stop_{$this->voteId}";
65 private function isVoteStopEventsAlreadyHandled(): bool
67 $row = VoteTable::query()
68 ->where(
'ID', $this->voteId)
69 ->where(
'STOP_EVENT_HANDLED', 1)
79 private function setStopEventHandled(
bool $handled): Result
81 return VoteTable::update($this->voteId, [
'STOP_EVENT_HANDLED' => (
int)$handled]);
84 private function sendMessage(): Result
88 return (
new Result())->addError(
new Error(
'No module im'));
91 $message =
new Message($this->messageId);
93 if ($chat->getChatId() <= 0)
95 return (
new Result())->addError(
new Error(
'Chat not found'));
100 'TO_CHAT_ID' => $chat->getChatId(),
101 'FROM_USER_ID' =>
$message->getAuthorId(),
102 'MESSAGE' => $this->getMessageText(
$message->getAuthorId(), $chat),
108 return (
new Result())->addError(
new Error(
'Message not sended'));
114 private function getMessageText(
int $userId, Chat $chat): ?string
119 '#DIALOG_ID#' => $chat->getDialogContextId(),
121 $user = UserTable::query()
122 ->setSelect([
'ID',
'PERSONAL_GENDER'])
127 return $user?->getPersonalGender() ===
'F'
128 ? Loc::getMessage(
'VOTE_INTEGRATION_IM_EVENT_HANDLER_MESSAGE_STOP_F', $replaces)
129 : Loc::getMessage(
'VOTE_INTEGRATION_IM_EVENT_HANDLER_MESSAGE_STOP', $replaces)
static includeModule($moduleName)