18use Bitrix\Im\Model\EO_Message;
20use Bitrix\Im\V2\Common\ContextCustomer;
21use Bitrix\Im\V2\Common\FieldAccessImplementation;
22use Bitrix\Im\V2\Common\ActiveRecordImplementation;
23use Bitrix\Im\V2\Common\RegistryEntryImplementation;
45 use FieldAccessImplementation;
46 use ActiveRecordImplementation
50 use RegistryEntryImplementation;
54 public const REST_FIELDS = [
'ID',
'CHAT_ID',
'AUTHOR_ID',
'DATE_CREATE',
'MESSAGE',
'NOTIFY_EVENT'];
177 $this->params =
new Params;
179 $this->initByDefault();
183 $this->
load($source);
189 $checkParamsIsValid = $this->
getParams()->isValid();
191 if (!$checkParamsIsValid->isSuccess())
193 return $checkParamsIsValid;
196 $result = $this->defaultSave();
198 if ($result->isSuccess())
202 $paramsSaveResult = $this->params->save();
203 if (!$paramsSaveResult->isSuccess())
205 $result->addErrors($paramsSaveResult->getErrors());
228 return $this->isSystem;
240 $row = Im\Model\MessageDisappearingTable::getRowById($this->
getMessageId());
242 return $row[
'DATE_REMOVE'];
250 return $this->isImportant;
267 $this->importantFor = $importantFor;
274 return $this->forwardUuid;
281 $this->forwardUuid = $forwardUuid;
316 $disallowLazyLoad !=
true
317 && !$this->params->isLoaded()
318 && $this->getMessageId()
325 return $this->params;
334 $this->
getParams()->get(Params::ATTACH)->setValue($attach);
343 return $this->
getParams()->get(Params::ATTACH);
349 $this->isUrlFilled =
true;
356 if (isset($this->url))
361 $urlId = $this->
getParams()->get(Params::URL_ID)->getValue()[0] ??
null;
362 if (isset($urlId) && !$this->isUrlFilled)
364 return UrlItem::initByPreviewUrlId($urlId,
false);
381 return $this->isUnread;
387 return $this->isUnread;
401 return $this->isViewed;
404 if ($this->authorId === $this->getContext()->getUserId())
408 return $this->isViewed;
414 return $this->isViewed;
428 return $this->isViewedByOthers;
433 return $this->isViewedByOthers;
438 $this->botId = $botId;
449 if (is_array($keyboard))
452 if (!isset($keyboard[
'BUTTONS']))
454 $value[
'BUTTONS'] = $keyboard;
460 if (!isset($value[
'BOT_ID']))
462 $value[
'BOT_ID'] = $this->botId;
467 $this->
getParams()->get(Params::KEYBOARD)->setValue($keyboard);
476 return $this->
getParams()->get(Params::KEYBOARD);
487 if (!isset($menu[
'ITEMS']))
489 $value[
'ITEMS'] = $menu;
495 if (!isset($value[
'BOT_ID']))
497 $value[
'BOT_ID'] = $this->botId;
502 $this->
getParams()->get(Params::MENU)->setValue($menu);
511 return $this->
getParams()->get(Params::MENU);
522 $this->isUuidFilled =
true;
534 if ($this->isUuidFilled)
539 $this->isUuidFilled =
true;
544 $row = Im\Model\MessageUuidTable::query()
545 ->setSelect([
'UUID'])
549 $this->uuid = $row[
'UUID'] ??
null;
557 $this->fileUuid = $uuid;
563 return $this->fileUuid;
575 if ($this->
getParams()->isSet(Params::FILE_ID))
577 return $this->
getParams()->get(Params::FILE_ID)->getValue();
589 $this->
getParams()->isSet(Params::FILE_ID)
590 && ($this->
getParams()->get(Params::FILE_ID)->count() > 0);
602 $this->files = $files;
603 foreach ($this->files as $fileItem)
605 $fileIds[] = $fileItem->getDiskFileId();
608 elseif (is_array($files))
610 $fileIds = array_filter(array_map(
'intval', array_values($files)));
613 $this->
getParams()->get(Params::FILE_ID)->setValue($fileIds);
620 $this->files = $files;
628 $this->
getParams()->get(Params::FILE_ID)->addValue($file->getId());
641 if (!empty($fileIds))
660 $this->reactions = $reactions;
670 $this->reactions ??= Im\V2\Message\Reaction\ReactionMessage::getByMessageId($this->
getMessageId());
672 return $this->reactions;
686 if (preg_match_all(
"/\[DISK=([0-9]+)\]/i", $message, $matches))
689 foreach ($matches[1] as $fileId)
691 $newFile = \CIMDisk::SaveFromLocalDisk($this->
getChatId(), $fileId,
false, $this->getContext()->getUserId());
702 $message = preg_replace(
"/\[DISK\=([0-9]+)\]/i",
'', $message);
715 $messageFiles = $this->formatFileLinks();
716 if (!empty($messageFiles))
719 $messageOut .= implode(
"\n", $messageFiles);
740 private function formatFileLinks(): array
744 $filesDataList = $this->getFilesDiskData();
745 if (!empty($filesDataList))
747 $urlManager = UrlManager::getInstance();
748 $hostUrl = $urlManager->getHostUrl();
749 foreach ($filesDataList as $fileData)
751 if ($fileData[
'status'] ==
'done')
754 $fileData[
'name'] .
' (' . \CFile::formatSize($fileData[
'size']) .
')'
756 .
' ' . $hostUrl . $fileData[
'urlDownload']
762 return $messageFiles;
769 return Link\Reminder\ReminderItem::getByMessageAndUserId($this, $this->getContext()->getUserId());
776 if ($this->
getParams()->isSet(Params::REPLY_ID))
778 $ids[] = $this->
getParams()->get(Params::REPLY_ID)->getValue();
793 if (!in_array(Im\V2\
Entity\
File\FilePopupItem::class, $excludedList,
true))
798 if (!in_array(Im\V2\
Link\Reminder\ReminderPopupItem::class, $excludedList,
true))
808 if (!$this->messageId)
810 $this->messageId = $messageId;
811 $this->params->setMessageId($messageId);
818 return $this->messageId;
823 $this->authorId = $authorId;
842 return $this->authorId;
857 $this->chatId = $value;
869 return $this->chatId;
878 $this->chat = Chat::getInstance($this->
getChatId());
882 $this->
setRegistry($this->chat->getMessageRegistry());
898 $value = \trim(\str_replace([
'[BR]',
'[br]',
'#BR#'],
"\n", $value));
900 if (\mb_strlen($value) > self::MESSAGE_MAX_LENGTH + 6)
902 $value = \mb_substr($value, 0, self::MESSAGE_MAX_LENGTH).
' (...)';
906 $this->message = $value ?:
'';
907 unset($this->parsedMessage, $this->formattedMessage, $this->url);
913 return $this->message;
918 $this->parsedMessage ??= Im\Text::parse($this->
getMessage() ??
'');
920 return $this->parsedMessage;
925 if (isset($this->formattedMessage))
927 return $this->formattedMessage;
930 $this->formattedMessage =
936 return $this->formattedMessage;
942 $userName = isset($user) ? $user->getFullName(
false) :
'';
943 $date = FormatDate(
'X', $this->
getDateCreate(), time() + \CTimeZone::GetOffset());
945 $quoteDelimiter =
'------------------------------------------------------';
951 .
"{$userName} [{$date}] $contextTag\n"
957 return $quotedMessage;
968 $this->messageOut = $value ? trim($value) :
'';
974 return $this->messageOut;
985 if (!empty($value) && !($value instanceof
DateTime))
994 $this->dateCreate = $value ?:
null;
1000 return $this->dateCreate;
1010 $this->emailTemplate = $value ?:
'';
1016 return $this->emailTemplate;
1021 if (in_array($value, [\IM_NOTIFY_MESSAGE, \IM_NOTIFY_CONFIRM, \IM_NOTIFY_SYSTEM, \IM_NOTIFY_FROM],
true))
1023 $this->notifyType = $value;
1030 return $this->notifyType;
1035 return \IM_NOTIFY_MESSAGE;
1040 $this->notifyModule = $value;
1046 return $this->notifyModule;
1061 $this->notifyEvent = $notifyEvent;
1075 return $notifyEvent;
1085 return $this->notifyEvent;
1095 $this->notifyTag = $value;
1101 return $this->notifyTag;
1106 $this->notifySubTag = $value;
1112 return $this->notifySubTag;
1117 $this->notifyTitle = $value ? mb_substr(trim($value), 0, 255) :
null;
1123 return $this->notifyTitle;
1128 $this->notifyLink = $value;
1134 return $this->notifyLink;
1139 if (is_string($value))
1143 $this->notifyButtons = $value;
1149 return $this->notifyButtons;
1154 return $value ? serialize($value) :
null;
1159 return $value ? unserialize($value, [
'allowed_classes' =>
false]) :
null;
1164 $this->notifyRead = $value ?:
false;
1170 return $this->notifyRead;
1180 $this->notifyAnswer = $value ?:
false;
1186 return $this->notifyAnswer;
1191 $this->notifyFlash = $value;
1197 return $this->notifyFlash;
1202 $this->importId = $value;
1208 return $this->importId;
1222 $this->pushMessage = $message;
1228 return $this->pushMessage;
1238 $this->pushParams = $params;
1244 return $this->pushParams;
1254 $this->pushAppId = $message;
1260 return $this->pushAppId;
1276 'field' =>
'messageId',
1277 'set' =>
'setMessageId',
1278 'get' =>
'getMessageId',
1281 'field' =>
'chatId',
1282 'set' =>
'setChatId',
1283 'get' =>
'getChatId',
1286 'field' =>
'authorId',
1287 'set' =>
'setAuthorId',
1288 'get' =>
'getAuthorId',
1289 'loadFilter' =>
'processChangeAuthorId',
1292 'alias' =>
'AUTHOR_ID',
1295 'field' =>
'message',
1296 'set' =>
'setMessage',
1297 'get' =>
'getMessage',
1300 'field' =>
'messageOut',
1301 'set' =>
'setMessageOut',
1302 'get' =>
'getMessageOut',
1305 'field' =>
'dateCreate',
1306 'set' =>
'setDateCreate',
1307 'get' =>
'getDateCreate',
1308 'default' =>
'getDefaultDateCreate',
1312 'alias' =>
'DATE_CREATE',
1314 'EMAIL_TEMPLATE' => [
1315 'field' =>
'emailTemplate',
1316 'set' =>
'setEmailTemplate',
1317 'get' =>
'getEmailTemplate',
1320 'field' =>
'notifyType',
1321 'set' =>
'setNotifyType',
1322 'get' =>
'getNotifyType',
1323 'default' =>
'getDefaultNotifyType',
1325 'NOTIFY_MODULE' => [
1326 'field' =>
'notifyModule',
1327 'set' =>
'setNotifyModule',
1328 'get' =>
'getNotifyModule',
1329 'default' =>
'getDefaultNotifyModule',
1332 'field' =>
'notifyEvent',
1333 'set' =>
'setNotifyEvent',
1334 'get' =>
'getNotifyEvent',
1335 'default' =>
'getDefaultNotifyEvent',
1336 'loadFilter' =>
'processChangeNotifyEvent',
1339 'field' =>
'notifyTag',
1340 'set' =>
'setNotifyTag',
1341 'get' =>
'getNotifyTag',
1343 'NOTIFY_SUB_TAG' => [
1344 'field' =>
'notifySubTag',
1345 'set' =>
'setNotifySubTag',
1346 'get' =>
'getNotifySubTag',
1349 'field' =>
'notifyTitle',
1350 'set' =>
'setNotifyTitle',
1351 'get' =>
'getNotifyTitle',
1354 'set' =>
'setNotifyLink',
1355 'get' =>
'getNotifyLink',
1358 'alias' =>
'NOTIFY_TITLE',
1360 'NOTIFY_MESSAGE' => [
1361 'alias' =>
'MESSAGE',
1363 'NOTIFY_MESSAGE_OUT' => [
1364 'alias' =>
'MESSAGE_OUT',
1366 'NOTIFY_BUTTONS' => [
1367 'field' =>
'notifyButtons',
1368 'set' =>
'setNotifyButtons',
1369 'get' =>
'getNotifyButtons',
1370 'saveFilter' =>
'serializeNotifyButtons',
1371 'loadFilter' =>
'unserializeNotifyButtons',
1374 'field' =>
'notifyRead',
1375 'set' =>
'markNotifyRead',
1376 'get' =>
'isNotifyRead',
1377 'default' =>
'getDefaultNotifyRead',
1379 'NOTIFY_ANSWER' => [
1380 'set' =>
'markNotifyAnswer',
1381 'get' =>
'allowNotifyAnswer',
1384 'set' =>
'markNotifyFlash',
1385 'get' =>
'isNotifyFlash',
1387 'NOTIFY_ONLY_FLASH' => [
1388 'alias' =>
'NOTIFY_FLASH',
1391 'field' =>
'importId',
1392 'set' =>
'setImportId',
1393 'get' =>
'getImportId',
1396 'set' =>
'markAsSystem',
1397 'get' =>
'isSystem',
1400 'set' =>
'setParams',
1401 'get' =>
'getParams',
1404 'set' =>
'setAttach',
1405 'get' =>
'getAttach',
1408 'set' =>
'setFiles',
1409 'get' =>
'getFiles',
1412 'set' =>
'setKeyboard',
1413 'get' =>
'getKeyboard',
1429 'FILE_TEMPLATE_ID' => [
1430 'set' =>
'setFileUuid',
1431 'get' =>
'getFileUuid',
1434 'set' =>
'setPushMessage',
1435 'get' =>
'getPushMessage',
1438 'alias' =>
'PUSH_MESSAGE'
1441 'set' =>
'setPushParams',
1442 'get' =>
'getPushParams',
1445 'alias' =>
'PUSH_PARAMS'
1448 'set' =>
'setPushAppId',
1449 'get' =>
'getPushAppId',
1459 return MessageTable::class;
1484 $favoriteMessageService->setContext($this->context);
1486 return $favoriteMessageService->markMessageAsFavorite($this);
1492 $favoriteMessageService->setContext($this->context);
1494 return $favoriteMessageService->unmarkMessageAsFavorite($this);
1500 $pinService->setContext($this->context);
1502 return $pinService->pinMessage($this);
1508 $pinService->setContext($this->context);
1510 return $pinService->unpinMessage($this);
1518 $this->
getChat()->getDialogId(),
1520 $this->getContext()->getUserId(),
1524 if (!$isSuccessMark)
1535 $reminderService->setContext($this->context);
1537 return $reminderService->addMessageToReminders($this, $dateRemind);
1544 $hasAttach = mb_strpos($previewMessage,
'[ATTACH=') !==
false;
1554 foreach ($files as $file)
1557 $previewMessage .=
" [{$file->getDiskFile()->getName()}]";
1561 $previewMessage = preg_replace(
1562 "/\[ATTACH=([0-9]{1,})\]/i",
1566 $previewMessage = preg_replace(
1567 '#\-{54}.+?\-{54}#s',
1569 str_replace([
"#BR#"], [
" "], $previewMessage)
1571 $previewMessage = preg_replace(
1572 '/^(>>(.*)(\n)?)/mi',
1574 str_replace([
"#BR#"], [
" "], $previewMessage)
1577 if (!$hasFiles && !$hasAttach)
1579 if ($this->
getParams()->isSet(Params::ATTACH))
1581 $previewMessage .=
" [".Loc::getMessage(
'IM_MESSAGE_ATTACH').
"]";
1585 if ($messageSize !==
null)
1587 $dots = mb_strlen($previewMessage) >= $messageSize ?
'...' :
'';
1588 $previewMessage = mb_substr($previewMessage, 0, $messageSize - 1) . $dots;
1591 return $previewMessage;
1603 foreach ($this->
getFiles() as $file)
1605 $files[] = [
'name' => $file->getDiskFile()->getName()];
1608 $message = [
'MESSAGE' => $this->
getMessage(),
'FILES' => $files];
1609 $text = \CIMMessenger::PrepareParamsForPush($message);
1611 if ($messageSize !==
null)
1613 $dots = mb_strlen($text) >= $messageSize ?
'...' :
'';
1614 $text = mb_substr($text, 0, $messageSize - 1) . $dots;
1622 $userId ??= $this->getContext()->getUserId();
1625 return $this->
getId() && $chat->
hasAccess($userId) && $chat->getStartId($userId) <= $this->
getId();
1642 if ($this->
getParams()->isSet(Params::FORWARD_USER_ID))
1644 $userId = (int)$this->
getParams()->get(Params::FORWARD_USER_ID)->getValue();
1645 $userIds[$userId] = $userId;
1653 if (isset($this->userIdsFromMention))
1655 return $this->userIdsFromMention;
1658 $this->userIdsFromMention = [];
1659 if (preg_match_all(
"/\[USER=([0-9]+)( REPLACE)?](.*?)\[\/USER]/i", $this->
getParsedMessage(), $matches))
1661 foreach ($matches[1] as $userId)
1663 $this->userIdsFromMention[(int)$userId] = (
int)$userId;
1667 return $this->userIdsFromMention;
1682 $params->
get(Params::IS_DELETED)->setValue(
true);
1692 && !$this->
getParams()->isSet(Params::FILE_ID)
1693 && !$this->
getParams()->isSet(Params::KEYBOARD)
1694 && !$this->
getParams()->isSet(Params::ATTACH)
1704 $userIds = $chat->getRelations()->getUserIds();
1705 $implodeUserIds = implode(
':', $userIds);
1707 return "{$implodeUserIds}/{$this->getMessageId()}";
1710 return "{$chat->getDialogId()}/{$this->getMessageId()}";
1715 return "#{$this->getContextId()}";
1720 return $this->
getParams()->isSet(Params::FORWARD_ID)
1721 && $this->
getParams()->isSet(Params::FORWARD_CONTEXT_ID)
1733 'id' => $this->
getParams()->get(Params::FORWARD_CONTEXT_ID)->getValue(),
1734 'userId' => (int)$this->
getParams()->get(Params::FORWARD_USER_ID)->getValue(),
1747 'id' => $this->
getId(),
1749 'author_id' => $authorId,
1750 'date' => isset($dateCreate) ? $dateCreate->format(
'c') :
null,
1758 $rest = $onlyCommonRest;
1760 if (!isset($option[
'MESSAGE_ONLY_COMMON_FIELDS']) || $option[
'MESSAGE_ONLY_COMMON_FIELDS'] ===
false)
1762 $rest = array_merge($onlyCommonRest, [
1780 $urls = UrlItem::getUrlsFromText($this->
getMessage());
1781 foreach ($urls as $url)
1783 $metadata = UrlPreview::getMetadataByUrl($url,
true,
false);
1784 if ($metadata !==
false)
1786 $urlItem = UrlItem::initByMetadata($metadata);
1787 if ($urlItem->getId())
1789 $this->
getParams()->get(Params::URL_ID)->addValue($urlItem->getId());
1790 $this->
getParams()->get(Params::ATTACH)->addValue($urlItem->getUrlAttach());
1793 if ($urlItem->isStaticUrl())
1795 $staticUrl = [$url];
1796 if (mb_substr($url, -1) ==
'/')
1798 $staticUrl[] = mb_substr($url, 0, -1);
1800 $checkMessage = trim(str_replace($staticUrl,
'', $this->
getMessage()));
1802 if (empty($checkMessage))
1804 $this->
getParams()->get(Params::URL_ONLY)->setValue(
true);
1822 foreach ($dateConvertResult as $row)
1824 $this->
getParams()->get(Params::DATE_TEXT)->addValue($row->getText());
1825 $this->
getParams()->get(Params::DATE_TS)->addValue($row->getDate()->getTimestamp());
1842 $this->
getParams()->get(Params::LARGE_FONT)->setValue(
true);
1859 foreach ($results as $result)
1861 $dateText[] = $result->getText();
1862 $dateTs[] = $result->getDate()->getTimestamp();
1865 $url = UrlItem::getByMessage($this);
1868 if ($url->
getId() !==
null)
1870 $urlIds[] = $url->
getId();
1873 $isUrlOnly = $this->isUrlOnly($url);
1876 $this->
getParams()->get(Params::DATE_TEXT)->setValue($dateText);
1877 $this->
getParams()->get(Params::DATE_TS)->setValue($dateTs);
1878 $this->
getParams()->get(Params::URL_ID)->setValue($urlIds);
1879 $this->
getParams()->get(Params::URL_ONLY)->setValue($isUrlOnly);
1884 private function isUrlOnly(?
UrlItem $url): bool
1896 $messageWithoutUrl = str_replace($url->
getUrl(),
'', $this->getMessage() ??
'');
1898 return trim($messageWithoutUrl) ===
'';
1925 $service->setMode(Im\V2\
Message\Delete\DeleteService::MODE_SOFT);
1926 return $service->delete();
1932 $service->setMode(Im\V2\
Message\Delete\DeleteService::MODE_HARD);
1933 return $service->delete();
1939 $service->setMode(Im\V2\
Message\Delete\DeleteService::MODE_COMPLETE);
1940 return $service->delete();
static hasAccess($chatId)
static validate(string $uuid)
const EVENT_PRIVATE_SYSTEM
static unread($dialogId, $unread, $userId=null, ?int $markedId=null)
static getDateConverterParams($text)
static isOnlyEmoji($text)
static getInstance($userId=null)
static initByDiskFilesIds(array $diskFilesIds, ?int $chatId=null)
loadByMessageId(int $messageId)
markAsImportant(?bool $isImportant=true)
static getRestEntityName()
setEmailTemplate(?string $value)
setPrimaryId(int $primaryId)
autocompleteParams(bool $urlPreview)
markNotifyRead(?bool $value)
setNotifyType(?int $value)
toRestFormat(array $option=[])
setMessage(?string $value)
setFileUuid(?string $uuid)
setNotifyModule(?string $value)
getQuotedMessage(?int $messageSize=null)
setPushAppId(?string $message)
setNotifyLink(?string $value)
setPushParams(?array $params)
getPopupData(array $excludedList=[])
__construct($source=null)
getPreviewMessage(?int $messageSize=200)
processChangeAuthorId(int $authorId)
setUnread(bool $isUnread)
getParams(bool $disallowLazyLoad=false)
addToReminder(DateTime $dateRemind)
getAdditionalMessageIds()
setImportantFor(array $importantFor)
fillFiles(FileCollection $files)
setNotifyTag(?string $value)
getForPush(?int $messageSize=200)
setMessageId(int $messageId)
setViewedByOthers(bool $isViewedByOthers)
markNotifyAnswer(?bool $value)
Im V2 Message Reaction ReactionMessage $reactions
serializeNotifyButtons($value)
markNotifyFlash(?bool $value)
setNotifyEvent(?string $notifyEvent)
setNotifySubTag(?string $value)
hasAccess(?int $userId=null)
setMessageOut(?string $value)
setViewed(bool $isViewed)
setForwardUuid(?string $forwardUuid)
setReactions(Im\V2\Message\Reaction\ReactionMessage $reactions)
setPushMessage(?string $message)
setNotifyTitle(?string $value)
array $userIdsFromMention
addFile(Im\V2\Entity\File\FileItem $file)
unserializeNotifyButtons($value)
setAuthorId(int $authorId)
static mirrorDataEntityFields()
processChangeNotifyEvent(?string $notifyEvent)
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
setRegistry(Registry $registry)