1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
PushFormat.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Message;
4
5use Bitrix\Im\Text;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Chat\PrivateChat;
8use Bitrix\Im\V2\Entity\User\NullUser;
9use Bitrix\Im\V2\Entity\User\User;
10use Bitrix\Im\V2\Message;
11use Bitrix\Im\V2\MessageCollection;
12use Bitrix\Im\V2\Result;
13use Bitrix\Main\Engine\Response\Converter;
14use Bitrix\Main\Loader;
15use Bitrix\Main\Type\DateTime;
16use Bitrix\Im\V2\Common\ContextCustomer;
17
19{
20 use ContextCustomer;
21
22 protected Message $message;
23 private const MAX_CHAT_MESSAGE_TIME = 600;
24 private const DND_USER_STATUS = 'dnd';
25
26 public function __construct(Message $message)
27 {
28 $this->message = $message;
29 }
30
31 public function format(): array
32 {
34 $chat = $this->message->getChat();
35 $chatLegacyFormat = $chat->toPullFormat();
36 $users = $this->getUsers();
37 $notify = !empty($chat->getRecentSections());
38
39 return [
40 'chatId' => $chat->getChatId(),
41 'dateLastActivity' => $message->getDateCreate(), // todo test it
42 'dialogId' => $chat instanceof PrivateChat ? 0 : $chat->getDialogId(),
43 'chat' => $chat instanceof PrivateChat ? [] : [$chat->getId() => $chatLegacyFormat],
44 'copilot' => $message->getCopilotData(),
45 'lines' => $chat instanceof Chat\OpenLineChat ? $chat->getLineData() : null,
46 'multidialog' => $chat->getMultidialogData(),
47 'userInChat' => $this->getUserInChat(),
48 'userBlockChat' => [$chat->getId() => $chatLegacyFormat['mute_list'] ?? []],
49 'users' => $users ?: null,
50 'message' => [
51 'id' => $message->getMessageId(),
52 'templateId' => $message->getUuid(),
53 'templateFileId' => $message->getFileUuid(),
54 'prevId' => $message->getPrevId(),
55 'chatId' => $chat->getChatId(),
56 'senderId' => $message->getActionContextUserId(),
57 'recipientId' => $chat->getDialogId(),
58 'system' => ($message->isSystem() ? 'Y': 'N'),
59 'date' => DateTime::createFromTimestamp(time()), // DATE_CREATE
60 'text' => Text::parse($message->getMessage()),
61 'textLegacy' => Text::parseLegacyFormat($message->getMessage()),
62 'params' => $message->getEnrichedParams()->toPullFormat(),
63 'counter' => 0,
64 'isImportant' => $message->isImportant(),
65 'importantFor' => $message->getImportantFor(),
66 'additionalEntities' => $this->getAdditionalEntities(),
67 'forward' => $message->getForwardInfo(),
68 ],
69 'counterType' => $chat->getCounterType()->value,
70 'recentConfig' => $chat->getRecentConfig()->toPullFormat(),
71 'files' => $message->getFiles()->toRestFormat(['IDS_AS_KEY' => true]),
72 'notify' => $notify,
73 'messagesAutoDeleteConfigs' => $chat->getMessageAutoDeleteConfigs()->toRestFormat(),
74 ];
75 }
76
77 protected function getUserInChat(): array
78 {
79 if ($this->message->getChat() instanceof PrivateChat)
80 {
81 return [];
82 }
83
84 $userIds = $this->message->getChat()->getRelations()->getUserIds();
85
86 return [$this->message->getChatId() => array_values($userIds)];
87 }
88
89 protected function getUsers(): array
90 {
91 $pushParams = $this->message->getPushParams();
92 $extraParamContext = $pushParams['CONTEXT'] ?? null;
93
94 if ($extraParamContext === Chat::ENTITY_TYPE_LIVECHAT && Loader::includeModule('imopenlines'))
95 {
96 return $this->getUserForLiveChat();
97 }
98
99 $contextUserId = $this->message->getActionContextUserId();
100 $contextUser = User::getInstance($contextUserId);
101
102 if ($this->message->getChat() instanceof PrivateChat)
103 {
104 $toUser = $this->message->getChat()->getCompanion($contextUserId);
105
106 return $this->getUsersLegacyFormat([$contextUser, $toUser]);
107 }
108
109 return $this->getUsersLegacyFormat([$contextUser]);
110 }
111
112 protected function getUserForLiveChat(): array
113 {
114 [$lineId, $userId] = explode('|', $this->message->getChat()->getEntityId() ?? '');
115 $userCode = 'livechat|' . $lineId . '|' . $this->message->getChatId() . '|' . $userId;
116 $operatorInfo = \Bitrix\ImOpenLines\Connector::getOperatorInfo(
117 $this->message->getPushParams()['LINE_ID'],
118 $this->message->getAuthor()?->getId() ?? 0,
119 $userCode
120 );
121
122 return [$this->message->getAuthor()?->getId() => $operatorInfo];
123 }
124
129 protected function getUsersLegacyFormat(array $users): array
130 {
131 $result = [];
132 foreach ($users as $user)
133 {
134 if (!$user)
135 {
136 continue;
137 }
138
139 $userLegacy = $this->getUserLegacyFormat($user);
140
141 if ($userLegacy)
142 {
143 $result[$user?->getId()] = $userLegacy;
144 }
145 }
146
147 return $result;
148 }
149
150 protected function getUserLegacyFormat(User $user): ?array
151 {
152 if ($user instanceof NullUser)
153 {
154 return null;
155 }
156
157 $converter = new Converter(Converter::KEYS | Converter::TO_LOWER | Converter::RECURSIVE);
158 $result = $user->getArray();
159 $result = $converter->process($result);
160 $services = $user->getServices();
161 $result['avatar_id'] = $user->getAvatarId();
162 $result['phone_device'] = $user->getPhoneDevice();
163 $result['services'] = empty($services) ? null : $services;
164 $result['profile'] = \CIMContactList::GetUserPath($user->getId());
165
166 return $result;
167 }
168
169 protected function getAdditionalEntities(): array
170 {
172 $additionalEntitiesAdapter = new \Bitrix\Im\V2\Rest\RestAdapter();
173 $additionalPopupData = new \Bitrix\Im\V2\Rest\PopupData([]);
174
175 if ($message->getParams()->isSet(Message\Params::FORWARD_CONTEXT_ID))
176 {
177 $additionalUserId = (int)$message->getParams()->get(Message\Params::FORWARD_USER_ID)->getValue();
178 $additionalPopupData->add(new \Bitrix\Im\V2\Entity\User\UserPopupItem([$additionalUserId]));
179 }
180
181 $replyIds = [];
182 if ($message->getParams()->isSet(Message\Params::REPLY_ID))
183 {
184 $replyIds[] = (int)$message->getParams()->get(Message\Params::REPLY_ID)->getValue();
185 }
186 $messages = new MessageCollection($replyIds);
187 $messages->fillAllForRest();
188 $additionalEntitiesAdapter->addEntities($messages);
189 $additionalEntitiesAdapter->setAdditionalPopupData($additionalPopupData);
190 return $additionalEntitiesAdapter->toRestFormat([
191 'WITHOUT_OWN_REACTIONS' => true,
192 'MESSAGE_ONLY_COMMON_FIELDS' => true,
193 ]);
194 }
195
196 public static function formatStartRecordVoice(Chat $chat): array
197 {
198 $userId = $chat->getContext()->getUserId();
199 return [
200 'module_id' => 'im',
201 'command' => 'startRecordVoice',
202 'expiry' => 60,
203 'params' => [
204 'dialogId' => $chat instanceof PrivateChat ? (string)$userId : $chat->getDialogId(),
205 'userId' => $userId,
206 'userName' => $chat->getContext()->getUser()->getName()
207 ],
209 ];
210 }
211
212 public function formatMessageUpdate(): array
213 {
215
216 return [
217 'module_id' => 'im',
218 'command' => 'messageUpdate',
219 'params' => [
220 'id' => $message->getId(),
221 'type' => $message->getChat()->getType() === Chat::IM_TYPE_PRIVATE ? 'private' : 'chat',
222 'text' => $message->getParsedMessage(),
223 'textLegacy' => Text::parseLegacyFormat($message->getMessage()),
224 'chatId' => $message->getChatId(),
225 'senderId' => $message->getAuthorId(),
226 'params' => $message->getEnrichedParams()->toPullFormat(['IS_EDITED', 'URL_ID', 'ATTACH', 'DATE_TEXT', 'DATE_TS']),
227 ],
229 ];
230 }
231
232 public function validateDataForInform(): Result
233 {
234 $result = new Result();
235
237 $toUser = $message->getChat()->getCompanion();
238 $toUserStatus = $toUser->getStatus(true);
239
240 if (!($message->getAuthorId() === $this->getContext()->getUserId()))
241 {
243 }
244
245 if ($message->isViewedByOthers())
246 {
248 }
249
250 $timestampTimeNow = DateTime::createFromTimestamp(time())->getTimestamp();
251 if (!($timestampTimeNow - $message->getDateCreate()->getTimestamp() <= self::MAX_CHAT_MESSAGE_TIME))
252 {
254 }
255
256 if (!($toUserStatus === self::DND_USER_STATUS))
257 {
259 }
260
261 return $result;
262 }
263}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
const ENTITY_TYPE_LIVECHAT
Определения alias.php:13
static getPullExtra()
Определения common.php:127
static parse($text, $params=Array())
Определения text.php:28
static parseLegacyFormat($text, $params=Array())
Определения text.php:94
static getInstance($userId=null)
Определения user.php:45
getDialogId(?int $contextUserId=null)
Определения PrivateChat.php:94
const INFORM_TIMEOUT_ERROR
Определения MessageError.php:33
const INFORM_USER_STATUS_ERROR
Определения MessageError.php:34
const INFORM_USER_CONTEXT_ERROR
Определения MessageError.php:31
const INFORM_VIEWED_ERROR
Определения MessageError.php:32
const REPLY_ID
Определения Params.php:68
const FORWARD_CONTEXT_ID
Определения Params.php:65
const FORWARD_USER_ID
Определения Params.php:66
getUserLegacyFormat(User $user)
Определения PushFormat.php:150
static formatStartRecordVoice(Chat $chat)
Определения PushFormat.php:196
__construct(Message $message)
Определения PushFormat.php:26
Message $message
Определения PushFormat.php:22
getUsersLegacyFormat(array $users)
Определения PushFormat.php:129
getForwardInfo()
Определения Message.php:1786
getChat()
Определения Message.php:867
Определения result.php:20
</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
Определения Uuid.php:3
Определения ActionUuid.php:3
Определения ufield.php:9
$user
Определения mysql_to_pgsql.php:33
$messages
Определения template.php:8