Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Chat.php
1<?php
2
4
32use Bitrix\Intranet\ActionFilter\IntranetUser;
35
36class Chat extends BaseController
37{
38 public function configureActions()
39 {
40 return [
41 'add' => [
42 '+prefilters' => [
43 new IntranetUser(),
46 ],
47 ],
48 'update' => [
49 '+prefilters' => [
50 new IntranetUser(),
53 ],
54 ],
55 'setOwner' => [
56 '+prefilters' => [
57 new CheckChatAccess(),
58 new CheckChatUpdate(),
59 new CheckChatOwner(),
60 ]
61 ],
62 'setTitle' => [
63 '+prefilters' => [
64 new CheckChatAccess(),
65 new CheckChatUpdate(),
66 ]
67 ],
68 'setDescription' => [
69 '+prefilters' => [
70 new CheckChatAccess(),
71 new CheckChatUpdate(),
72 ]
73 ],
74 'setColor' => [
75 '+prefilters' => [
76 new CheckChatAccess(),
77 new CheckChatUpdate(),
78 ]
79 ],
80 'setAvatarId' => [
81 '+prefilters' => [
82 new CheckChatAccess(),
83 new CheckChatUpdate(),
84 new CheckAvatarId(),
85 ]
86 ],
87 'setAvatar' => [
88 '+prefilters' => [
89 new CheckChatAccess(),
90 new CheckChatUpdate(),
91 ]
92 ],
93 'addUsers' => [
94 '+prefilters' => [
95 new CheckChatAccess(),
96 new CheckChatUpdate(),
97 ]
98 ],
99 'deleteUser' => [
100 '+prefilters' => [
101 new CheckChatAccess(),
102 new CheckChatUpdate(),
103 ]
104 ],
105 'setManagers' => [
106 '+prefilters' => [
107 new CheckChatAccess(),
108 new CheckChatUpdate(),
109 ]
110 ],
111 'setManageUsersAdd' => [
112 '+prefilters' => [
113 new CheckChatAccess(),
115 new CheckChatUpdate(),
116 ]
117 ],
118 'setManageUsersDelete' => [
119 '+prefilters' => [
120 new CheckChatAccess(),
122 new CheckChatUpdate(),
123 ]
124 ],
125 'setManageUI' => [
126 '+prefilters' => [
127 new CheckChatAccess(),
129 new CheckChatUpdate(),
130 ]
131 ],
132 'setManageSettings' => [
133 '+prefilters' => [
134 new CheckChatAccess(),
136 new CheckChatUpdate(),
137 ]
138 ],
139 'setDisappearingDuration' => [
140 '+prefilters' => [
141 new CheckChatAccess(),
142 new CheckChatUpdate(),
144 ]
145 ],
146 'setCanPost' => [
147 '+prefilters' => [
148 new CheckChatAccess(),
149 new CheckChatCanPost(),
150 new CheckChatUpdate(),
151 ]
152 ],
153 'load' => [
154 '+prefilters' => [
156 ],
157 '+postfilters' => [
158 new UpdateStatus(),
159 ],
160 ],
161 'loadInContext' => [
162 '+prefilters' => [
164 ],
165 '+postfilters' => [
166 new UpdateStatus(),
167 ],
168 ],
169 'join' => [
170 '+prefilters' => [
171 new ChatTypeFilter([OpenChat::class, OpenLineChat::class, GeneralChat::class]),
172 ],
173 ],
174 'extendPullWatch' => [
175 '+prefilters' => [
176 new ChatTypeFilter([OpenChat::class, OpenLineChat::class]),
177 ],
178 ],
179 'read' => [
180 '+postfilters' => [
181 new UpdateStatus(),
182 ],
183 ],
184 'readAll' => [
185 '+postfilters' => [
186 new UpdateStatus(),
187 ],
188 ],
189 ];
190 }
191
193 {
194 return new ExactParameter(
195 \Bitrix\Im\V2\Chat::class,
196 'chat',
197 function ($className, $id) {
198 return \Bitrix\Im\V2\Chat::getInstance((int)$id);
199 }
200 );
201 }
202
206 public function shallowLoadAction(\Bitrix\Im\V2\Chat $chat): ?array
207 {
208 return $this->toRestFormat($chat);
209 }
210
214 public function loadAction(
215 \Bitrix\Im\V2\Chat $chat,
216 CurrentUser $user,
217 int $messageLimit = Chat\Message::DEFAULT_LIMIT,
218 int $pinLimit = Pin::DEFAULT_LIMIT
219 ): ?array
220 {
221 $result = $this->load($chat, $user, $messageLimit, $pinLimit);
222
223 if (!empty($this->getErrors()))
224 {
225 return null;
226 }
227
228 return $result;
229 }
230
234 public function loadInContextAction(
235 Message $message,
236 CurrentUser $user,
237 int $messageLimit = Chat\Message::DEFAULT_LIMIT,
238 int $pinLimit = Pin::DEFAULT_LIMIT
239 ): ?array
240 {
241 $result = $this->load($message->getChat(), $user, $messageLimit, $pinLimit, $message);
242
243 if (!empty($this->getErrors()))
244 {
245 return null;
246 }
247
248 return $result;
249 }
250
254 public function getAction(\Bitrix\Im\V2\Chat $chat): ?array
255 {
256 return (new RestAdapter($chat))->toRestFormat(['POPUP_DATA_EXCLUDE' => [UserPopupItem::class]]);
257 }
258
262 public function listSharedAction(array $filter, int $limit = self::DEFAULT_LIMIT, int $offset = 0): ?array
263 {
264 if (!isset($filter['userId']))
265 {
267
268 return null;
269 }
270 $userId = (int)$filter['userId'];
271 $chats = \Bitrix\Im\V2\Chat::getSharedChatsWithUser($userId, $this->getLimit($limit), $offset);
272 \Bitrix\Im\V2\Chat::fillRole($chats);
273 $result = ['chats' => []];
274
275 foreach ($chats as $chat)
276 {
277 $result['chats'][] = $chat->toRestFormat(['CHAT_SHORT_FORMAT' => true, 'CHAT_WITH_DATE_MESSAGE' => true]);
278 }
279
280 return $result;
281 }
282
287 public function getDialogIdAction(string $externalId): ?array
288 {
289 $chatId = Dialog::getChatId($externalId);
290
291 if ($chatId === false || $chatId === 0)
292 {
294
295 return null;
296 }
297
298 return ['dialogId' => "chat{$chatId}"];
299 }
300
304 public function readAction(\Bitrix\Im\V2\Chat $chat, string $onlyRecent = 'N'): ?array
305 {
306 $result = $chat->read($this->convertCharToBool($onlyRecent));
307
308 return $this->convertKeysToCamelCase($result->getResult());
309 }
310
314 public function readAllAction(CurrentUser $user): ?array
315 {
316 \Bitrix\Im\V2\Chat::readAllChats((int)$user->getId());
317
318 return [];
319 }
320
324 public function unreadAction(\Bitrix\Im\V2\Chat $chat): ?array
325 {
326 Recent::unread($chat->getDialogId(), true);
327
328 return [];
329 }
330
334 public function startRecordVoiceAction(\Bitrix\Im\V2\Chat $chat): ?array
335 {
336 $chat->startRecordVoice();
337
338 return ['result' => true];
339 }
340
344 public function addAction(array $fields): ?array
345 {
346 $fields['type'] = $this->getValidatedType($fields['type'] ?? null);
347
348 if (
349 !isset($fields['entityType'])
350 || $fields['entityType'] !== 'VIDEOCONF'
351 || !isset($fields['conferencePassword'])
352 )
353 {
354 unset($fields['conferencePassword']);
355 }
356
357 $data = self::recursiveWhiteList($fields, \Bitrix\Im\V2\Chat::AVAILABLE_PARAMS);
358 $result = ChatFactory::getInstance()->addChat($data);
359 if (!$result->isSuccess())
360 {
361 $this->addErrors($result->getErrors());
362 return null;
363 }
364
365 return $this->convertKeysToCamelCase($result->getResult());
366 }
367
368 //region Update Chat
372 public function updateAction(\Bitrix\Im\V2\Chat $chat, array $fields)
373 {
374 $currentUser = $this->getCurrentUser();
375 $userId = isset($currentUser) ? $currentUser->getId() : null;
376 $relation = $chat->getSelfRelation();
377
378 $changeSettings = false;
379 $changeUI = false;
380 if (!($chat instanceof PrivateChat))
381 {
382 if ($chat->getAuthorId() === (int)$userId)
383 {
384 $changeSettings = true;
385 $changeUI = true;
386 }
387 elseif ($relation->getManager())
388 {
389 if ($chat->getManageSettings() === \Bitrix\Im\V2\Chat::MANAGE_RIGHTS_MANAGERS)
390 {
391 $changeSettings = true;
392 }
393 if ($chat->getManageUI() === \Bitrix\Im\V2\Chat::MANAGE_RIGHTS_MANAGERS)
394 {
395 $changeUI = true;
396 }
397 }
398 else
399 {
400 if ($chat->getManageUI() === \Bitrix\Im\V2\Chat::MANAGE_RIGHTS_MEMBER)
401 {
402 $changeUI = true;
403 }
404 }
405 }
406
407 if ($changeSettings)
408 {
409 if (isset($fields['entityType']))
410 {
411 $chat->setEntityType($fields['entityType']);
412 }
413 if (isset($fields['entityId']))
414 {
415 $chat->setEntityId($fields['entityId']);
416 }
417 if (isset($fields['entityData1']))
418 {
419 $chat->setEntityData1($fields['entityData1']);
420 }
421 if (isset($fields['entityData2']))
422 {
423 $chat->setEntityData2($fields['entityData2']);
424 }
425 if (isset($fields['entityData3']))
426 {
427 $chat->setEntityData3($fields['entityData3']);
428 }
429 if (isset($fields['ownerId']))
430 {
431 $chat->setAuthorId($fields['ownerId']);
432 }
433 if (isset($fields['manageUsersAdd']))
434 {
435 $chat->setManageUsersAdd($fields['manageUsersAdd']);
436 }
437 if (isset($fields['manageUsersDelete']))
438 {
439 $chat->setManageUsersDelete($fields['manageUsersDelete']);
440 }
441 if (isset($fields['manageUI']))
442 {
443 $chat->setManageUI($fields['manageUI']);
444 }
445 if (isset($fields['manageSettings']))
446 {
447 $chat->setManageSettings($fields['manageSettings']);
448 }
449 if (isset($fields['canPost']))
450 {
451 $chat->setCanPost($fields['canPost']);
452 }
453 if (isset($fields['managers']))
454 {
455 $chat->setManagers($fields['managers']);
456 }
457 }
458
459 if ($changeUI)
460 {
461 if (isset($fields['title']))
462 {
463 $chat->setTitle($fields['title']);
464 }
465 if (isset($fields['description']))
466 {
467 $chat->setDescription($fields['description']);
468 }
469 if (isset($fields['color']))
470 {
471 $chat->setColor($fields['color']);
472 }
473 if (isset($fields['avatar']) && $fields['avatar'])
474 {
475 if (is_numeric((string)$fields['avatar']))
476 {
477 $this->setAvatarIdAction($chat, $fields['avatar']);
478 }
479 else
480 {
481 $this->setAvatarAction($chat, $fields['avatar']);
482 }
483 }
484 }
485
486 $result = $chat->save();
487
488 if (!$result->isSuccess())
489 {
490 return $this->convertKeysToCamelCase($result->getErrors());
491 }
492
493 return $result->isSuccess();
494 }
495
496 //region Manage Users
500 public function addUsersAction(\Bitrix\Im\V2\Chat $chat, array $userIds, ?string $hideHistory = null): ?array
501 {
502 $hideHistoryBool = $hideHistory === null ? null : $this->convertCharToBool($hideHistory, true);
503 $chat->addUsers($userIds, [], $hideHistoryBool);
504
505 return ['result' => true];
506 }
507
511 public function joinAction(\Bitrix\Im\V2\Chat $chat): ?array
512 {
513 $chat->join();
514
515 return ['result' => true];
516 }
517
521 public function extendPullWatchAction(\Bitrix\Im\V2\Chat $chat): ?array
522 {
523 if ($chat instanceof OpenChat || $chat instanceof OpenLineChat)
524 {
525 $chat->extendPullWatch();
526 }
527
528 return ['result' => true];
529 }
530
534 public function deleteUserAction(\Bitrix\Im\V2\Chat $chat, int $userId): ?array
535 {
536 $result = $chat->deleteUser($userId);
537
538 if (!$result->isSuccess())
539 {
540 $this->addErrors($result->getErrors());
541
542 return null;
543 }
544
545 return ['result' => true];
546 }
547 //endregion
548
549 //region Manage UI
553 public function setTitleAction(\Bitrix\Im\V2\Chat $chat, string $title)
554 {
555 $chat->setTitle($title);
556 $result = $chat->save();
557 if (!$result->isSuccess())
558 {
559 return $this->convertKeysToCamelCase($result->getErrors());
560 }
561
562 return $result->isSuccess();
563 }
564
568 public function setDescriptionAction(\Bitrix\Im\V2\Chat $chat, string $description)
569 {
570 $chat->setDescription($description);
571 $result = $chat->save();
572 if (!$result->isSuccess())
573 {
574 return $this->convertKeysToCamelCase($result->getErrors());
575 }
576
577 return $result->isSuccess();
578 }
579
583 public function setColorAction(\Bitrix\Im\V2\Chat $chat, string $color)
584 {
585 $result = $chat->validateColor();
586 if (!$result->isSuccess())
587 {
588 return $this->convertKeysToCamelCase($result->getErrors());
589 }
590
591 $chat->setColor($color);
592 $result = $chat->save();
593
594 if (!$result->isSuccess())
595 {
596 return $this->convertKeysToCamelCase($result->getErrors());
597 }
598
599 return $result->isSuccess();
600 }
601
605 public function setAvatarIdAction(\Bitrix\Im\V2\Chat $chat, int $avatarId)
606 {
607 $result = $chat->updateAvatarId($avatarId);
608
609 if (!$result->isSuccess())
610 {
611 return $this->convertKeysToCamelCase($result->getErrors());
612 }
613
614 return ['result' => true];
615 }
616
620 public function setAvatarAction(\Bitrix\Im\V2\Chat $chat, string $avatarBase64)
621 {
622 $result = $chat->updateAvatar($avatarBase64);
623
624 if (!$result->isSuccess())
625 {
626 return $this->convertKeysToCamelCase($result->getErrors());
627 }
628
629 return ['avatarId' => $result->getResult()];
630 }
631 //endregion
632
633 //region Manage Settings
637 public function setDisappearingDurationAction(\Bitrix\Im\V2\Chat $chat, int $hours)
638 {
639 $result = Message\Delete\DisappearService::disappearChat($chat, $hours);
640 if (!$result->isSuccess())
641 {
642 return $this->convertKeysToCamelCase($result->getErrors());
643 }
644
645 return $result->isSuccess();
646 }
647
651 public function setOwnerAction(\Bitrix\Im\V2\Chat $chat, int $ownerId)
652 {
653 $chat->setAuthorId($ownerId);
654 $result = $chat->save();
655 if (!$result->isSuccess())
656 {
657 return $this->convertKeysToCamelCase($result->getErrors());
658 }
659
660 return $result->isSuccess();
661 }
662
666 public function setManagersAction(\Bitrix\Im\V2\Chat $chat, array $userIds)
667 {
668 $chat->setManagers($userIds);
669 $result = $chat->save();
670 if (!$result->isSuccess())
671 {
672 return $this->convertKeysToCamelCase($result->getErrors());
673 }
674
675 return $result->isSuccess();
676 }
677
681 public function setManageUsersAddAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
682 {
683 $chat->setManageUsersAdd($rightsLevel);
684 $result = $chat->save();
685 if (!$result->isSuccess())
686 {
687 return $this->convertKeysToCamelCase($result->getErrors());
688 }
689
690 return $result->isSuccess();
691 }
692
696 public function setManageUsersDeleteAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
697 {
698 $chat->setManageUsersDelete($rightsLevel);
699 $result = $chat->save();
700 if (!$result->isSuccess())
701 {
702 return $this->convertKeysToCamelCase($result->getErrors());
703 }
704
705 return $result->isSuccess();
706 }
707
711 public function setManageUIAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
712 {
713 $chat->setManageUI($rightsLevel);
714 $result = $chat->save();
715 if (!$result->isSuccess())
716 {
717 return $this->convertKeysToCamelCase($result->getErrors());
718 }
719
720 return $result->isSuccess();
721 }
722
726 public function setManageSettingsAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
727 {
728 $chat->setManageSettings($rightsLevel);
729 $result = $chat->save();
730 if (!$result->isSuccess())
731 {
732 return $this->convertKeysToCamelCase($result->getErrors());
733 }
734
735 return $result->isSuccess();
736
737 }
738
742 public function setCanPostAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
743 {
744 $chat->setCanPost($rightsLevel);
745 $result = $chat->save();
746 if (!$result->isSuccess())
747 {
748 return $this->convertKeysToCamelCase($result->getErrors());
749 }
750
751 return $result->isSuccess();
752 }
753
757 public function pinAction(\Bitrix\Im\V2\Chat $chat, CurrentUser $user): ?array
758 {
759 Recent::pin($chat->getDialogId(), true, $user->getId());
760
762 {
764
765 return null;
766 }
767
768 return ['result' => true];
769 }
770
774 public function unpinAction(\Bitrix\Im\V2\Chat $chat, CurrentUser $user): ?array
775 {
776 Recent::pin($chat->getDialogId(), false, $user->getId());
777
778 return ['result' => true];
779 }
780
784 public function sortPinAction(\Bitrix\Im\V2\Chat $chat, int $newPosition, CurrentUser $user): ?array
785 {
786 if ($newPosition <= 0 || $newPosition > Recent::getPinLimit())
787 {
789
790 return null;
791 }
792
793 Recent::sortPin($chat, $newPosition, $user->getId());
794
795 return ['result' => true];
796 }
797
798 private function load(\Bitrix\Im\V2\Chat $chat, CurrentUser $user, int $messageLimit, int $pinLimit, ?Message $targetMessage = null): array
799 {
800 $messageLimit = $this->getLimit($messageLimit);
801 $pinLimit = $this->getLimit($pinLimit);
802 $messageService = new MessageService($targetMessage ?? $chat->getLoadContextMessage());
803 $messages = $messageService->getMessageContext($messageLimit, Message::REST_FIELDS)->getResult();
804 $pins = PinCollection::find(
805 ['CHAT_ID' => $chat->getChatId(), 'START_ID' => $chat->getStartId() ?: null],
806 ['ID' => 'DESC'],
807 $pinLimit
808 );
809 $restAdapter = new RestAdapter($chat, $messages, $pins);
810
811 $rest = $restAdapter->toRestFormat();
812
813 return $messageService->fillContextPaginationData($rest, $messages, $messageLimit);
814 }
815
816 private function getValidatedType(?string $type): string
817 {
818 if ($type === 'CHANNEL')
819 {
820 return \Bitrix\Im\V2\Chat::IM_TYPE_CHANNEL;
821 }
822 if ($type === 'COPILOT')
823 {
824 return \Bitrix\Im\V2\Chat::IM_TYPE_COPILOT;
825 }
826
827 return \Bitrix\Im\V2\Chat::IM_TYPE_CHAT;
828 }
829 //endregion
830 //endregion
831}
static getChatId($dialogId, $userId=null)
Definition dialog.php:91
static sortPin(\Bitrix\Im\V2\Chat $chat, int $newPosition, int $userId)
Definition recent.php:1182
static unread($dialogId, $unread, $userId=null, ?int $markedId=null)
Definition recent.php:1308
static getPinLimit()
Definition recent.php:1280
static isLimitError()
Definition recent.php:1791
static pin($dialogId, $pin, $userId=null)
Definition recent.php:983
const AVAILABLE_PARAMS
Definition Chat.php:107
const MANAGE_RIGHTS_MEMBER
Definition Chat.php:133
const MANAGE_RIGHTS_MANAGERS
Definition Chat.php:135
toRestFormat(RestConvertible ... $entities)
convertCharToBool(string $char, bool $default=false)
loadInContextAction(Message $message, CurrentUser $user, int $messageLimit=Chat\Message::DEFAULT_LIMIT, int $pinLimit=Pin::DEFAULT_LIMIT)
Definition Chat.php:234
addUsersAction(\Bitrix\Im\V2\Chat $chat, array $userIds, ?string $hideHistory=null)
Definition Chat.php:500
updateAction(\Bitrix\Im\V2\Chat $chat, array $fields)
Definition Chat.php:372
setManageUsersAddAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
Definition Chat.php:681
setColorAction(\Bitrix\Im\V2\Chat $chat, string $color)
Definition Chat.php:583
addAction(array $fields)
Definition Chat.php:344
unpinAction(\Bitrix\Im\V2\Chat $chat, CurrentUser $user)
Definition Chat.php:774
setManageUsersDeleteAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
Definition Chat.php:696
setTitleAction(\Bitrix\Im\V2\Chat $chat, string $title)
Definition Chat.php:553
setManageSettingsAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
Definition Chat.php:726
extendPullWatchAction(\Bitrix\Im\V2\Chat $chat)
Definition Chat.php:521
readAllAction(CurrentUser $user)
Definition Chat.php:314
joinAction(\Bitrix\Im\V2\Chat $chat)
Definition Chat.php:511
unreadAction(\Bitrix\Im\V2\Chat $chat)
Definition Chat.php:324
setOwnerAction(\Bitrix\Im\V2\Chat $chat, int $ownerId)
Definition Chat.php:651
pinAction(\Bitrix\Im\V2\Chat $chat, CurrentUser $user)
Definition Chat.php:757
listSharedAction(array $filter, int $limit=self::DEFAULT_LIMIT, int $offset=0)
Definition Chat.php:262
setDisappearingDurationAction(\Bitrix\Im\V2\Chat $chat, int $hours)
Definition Chat.php:637
setCanPostAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
Definition Chat.php:742
shallowLoadAction(\Bitrix\Im\V2\Chat $chat)
Definition Chat.php:206
deleteUserAction(\Bitrix\Im\V2\Chat $chat, int $userId)
Definition Chat.php:534
setManagersAction(\Bitrix\Im\V2\Chat $chat, array $userIds)
Definition Chat.php:666
sortPinAction(\Bitrix\Im\V2\Chat $chat, int $newPosition, CurrentUser $user)
Definition Chat.php:784
readAction(\Bitrix\Im\V2\Chat $chat, string $onlyRecent='N')
Definition Chat.php:304
getAction(\Bitrix\Im\V2\Chat $chat)
Definition Chat.php:254
loadAction(\Bitrix\Im\V2\Chat $chat, CurrentUser $user, int $messageLimit=Chat\Message::DEFAULT_LIMIT, int $pinLimit=Pin::DEFAULT_LIMIT)
Definition Chat.php:214
setAvatarAction(\Bitrix\Im\V2\Chat $chat, string $avatarBase64)
Definition Chat.php:620
setAvatarIdAction(\Bitrix\Im\V2\Chat $chat, int $avatarId)
Definition Chat.php:605
setDescriptionAction(\Bitrix\Im\V2\Chat $chat, string $description)
Definition Chat.php:568
startRecordVoiceAction(\Bitrix\Im\V2\Chat $chat)
Definition Chat.php:334
getDialogIdAction(string $externalId)
Definition Chat.php:287
setManageUIAction(\Bitrix\Im\V2\Chat $chat, string $rightsLevel)
Definition Chat.php:711