1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ReactionService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Message\Reaction;
4
5use Bitrix\Im\Model\ReactionTable;
6use Bitrix\Im\V2\Analytics\MessageAnalytics;
7use Bitrix\Im\V2\Anchor\AnchorFeature;
8use Bitrix\Im\V2\Anchor\DI\AnchorContainer;
9use Bitrix\Im\V2\Common\ContextCustomer;
10use Bitrix\Im\V2\Message;
11use Bitrix\Im\V2\Result;
12use Bitrix\Imopenlines\MessageParameter;
13use Bitrix\Main\SystemException;
14
16{
17 use ContextCustomer;
18
19 private bool $withLegacy;
20 private Message $message;
21
22 public function __construct(Message $message, bool $withLegacy = true)
23 {
24 $this->message = $message;
25 $message->setContext($this->context);
26 $this->withLegacy = $withLegacy;
27 }
28
29 public function addReaction(string $reaction, bool $byEvent = false): Result
30 {
31 $result = new Result();
32 $reactionItem = new ReactionItem();
33 $reactionItem
34 ->setMessageId($this->message->getMessageId())
35 ->setChatId($this->message->getChatId())
36 ->setUserId($this->getContext()->getUserId())
37 ->setContext($this->getContext())
38 ->setReaction($reaction)
39 ;
40
41 $this->deleteAllReactions();
42
43 try
44 {
45 $saveResult = $reactionItem->save();
46 if (!$saveResult->isSuccess())
47 {
48 return $result->addErrors($saveResult->getErrors());
49 }
50 }
51 catch (SystemException $exception)
52 {
54 }
55
56 if (!$byEvent && $this->isMessageLiveChat())
57 {
58 $this->processAddForLiveChat($reaction);
59 }
60
61 if ($this->withLegacy)
62 {
63 $this->addLegacy();
64 }
65
66 (new PushService())->add($reactionItem);
67
68 (new MessageAnalytics($this->message))->addAddReaction($reaction);
69
70 $this->addAnchors($reaction);
71
72 return $result;
73 }
74
75 public function deleteReaction(string $reaction, bool $byEvent = false): Result
76 {
77 $result = new Result();
78 $reactionItem = ReactionItem::getByMessage($this->message->getMessageId(), $reaction, $this->getContext()->getUserId());
79
80 if ($reactionItem === null)
81 {
82 return $result->addError(new ReactionError(ReactionError::NOT_FOUND));
83 }
84
85 $deleteResult = $reactionItem->delete();
86
87 if (!$deleteResult->isSuccess())
88 {
89 return $result->addErrors($deleteResult->getErrors());
90 }
91
92 if (!$byEvent && $this->isMessageLiveChat())
93 {
94 $this->processDeleteForLiveChat($reaction);
95 }
96
97 if ($this->withLegacy)
98 {
99 $this->deleteLegacy();
100 }
101
102 (new PushService())->delete($reactionItem);
103
104 $this->deleteAnchor();
105
106 return $result;
107 }
108
109 private function processAddForLiveChat(string $reaction): void
110 {
111 $connectorMid = $this->message->getParams()->get(MessageParameter::CONNECTOR_MID)->getValue();
112
113 foreach ($connectorMid as $messageId)
114 {
115 $service = new static(new Message((int)$messageId), false);
116 $service->setContext($this->getContext());
117 $service->addReaction($reaction, true);
118 }
119 }
120
121 private function processDeleteForLiveChat(string $reaction): void
122 {
123 $connectorMid = $this->message->getParams()->get(MessageParameter::CONNECTOR_MID)->getValue();
124
125 foreach ($connectorMid as $messageId)
126 {
127 $service = new static(new Message((int)$messageId), false);
128 $service->setContext($this->getContext());
129 $service->deleteReaction($reaction, true);
130 }
131 }
132
133 private function isMessageLiveChat(): bool
134 {
135 $chat = $this->message->getChat();
136 $isLiveChat = $chat->getEntityType() === 'LIVECHAT';
137 $isToLiveChat = false;
138 if ($chat->getEntityType() === 'LINES')
139 {
140 [$connectorType] = explode('|', $chat->getEntityId());
141 $isToLiveChat = $connectorType === 'livechat';
142 }
143
144 return $isLiveChat || $isToLiveChat;
145 }
146
147 private function hasAnyReaction(): bool
148 {
149 $result = ReactionTable::query()
150 ->setSelect(['MESSAGE_ID'])
151 ->where('MESSAGE_ID', $this->message->getMessageId())
152 ->where('USER_ID', $this->getContext()->getUserId())
153 ->setLimit(1)
154 ->fetch()
155 ;
156
157 return $result !== false;
158 }
159
160 public function deleteAllReactions(): void
161 {
162 ReactionTable::deleteByFilter(['=MESSAGE_ID' => $this->message->getMessageId(), '=USER_ID' => $this->getContext()->getUserId()]);
163 }
164
165 private function addLegacy(): void
166 {
167 \CIMMessenger::Like($this->message->getMessageId(), 'plus', $this->getContext()->getUserId(), false, false);
168 }
169
170 private function deleteLegacy(): void
171 {
172 if (!$this->hasAnyReaction())
173 {
174 \CIMMessenger::Like($this->message->getMessageId(), 'minus', $this->getContext()->getUserId(), false, false);
175 }
176 }
177
178 private function addAnchors(string $reaction): void
179 {
180 if (!AnchorFeature::isOn())
181 {
182 return;
183 }
184
185 $anchorService = AnchorContainer::getInstance()
186 ->getAnchorService($this->message)
187 ->setContext($this->getContext());
188
189 $anchorService->addReactionAnchor($reaction);
190 }
191
192 private function deleteAnchor(): void
193 {
194 $anchorService = AnchorContainer::getInstance()
195 ->getAnchorService($this->message)
196 ->setContext($this->getContext());
197
198 $anchorService->deleteReactionAnchors();
199 }
200}
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
static getByMessage(int $messageId, string $reaction, int $userId)
Определения ReactionItem.php:60
__construct(Message $message, bool $withLegacy=true)
Определения ReactionService.php:22
addReaction(string $reaction, bool $byEvent=false)
Определения ReactionService.php:29
deleteReaction(string $reaction, bool $byEvent=false)
Определения ReactionService.php:75
Определения result.php:20
$result
Определения get_property_values.php:14
Определения Uuid.php:3
$service
Определения payment.php:18