1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Message.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2;
4
5use ArrayAccess;
6use Bitrix\Im\V2\Integration\AI\RoleManager;
7use Bitrix\Im\V2\Message\Delete\DeletionMode;
8use Bitrix\Im\V2\Message\MessageError;
9use Bitrix\Im\V2\Message\Reaction\ReactionMessage;
10use Bitrix\Im\V2\TariffLimit\DateFilterable;
11use Bitrix\Im\V2\TariffLimit\FilterResult;
12use Bitrix\Im\V2\TariffLimit\Limit;
13use Bitrix\Main\Localization\Loc;
14use Bitrix\Main\ORM\Data\DataManager;
15use Bitrix\Main\Type\DateTime;
16use Bitrix\Main\ObjectException;
17use Bitrix\Main\UrlPreview\UrlPreview;
18use Bitrix\Im;
19use Bitrix\Im\Text;
20use Bitrix\Im\Notify;
21use Bitrix\Im\Recent;
22use Bitrix\Im\Model\EO_Message;
23use Bitrix\Im\Model\MessageTable;
24use Bitrix\Im\V2\Common\ContextCustomer;
25use Bitrix\Im\V2\Common\FieldAccessImplementation;
26use Bitrix\Im\V2\Common\ActiveRecordImplementation;
27use Bitrix\Im\V2\Common\RegistryEntryImplementation;
28use Bitrix\Im\V2\Entity\File\FileCollection;
29use Bitrix\Im\V2\Entity\Url\UrlItem;
30use Bitrix\Im\V2\Link\Pin\PinService;
31use Bitrix\Im\V2\Link\Favorite\FavoriteService;
32use Bitrix\Im\V2\Message\Param;
33use Bitrix\Im\V2\Message\Params;
34use Bitrix\Im\V2\Message\Param\Menu;
35use Bitrix\Im\V2\Message\Param\Keyboard;
36use Bitrix\Im\V2\Message\Param\AttachArray;
37use Bitrix\Im\V2\Message\ReadService;
38use Bitrix\Im\V2\Message\MessageParameter;
39use Bitrix\Im\V2\Rest\PopupData;
40use Bitrix\Im\V2\Rest\RestEntity;
41use Bitrix\Im\V2\Rest\PopupDataAggregatable;
42
47{
48 use FieldAccessImplementation;
49 use ActiveRecordImplementation
50 {
51 save as defaultSave;
52 }
53 use RegistryEntryImplementation;
54 use ContextCustomer;
55
56 public const MESSAGE_MAX_LENGTH = 20000;
57 public const REST_FIELDS = ['ID', 'CHAT_ID', 'AUTHOR_ID', 'DATE_CREATE', 'MESSAGE', 'NOTIFY_EVENT', 'NOTIFY_READ'];
58
59 protected ?int $messageId = null;
60
61 protected ?int $chatId = null;
62
63 protected ?Chat $chat = null;
64
66 protected int $authorId = 0;
68
70 protected ?string $message = null;
71
72 protected ?string $parsedMessage = null;
73
74 protected ?string $formattedMessage = null;
75
77 protected ?string $messageOut = null;
78
80 protected ?DateTime $dateCreate = null;
81
83 protected ?string $emailTemplate = null;
84
93
95 protected ?string $notifyModule = null;
96
98 protected ?string $notifyEvent = null;
99
101 protected ?string $notifyTag = null;
102
104 protected ?string $notifySubTag = null;
105
107 protected ?string $notifyTitle = null;
108
110 protected ?string $notifyLink = null;
111
118 protected ?array $notifyButtons = null;
119
121 protected ?bool $notifyRead = null;
122
124 protected ?bool $notifyAnswer = null;
125
127 protected ?bool $notifyFlash = null;
128
130 protected ?int $importId = null;
131
132 protected ?bool $isUnread = null;
133
134 protected ?bool $isViewed = null;
135
137 protected Params $params;
138
143 protected ?FileCollection $files = null;
144
146
148 protected bool $isSystem = false;
149
150 protected ?UrlItem $url;
151
152 protected int $botId = 0;
153
155 protected ?string $uuid = null;
156
157 protected ?string $forwardUuid = null;
158
160 protected ?string $fileUuid = null;
161
162 protected bool $isUuidFilled = false;
163 protected bool $isUrlFilled = false;
164 protected bool $isMessageOutFilled = false;
165
166 protected ?string $pushMessage = null;
167 protected ?array $pushParams = null;
168 protected ?string $pushAppId = null;
169
170 protected ?bool $isImportant = false;
171
172 protected ?array $importantFor = null;
173 protected ?string $dialogId = null;
174 protected ?int $prevId = null;
175
179 public function __construct($source = null)
180 {
181 $this->params = new Params;
182
183 $this->initByDefault();
184
185 if (!empty($source))
186 {
187 $this->load($source);
188 }
189 }
190
191 public function save(): Result
192 {
193 $checkParamsIsValid = $this->getParams()->isValid();
194
195 if (!$checkParamsIsValid->isSuccess())
196 {
197 return $checkParamsIsValid;
198 }
199
200 $result = $this->defaultSave();
201
202 if ($result->isSuccess())
203 {
204 $this->params->setMessageId($this->getMessageId());
205
206 $paramsSaveResult = $this->params->save();
207 if (!$paramsSaveResult->isSuccess())
208 {
209 $result->addErrors($paramsSaveResult->getErrors());
210 }
211
212 $this->params = new Params();
213 }
214
215 return $result;
216 }
217
218 //region Setters & Getters
219
220 public function getId(): ?int
221 {
222 return $this->getMessageId();
223 }
224
225 public function markAsSystem(bool $flag): self
226 {
227 $this->isSystem = $flag;
228
229 return $this;
230 }
231
232 public function isSystem(): bool
233 {
234 return $this->isSystem;
235 }
236
237 public function isDisappearing(): bool
238 {
239 return (bool)$this->getDisappearingTime();
240 }
241
242 public function getDisappearingTime(): ?DateTime
243 {
244 if ($this->getMessageId())
245 {
247
248 return $row['DATE_REMOVE'];
249 }
250
251 return null;
252 }
253
254 public function isImportant(): ?bool
255 {
256 return $this->isImportant;
257 }
258
259 public function markAsImportant(?bool $isImportant = true): self
260 {
261 $this->isImportant = $isImportant;
262
263 return $this;
264 }
265
266 public function getImportantFor(): array
267 {
268 return $this->importantFor ?? array_values($this->getUserIdsFromMention());
269 }
270
271 public function setImportantFor(array $importantFor): self
272 {
273 $this->importantFor = $importantFor;
274
275 return $this;
276 }
277
278 public function getForwardUuid(): ?string
279 {
280 return $this->forwardUuid;
281 }
282
283 public function setForwardUuid(?string $forwardUuid): self
284 {
286 {
287 $this->forwardUuid = $forwardUuid;
288 }
289
290 return $this;
291 }
292
293 public function addParam(string $name, mixed $value): self
294 {
295 $this->getParams()->get($name)->setValue($value);
296
297 return $this;
298 }
299
304 public function setParams($params): self
305 {
306 $this->getParams()->load($params);
307
308 return $this;
309 }
310
315 public function resetParams($params): self
316 {
317 $this->getParams()->delete();
318
319 return $this->setParams($params);
320 }
321
326 public function getParams(bool $disallowLazyLoad = false): Params
327 {
328 if (
329 $disallowLazyLoad != true
330 && !$this->params->isLoaded()
331 && $this->getMessageId()
332 )
333 {
334 // lazyload
335 $this->params->loadByMessageId($this->getMessageId());
336 }
337
338 return $this->params;
339 }
340
341 public function enableNotify(): self
342 {
343 $this->getParams()->remove(Message\Params::NOTIFY);
344
345 return $this;
346 }
347
348 public function disableNotify(): self
349 {
350 $this->getParams()->get(Message\Params::NOTIFY)->setValue(false);
351
352 return $this;
353 }
354
359 public function setAttach($attach): self
360 {
361 $this->getParams()->get(Params::ATTACH)->setValue($attach);
362 return $this;
363 }
364
368 public function getAttach(): AttachArray
369 {
370 return $this->getParams()->get(Params::ATTACH);
371 }
372
373 public function setUrl(?UrlItem $url): self
374 {
375 $this->url = $url;
376 $this->isUrlFilled = true;
377
378 return $this;
379 }
380
381 public function getUrl(): ?UrlItem
382 {
383 if (isset($this->url))
384 {
385 return $this->url;
386 }
387
388 $urlId = $this->getParams()->get(Params::URL_ID)->getValue()[0] ?? null;
389 if (isset($urlId) && !$this->isUrlFilled)
390 {
391 return UrlItem::initByPreviewUrlId($urlId, false);
392 }
393
394 return null;
395 }
396
397 public function setUnread(bool $isUnread): self
398 {
399 $this->isUnread = $isUnread;
400
401 return $this;
402 }
403
404 public function isUnread(): bool
405 {
406 if (isset($this->isUnread))
407 {
408 return $this->isUnread;
409 }
410
411 $messageIds = [$this->getMessageId()];
412 $this->isUnread = !(new ReadService())->getReadStatusesByMessageIds($messageIds)[$this->getMessageId()];
413
414 return $this->isUnread;
415 }
416
417 public function setViewed(bool $isViewed): self
418 {
419 $this->isViewed = $isViewed;
420
421 return $this;
422 }
423
424 public function isViewed(): bool
425 {
426 if (isset($this->isViewed))
427 {
428 return $this->isViewed;
429 }
430
431 if ($this->authorId === $this->getContext()->getUserId())
432 {
433 $this->isViewed = true;
434
435 return $this->isViewed;
436 }
437
438 $messageIds = [$this->getMessageId()];
439 $this->isViewed = (new ReadService())->getViewStatusesByMessageIds($messageIds)[$this->getMessageId()];
440
441 return $this->isViewed;
442 }
443
444 public function isViewedByOthers(): bool
445 {
446 return $this->isNotifyRead() ?? false;
447 }
448
449 public function setBotId(int $botId): self
450 {
451 $this->botId = $botId;
452
453 return $this;
454 }
455
460 public function setKeyboard($keyboard): self
461 {
462 if (is_array($keyboard))
463 {
464 $value = [];
465 if (!isset($keyboard['BUTTONS']))
466 {
467 $value['BUTTONS'] = $keyboard;
468 }
469 else
470 {
471 $value = $keyboard;
472 }
473 if (!isset($value['BOT_ID']))
474 {
475 $value['BOT_ID'] = $this->botId;
476 }
477 $keyboard = $value;
478 }
479
480 $this->getParams()->get(Params::KEYBOARD)->setValue($keyboard);
481 return $this;
482 }
483
487 public function getKeyboard(): Keyboard
488 {
489 return $this->getParams()->get(Params::KEYBOARD);
490 }
491
492 /*
493 * @param array|Parameter|Menu $menu
494 */
495 public function setMenu($menu): self
496 {
497 if (is_array($menu))
498 {
499 $value = [];
500 if (!isset($menu['ITEMS']))
501 {
502 $value['ITEMS'] = $menu;
503 }
504 else
505 {
506 $value = $menu;
507 }
508 if (!isset($value['BOT_ID']))
509 {
510 $value['BOT_ID'] = $this->botId;
511 }
512 $menu = $value;
513 }
514
515 $this->getParams()->get(Params::MENU)->setValue($menu);
516 return $this;
517 }
518
522 public function getMenu(): Menu
523 {
524 return $this->getParams()->get(Params::MENU);
525 }
526
527 //region UUID
528
533 public function setUuid(?string $uuid): self
534 {
535 $this->isUuidFilled = true;
536 $this->uuid = $uuid;
537
538 return $this;
539 }
540
541 public function getUuid(): ?string
542 {
543 if ($this->isUuidFilled)
544 {
545 return $this->uuid;
546 }
547
548 $this->isUuidFilled = true;
549 $this->uuid = null;
550
551 if ($this->getMessageId())
552 {
553 $row = Im\Model\MessageUuidTable::query()
554 ->setSelect(['UUID'])
555 ->where('MESSAGE_ID', $this->getMessageId())
556 ->fetch() ?: [];
557
558 $this->uuid = $row['UUID'] ?? null;
559 }
560
561 return $this->uuid;
562 }
563
564 public function setFileUuid(?string $uuid): self
565 {
566 $this->fileUuid = $uuid;
567 return $this;
568 }
569
570 public function getFileUuid(): ?string
571 {
572 return $this->fileUuid;
573 }
574
575 //endregion
576
577 //region Files
578
582 public function getFileIds(): array
583 {
584 if ($this->getParams()->isSet(Params::FILE_ID))
585 {
586 return $this->getParams()->get(Params::FILE_ID)->getValue();
587 }
588
589 return [];
590 }
591
595 public function hasFiles(): bool
596 {
597 return
598 $this->getParams()->isSet(Params::FILE_ID)
599 && ($this->getParams()->get(Params::FILE_ID)->count() > 0);
600 }
601
606 public function setFiles($files): self
607 {
608 $fileIds = [];
609 if ($files instanceof FileCollection)
610 {
611 $this->files = $files;
612 foreach ($this->files as $fileItem)
613 {
614 $fileIds[] = $fileItem->getDiskFileId();
615 }
616 }
617 elseif (is_array($files))
618 {
619 $fileIds = array_filter(array_map('intval', array_values($files)));
620 }
621
622 $this->getParams()->get(Params::FILE_ID)->setValue($fileIds);
623
624 return $this;
625 }
626
627 public function fillFiles(FileCollection $files): self
628 {
629 $this->files = $files;
630
631 return $this;
632 }
633
634 public function addFile(Im\V2\Entity\File\FileItem $file): self
635 {
636 $this->getFiles()[] = $file;
637 $this->getParams()->get(Params::FILE_ID)->addValue($file->getId());
638
639 return $this;
640 }
641
645 public function getFiles(): FileCollection
646 {
647 if (!$this->files instanceof FileCollection)
648 {
649 $fileIds = $this->getFileIds();
650 if (!empty($fileIds))
651 {
652 $this->files = FileCollection::initByDiskFilesIds($fileIds, $this->getChatId());
653 }
654 else
655 {
656 $this->files = new FileCollection;
657 }
658 }
659
660 return $this->files;
661 }
662
663 public function getPrevId(): int
664 {
665 if ($this->prevId !== null)
666 {
667 return $this->prevId;
668 }
669
670 $result = \Bitrix\Im\Model\MessageTable::query()
671 ->setSelect(['ID'])
672 ->where('CHAT_ID', $this->getChatId() ?? 0)
673 ->where('ID', '<', $this->getId() ?? 0)
674 ->setOrder(['DATE_CREATE' => 'DESC', 'ID' => 'DESC'])
675 ->setLimit(1)
676 ->fetch() ?: []
677 ;
678 $this->prevId = (int)($result['ID'] ?? 0);
679
680 return $this->prevId;
681 }
682
688 {
689 $this->reactions = $reactions;
690
691 return $this;
692 }
693
698 {
700
701 return $this->reactions;
702 }
703
709 public function uploadFileFromText(): array
710 {
711 $files = [];
712 $message = $this->getMessage();
713 $chatId = $this->getChatId();
714
715 if (!$message || !$chatId)
716 {
717 return $files;
718 }
719
721
722 foreach ($diskFileIds as $fileId)
723 {
724 $newFile = \CIMDisk::SaveFromLocalDisk($this->getChatId(), $fileId, false, $this->getContext()->getUserId());
725 if (!$newFile)
726 {
727 continue;
728 }
729 $files[] = $newFile;
730 $file = new Im\V2\Entity\File\FileItem($newFile, $this->getChatId());
731 $this->addFile($file);
732 }
733
734 if (!empty($diskFileIds))
735 {
737 }
738
739 return $files;
740 }
741
745 public function getFilesDiskData(): array
746 {
747 if ($this->hasFiles())
748 {
749 return $this->getFiles()->getFileDiskAttributes($this->getChatId());
750 }
751
752 return [];
753 }
754
755 //endregion
756
757 public function getReminder(): ?Link\Reminder\ReminderItem
758 {
759 return Link\Reminder\ReminderItem::getByMessageAndUserId($this, $this->getContext()->getUserId());
760 }
761
763 {
764 $ids = [];
765
766 if ($this->getParams()->isSet(Params::REPLY_ID))
767 {
768 $ids[] = $this->getParams()->get(Params::REPLY_ID)->getValue();
769 }
770
771 return $ids;
772 }
773
774 public function getPopupData(array $excludedList = []): PopupData
775 {
776 $data = new PopupData([
777 new Im\V2\Entity\User\UserPopupItem($this->getUserIds()),
779 new Im\V2\Link\Reminder\ReminderPopupItem(),
781 ], $excludedList);
782
783 if (!in_array(Im\V2\Entity\File\FilePopupItem::class, $excludedList, true))
784 {
785 $data->add(new Im\V2\Entity\File\FilePopupItem($this->getFiles()));
786 }
787
788 /*if (!in_array(Im\V2\Link\Reminder\ReminderPopupItem::class, $excludedList, true))
789 {
790 $data->add(new Im\V2\Link\Reminder\ReminderPopupItem($this->getReminder()));
791 }*/
792
793 return $data->mergeFromEntity($this->getReactions());
794 }
795
796 public function setMessageId(int $messageId): self
797 {
798 if (!$this->messageId)
799 {
800 $this->messageId = $messageId;
801 $this->params->setMessageId($messageId);
802 }
803 return $this;
804 }
805
806 public function getMessageId(): ?int
807 {
808 return $this->messageId;
809 }
810
811 public function setAuthorId(int $authorId): self
812 {
813 $this->authorId = $authorId;
814
816
817 return $this;
818 }
819
820 public function processChangeAuthorId(int $authorId): int
821 {
822 if ($authorId === 0)
823 {
824 $this->markAsSystem(true);
825 }
826
827 if ($this->context && $authorId)
828 {
829 $this->context->setUserId($authorId);
830 }
831
832 return $authorId;
833 }
834
835 public function getAuthorId(): int
836 {
837 return $this->authorId;
838 }
839
840 public function getAuthor(): ?Entity\User\User
841 {
842 if ($this->getAuthorId())
843 {
845 }
846
847 return null;
848 }
849
850 public function setChatId(int $value): self
851 {
852 $this->chatId = $value;
853 return $this;
854 }
855
856 public function setChat(Chat $chat): self
857 {
858 $this->chat = $chat;
859 return $this;
860 }
861
862 public function getChatId(): ?int
863 {
864 return $this->chatId;
865 }
866
867 public function getChat(): Chat
868 {
869 if (!$this->chat)
870 {
871 if ($this->getChatId())
872 {
873 $this->chat = Chat::getInstance($this->getChatId());
874 }
875 if ($this->chat)
876 {
877 $this->setRegistry($this->chat->getMessageRegistry());
878 }
879 else
880 {
881 return new Im\V2\Chat\NullChat();
882 }
883 }
884
885 return $this->chat;
886 }
887
888 // message
889 public function setMessage(?string $value): self
890 {
891 if ($value)
892 {
893 $value = \trim(\str_replace(['[BR]', '[br]', '#BR#'], "\n", $value));
894
895 if (\mb_strlen($value) > self::MESSAGE_MAX_LENGTH + 6)
896 {
897 $value = \mb_substr($value, 0, self::MESSAGE_MAX_LENGTH). ' (...)';
898 }
899 }
900
901 $this->message = $value ?? '';
902 unset($this->parsedMessage, $this->formattedMessage, $this->url);
903 return $this;
904 }
905
906 public function getMessage(): ?string
907 {
908 return $this->message;
909 }
910
911 public function getParsedMessage(): string
912 {
913 $this->parsedMessage ??= Im\Text::parse($this->getMessage() ?? '');
914
915 return $this->parsedMessage;
916 }
917
918 public function getFormattedMessage(): string
919 {
920 if (isset($this->formattedMessage))
921 {
922 return $this->formattedMessage;
923 }
924
925 $this->formattedMessage =
926 $this->isCompletelyEmpty()
927 ? Loc::getMessage('IM_MESSAGE_DELETED')
928 : $this->getParsedMessage()
929 ;
930
931 return $this->formattedMessage;
932 }
933
934 public function getQuotedMessage(?int $messageSize = null): string
935 {
936 $user = $this->getAuthor();
937 $userName = $user?->getName() ?? '';
938 $date = FormatDate('X', $this->getDateCreate(), time() + \CTimeZone::GetOffset());
939 $contextTag = $this->getContextTag();
940 $quoteDelimiter = '------------------------------------------------------';
941 $messageContent = $this->getPreviewMessage($messageSize);
942
943 $quotedMessage =
944 $quoteDelimiter
945 . "\n"
946 . "{$userName} [{$date}] $contextTag\n"
947 . $messageContent
948 . "\n"
949 . $quoteDelimiter
950 ;
951
952 return $quotedMessage;
953 }
954
955 // formatted rich message to output
956 public function setMessageOut(?string $value): self
957 {
958 $this->messageOut = $value ? trim($value) : '';
959 return $this;
960 }
961
962 public function getMessageOut(): ?string
963 {
964 if ($this->isMessageOutFilled)
965 {
966 return $this->messageOut;
967 }
968
969 $this->fillMessageOut();
970
971 return $this->messageOut;
972 }
973
974 public function fillMessageOut(): ?string
975 {
976 if ($this->isMessageOutFilled)
977 {
978 return $this->messageOut;
979 }
980
981 if ($this->getChatId() && $this->hasFiles())
982 {
983 $messageFiles = $this->getFiles()->getMessageOut();
984 if (!empty($messageFiles))
985 {
986 $messageOut = $this->messageOut ?: $this->message;
987 $messageOut .= "\n";
988 $messageOut .= implode("\n", $messageFiles);
990 }
991 }
992
993 $this->isMessageOutFilled = true;
994
995 return $this->messageOut;
996 }
997
998 // date create
999
1004 public function setDateCreate($value): self
1005 {
1006 if (!empty($value) && !($value instanceof DateTime))
1007 {
1008 try
1009 {
1010 $value = new DateTime($value);
1011 }
1012 catch (ObjectException $exception)
1013 {}
1014 }
1015 $this->dateCreate = $value ?: null;
1016 return $this;
1017 }
1018
1019 public function getDateCreate(): ?DateTime
1020 {
1021 return $this->dateCreate;
1022 }
1023
1025 {
1026 return new DateTime;
1027 }
1028
1029 public function setEmailTemplate(?string $value): self
1030 {
1031 $this->emailTemplate = $value ?: '';
1032 return $this;
1033 }
1034
1035 public function getEmailTemplate(): ?string
1036 {
1037 return $this->emailTemplate;
1038 }
1039
1040 public function setNotifyType(?int $value): self
1041 {
1042 if (in_array($value, [\IM_NOTIFY_MESSAGE, \IM_NOTIFY_CONFIRM, \IM_NOTIFY_SYSTEM, \IM_NOTIFY_FROM], true))
1043 {
1044 $this->notifyType = $value;
1045 }
1046 return $this;
1047 }
1048
1049 public function getNotifyType(): int
1050 {
1051 return $this->notifyType;
1052 }
1053
1054 public function getDefaultNotifyType(): int
1055 {
1056 return \IM_NOTIFY_MESSAGE;
1057 }
1058
1059 public function setNotifyModule(?string $value): self
1060 {
1061 $this->notifyModule = $value;
1062 return $this;
1063 }
1064
1065 public function getNotifyModule(): ?string
1066 {
1067 return $this->notifyModule;
1068 }
1069
1070 public function getDefaultNotifyModule(): ?string
1071 {
1072 return 'im';
1073 }
1074
1080 public function setNotifyEvent(?string $notifyEvent): self
1081 {
1082 $this->notifyEvent = $notifyEvent;
1083
1085
1086 return $this;
1087 }
1088
1089 public function processChangeNotifyEvent(?string $notifyEvent): ?string
1090 {
1092 {
1093 $this->markAsSystem(true);
1094 }
1095
1096 return $notifyEvent;
1097 }
1098
1104 public function getNotifyEvent(): ?string
1105 {
1106 return $this->notifyEvent;
1107 }
1108
1109 public function getDefaultNotifyEvent(): ?string
1110 {
1111 return Notify::EVENT_DEFAULT;
1112 }
1113
1114 public function setNotifyTag(?string $value): self
1115 {
1116 $this->notifyTag = $value;
1117 return $this;
1118 }
1119
1120 public function getNotifyTag(): ?string
1121 {
1122 return $this->notifyTag;
1123 }
1124
1125 public function setNotifySubTag(?string $value): self
1126 {
1127 $this->notifySubTag = $value;
1128 return $this;
1129 }
1130
1131 public function getNotifySubTag(): ?string
1132 {
1133 return $this->notifySubTag;
1134 }
1135
1136 public function setNotifyTitle(?string $value): self
1137 {
1138 $this->notifyTitle = $value ? mb_substr(trim($value), 0, 255) : null;
1139 return $this;
1140 }
1141
1142 public function getNotifyTitle(): ?string
1143 {
1144 return $this->notifyTitle;
1145 }
1146
1147 public function setNotifyLink(?string $value): self
1148 {
1149 $this->notifyLink = $value;
1150 return $this;
1151 }
1152
1153 public function getNotifyLink(): ?string
1154 {
1155 return $this->notifyLink;
1156 }
1157
1158 public function setNotifyButtons($value): self
1159 {
1160 if (is_string($value))
1161 {
1162 $value = $this->unserializeNotifyButtons($value);
1163 }
1164 $this->notifyButtons = $value;
1165 return $this;
1166 }
1167
1168 public function getNotifyButtons(): ?array
1169 {
1170 return $this->notifyButtons;
1171 }
1172
1173 protected function serializeNotifyButtons($value)
1174 {
1175 return $value ? serialize($value) : null;
1176 }
1177
1178 protected function unserializeNotifyButtons($value)
1179 {
1180 return $value ? unserialize($value, ['allowed_classes' => false]) : null;
1181 }
1182
1183 public function markNotifyRead(?bool $value): self
1184 {
1185 $this->notifyRead = $value ?: false;
1186 return $this;
1187 }
1188
1189 public function isNotifyRead(): ?bool
1190 {
1191 return $this->notifyRead;
1192 }
1193
1194 public function getDefaultNotifyRead(): bool
1195 {
1196 return false;
1197 }
1198
1199 public function markNotifyAnswer(?bool $value): self
1200 {
1201 $this->notifyAnswer = $value ?: false;
1202 return $this;
1203 }
1204
1205 public function allowNotifyAnswer(): ?bool
1206 {
1207 return $this->notifyAnswer;
1208 }
1209
1210 public function markNotifyFlash(?bool $value): self
1211 {
1212 $this->notifyFlash = $value;
1213 return $this;
1214 }
1215
1216 public function isNotifyFlash(): ?bool
1217 {
1218 return $this->notifyFlash;
1219 }
1220
1221 public function setImportId(?int $value): self
1222 {
1223 $this->importId = $value;
1224 return $this;
1225 }
1226
1227 public function getImportId(): ?int
1228 {
1229 return $this->importId;
1230 }
1231
1232 //endregion
1233
1234 //region Push
1235
1241 public function setPushMessage(?string $message): self
1242 {
1243 $this->pushMessage = $message;
1244 return $this;
1245 }
1246
1247 public function getPushMessage(): ?string
1248 {
1249 return $this->pushMessage;
1250 }
1251
1257 public function setPushParams(?array $params): self
1258 {
1259 $this->pushParams = $params;
1260 return $this;
1261 }
1262
1263 public function getPushParams(): ?array
1264 {
1265 return $this->pushParams;
1266 }
1267
1273 public function setPushAppId(?string $message): self
1274 {
1275 $this->pushAppId = $message;
1276 return $this;
1277 }
1278
1279 public function getPushAppId(): ?string
1280 {
1281 return $this->pushAppId;
1282 }
1283
1284
1285 //endregion
1286
1287 //region Data storage
1288
1292 protected static function mirrorDataEntityFields(): array
1293 {
1294 return [
1295 'ID' => [
1296 'primary' => true,
1297 'field' => 'messageId',
1298 'set' => 'setMessageId',
1299 'get' => 'getMessageId',
1300 ],
1301 'CHAT_ID' => [
1302 'field' => 'chatId',
1303 'set' => 'setChatId',
1304 'get' => 'getChatId',
1305 ],
1306 'AUTHOR_ID' => [
1307 'field' => 'authorId',
1308 'set' => 'setAuthorId',
1309 'get' => 'getAuthorId',
1310 'loadFilter' => 'processChangeAuthorId',
1311 ],
1312 'MESSAGE' => [
1313 'field' => 'message',
1314 'set' => 'setMessage',
1315 'get' => 'getMessage',
1316 ],
1317 'MESSAGE_OUT' => [
1318 'field' => 'messageOut',
1319 'set' => 'setMessageOut',
1320 'get' => 'getMessageOut',
1321 'saveFilter' => 'fillMessageOut',
1322 ],
1323 'DATE_CREATE' => [
1324 'field' => 'dateCreate',
1325 'set' => 'setDateCreate',
1326 'get' => 'getDateCreate',
1327 'default' => 'getDefaultDateCreate',
1328 ],
1329 'MESSAGE_DATE' =>
1330 [
1331 'alias' => 'DATE_CREATE',
1332 ],
1333 'EMAIL_TEMPLATE' => [
1334 'field' => 'emailTemplate',
1335 'set' => 'setEmailTemplate',
1336 'get' => 'getEmailTemplate',
1337 ],
1338 'NOTIFY_TYPE' => [
1339 'field' => 'notifyType',
1340 'set' => 'setNotifyType',
1341 'get' => 'getNotifyType',
1342 'default' => 'getDefaultNotifyType',
1343 ],
1344 'NOTIFY_MODULE' => [
1345 'field' => 'notifyModule',
1346 'set' => 'setNotifyModule',
1347 'get' => 'getNotifyModule',
1348 'default' => 'getDefaultNotifyModule',
1349 ],
1350 'NOTIFY_EVENT' => [
1351 'field' => 'notifyEvent',
1352 'set' => 'setNotifyEvent',
1353 'get' => 'getNotifyEvent',
1354 'default' => 'getDefaultNotifyEvent',
1355 'loadFilter' => 'processChangeNotifyEvent',
1356 ],
1357 'NOTIFY_TAG' => [
1358 'field' => 'notifyTag',
1359 'set' => 'setNotifyTag',
1360 'get' => 'getNotifyTag',
1361 ],
1362 'NOTIFY_SUB_TAG' => [
1363 'field' => 'notifySubTag',
1364 'set' => 'setNotifySubTag',
1365 'get' => 'getNotifySubTag',
1366 ],
1367 'NOTIFY_TITLE' => [
1368 'field' => 'notifyTitle',
1369 'set' => 'setNotifyTitle',
1370 'get' => 'getNotifyTitle',
1371 ],
1372 'NOTIFY_LINK' => [
1373 'set' => 'setNotifyLink',
1374 'get' => 'getNotifyLink',
1375 ],
1376 'TITLE' => [
1377 'alias' => 'NOTIFY_TITLE',
1378 ],
1379 'NOTIFY_MESSAGE' => [
1380 'alias' => 'MESSAGE',
1381 ],
1382 'NOTIFY_MESSAGE_OUT' => [
1383 'alias' => 'MESSAGE_OUT',
1384 ],
1385 'NOTIFY_BUTTONS' => [
1386 'field' => 'notifyButtons',
1387 'set' => 'setNotifyButtons',
1388 'get' => 'getNotifyButtons',
1389 'saveFilter' => 'serializeNotifyButtons',
1390 'loadFilter' => 'unserializeNotifyButtons',
1391 ],
1392 'NOTIFY_READ' => [
1393 'field' => 'notifyRead',
1394 'set' => 'markNotifyRead',
1395 'get' => 'isNotifyRead',
1396 'default' => 'getDefaultNotifyRead',
1397 ],
1398 'NOTIFY_ANSWER' => [
1399 'set' => 'markNotifyAnswer',
1400 'get' => 'allowNotifyAnswer',
1401 ],
1402 'NOTIFY_FLASH' => [
1403 'set' => 'markNotifyFlash',
1404 'get' => 'isNotifyFlash',
1405 ],
1406 'NOTIFY_ONLY_FLASH' => [
1407 'alias' => 'NOTIFY_FLASH',
1408 ],
1409 'IMPORT_ID' => [
1410 'field' => 'importId',
1411 'set' => 'setImportId',
1412 'get' => 'getImportId',
1413 ],
1414 'SYSTEM' => [
1415 'set' => 'markAsSystem',
1416 'get' => 'isSystem',
1417 ],
1418 'PARAMS' => [
1419 'set' => 'setParams',
1420 'get' => 'getParams',
1421 ],
1422 'ATTACH' => [
1423 'set' => 'setAttach',
1424 'get' => 'getAttach',
1425 ],
1426 'FILES' => [
1427 'set' => 'setFiles',
1428 'get' => 'getFiles',
1429 ],
1430 'KEYBOARD' => [
1431 'set' => 'setKeyboard',
1432 'get' => 'getKeyboard',
1433 ],
1434 'MENU' => [
1435 'set' => 'setMenu',
1436 'get' => 'getMenu',
1437 ],
1438 'UUID' => [
1439 'set' => 'setUuid',
1440 'get' => 'getUuid',
1441 ],
1442 'MESSAGE_UUID' => [
1443 'alias' => 'UUID',
1444 ],
1445 'TEMPLATE_ID' => [
1446 'alias' => 'UUID',
1447 ],
1448 'FILE_TEMPLATE_ID' => [
1449 'set' => 'setFileUuid',
1450 'get' => 'getFileUuid',
1451 ],
1452 'PUSH_MESSAGE' => [
1453 'set' => 'setPushMessage',
1454 'get' => 'getPushMessage',
1455 ],
1456 'MESSAGE_PUSH' => [
1457 'alias' => 'PUSH_MESSAGE'
1458 ],
1459 'PUSH_PARAMS' => [
1460 'set' => 'setPushParams',
1461 'get' => 'getPushParams',
1462 ],
1463 'EXTRA_PARAMS' => [
1464 'alias' => 'PUSH_PARAMS'
1465 ],
1466 'PUSH_APP_ID' => [
1467 'set' => 'setPushAppId',
1468 'get' => 'getPushAppId',
1469 ],
1470 'TO_CHAT_ID' => [
1471 'alias' => 'CHAT_ID',
1472 ],
1473 ];
1474 }
1475
1479 public static function getDataClass(): string
1480 {
1481 return MessageTable::class;
1482 }
1483
1487 public function getPrimaryId(): ?int
1488 {
1489 return $this->getMessageId();
1490 }
1491
1496 public function setPrimaryId(int $primaryId): self
1497 {
1498 return $this->setMessageId($primaryId);
1499 }
1500
1501 //endregion
1502
1503 public function markAsFavorite(): Result
1504 {
1505 $favoriteMessageService = new FavoriteService();
1506 $favoriteMessageService->setContext($this->context);
1507
1508 return $favoriteMessageService->markMessageAsFavorite($this);
1509 }
1510
1511 public function unmarkAsFavorite(): Result
1512 {
1513 $favoriteMessageService = new FavoriteService();
1514 $favoriteMessageService->setContext($this->context);
1515
1516 return $favoriteMessageService->unmarkMessageAsFavorite($this);
1517 }
1518
1519 public function pin(): Result
1520 {
1521 $pinService = new PinService();
1522 $pinService->setContext($this->context);
1523
1524 return $pinService->pinMessage($this);
1525 }
1526
1527 public function unpin(): Result
1528 {
1529 $pinService = new PinService();
1530 $pinService->setContext($this->context);
1531
1532 return $pinService->unpinMessage($this);
1533 }
1534
1535 public function mark(): Result
1536 {
1537 $result = new Result();
1538
1539 $isSuccessMark = Recent::unread(
1540 $this->getChat()->getDialogId(),
1541 true,
1542 $this->getContext()->getUserId(),
1543 $this->getId()
1544 );
1545
1546 if (!$isSuccessMark)
1547 {
1548 $result->addError(new MessageError(MessageError::MARK_FAILED));
1549 }
1550
1551 return $result;
1552 }
1553
1554 public function addToReminder(DateTime $dateRemind): Result
1555 {
1556 $reminderService = new Link\Reminder\ReminderService();
1557 $reminderService->setContext($this->context);
1558
1559 return $reminderService->addMessageToReminders($this, $dateRemind);
1560 }
1561
1562 public function getPreviewMessage(?int $messageSize = 200): string
1563 {
1564 $previewMessage = trim($this->getFormattedMessage());
1565 $hasFiles = $this->hasFiles();
1566 $hasAttach = mb_strpos($previewMessage, '[ATTACH=') !== false;
1567
1568 if ($this->getRegistry() instanceof MessageCollection)
1569 {
1570 $this->getRegistry()->fillFiles();
1571 }
1572
1573 if ($hasFiles)
1574 {
1575 $files = $this->getFiles();
1576 foreach ($files as $file)
1577 {
1578 $hasFiles = true;
1579 $previewMessage .= " [{$file->getDiskFile()->getName()}]";
1580 }
1581 }
1582
1583 $previewMessage = preg_replace(
1584 "/\[ATTACH=([0-9]{1,})\]/i",
1585 " [".Loc::getMessage('IM_MESSAGE_ATTACH')."] ",
1586 $previewMessage
1587 );
1588 $previewMessage = preg_replace(
1589 '#\-{54}.+?\-{54}#s',
1590 " [".Loc::getMessage('IM_MESSAGE_QUOTE')."] ",
1591 str_replace(["#BR#"], [" "], $previewMessage)
1592 );
1593 $previewMessage = preg_replace(
1594 '/^(>>(.*)(\n)?)/mi',
1595 " [".Loc::getMessage('IM_MESSAGE_QUOTE')."] ",
1596 str_replace(["#BR#"], [" "], $previewMessage)
1597 );
1598
1599 if (!$hasFiles && !$hasAttach)
1600 {
1601 if ($this->getParams()->isSet(Params::ATTACH))
1602 {
1603 $previewMessage .= " [".Loc::getMessage('IM_MESSAGE_ATTACH')."]";
1604 }
1605 }
1606
1607 if ($messageSize !== null)
1608 {
1609 $dots = mb_strlen($previewMessage) >= $messageSize ? '...' : '';
1610 $previewMessage = mb_substr($previewMessage, 0, $messageSize - 1) . $dots;
1611 }
1612
1613 return $previewMessage;
1614 }
1615
1616 public function getForPush(?int $messageSize = 200): string
1617 {
1618 if ($this->getRegistry() instanceof MessageCollection)
1619 {
1620 $this->getRegistry()->fillFiles();
1621 }
1622
1623 $files = [];
1624
1625 foreach ($this->getFiles() as $file)
1626 {
1627 $files[] = ['name' => $file->getDiskFile()->getName()];
1628 }
1629
1630 $message = ['MESSAGE' => $this->getMessage(), 'FILES' => $files];
1631 $text = \CIMMessenger::PrepareParamsForPush($message);
1632
1633 if ($messageSize !== null)
1634 {
1635 $dots = mb_strlen($text) >= $messageSize ? '...' : '';
1636 $text = mb_substr($text, 0, $messageSize - 1) . $dots;
1637 }
1638
1639 return $text;
1640 }
1641
1642 public function checkAccess(?int $userId = null): Result
1643 {
1644 $userId ??= $this->getContext()->getUserId();
1645 $chat = $this->getChat();
1646 $result = new Result();
1647
1648 if (!$this->getId())
1649 {
1650 return $result->addError(new MessageError(MessageError::NOT_FOUND));
1651 }
1652
1653 $chatAccess = $chat->checkAccess($userId);
1654 if (!$chatAccess->isSuccess())
1655 {
1656 return $chatAccess;
1657 }
1658
1659 if ($chat->getStartId($userId) > $this->getId())
1660 {
1661 return $result->addError(new MessageError(MessageError::ACCESS_DENIED));
1662 }
1663
1664 if (!Limit::getInstance()->hasAccessByDate($this, $this->getDateCreate() ?? new DateTime()))
1665 {
1666 return $result->addError(new MessageError(MessageError::MESSAGE_ACCESS_DENIED_BY_TARIFF));
1667 }
1668
1669 return $result;
1670 }
1671
1672 public static function getRestEntityName(): string
1673 {
1674 return 'message';
1675 }
1676
1677 public function getUserIds(): array
1678 {
1679 $userIds = $this->getUserIdsFromMention();
1680
1681 if ($this->getAuthorId() !== 0)
1682 {
1683 $userIds[$this->getAuthorId()] = $this->getAuthorId();
1684 }
1685
1686 if ($this->getParams()->isSet(Params::FORWARD_USER_ID))
1687 {
1688 $userId = (int)$this->getParams()->get(Params::FORWARD_USER_ID)->getValue();
1689 $userIds[$userId] = $userId;
1690 }
1691
1692 return $userIds;
1693 }
1694
1695 public function getUserIdsFromMention(): array
1696 {
1697 if (isset($this->userIdsFromMention))
1698 {
1699 return $this->userIdsFromMention;
1700 }
1701
1702 $this->userIdsFromMention = [];
1703 if (preg_match_all("/\[USER=([0-9]+)( REPLACE)?](.*?)\[\/USER]/i", $this->getParsedMessage(), $matches))
1704 {
1705 foreach ($matches[1] as $userId)
1706 {
1707 $this->userIdsFromMention[(int)$userId] = (int)$userId;
1708 }
1709 }
1710
1711 return $this->userIdsFromMention;
1712 }
1713
1715 {
1716 $mentionedUsers = $this->getUserIdsFromMention();
1717
1718 return $this->getChat()->filterUsersToMention($mentionedUsers);
1719 }
1720
1722 {
1723 $mentionedUsers = $this->getUserIdsFromMention();
1724
1725 return $this->getChat()->filterUsersToMentionAnchor($mentionedUsers);
1726 }
1727
1728 public function getEnrichedParams(bool $withUrl = true): Params
1729 {
1730 $params = clone $this->getParams();
1731
1732 if ($withUrl)
1733 {
1734 $url = $this->getUrl();
1735 if (isset($url))
1736 {
1737 $params->get(Params::ATTACH)->addValue($url->getUrlAttach());
1738 }
1739 }
1740
1741 if ($this->isCompletelyEmpty())
1742 {
1743 $params->get(Params::IS_DELETED)->setValue(true);
1744 }
1745
1746 return $params;
1747 }
1748
1749 public function isCompletelyEmpty(): bool
1750 {
1751 return (
1752 $this->getParsedMessage() === ''
1753 && !$this->getParams()->isSet(Params::FILE_ID)
1754 && !$this->getParams()->isSet(Params::KEYBOARD)
1755 && !$this->getParams()->isSet(Params::ATTACH)
1756 );
1757 }
1758
1759 public function getContextId(): string
1760 {
1761 $chat = $this->getChat();
1762
1763 if ($chat instanceof Im\V2\Chat\PrivateChat)
1764 {
1765 $userIds = $chat->getRelations()->getUserIds();
1766 $implodeUserIds = implode(':', $userIds);
1767
1768 return "{$implodeUserIds}/{$this->getMessageId()}";
1769 }
1770
1771 return "{$chat->getDialogId()}/{$this->getMessageId()}";
1772 }
1773
1774 protected function getContextTag(): string
1775 {
1776 return "#{$this->getContextId()}";
1777 }
1778
1779 public function isForward(): bool
1780 {
1781 return $this->getParams()->isSet(Params::FORWARD_ID)
1782 && $this->getParams()->isSet(Params::FORWARD_CONTEXT_ID)
1783 ;
1784 }
1785
1786 public function getForwardInfo(): ?array
1787 {
1788 if (!$this->isForward())
1789 {
1790 return null;
1791 }
1792
1793 $contextId = $this->getParams()->get(Params::FORWARD_CONTEXT_ID)->getValue();
1794
1795 return [
1796 'id' => $contextId,
1797 'userId' => (int)$this->getParams()->get(Params::FORWARD_USER_ID)->getValue(),
1798 'chatTitle' => $this->getParams()->get(Params::FORWARD_CHAT_TITLE)->getValue() ?? null,
1800 ];
1801 }
1802
1807 public function toRestFormat(array $option = []): array
1808 {
1809 $dateCreate = $this->getDateCreate();
1810 $authorId = $this->getNotifyEvent() === Notify::EVENT_SYSTEM ? 0 : $this->getAuthorId();
1811 $messageShortInfo = $option['MESSAGE_SHORT_INFO'] ?? false;
1812 $onlyCommonRest = [
1813 'id' => $this->getId(),
1814 'chat_id' => $this->getChatId(),
1815 'author_id' => $authorId,
1816 'date' => isset($dateCreate) ? $dateCreate->format('c') : null,
1817 'text' => $this->getFormattedMessage(),
1818 'isSystem' => $this->isSystem(),
1819 'uuid' => $this->getUuid(),
1820 'forward' => $this->getForwardInfo(),
1821 'params' => $this->getEnrichedParams(!$messageShortInfo)->toRestFormat(),
1822 'viewedByOthers' => $this->isViewedByOthers(),
1823 ];
1824 $rest = $onlyCommonRest;
1825
1826 if (!isset($option['MESSAGE_ONLY_COMMON_FIELDS']) || $option['MESSAGE_ONLY_COMMON_FIELDS'] === false)
1827 {
1828 $rest = array_merge($onlyCommonRest, [
1829 'unread' => $this->isUnread(),
1830 'viewed' => $this->isViewed(),
1831 ]);
1832 }
1833
1834 return $rest;
1835 }
1836
1841 public function generateUrlPreview(): void
1842 {
1843 if ($this->getMessage())
1844 {
1845 $urls = UrlItem::getUrlsFromText($this->getMessage());
1846 foreach ($urls as $url)
1847 {
1848 $metadata = UrlPreview::getMetadataByUrl($url, true, false);
1849 if ($metadata !== false)
1850 {
1851 $urlItem = UrlItem::initByMetadata($metadata);
1852 if ($urlItem->getId())
1853 {
1854 $this->getParams()->get(Params::URL_ID)->addValue($urlItem->getId());
1855 $this->getParams()->get(Params::ATTACH)->addValue($urlItem->getUrlAttach());
1856
1857 // check if message contains only link
1858 if ($urlItem->isStaticUrl())
1859 {
1860 $staticUrl = [$url];
1861 if (mb_substr($url, -1) == '/')
1862 {
1863 $staticUrl[] = mb_substr($url, 0, -1);
1864 }
1865 $checkMessage = trim(str_replace($staticUrl, '', $this->getMessage()));
1866
1867 if (empty($checkMessage))
1868 {
1869 $this->getParams()->get(Params::URL_ONLY)->setValue(true);
1870 }
1871 }
1872 }
1873 }
1874 }
1875 }
1876 }
1877
1882 public function parseDates(): self
1883 {
1884 if ($this->getMessage())
1885 {
1886 $dateConvertResult = Text::getDateConverterParams($this->getMessage());
1887 foreach ($dateConvertResult as $row)
1888 {
1889 $this->getParams()->get(Params::DATE_TEXT)->addValue($row->getText());
1890 $this->getParams()->get(Params::DATE_TS)->addValue($row->getDate()->getTimestamp());
1891 }
1892 }
1893
1894 return $this;
1895 }
1896
1901 public function checkEmoji(): self
1902 {
1903 if ($this->getMessage())
1904 {
1905 if (Text::isOnlyEmoji($this->getMessage()))
1906 {
1907 $this->getParams()->get(Params::LARGE_FONT)->setValue(true);
1908 }
1909 }
1910
1911 return $this;
1912 }
1913
1915 {
1916 $this->getParams()->get(Params::LARGE_FONT)->setValue(Text::isOnlyEmoji($this->getMessage() ?? ''));
1917 $dateText = [];
1918 $dateTs = [];
1919 $urlIds = [];
1920 $isUrlOnly = false;
1921 if ($config->generateUrlPreview())
1922 {
1923 $results = Text::getDateConverterParams($this->getMessage() ?? '');
1924 foreach ($results as $result)
1925 {
1926 $dateText[] = $result->getText();
1927 $dateTs[] = $result->getDate()->getTimestamp();
1928 }
1929
1930 $url = UrlItem::getByMessage($this);
1931 if (isset($url))
1932 {
1933 if ($url->getId() !== null)
1934 {
1935 $urlIds[] = $url->getId();
1936 }
1937 $this->setUrl($url);
1938 $isUrlOnly = $this->isUrlOnly($url);
1939 }
1940 }
1941 if ($config->keepConnectorSilence())
1942 {
1943 $this->getParams()->get(Params::STYLE_CLASS)->setValue('bx-messenger-content-item-system');
1944 if ($this->chat instanceof Im\V2\Chat\OpenLineChat)
1945 {
1946 $this->getParams()->get(Params::COMPONENT_ID)->setValue('HiddenMessage');
1947 }
1948 }
1949 $this->getParams()->get(Params::DATE_TEXT)->setValue($dateText);
1950 $this->getParams()->get(Params::DATE_TS)->setValue($dateTs);
1951 $this->getParams()->get(Params::URL_ID)->setValue($urlIds);
1952 $this->getParams()->get(Params::URL_ONLY)->setValue($isUrlOnly);
1953
1954 return $this;
1955 }
1956
1957 public function getCopilotData(): ?array
1958 {
1959 $chat = $this->getChat();
1960 $roleManager = (new RoleManager())->setContextUser($this->getAuthorId());
1961
1962 if (!$this->isCopilotMessage())
1963 {
1964 return null;
1965 }
1966
1967 $roles = [];
1968 $messageRole = $this->getCopilotRole();
1969 $roles[] = $messageRole;
1970 $chatData = null;
1971 $engineData = null;
1972
1973 if ($chat instanceof Im\V2\Chat\CopilotChat)
1974 {
1975 $engineManager = new Im\V2\Integration\AI\EngineManager();
1976 $engineCode = $chat->getEngineCode() ?: null;
1977 $engineName = isset($engineCode) ? $engineManager->getEngineNameByCode($engineCode) : null;
1978
1979 $chatRole = $roleManager->getMainRole($this->getChatId());
1980 $roles[] = $chatRole;
1981 $chatData = [[
1982 'dialogId' => $this->getChat()->getDialogId(),
1983 'role' => $chatRole,
1984 'engine' => $engineCode,
1985 ]];
1986
1987 $engineData =
1988 isset($engineCode, $engineName)
1989 ? [['code' => $engineCode, 'name' => $engineName]]
1990 : null
1991 ;
1992 }
1993
1994 return [
1995 'chats' => $chatData,
1996 'messages' => $messageRole ? [['id' => $this->getId(), 'role' => $messageRole]] : null,
1997 'roles' => $roleManager->getRoles($roles),
1998 'engines' => $engineData,
1999 ];
2000 }
2001
2002 public function getCopilotRole(): ?string
2003 {
2004 if (!$this->isCopilotMessage())
2005 {
2006 return null;
2007 }
2008
2009 return $this->getParams()->get(Params::COPILOT_ROLE)->getValue() ?? $this->getDefaultCopilotRole();
2010 }
2011
2012 public function isCopilotMessage(): bool
2013 {
2014 return $this->getParams()->isSet(Params::COPILOT_ROLE);
2015 }
2016
2017 protected function getDefaultCopilotRole(): ?string
2018 {
2019 if (\Bitrix\Main\Loader::includeModule('imbot')
2020 && $this->getAuthorId() === \Bitrix\Imbot\Bot\CopilotChatBot::getBotId()
2021 )
2022 {
2023 return RoleManager::getDefaultRoleCode();
2024 }
2025
2026 return null;
2027 }
2028
2029 private function isUrlOnly(?UrlItem $url): bool
2030 {
2031 if ($url === null)
2032 {
2033 return false;
2034 }
2035
2036 if (!$url->isStaticUrl())
2037 {
2038 return false;
2039 }
2040
2041 $messageWithoutUrl = str_replace($url->getUrl(), '', $this->getMessage() ?? '');
2042
2043 return trim($messageWithoutUrl) === '';
2044 }
2045
2050 public function updateSearchIndex(): void
2051 {
2052 if ($this->getMessageId())
2053 {
2054 MessageTable::indexRecord($this->getMessageId());
2055 }
2056 }
2057
2062 public static function loadPhrases(): void
2063 {
2064 Loc::loadMessages(__FILE__);
2065 }
2066
2067 public function deleteSoft(): Result
2068 {
2070 $service->setMode(DeletionMode::Soft);
2071 return $service->delete();
2072 }
2073
2074 public function deleteComplete(): Result
2075 {
2077 $service->setMode(DeletionMode::Complete);
2078 return $service->delete();
2079 }
2080
2081 public function filterByDate(DateTime $date): FilterResult
2082 {
2083 $result = new FilterResult();
2084
2085 if ($this->getDateCreate()?->getTimestamp() > $date->getTimestamp())
2086 {
2087 return $result->setResult($this);
2088 }
2089
2090 return $result->setResult(null)->setFiltered(true);
2091 }
2092
2093 public function getRelatedChatId(): ?int
2094 {
2095 return $this->getChatId();
2096 }
2097
2098 public function filterMessageText(): void
2099 {
2100 if (!$this->isSystem && $this->getMessage() !== null)
2101 {
2102 $this->setMessage(Text::filterUserBbCodes($this->getMessage(), $this->getContext()->getUserId()));
2103 }
2104 }
2105
2106 public function getActionContextUserId(): int
2107 {
2108 return $this->getAuthorId() ?: $this->getContext()->getUserId();
2109 }
2110}
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 validate(string $uuid)
Определения Uuid.php:87
const EVENT_PRIVATE_SYSTEM
Определения notify.php:24
const EVENT_DEFAULT
Определения notify.php:20
const EVENT_SYSTEM
Определения notify.php:21
static unread($dialogId, $unread, $userId=null, ?int $markedId=null, ?string $itemTypes=null)
Определения recent.php:1674
static parse($text, $params=Array())
Определения text.php:28
static filterUserBbCodes(string $text, int $currentUserId)
Определения text.php:554
static getDateConverterParams($text)
Определения text.php:219
static isOnlyEmoji($text)
Определения text.php:233
static initByDiskFilesIds(array $diskFilesIds, ?int $chatId=null)
Определения FileCollection.php:57
static removeDiskBbCodesFromText(string $text)
Определения FileItem.php:87
static getDiskFileIdsFromBbCodesInText(string $text)
Определения FileItem.php:79
static initByPreviewUrlId(int $previewUrlId, bool $withHtml=true)
Определения UrlItem.php:60
static initByMetadata(array $metadata)
Определения UrlItem.php:55
static getUrlsFromText(?string $text)
Определения UrlItem.php:82
static getByMessage(Message $message)
Определения UrlItem.php:126
static getInstance(?int $id)
Определения User.php:72
static getInstanceByMessage(Message $message)
Определения DeleteService.php:73
static getChatTypeByContextId(string $contextId)
Определения ForwardService.php:66
static getByMessageId(int $messageId)
Определения ReactionMessage.php:36
array $pushParams
Определения Message.php:167
markAsImportant(?bool $isImportant=true)
Определения Message.php:259
getChatId()
Определения Message.php:862
isViewed()
Определения Message.php:424
static getRestEntityName()
Определения Message.php:1672
getNotifyType()
Определения Message.php:1049
setEmailTemplate(?string $value)
Определения Message.php:1029
setPrimaryId(int $primaryId)
Определения Message.php:1496
getDisappearingTime()
Определения Message.php:242
getMessage()
Определения Message.php:906
enableNotify()
Определения Message.php:341
string $messageOut
Определения Message.php:77
getId()
Определения Message.php:220
isForward()
Определения Message.php:1779
getImportantFor()
Определения Message.php:266
pin()
Определения Message.php:1519
isNotifyFlash()
Определения Message.php:1216
getImportId()
Определения Message.php:1227
getNotifyLink()
Определения Message.php:1153
parseDates()
Определения Message.php:1882
getForwardInfo()
Определения Message.php:1786
markNotifyRead(?bool $value)
Определения Message.php:1183
setNotifyType(?int $value)
Определения Message.php:1040
deleteSoft()
Определения Message.php:2067
getNotifyButtons()
Определения Message.php:1168
addParam(string $name, mixed $value)
Определения Message.php:293
toRestFormat(array $option=[])
Определения Message.php:1807
getForwardUuid()
Определения Message.php:278
getUuid()
Определения Message.php:541
setMessage(?string $value)
Определения Message.php:889
setNotifyButtons($value)
Определения Message.php:1158
string $parsedMessage
Определения Message.php:72
setFileUuid(?string $uuid)
Определения Message.php:564
uploadFileFromText()
Определения Message.php:709
getDateCreate()
Определения Message.php:1019
setNotifyModule(?string $value)
Определения Message.php:1059
getUserIdsToSendMentions()
Определения Message.php:1714
getUserIds()
Определения Message.php:1677
string $emailTemplate
Определения Message.php:83
string $message
Определения Message.php:70
fillMessageOut()
Определения Message.php:974
array $notifyButtons
Определения Message.php:118
autocompleteParams(Im\V2\Message\Send\SendingConfig $config)
Определения Message.php:1914
unmarkAsFavorite()
Определения Message.php:1511
getFormattedMessage()
Определения Message.php:918
getQuotedMessage(?int $messageSize=null)
Определения Message.php:934
getReminder()
Определения Message.php:757
isImportant()
Определения Message.php:254
getContextTag()
Определения Message.php:1774
setPushAppId(?string $message)
Определения Message.php:1273
getDefaultNotifyEvent()
Определения Message.php:1109
getAuthorId()
Определения Message.php:835
setNotifyLink(?string $value)
Определения Message.php:1147
setPushParams(?array $params)
Определения Message.php:1257
setBotId(int $botId)
Определения Message.php:449
bool $isViewed
Определения Message.php:134
checkAccess(?int $userId=null)
Определения Message.php:1642
getFilesDiskData()
Определения Message.php:745
getPopupData(array $excludedList=[])
Определения Message.php:774
__construct($source=null)
Определения Message.php:179
getPrimaryId()
Определения Message.php:1487
int $authorId
Определения Message.php:66
string $notifyEvent
Определения Message.php:98
getAuthor()
Определения Message.php:840
getPreviewMessage(?int $messageSize=200)
Определения Message.php:1562
setKeyboard($keyboard)
Определения Message.php:460
processChangeAuthorId(int $authorId)
Определения Message.php:820
string $notifyModule
Определения Message.php:95
getMessageId()
Определения Message.php:806
setUrl(?UrlItem $url)
Определения Message.php:373
getPushAppId()
Определения Message.php:1279
getNotifyEvent()
Определения Message.php:1104
setMenu($menu)
Определения Message.php:495
string $notifySubTag
Определения Message.php:104
getCopilotRole()
Определения Message.php:2002
string $notifyTag
Определения Message.php:101
int $notifyType
Определения Message.php:92
getMenu()
Определения Message.php:522
getNotifyTag()
Определения Message.php:1120
bool $isMessageOutFilled
Определения Message.php:164
static loadPhrases()
Определения Message.php:2062
deleteComplete()
Определения Message.php:2074
setUnread(bool $isUnread)
Определения Message.php:397
bool $isSystem
Определения Message.php:148
resetParams($params)
Определения Message.php:315
getFiles()
Определения Message.php:645
getParams(bool $disallowLazyLoad=false)
Определения Message.php:326
Chat $chat
Определения Message.php:63
string $dialogId
Определения Message.php:173
getNotifyModule()
Определения Message.php:1065
bool $notifyRead
Определения Message.php:121
addToReminder(DateTime $dateRemind)
Определения Message.php:1554
int $chatId
Определения Message.php:61
mark()
Определения Message.php:1535
getChat()
Определения Message.php:867
string $formattedMessage
Определения Message.php:74
isNotifyRead()
Определения Message.php:1189
getAdditionalMessageIds()
Определения Message.php:762
string $pushMessage
Определения Message.php:166
setDateCreate($value)
Определения Message.php:1004
getParsedMessage()
Определения Message.php:911
filterMessageText()
Определения Message.php:2098
getActionContextUserId()
Определения Message.php:2106
setChat(Chat $chat)
Определения Message.php:856
getUserIdsToSendMentionAnchors()
Определения Message.php:1721
bool $isUnread
Определения Message.php:132
getReactions()
Определения Message.php:697
bool $notifyAnswer
Определения Message.php:124
getAttach()
Определения Message.php:368
setParams($params)
Определения Message.php:304
isSystem()
Определения Message.php:232
getDefaultNotifyModule()
Определения Message.php:1070
bool $isUuidFilled
Определения Message.php:162
setImportantFor(array $importantFor)
Определения Message.php:271
getEmailTemplate()
Определения Message.php:1035
string $fileUuid
Определения Message.php:160
getDefaultDateCreate()
Определения Message.php:1024
getNotifySubTag()
Определения Message.php:1131
fillFiles(FileCollection $files)
Определения Message.php:627
setNotifyTag(?string $value)
Определения Message.php:1114
getForPush(?int $messageSize=200)
Определения Message.php:1616
string $pushAppId
Определения Message.php:168
setImportId(?int $value)
Определения Message.php:1221
int $prevId
Определения Message.php:174
setMessageId(int $messageId)
Определения Message.php:796
markAsFavorite()
Определения Message.php:1503
isDisappearing()
Определения Message.php:237
checkEmoji()
Определения Message.php:1901
getFileIds()
Определения Message.php:582
bool $notifyFlash
Определения Message.php:127
DateTime $dateCreate
Определения Message.php:80
markNotifyAnswer(?bool $value)
Определения Message.php:1199
Im V2 Message Reaction ReactionMessage $reactions
Определения Message.php:145
isCompletelyEmpty()
Определения Message.php:1749
serializeNotifyButtons($value)
Определения Message.php:1173
isUnread()
Определения Message.php:404
getPrevId()
Определения Message.php:663
setFiles($files)
Определения Message.php:606
int $importId
Определения Message.php:130
filterByDate(DateTime $date)
Определения Message.php:2081
markNotifyFlash(?bool $value)
Определения Message.php:1210
getDefaultNotifyRead()
Определения Message.php:1194
setNotifyEvent(?string $notifyEvent)
Определения Message.php:1080
updateSearchIndex()
Определения Message.php:2050
hasFiles()
Определения Message.php:595
const REST_FIELDS
Определения Message.php:57
string $forwardUuid
Определения Message.php:157
Params $params
Определения Message.php:137
bool $isUrlFilled
Определения Message.php:163
setNotifySubTag(?string $value)
Определения Message.php:1125
bool $isImportant
Определения Message.php:170
static getDataClass()
Определения Message.php:1479
setMessageOut(?string $value)
Определения Message.php:956
setViewed(bool $isViewed)
Определения Message.php:417
FileCollection $files
Определения Message.php:143
setForwardUuid(?string $forwardUuid)
Определения Message.php:283
const MESSAGE_MAX_LENGTH
Определения Message.php:56
getUrl()
Определения Message.php:381
disableNotify()
Определения Message.php:348
isViewedByOthers()
Определения Message.php:444
setReactions(Im\V2\Message\Reaction\ReactionMessage $reactions)
Определения Message.php:687
getPushParams()
Определения Message.php:1263
getDefaultCopilotRole()
Определения Message.php:2017
setPushMessage(?string $message)
Определения Message.php:1241
getPushMessage()
Определения Message.php:1247
int $botId
Определения Message.php:152
UrlItem $url
Определения Message.php:150
setAttach($attach)
Определения Message.php:359
getEnrichedParams(bool $withUrl=true)
Определения Message.php:1728
setNotifyTitle(?string $value)
Определения Message.php:1136
array $userIdsFromMention
Определения Message.php:67
setUuid(?string $uuid)
Определения Message.php:533
addFile(Im\V2\Entity\File\FileItem $file)
Определения Message.php:634
getKeyboard()
Определения Message.php:487
isCopilotMessage()
Определения Message.php:2012
getCopilotData()
Определения Message.php:1957
array $importantFor
Определения Message.php:172
generateUrlPreview()
Определения Message.php:1841
setChatId(int $value)
Определения Message.php:850
int $messageId
Определения Message.php:59
getFileUuid()
Определения Message.php:570
getMessageOut()
Определения Message.php:962
string $uuid
Определения Message.php:155
unserializeNotifyButtons($value)
Определения Message.php:1178
string $notifyTitle
Определения Message.php:107
setAuthorId(int $authorId)
Определения Message.php:811
unpin()
Определения Message.php:1527
markAsSystem(bool $flag)
Определения Message.php:225
static mirrorDataEntityFields()
Определения Message.php:1292
string $notifyLink
Определения Message.php:110
getDefaultNotifyType()
Определения Message.php:1054
getContextId()
Определения Message.php:1759
getNotifyTitle()
Определения Message.php:1142
save()
Определения Message.php:191
getRelatedChatId()
Определения Message.php:2093
allowNotifyAnswer()
Определения Message.php:1205
getUserIdsFromMention()
Определения Message.php:1695
processChangeNotifyEvent(?string $notifyEvent)
Определения Message.php:1089
static getInstance()
Определения application.php:98
Определения result.php:20
static getRowById($id, array $parameters=[])
Определения datamanager.php:380
$name
Определения entity.php:33
getTimestamp()
Определения date.php:218
static SaveFromLocalDisk($chatId, $fileId, bool $symlink=false, ?int $userId=null)
Определения im_disk.php:1001
$data['IS_AVAILABLE']
Определения .description.php:13
</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
const IM_NOTIFY_MESSAGE
Определения include.php:35
const IM_NOTIFY_SYSTEM
Определения include.php:38
const IM_NOTIFY_CONFIRM
Определения include.php:36
const IM_NOTIFY_FROM
Определения include.php:37
Определения RegistryEntry.php:6
getRegistry()
setRegistry(Registry $registry)
FormatDate($format="", $timestamp=false, $now=false, ?string $languageId=null)
Определения tools.php:871
Определения contextmenu.php:9
Определения Uuid.php:3
Определения ActionUuid.php:3
Определения ufield.php:9
Определения Image.php:9
$user
Определения mysql_to_pgsql.php:33
$files
Определения mysql_to_pgsql.php:30
$service
Определения payment.php:18
$message
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69
$text
Определения template_pdf.php:79
else $userName
Определения order_form.php:75
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$option
Определения options.php:1711
$matches
Определения index.php:22
$url
Определения iframe.php:7