1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
AnchorService.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Im\V2\Anchor;
6
7use Bitrix\Im\V2\Anchor\DI\AnchorContainer;
8use Bitrix\Im\V2\Anchor\Push\PushService;
9use Bitrix\Im\V2\Chat;
10use Bitrix\Im\V2\Common\ContextCustomer;
11use Bitrix\Im\V2\Message;
12use Bitrix\Im\V2\Result;
13use Bitrix\Main\Application;
14use Bitrix\Main\DB\DuplicateEntryException;
15use Bitrix\Main\Diag\ExceptionHandler;
16use Bitrix\Main\SystemException;
17
18final class AnchorService
19{
20 use ContextCustomer;
21
22 private Message $message;
23 private Chat $chat;
24 private PushService $pushService;
25 private AnchorProvider $anchorProvider;
26 private ExceptionHandler $exceptionHandler;
27
28 public function __construct(Message $message)
29 {
30 $this->message = $message;
31
32 $this->init();
33 }
34
35 public function addMentionAnchor(array $userIds): Result
36 {
37 $anchorCollection = new AnchorCollection();
38
39 $authorId = $this->message->getAuthorId();
40
41 foreach ($userIds as $userId)
42 {
43 if ($userId <= 0 || $authorId === $userId)
44 {
45 continue;
46 }
47
48 $anchorItem = (new AnchorItem())
49 ->setChatId($this->message->getChatId())
50 ->setMessageId($this->message->getMessageId())
51 ->setUserId($userId)
52 ->setFromUserId($authorId)
53 ->setType(AnchorItem::MENTION)
54 ;
55
56 $anchorCollection->add($anchorItem);
57 }
58
59 return $this->saveAnchors($anchorCollection);
60 }
61
62 public function addReactionAnchor(string $reaction, ?int $fromUserId = null): Result
63 {
64 $fromUserId ??= $this->getContext()->getUserId();
65 if ($this->message->getAuthorId() === $fromUserId)
66 {
67 return new Result();
68 }
69
70 $authorId = $this->message->getAuthorId();
71 if ($authorId <= 0)
72 {
73 return new Result();
74 }
75
76 $anchorItem = (new AnchorItem())
77 ->setChatId($this->message->getChatId())
78 ->setMessageId($this->message->getMessageId())
79 ->setUserId($authorId)
80 ->setFromUserId($fromUserId)
81 ->setType(AnchorItem::REACTION)
82 ->setSubType($reaction)
83 ;
84
85 return $this->saveAnchor($anchorItem);
86 }
87
88 public function deleteReactionAnchors(?int $fromUserId = null): Result
89 {
90 $fromUserId ??= $this->getContext()->getUserId();
91 if ($this->message->getAuthorId() === $fromUserId)
92 {
93 return new Result();
94 }
95
96 return $this->deleteAnchors(
98 fromUserId: $fromUserId,
99 messageId: $this->message->getId()
100 );
101 }
102
103 public function deleteMentionAnchors(?int $fromUserId = null): Result
104 {
105 $fromUserId ??= $this->getContext()->getUserId();
106
107 return $this->deleteAnchors(
109 fromUserId: $fromUserId,
110 messageId: $this->message->getId()
111 );
112 }
113
114 public function deleteUsersMentionAnchors(array $userIds): Result
115 {
116 if (empty($userIds))
117 {
118 return new Result();
119 }
120
121 return $this->deleteAnchors(
123 fromUserId: $this->getContext()->getUserId(),
124 userIds: $userIds,
125 messageId: $this->message->getId(),
126 );
127 }
128
129 private function deleteAnchors(
130 ?string $type = null,
131 ?int $fromUserId = null,
132 ?array $userIds = null,
133 ?int $messageId = null,
134 ?int $chatId = null,
135 ): Result
136 {
137 $result = new Result();
138
139 $filter = [
140 'TYPE' => $type,
141 'FROM_USER_ID' => $fromUserId,
142 'USER_ID' => $userIds,
143 'MESSAGE_ID' => $messageId,
144 'CHAT_ID' => $chatId,
145 ];
146
147 try
148 {
149 $anchorCollection = AnchorCollection::find($filter);
150 if ($anchorCollection->isEmpty())
151 {
152 return $result;
153 }
154
155 $deleteResult = $anchorCollection->delete();
156 if (!$deleteResult->isSuccess())
157 {
158 return $result->addErrors($deleteResult->getErrors());
159 }
160 }
161 catch (SystemException $exception)
162 {
163 $this->exceptionHandler->writeToLog($exception);
164
165 return $result->addError(new AnchorError(AnchorError::UNEXPECTED));
166 }
167
168 $this->anchorProvider->cleanUsersCache((array)$anchorCollection->getUserIdList());
169
170 $this->pushService->deleteMulti($anchorCollection);
171
172 return $result;
173 }
174
175 private function saveAnchors(AnchorCollection $anchorCollection): Result
176 {
177 $result = new Result();
178
179 if ($anchorCollection->isEmpty())
180 {
181 return $result;
182 }
183
184 if (!$anchorCollection->getCommonMessageId())
185 {
186 return $result->addError(new AnchorError(AnchorError::UNCOMMON_MESSAGE));
187 }
188
189 try
190 {
191 $saveResult = $anchorCollection->save(true);
192 if (!$saveResult->isSuccess())
193 {
194 return $result->addErrors($saveResult->getErrors());
195 }
196 }
197 catch (SystemException $exception)
198 {
199 $this->exceptionHandler->writeToLog($exception);
200
201 return $result->addError(new AnchorError(AnchorError::UNEXPECTED));
202 }
203
204 $chat = $this->message->getChat();
205 $anchorCollection->setParentChatId($chat->getParentChatId());
206 $anchorCollection->setParentMessageId($chat->getParentMessageId());
207
208 $this->anchorProvider->cleanUsersCache((array)$anchorCollection->getUserIdList());
209
210 $this->pushService->addMulti($anchorCollection);
211
212 return $result;
213 }
214
215 private function saveAnchor(AnchorItem $anchorItem): Result
216 {
217 $result = new Result();
218
219 try
220 {
221 $saveResult = $anchorItem->save();
222 if (!$saveResult->isSuccess())
223 {
224 return $result->addErrors($saveResult->getErrors());
225 }
226 }
227 catch (DuplicateEntryException)
228 {
229 return $result;
230 }
231 catch (SystemException $exception)
232 {
233 $this->exceptionHandler->writeToLog($exception);
234
235 return $result->addError(new AnchorError(AnchorError::UNEXPECTED));
236 }
237
238 $chat = $this->message->getChat();
239 $anchorItem->setParentChatId($chat->getParentChatId());
240 $anchorItem->setParentMessageId($chat->getParentMessageId());
241
242 $this->anchorProvider->cleanCache($anchorItem->getUserId());
243
244 $this->pushService->add($anchorItem);
245
246 return $result;
247 }
248
249 private function init(): void
250 {
251 $this->chat = Chat::getInstance($this->message->getChatId());
252 $this->pushService = AnchorContainer::getInstance()->getPushService();
253 $this->anchorProvider = AnchorContainer::getInstance()->getAnchorProvider();
254 $this->exceptionHandler = Application::getInstance()->getExceptionHandler();
255 }
256}
$type
Определения options.php:106
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
static find(array $filter, array $order=['MESSAGE_ID'=> 'desc'], ?int $limit=null, ?Context $context=null)
Определения AnchorCollection.php:35
const UNCOMMON_MESSAGE
Определения AnchorError.php:12
deleteMentionAnchors(?int $fromUserId=null)
Определения AnchorService.php:103
addMentionAnchor(array $userIds)
Определения AnchorService.php:35
__construct(Message $message)
Определения AnchorService.php:28
deleteReactionAnchors(?int $fromUserId=null)
Определения AnchorService.php:88
deleteUsersMentionAnchors(array $userIds)
Определения AnchorService.php:114
addReactionAnchor(string $reaction, ?int $fromUserId=null)
Определения AnchorService.php:62
static getInstance()
Определения application.php:98
Определения result.php:20
static getInstance()
Определения servicelocator.php:33
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
Определения Uuid.php:3