1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ImVoteStopEventHandler.php
См. документацию.
1<?php
2
3namespace Bitrix\Vote\Integration\Im;
4
5use Bitrix\Im\V2\Chat;
6use Bitrix\Im\V2\Message;
7use Bitrix\Main\Application;
8use Bitrix\Main\Error;
9use Bitrix\Main\Loader;
10use Bitrix\Main\Localization\Loc;
11use Bitrix\Main\Result;
12use Bitrix\Main\UserTable;
13use Bitrix\Vote\VoteTable;
15{
16 public function __construct(
17 private readonly int $voteId,
18 private readonly int $messageId,
19 ) {}
20
21 public function handle(): Result
22 {
23 if ($this->voteId <= 0 || $this->messageId <= 0)
24 {
25 return (new Result())->addError(new Error('Incorrect input params'));
26 }
27
28 if (!$this->obtainLock())
29 {
30 return (new Result())->addError(new Error('Another process already handling stop event'));
31 }
32
33 if ($this->isVoteStopEventsAlreadyHandled())
34 {
35 return (new Result())->addError(new Error('Event already handled'));
36 }
37
38 $this->setStopEventHandled(true);
39 $this->releaseLock();
40
41 $result = $this->sendMessage();
42 if (!$result->isSuccess())
43 {
44 $this->setStopEventHandled(false);
45 }
46
47 return $result;
48 }
49
50 private function releaseLock(): void
51 {
52 Application::getConnection()->unlock($this->getLockName());
53 }
54
55 private function obtainLock(): bool
56 {
57 return Application::getConnection()->lock($this->getLockName());
58 }
59
60 private function getLockName(): string
61 {
62 return "vote_stop_{$this->voteId}";
63 }
64
65 private function isVoteStopEventsAlreadyHandled(): bool
66 {
67 $row = VoteTable::query()
68 ->where('ID', $this->voteId)
69 ->where('STOP_EVENT_HANDLED', 1)
70 ->setSelect(['ID'])
71 ->setLimit(1)
72 ->exec()
73 ->fetch()
74 ;
75
76 return !empty($row);
77 }
78
79 private function setStopEventHandled(bool $handled): Result
80 {
81 return VoteTable::update($this->voteId, ['STOP_EVENT_HANDLED' => (int)$handled]);
82 }
83
84 private function sendMessage(): Result
85 {
86 if (!Loader::includeModule('im'))
87 {
88 return (new Result())->addError(new Error('No module im'));
89 }
90
91 $message = new Message($this->messageId);
92 $chat = $message->getChat();
93 if ($chat->getChatId() <= 0)
94 {
95 return (new Result())->addError(new Error('Chat not found'));
96 }
97
98 $result = \CIMMessenger::Add([
99 'MESSAGE_TYPE' => IM_MESSAGE_CHAT,
100 'TO_CHAT_ID' => $chat->getChatId(),
101 'FROM_USER_ID' => $message->getAuthorId(),
102 'MESSAGE' => $this->getMessageText($message->getAuthorId(), $chat),
103 'SYSTEM' => 'Y',
104 ]);
105
106 if (!$result)
107 {
108 return (new Result())->addError(new Error('Message not sended'));
109 }
110
111 return new Result();
112 }
113
114 private function getMessageText(int $userId, Chat $chat): ?string
115 {
116 $replaces = [
117 '#MESSAGE_ID#' => $this->messageId,
118 '#USER_ID#' => $userId,
119 '#DIALOG_ID#' => $chat->getDialogContextId(),
120 ];
121 $user = UserTable::query()
122 ->setSelect(['ID', 'PERSONAL_GENDER'])
123 ->where('ID', $userId)
124 ->fetchObject()
125 ;
126
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)
130 ;
131 }
132}
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
Определения error.php:15
static includeModule($moduleName)
Определения loader.php:67
__construct(private readonly int $voteId, private readonly int $messageId,)
Определения ImVoteStopEventHandler.php:16
$result
Определения get_property_values.php:14
const IM_MESSAGE_CHAT
Определения include.php:23
$user
Определения mysql_to_pgsql.php:33
trait Error
Определения error.php:11
$message
Определения payment.php:8