Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PushFormat.php
1<?php
2
4
11use Bitrix\Im\V2\Common\ContextCustomer;
12
14{
15 use ContextCustomer;
16
17 private const MAX_CHAT_MESSAGE_TIME = 600;
18 private const DND_USER_STATUS = 'dnd';
19
20 public function formatPrivateMessage(Message $message, PrivateChat $chat): array
21 {
22 $fromUserId = $message->getAuthorId();
23 $toUserId = $chat->getCompanion()->getId();
24
25 $users = \CIMContactList::GetUserData([
26 'ID' => [$toUserId, $fromUserId],
27 'PHONES' => 'Y',
28 ]);
29
30 return [
31 'chatId' => $chat->getChatId(),
32 'dialogId' => 0,
33 'chat' => [],
34 'lines' => null,
35 'userInChat' => [],
36 'userBlockChat' => [],
37 'users' => !empty($users['users']) ? $users['users'] : null,
38 'message' => [
39 'id' => $message->getMessageId(),
40 'templateId' => $message->getUuid(),
41 'templateFileId' => $message->getFileUuid(),
42 'prevId' => $chat->getPrevMessageId(),
43 'chatId' => $chat->getChatId(),
44 'senderId' => $fromUserId,
45 'recipientId' => $toUserId,
46 'system' => ($message->isSystem() ? 'Y' : 'N'),
47 'date' => $message->getDateCreate() ?? DateTime::createFromTimestamp(time()),// DATE_CREATE
48 'text' => Text::parse($message->getMessage()),
49 'textLegacy' => Text::parseLegacyFormat($message->getMessage()),
50 'params' => $message->getParams()->toPullFormat(),
51 'counter' => 0,
52 'isImportant' => $message->isImportant(),
53 'importantFor' => $message->getImportantFor(),
54 ],
55 'files' => $message->getFilesDiskData(),
56 'notify' => true,
57 ];
58 }
59
60 public function formatStartRecordVoice(Chat $chat): array
61 {
62 $userId = $this->getContext()->getUserId();
63 return [
64 'module_id' => 'im',
65 'command' => 'startRecordVoice',
66 'expiry' => 60,
67 'params' => [
68 'dialogId' => $chat instanceof PrivateChat ? (string)$userId : $chat->getDialogId(),
69 'userId' => $userId,
70 'userName' => $this->getContext()->getUser()->getName()
71 ],
72 'extra' => \Bitrix\Im\Common::getPullExtra()
73 ];
74 }
75
76 public function formatMessageUpdate(Message $message): array
77 {
78 return [
79 'module_id' => 'im',
80 'command' => 'messageUpdate',
81 'params' => [
82 'id' => $message->getId(),
83 'type' => $message->getChat()->getType() === Chat::IM_TYPE_PRIVATE ? 'private' : 'chat',
84 'text' => $message->getParsedMessage(),
85 'textLegacy' => Text::parseLegacyFormat($message->getMessage()),
86 'chatId' => $message->getChatId(),
87 'senderId' => $message->getAuthorId(),
88 'params' => $message->getEnrichedParams()->toPullFormat(['IS_EDITED', 'URL_ID', 'ATTACH', 'DATE_TEXT', 'DATE_TS']),
89 ],
90 'extra' => \Bitrix\Im\Common::getPullExtra()
91 ];
92 }
93
94 public function validateDataForInform(Message $message, PrivateChat $chat): Result
95 {
96 $result = new Result();
97
98 $toUser = $chat->getCompanion();
99 $toUserStatus = $toUser->getStatus(true);
100
101 if (!($message->getAuthorId() === $this->getContext()->getUserId()))
102 {
104 }
105
106 if ($message->isViewedByOthers())
107 {
109 }
110
111 $timestampTimeNow = DateTime::createFromTimestamp(time())->getTimestamp();
112 if (!($timestampTimeNow - $message->getDateCreate()->getTimestamp() <= self::MAX_CHAT_MESSAGE_TIME))
113 {
115 }
116
117 if (!($toUserStatus === self::DND_USER_STATUS))
118 {
120 }
121
122 return $result;
123 }
124}
static parse($text, $params=Array())
Definition text.php:24
static parseLegacyFormat($text, $params=Array())
Definition text.php:90
getCompanion(?int $userId=null)
const IM_TYPE_PRIVATE
Definition Chat.php:55
formatPrivateMessage(Message $message, PrivateChat $chat)
validateDataForInform(Message $message, PrivateChat $chat)
formatMessageUpdate(Message $message)
getParams(bool $disallowLazyLoad=false)
Definition Message.php:313
static createFromTimestamp($timestamp)
Definition datetime.php:246