1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Message.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Controller\Chat;
4
5use Bitrix\Im\V2\Analytics\MessageAnalytics;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Controller\BaseController;
8use Bitrix\Im\V2\Controller\Filter\CheckActionAccess;
9use Bitrix\Im\V2\Controller\Filter\PlatformContext;
10use Bitrix\Im\V2\Controller\Filter\DiskQuickAccessGrantor;
11use Bitrix\Im\V2\Entity\View\ViewCollection;
12use Bitrix\Im\V2\Message\Delete\DeletionMode;
13use Bitrix\Im\V2\Message\Delete\DisappearService;
14use Bitrix\Im\V2\Message\Forward\ForwardService;
15use Bitrix\Im\V2\Message\MessageError;
16use Bitrix\Im\V2\Message\PushFormat;
17use Bitrix\Im\V2\Message\Send\SendingService;
18use Bitrix\Im\V2\Message\Update\UpdateService;
19use Bitrix\Im\V2\Message\Delete\DeleteService;
20use Bitrix\Im\V2\MessageCollection;
21use Bitrix\Im\V2\Message\MessageService;
22use Bitrix\Im\V2\Permission\Action;
23use Bitrix\Im\V2\Result;
24use Bitrix\Main\Engine\AutoWire\ExactParameter;
25use Bitrix\Main\Engine\CurrentUser;
26
28{
29 protected const MAX_MESSAGES_COUNT = 100;
30 protected const MESSAGE_ON_PAGE_COUNT = 50;
31 private const ALLOWED_FIELDS_UPDATE = [
32 'MESSAGE',
33 'ATTACH',
34 'KEYBOARD',
35 'MENU',
36 ];
37 private const ALLOWED_FIELDS_SEND = [
38 'MESSAGE',
39 'ATTACH',
40 'SYSTEM',
41 'KEYBOARD',
42 'MENU',
43 'URL_PREVIEW',
44 'SKIP_CONNECTOR',
45 'TEMPLATE_ID',
46 'REPLY_ID',
47 'BOT_ID',
48 'COPILOT',
49 'SILENT_CONNECTOR',
50 ];
51
53 {
54 return new ExactParameter(
55 \Bitrix\Im\V2\Message::class,
56 'message',
57 function ($className, int $id) {
58 return new \Bitrix\Im\V2\Message($id);
59 }
60 );
61 }
62
63 public function getAutoWiredParameters()
64 {
65 return array_merge([
67 MessageCollection::class,
68 'messages',
69 function($className, array $ids) {
70 return $this->getMessagesByIds($ids);
71 }
72 ),
73
75 MessageCollection::class,
76 'forwardMessages',
77 function($className, array $fields) {
78 $forwardIds = $fields['forwardIds'] ?? [];
79
80 if (empty($forwardIds))
81 {
82 return null;
83 }
84
85 if (count($forwardIds) > self::MAX_MESSAGES_COUNT)
86 {
87 $this->addError(new MessageError(MessageError::TOO_MANY_MESSAGES));
88
89 return null;
90 }
91
92 $forwardIds = array_map('intval', $forwardIds);
93 $messageCollection = new MessageCollection($forwardIds);
94 foreach ($messageCollection as $message)
95 {
96 $messageId = $message->getId();
97
98 $uuid = array_search($messageId, $forwardIds, true);
99 if ($uuid)
100 {
101 $message->setForwardUuid($uuid);
102
103 if ($message->getForwardUuid() === null)
104 {
105 $this->addError(new MessageError(MessageError::WRONG_UUID));
106
107 return null;
108 }
109 }
110 }
111
112 return $messageCollection;
113 }
114 ),
115 ], parent::getAutoWiredParameters());
116 }
117
118 public function configureActions()
119 {
120 return [
121 'send' => [
122 '+prefilters' => [
123 new CheckActionAccess(Action::Send),
124 new PlatformContext(),
125 ],
126 ],
127 'update' => [
128 '+prefilters' => [
129 new PlatformContext(),
130 ],
131 ],
132 'pin' => [
133 '+prefilters' => [
134 new CheckActionAccess(Action::PinMessage),
135 ],
136 ],
137 'unpin' => [
138 '+prefilters' => [
139 new CheckActionAccess(Action::PinMessage),
140 ],
141 ],
142 'list' => [
143 '+prefilters' => [
145 ],
146 ],
147 ];
148 }
149
154 {
155 $readResult = Chat::getInstance($messages->getCommonChatId())->readMessages($messages);
156
157 if (!$readResult->isSuccess())
158 {
159 $this->addErrors($readResult->getErrors());
160
161 return null;
162 }
163
164 return $this->convertKeysToCamelCase($readResult->getResult());
165 }
166
170 public function tailViewersAction(\Bitrix\Im\V2\Message $message, array $filter = [], array $order = [], int $limit = 50): ?array
171 {
172 $viewFilter = [
173 'LAST_ID' => isset($filter['lastId']) ? (int)$filter['lastId'] : null,
174 'MESSAGE_ID' => $message->getId(),
175 ];
176 $viewOrder = ['ID' => $order['id'] ?? 'ASC'];
177 $viewLimit = $this->getLimit($limit);
178
179 $views = ViewCollection::find($viewFilter, $viewOrder, $viewLimit);
180
181 return $this->toRestFormat($views);
182 }
183
188 {
189 $markResult = $message->mark();
190
191 if (!$markResult->isSuccess())
192 {
193 $this->addErrors($markResult->getErrors());
194
195 return null;
196 }
197
198 return [];
199 }
200
204 public function listAction(Chat $chat, int $limit = self::MESSAGE_ON_PAGE_COUNT, string $ignoreMark = 'N'): ?array
205 {
206 $messageService = new MessageService($chat->getLoadContextMessage($this->convertCharToBool($ignoreMark)));
207 $messages = $messageService->getMessageContext($limit, \Bitrix\Im\V2\Message::REST_FIELDS)->getResult();
208
209 return $messageService->fillContextPaginationData($this->toRestFormat($messages), $messages, $limit);
210 }
211
215 public function getContextAction(\Bitrix\Im\V2\Message $message, int $range = self::MESSAGE_ON_PAGE_COUNT): ?array
216 {
217 $messageService = new MessageService($message);
218 $messages = $messageService->getMessageContext($range, \Bitrix\Im\V2\Message::REST_FIELDS)->getResult();
219
220 return $messageService->fillContextPaginationData($this->toRestFormat($messages), $messages, $range);
221 }
222
226 public function tailAction(Chat $chat, array $filter = [], array $order = [], int $limit = 50): ?array
227 {
228 [$messageFilter, $messageOrder] = $this->prepareParamsForTail($chat, $filter, $order);
229
230 return $this->getMessages($messageFilter, $messageOrder, $limit);
231 }
232
236 public function searchAction(Chat $chat, array $filter = [], array $order = [], int $limit = 50): ?array
237 {
238 [$messageFilter, $messageOrder] = $this->prepareParamsForSearch($chat, $filter, $order);
239
240 return $this->getMessages($messageFilter, $messageOrder, $limit);
241 }
242
247 {
248 $pinResult = $message->pin();
249
250 if (!$pinResult->isSuccess())
251 {
252 $this->addErrors($pinResult->getErrors());
253
254 return null;
255 }
256
257 return [];
258 }
259
264 {
265 $unpinResult = $message->unpin();
266
267 if (!$unpinResult->isSuccess())
268 {
269 $this->addErrors($unpinResult->getErrors());
270
271 return null;
272 }
273
274 return [];
275 }
276
281 {
283 {
284 $this->addError(new MessageError(MessageError::TOO_MANY_MESSAGES));
285
286 return null;
287 }
288
290 $result = $service->delete();
291
292 if (!$result->isSuccess())
293 {
294 $this->addErrors($result->getErrors());
295
296 return null;
297 }
298
299 return true;
300 }
301
305 public function disappearAction(\Bitrix\Im\V2\Message $message, int $hours): ?bool
306 {
308
309 if ($deleteService->canDelete($message->getId()) !== DeletionMode::Complete)
310 {
311 $this->addError(new MessageError(MessageError::ACCESS_DENIED));
312
313 return null;
314 }
315
316 $result = DisappearService::disappearMessage($message, $hours);
317
318 if (!$result->isSuccess())
319 {
320 $this->addErrors($result->getErrors());
321
322 return null;
323 }
324
325 return true;
326 }
327
331 public function sendAction(
332 Chat $chat,
333 ?\CRestServer $restServer = null,
334 array $fields = [],
335 ?MessageCollection $forwardMessages = null
336 ): ?array
337 {
338 if (!empty($this->getErrors()))
339 {
340 return null;
341 }
342
343 $fields['message'] = $this->getRawValue('fields')['message'] ?? $fields['message'] ?? null;
344 $fields = $this->prepareFields($fields, self::ALLOWED_FIELDS_SEND);
345 $result = (new SendingService())->prepareFields($chat, $fields, $forwardMessages, $restServer);
346
347 if (!$result->isSuccess())
348 {
349 $this->addErrors($result->getErrors());
350
351 return null;
352 }
353
354 $fields = $result->getResult();
355 $fields['SKIP_USER_CHECK'] = 'Y';
356 $fields['WAIT_FULL_EXECUTION'] = 'N';
357
358 $messageId = \CIMMessenger::Add($fields);
359
360 if (isset($forwardMessages) && $forwardMessages->count() > 0)
361 {
362 $forwardResult = $this->sendForwardMessages($chat, $forwardMessages);
363 if (!$forwardResult->isSuccess())
364 {
365 $this->addErrors($forwardResult->getErrors());
366
367 return null;
368 }
369
370 foreach ($forwardMessages as $message)
371 {
372 (new MessageAnalytics($message))->addShareMessage($chat);
373 }
374 }
375
376 if ($messageId === false && !isset($forwardResult))
377 {
378 $this->addError(new MessageError(MessageError::SENDING_FAILED));
379
380 return null;
381 }
382
383 return [
384 'id' => $messageId ?: null,
385 'uuidMap' => isset($forwardResult) ? $forwardResult->getResult() : []
386 ];
387 }
388
392 public function updateAction(
394 ?\CRestServer $restServer = null,
395 array $fields = [],
396 string $urlPreview = 'Y',
397 int $botId = 0
398 ): ?bool
399 {
400 $fields['message'] = $this->getRawValue('fields')['message'] ?? $fields['message'] ?? null;
401 $fields = $this->prepareFields($fields, self::ALLOWED_FIELDS_UPDATE);
402 $message->setBotId($botId);
404 ->setUrlPreview($this->convertCharToBool($urlPreview))
405 ->update($fields)
406 ;
407
408 if (!$result->isSuccess())
409 {
410 $this->addErrors($result->getErrors());
411
412 return null;
413 }
414
415 return true;
416 }
417
421 public function informAction(
423 ): ?array
424 {
425 $chat = $message->getChat();
426 if (!($chat instanceof Chat\PrivateChat))
427 {
428 $this->addError(new Chat\ChatError(Chat\ChatError::WRONG_CHAT_TYPE));
429
430 return null;
431 }
432
433 $message->markAsImportant(true);
434
435 $result = (new PushFormat($message))->validateDataForInform();
436 if (!$result->isSuccess())
437 {
438 $this->addErrors($result->getErrors());
439
440 return null;
441 }
442
443 $pushService = new \Bitrix\Im\V2\Message\Inform\PushService();
444 $pushService->sendInformPushPrivateChat($message);
445
446 return ['result' => true];
447 }
448
453 {
454 if ((int)$user->getId() !== $message->getAuthorId())
455 {
456 $this->addError(new MessageError(MessageError::WRONG_SENDER));
457
458 return null;
459 }
460
461 (new \Bitrix\Im\V2\Message\Attach\AttachService())->deleteRichUrl($message);
462
463 return ['result' => true];
464 }
465
467 {
468 $messageFilter = [];
469 $messageOrder = [];
470
471 if (isset($order['id']))
472 {
473 $messageOrder['ID'] = strtoupper($order['id']);
474 }
475
476 if (isset($filter['lastId']))
477 {
478 $messageFilter['LAST_ID'] = (int)$filter['lastId'];
479 }
480
481 $messageFilter['START_ID'] = $chat->getStartId();
482 $messageFilter['CHAT_ID'] = $chat->getChatId();
483
484 return [$messageFilter, $messageOrder];
485 }
486
488 {
489 [$messageFilter, $messageOrder] = $this->prepareParamsForTail($chat, $filter, $order);
490
491 if (isset($filter['searchMessage']) && is_string($filter['searchMessage']))
492 {
493 $messageFilter['SEARCH_MESSAGE'] = trim($filter['searchMessage']);
494 }
495
496 return [$messageFilter, $messageOrder];
497 }
498
499 protected function getMessages(array $filter, array $order, int $limit): array
500 {
502 $filter,
503 $order,
504 $this->getLimit($limit),
505 null,
506 \Bitrix\Im\V2\Message::REST_FIELDS
507 );
508
509 $rest = $this->toRestFormat($messages);
510 $wasFilteredByTariffRestrictions = $rest['tariffRestrictions']['isHistoryLimitExceeded'] ?? false;
511 //todo: refactor. Change to popup data.
512 $rest['hasNextPage'] = !$wasFilteredByTariffRestrictions && $messages->count() >= $limit;
513
514 return $rest;
515 }
516
517 private function sendForwardMessages(Chat $chat, MessageCollection $messages): Result
518 {
519 $result = new Result();
520
522 {
523 return $result->addError(new MessageError(MessageError::TOO_MANY_MESSAGES));
524 }
525
526 $service = new ForwardService($chat);
527 $result = $service->createMessages($messages);
528
529 if (!$result->hasResult())
530 {
531 return $result->addErrors($result->getErrors());
532 }
533
534 return $result;
535 }
536}
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
const WRONG_CHAT_TYPE
Определения ChatError.php:34
toRestFormat(RestConvertible ... $entities)
Определения BaseController.php:108
convertCharToBool(string $char, bool $default=false)
Определения BaseController.php:220
prepareFields(array $fields, array $whiteList)
Определения BaseController.php:255
informAction(\Bitrix\Im\V2\Message $message)
Определения Message.php:421
tailAction(Chat $chat, array $filter=[], array $order=[], int $limit=50)
Определения Message.php:226
getMessages(array $filter, array $order, int $limit)
Определения Message.php:499
const MAX_MESSAGES_COUNT
Определения Message.php:29
updateAction(\Bitrix\Im\V2\Message $message, ?\CRestServer $restServer=null, array $fields=[], string $urlPreview='Y', int $botId=0)
Определения Message.php:392
getPrimaryAutoWiredParameter()
Определения Message.php:52
sendAction(Chat $chat, ?\CRestServer $restServer=null, array $fields=[], ?MessageCollection $forwardMessages=null)
Определения Message.php:331
readAction(MessageCollection $messages)
Определения Message.php:153
tailViewersAction(\Bitrix\Im\V2\Message $message, array $filter=[], array $order=[], int $limit=50)
Определения Message.php:170
getContextAction(\Bitrix\Im\V2\Message $message, int $range=self::MESSAGE_ON_PAGE_COUNT)
Определения Message.php:215
const MESSAGE_ON_PAGE_COUNT
Определения Message.php:30
prepareParamsForSearch(Chat $chat, array $filter, array $order)
Определения Message.php:487
markAction(\Bitrix\Im\V2\Message $message)
Определения Message.php:187
deleteAction(\Bitrix\Im\V2\MessageCollection $messages)
Определения Message.php:280
disappearAction(\Bitrix\Im\V2\Message $message, int $hours)
Определения Message.php:305
listAction(Chat $chat, int $limit=self::MESSAGE_ON_PAGE_COUNT, string $ignoreMark='N')
Определения Message.php:204
pinAction(\Bitrix\Im\V2\Message $message)
Определения Message.php:246
searchAction(Chat $chat, array $filter=[], array $order=[], int $limit=50)
Определения Message.php:236
deleteRichUrlAction(\Bitrix\Im\V2\Message $message, CurrentUser $user)
Определения Message.php:452
unpinAction(\Bitrix\Im\V2\Message $message)
Определения Message.php:263
prepareParamsForTail(Chat $chat, array $filter, array $order)
Определения Message.php:466
static find(array $filter, array $order=['ID'=> 'ASC'], ?int $limit=null, ?Context $context=null)
Определения ViewCollection.php:13
static getInstanceByMessage(Message $message)
Определения DeleteService.php:73
static getMultipleActionMessageLimit()
Определения MessageService.php:123
static find(array $filter, array $order, ?int $limit=null, ?Context $context=null, array $select=[])
Определения MessageCollection.php:68
static getInstance()
Определения application.php:98
Определения result.php:20
Определения rest.php:24
$hours
Определения cron_html_pages.php:15
</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
$filter
Определения iblock_catalog_list.php:54
Определения Uuid.php:3
Определения ActionUuid.php:3
$user
Определения mysql_to_pgsql.php:33
getErrors()
Определения errorableimplementation.php:34
$order
Определения payment.php:8
$service
Определения payment.php:18
$message
Определения payment.php:8
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$messages
Определения template.php:8
$fields
Определения yandex_run.php:501