Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Message.php
1<?php
2
4
24
26{
27 protected const MAX_MESSAGES_COUNT = 100;
28 protected const MAX_MESSAGES_COUNT_FOR_FORWARD = 20;
29 protected const MESSAGE_ON_PAGE_COUNT = 50;
30 private const ALLOWED_FIELDS_UPDATE = [
31 'MESSAGE',
32 'ATTACH',
33 'KEYBOARD',
34 'MENU',
35 ];
36 private const ALLOWED_FIELDS_SEND = [
37 'MESSAGE',
38 'ATTACH',
39 'SYSTEM',
40 'KEYBOARD',
41 'MENU',
42 'URL_PREVIEW',
43 'SKIP_CONNECTOR',
44 'TEMPLATE_ID',
45 'REPLY_ID',
46 'BOT_ID',
47 ];
48
50 {
51 return new ExactParameter(
53 'message',
54 function ($className, int $id) {
55 return $this->getMessageById($id);
56 }
57 );
58 }
59
60 public function getAutoWiredParameters()
61 {
62 return array_merge([
64 MessageCollection::class,
65 'messages',
66 function($className, array $ids) {
67 if (count($ids) > self::MAX_MESSAGES_COUNT)
68 {
69 $this->addError(new MessageError(MessageError::TOO_MANY_MESSAGES));
70
71 return null;
72 }
73 $ids = array_map('intval', $ids);
74
75 return new MessageCollection($ids);
76 }
77 ),
78
80 MessageCollection::class,
81 'forwardMessages',
82 function($className, array $fields) {
83 $forwardIds = $fields['forwardIds'] ?? [];
84
85 if (empty($forwardIds))
86 {
87 return null;
88 }
89
90 if (count($forwardIds) > self::MAX_MESSAGES_COUNT)
91 {
92 $this->addError(new MessageError(MessageError::TOO_MANY_MESSAGES));
93
94 return null;
95 }
96
97 $forwardIds = array_map('intval', $forwardIds);
98 $messageCollection = new MessageCollection($forwardIds);
99 foreach ($messageCollection as $message)
100 {
101 $messageId = $message->getId();
102
103 $uuid = array_search($messageId, $forwardIds, true);
104 if ($uuid)
105 {
106 $message->setForwardUuid($uuid);
107
108 if ($message->getForwardUuid() === null)
109 {
110 $this->addError(new MessageError(MessageError::WRONG_UUID));
111
112 return null;
113 }
114 }
115 }
116
117 return $messageCollection;
118 }
119 ),
120 ], parent::getAutoWiredParameters());
121 }
122
123 public function configureActions()
124 {
125 return [
126 'disappear' => [
127 '+prefilters' => [
129 ]
130 ],
131 'read' => [
132 '+postfilters' => [
133 new UpdateStatus(),
134 ],
135 ],
136 'list' => [
137 '+postfilters' => [
138 new UpdateStatus(),
139 ],
140 ],
141 'getContext' => [
142 '+postfilters' => [
143 new UpdateStatus(),
144 ],
145 ],
146 'tail' => [
147 '+postfilters' => [
148 new UpdateStatus(),
149 ],
150 ],
151 'send' => [
152 '+prefilters' => [
153 new CheckMessageSend(),
154 ],
155 ],
156 ];
157 }
158
162 public function readAction(MessageCollection $messages): ?array
163 {
164 $readResult = Chat::getInstance($messages->getCommonChatId())->readMessages($messages);
165
166 if (!$readResult->isSuccess())
167 {
168 $this->addErrors($readResult->getErrors());
169
170 return null;
171 }
172
173 return $this->convertKeysToCamelCase($readResult->getResult());
174 }
175
179 public function tailViewersAction(\Bitrix\Im\V2\Message $message, array $filter = [], array $order = [], int $limit = 50): ?array
180 {
181 $viewFilter = [
182 'LAST_ID' => isset($filter['lastId']) ? (int)$filter['lastId'] : null,
183 'MESSAGE_ID' => $message->getId(),
184 ];
185 $viewOrder = ['ID' => $order['id'] ?? 'ASC'];
186 $viewLimit = $this->getLimit($limit);
187
188 $views = ViewCollection::find($viewFilter, $viewOrder, $viewLimit);
189
190 return $this->toRestFormat($views);
191 }
192
196 public function markAction(\Bitrix\Im\V2\Message $message): ?array
197 {
198 $markResult = $message->mark();
199
200 if (!$markResult->isSuccess())
201 {
202 $this->addErrors($markResult->getErrors());
203
204 return null;
205 }
206
207 return [];
208 }
209
213 public function listAction(Chat $chat, int $limit = self::MESSAGE_ON_PAGE_COUNT): ?array
214 {
215 $messageService = new MessageService($chat->getLoadContextMessage());
216 $messages = $messageService->getMessageContext($limit, \Bitrix\Im\V2\Message::REST_FIELDS)->getResult();
217
218 return $messageService->fillContextPaginationData($this->toRestFormat($messages), $messages, $limit);
219 }
220
224 public function getContextAction(\Bitrix\Im\V2\Message $message, int $range = self::MESSAGE_ON_PAGE_COUNT): ?array
225 {
226 $messageService = new MessageService($message);
227 $messages = $messageService->getMessageContext($range, \Bitrix\Im\V2\Message::REST_FIELDS)->getResult();
228
229 return $messageService->fillContextPaginationData($this->toRestFormat($messages), $messages, $range);
230 }
231
235 public function tailAction(Chat $chat, array $filter = [], array $order = [], int $limit = 50): ?array
236 {
237 [$messageFilter, $messageOrder] = $this->prepareParamsForTail($chat, $filter, $order);
238
239 return $this->getMessages($messageFilter, $messageOrder, $limit);
240 }
241
245 public function searchAction(Chat $chat, array $filter = [], array $order = [], int $limit = 50): ?array
246 {
247 [$messageFilter, $messageOrder] = $this->prepareParamsForSearch($chat, $filter, $order);
248
249 return $this->getMessages($messageFilter, $messageOrder, $limit);
250 }
251
255 public function pinAction(\Bitrix\Im\V2\Message $message): ?array
256 {
257 $pinResult = $message->pin();
258
259 if (!$pinResult->isSuccess())
260 {
261 $this->addErrors($pinResult->getErrors());
262
263 return null;
264 }
265
266 return [];
267 }
268
272 public function unpinAction(\Bitrix\Im\V2\Message $message): ?array
273 {
274 $unpinResult = $message->unpin();
275
276 if (!$unpinResult->isSuccess())
277 {
278 $this->addErrors($unpinResult->getErrors());
279
280 return null;
281 }
282
283 return [];
284 }
285
289 public function deleteAction(\Bitrix\Im\V2\Message $message): ?bool
290 {
291 $service = new DeleteService($message);
292 $service->setMode(DeleteService::MODE_AUTO);
293 $result = $service->delete();
294
295 if (!$result->isSuccess())
296 {
297 $this->addErrors($result->getErrors());
298
299 return null;
300 }
301
302 return true;
303 }
304
308 public function disappearAction(\Bitrix\Im\V2\Message $message, int $hours): ?bool
309 {
310 $deleteService = new DeleteService($message);
311 if ($deleteService->canDelete() < DeleteService::DELETE_HARD)
312 {
313 $this->addError(new MessageError(MessageError::MESSAGE_ACCESS_ERROR));
314
315 return null;
316 }
317
318 $result = DisappearService::disappearMessage($message, $hours);
319
320 if (!$result->isSuccess())
321 {
322 $this->addErrors($result->getErrors());
323
324 return null;
325 }
326
327 return true;
328 }
329
333 public function sendAction(
334 Chat $chat,
335 ?\CRestServer $restServer = null,
336 array $fields = [],
337 ?MessageCollection $forwardMessages = null
338 ): ?array
339 {
340 if (!empty($this->getErrors()))
341 {
342 return null;
343 }
344
345 $fields['message'] = $this->getRawValue('fields')['message'] ?? $fields['message'] ?? null;
346 $fields = $this->prepareMessageFields($fields, self::ALLOWED_FIELDS_SEND);
347 $result = (new SendingService())->prepareFields($chat, $fields, $forwardMessages, $restServer);
348
349 if (!$result->isSuccess())
350 {
351 $this->addErrors($result->getErrors());
352
353 return null;
354 }
355
356 $fields = $result->getResult();
357
358 $massageId = \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
371 return [
372 'id' => $massageId ?: null,
373 'uuidMap' => isset($forwardResult) ? $forwardResult->getResult() : []
374 ];
375 }
376
380 public function updateAction(
381 \Bitrix\Im\V2\Message $message,
382 array $fields = [],
383 string $urlPreview = 'Y',
384 int $botId = 0
385 ): ?bool
386 {
387 $fields['message'] = $this->getRawValue('fields')['message'] ?? $fields['message'] ?? null;
388 $fields = $this->prepareMessageFields($fields, self::ALLOWED_FIELDS_UPDATE);
389 $message->setBotId($botId);
390 $result = (new UpdateService($message))
391 ->setUrlPreview($this->convertCharToBool($urlPreview))
392 ->update($fields)
393 ;
394
395 if (!$result->isSuccess())
396 {
397 $this->addErrors($result->getErrors());
398
399 return null;
400 }
401
402 return true;
403 }
404
408 public function informAction(
409 \Bitrix\Im\V2\Message $message
410 ): ?array
411 {
412 $chat = $message->getChat();
413 if (!($chat instanceof Chat\PrivateChat))
414 {
415 $this->addError(new Chat\ChatError(Chat\ChatError::WRONG_TYPE));
416
417 return null;
418 }
419
420 $message->markAsImportant(true);
421
422 $result = (new PushFormat())->validateDataForInform($message, $chat);
423 if (!$result->isSuccess())
424 {
425 $this->addErrors($result->getErrors());
426
427 return null;
428 }
429
430 $pushService = new \Bitrix\Im\V2\Message\Inform\PushService();
431 $pushService->sendInformPushPrivateChat($chat, $message);
432
433 return ['result' => true];
434 }
435
439 public function deleteRichUrlAction(\Bitrix\Im\V2\Message $message, CurrentUser $user): ?array
440 {
441 if ((int)$user->getId() !== $message->getAuthorId())
442 {
443 $this->addError(new MessageError(MessageError::WRONG_SENDER));
444
445 return null;
446 }
447
448 (new \Bitrix\Im\V2\Message\Attach\AttachService())->deleteRichUrl($message);
449
450 return ['result' => true];
451 }
452
453 protected function prepareParamsForTail(Chat $chat, array $filter, array $order): array
454 {
455 $messageFilter = [];
456 $messageOrder = [];
457
458 if (isset($order['id']))
459 {
460 $messageOrder['ID'] = strtoupper($order['id']);
461 }
462
463 if (isset($filter['lastId']))
464 {
465 $messageFilter['LAST_ID'] = (int)$filter['lastId'];
466 }
467
468 $messageFilter['START_ID'] = $chat->getStartId();
469 $messageFilter['CHAT_ID'] = $chat->getChatId();
470
471 return [$messageFilter, $messageOrder];
472 }
473
474 protected function prepareParamsForSearch(Chat $chat, array $filter, array $order): array
475 {
476 [$messageFilter, $messageOrder] = $this->prepareParamsForTail($chat, $filter, $order);
477
478 if (isset($filter['searchMessage']) && is_string($filter['searchMessage']))
479 {
480 $messageFilter['SEARCH_MESSAGE'] = trim($filter['searchMessage']);
481 }
482
483 return [$messageFilter, $messageOrder];
484 }
485
486 protected function getMessages(array $filter, array $order, int $limit): array
487 {
488 $messages = MessageCollection::find(
489 $filter,
490 $order,
491 $this->getLimit($limit),
492 null,
493 \Bitrix\Im\V2\Message::REST_FIELDS
494 );
495
496 $rest = $this->toRestFormat($messages);
497 //todo: refactor. Change to popup data.
498 $rest['hasNextPage'] = $messages->count() >= $limit;
499
500 return $rest;
501 }
502
503 private function sendForwardMessages(Chat $chat, MessageCollection $messages): Result
504 {
505 $result = new Result();
506
507 if ($messages->count() > self::MAX_MESSAGES_COUNT_FOR_FORWARD)
508 {
509 return $result->addError(new MessageError(MessageError::TOO_MANY_MESSAGES));
510 }
511
512 $service = new ForwardService($chat);
513 $result = $service->createMessages($messages);
514
515 if (!$result->hasResult())
516 {
517 return $result->addErrors($result->getErrors());
518 }
519
520 return $result;
521 }
522
523 private function prepareMessageFields(array $fields, array $whiteList): array
524 {
525 $converter = new Converter(Converter::TO_SNAKE | Converter::TO_UPPER | Converter::KEYS);
526 $fields = $converter->process($fields);
527
528 return $this->checkWhiteList($fields, $whiteList);
529 }
530}
toRestFormat(RestConvertible ... $entities)
convertCharToBool(string $char, bool $default=false)
informAction(\Bitrix\Im\V2\Message $message)
Definition Message.php:408
tailAction(Chat $chat, array $filter=[], array $order=[], int $limit=50)
Definition Message.php:235
getMessages(array $filter, array $order, int $limit)
Definition Message.php:486
listAction(Chat $chat, int $limit=self::MESSAGE_ON_PAGE_COUNT)
Definition Message.php:213
sendAction(Chat $chat, ?\CRestServer $restServer=null, array $fields=[], ?MessageCollection $forwardMessages=null)
Definition Message.php:333
readAction(MessageCollection $messages)
Definition Message.php:162
tailViewersAction(\Bitrix\Im\V2\Message $message, array $filter=[], array $order=[], int $limit=50)
Definition Message.php:179
getContextAction(\Bitrix\Im\V2\Message $message, int $range=self::MESSAGE_ON_PAGE_COUNT)
Definition Message.php:224
prepareParamsForSearch(Chat $chat, array $filter, array $order)
Definition Message.php:474
updateAction(\Bitrix\Im\V2\Message $message, array $fields=[], string $urlPreview='Y', int $botId=0)
Definition Message.php:380
markAction(\Bitrix\Im\V2\Message $message)
Definition Message.php:196
deleteAction(\Bitrix\Im\V2\Message $message)
Definition Message.php:289
disappearAction(\Bitrix\Im\V2\Message $message, int $hours)
Definition Message.php:308
pinAction(\Bitrix\Im\V2\Message $message)
Definition Message.php:255
searchAction(Chat $chat, array $filter=[], array $order=[], int $limit=50)
Definition Message.php:245
deleteRichUrlAction(\Bitrix\Im\V2\Message $message, CurrentUser $user)
Definition Message.php:439
unpinAction(\Bitrix\Im\V2\Message $message)
Definition Message.php:272
prepareParamsForTail(Chat $chat, array $filter, array $order)
Definition Message.php:453
static disappearMessage(Message $message, int $hours)