30 private const ALLOWED_FIELDS_UPDATE = [
36 private const ALLOWED_FIELDS_SEND = [
54 function ($className,
int $id) {
64 MessageCollection::class,
66 function($className, array $ids) {
67 if (count($ids) > self::MAX_MESSAGES_COUNT)
73 $ids = array_map(
'intval', $ids);
80 MessageCollection::class,
82 function($className, array $fields) {
83 $forwardIds = $fields[
'forwardIds'] ?? [];
85 if (empty($forwardIds))
90 if (count($forwardIds) > self::MAX_MESSAGES_COUNT)
97 $forwardIds = array_map(
'intval', $forwardIds);
99 foreach ($messageCollection as $message)
101 $messageId = $message->getId();
103 $uuid = array_search($messageId, $forwardIds,
true);
106 $message->setForwardUuid($uuid);
108 if ($message->getForwardUuid() ===
null)
117 return $messageCollection;
120 ], parent::getAutoWiredParameters());
164 $readResult = Chat::getInstance($messages->
getCommonChatId())->readMessages($messages);
166 if (!$readResult->isSuccess())
168 $this->
addErrors($readResult->getErrors());
182 'LAST_ID' => isset($filter[
'lastId']) ? (int)$filter[
'lastId'] :
null,
183 'MESSAGE_ID' => $message->getId(),
185 $viewOrder = [
'ID' => $order[
'id'] ??
'ASC'];
186 $viewLimit = $this->
getLimit($limit);
188 $views = ViewCollection::find($viewFilter, $viewOrder, $viewLimit);
198 $markResult = $message->mark();
200 if (!$markResult->isSuccess())
202 $this->
addErrors($markResult->getErrors());
213 public function listAction(
Chat $chat,
int $limit = self::MESSAGE_ON_PAGE_COUNT): ?array
215 $messageService =
new MessageService($chat->getLoadContextMessage());
216 $messages = $messageService->getMessageContext($limit, \
Bitrix\Im\V2\Message::REST_FIELDS)->getResult();
218 return $messageService->fillContextPaginationData($this->
toRestFormat($messages), $messages, $limit);
227 $messages = $messageService->getMessageContext($range, \
Bitrix\Im\V2\Message::REST_FIELDS)->getResult();
229 return $messageService->fillContextPaginationData($this->
toRestFormat($messages), $messages, $range);
235 public function tailAction(
Chat $chat, array $filter = [], array $order = [],
int $limit = 50): ?array
239 return $this->
getMessages($messageFilter, $messageOrder, $limit);
245 public function searchAction(
Chat $chat, array $filter = [], array $order = [],
int $limit = 50): ?array
249 return $this->
getMessages($messageFilter, $messageOrder, $limit);
257 $pinResult = $message->pin();
259 if (!$pinResult->isSuccess())
261 $this->
addErrors($pinResult->getErrors());
274 $unpinResult = $message->unpin();
276 if (!$unpinResult->isSuccess())
278 $this->
addErrors($unpinResult->getErrors());
292 $service->setMode(DeleteService::MODE_AUTO);
293 $result = $service->delete();
295 if (!$result->isSuccess())
311 if ($deleteService->canDelete() < DeleteService::DELETE_HARD)
320 if (!$result->isSuccess())
335 ?\CRestServer $restServer =
null,
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);
349 if (!$result->isSuccess())
356 $fields = $result->getResult();
358 $massageId = \CIMMessenger::Add($fields);
360 if (isset($forwardMessages) && $forwardMessages->count() > 0)
362 $forwardResult = $this->sendForwardMessages($chat, $forwardMessages);
363 if (!$forwardResult->isSuccess())
365 $this->
addErrors($forwardResult->getErrors());
372 'id' => $massageId ?:
null,
373 'uuidMap' => isset($forwardResult) ? $forwardResult->getResult() : []
383 string $urlPreview =
'Y',
387 $fields[
'message'] = $this->
getRawValue(
'fields')[
'message'] ?? $fields[
'message'] ??
null;
388 $fields = $this->prepareMessageFields($fields, self::ALLOWED_FIELDS_UPDATE);
389 $message->setBotId($botId);
395 if (!$result->isSuccess())
412 $chat = $message->getChat();
420 $message->markAsImportant(
true);
422 $result = (
new PushFormat())->validateDataForInform($message, $chat);
423 if (!$result->isSuccess())
430 $pushService = new \Bitrix\Im\V2\Message\Inform\PushService();
431 $pushService->sendInformPushPrivateChat($chat, $message);
433 return [
'result' =>
true];
441 if ((
int)$user->
getId() !== $message->getAuthorId())
448 (new \Bitrix\Im\V2\Message\Attach\AttachService())->deleteRichUrl($message);
450 return [
'result' =>
true];
458 if (isset($order[
'id']))
460 $messageOrder[
'ID'] = strtoupper($order[
'id']);
463 if (isset($filter[
'lastId']))
465 $messageFilter[
'LAST_ID'] = (int)$filter[
'lastId'];
468 $messageFilter[
'START_ID'] = $chat->getStartId();
469 $messageFilter[
'CHAT_ID'] = $chat->getChatId();
471 return [$messageFilter, $messageOrder];
478 if (isset($filter[
'searchMessage']) && is_string($filter[
'searchMessage']))
480 $messageFilter[
'SEARCH_MESSAGE'] = trim($filter[
'searchMessage']);
483 return [$messageFilter, $messageOrder];
486 protected function getMessages(array $filter, array $order,
int $limit): array
488 $messages = MessageCollection::find(
493 \
Bitrix\Im\V2\Message::REST_FIELDS
498 $rest[
'hasNextPage'] = $messages->count() >= $limit;
507 if ($messages->count() > self::MAX_MESSAGES_COUNT_FOR_FORWARD)
509 return $result->addError(
new MessageError(MessageError::TOO_MANY_MESSAGES));
512 $service =
new ForwardService($chat);
513 $result = $service->createMessages($messages);
515 if (!$result->hasResult())
517 return $result->addErrors($result->getErrors());
523 private function prepareMessageFields(array $fields, array $whiteList): array
525 $converter =
new Converter(Converter::TO_SNAKE | Converter::TO_UPPER | Converter::KEYS);
526 $fields = $converter->process($fields);
528 return $this->checkWhiteList($fields, $whiteList);
toRestFormat(RestConvertible ... $entities)
convertCharToBool(string $char, bool $default=false)
informAction(\Bitrix\Im\V2\Message $message)
tailAction(Chat $chat, array $filter=[], array $order=[], int $limit=50)
getMessages(array $filter, array $order, int $limit)
listAction(Chat $chat, int $limit=self::MESSAGE_ON_PAGE_COUNT)
getPrimaryAutoWiredParameter()
sendAction(Chat $chat, ?\CRestServer $restServer=null, array $fields=[], ?MessageCollection $forwardMessages=null)
readAction(MessageCollection $messages)
tailViewersAction(\Bitrix\Im\V2\Message $message, array $filter=[], array $order=[], int $limit=50)
getContextAction(\Bitrix\Im\V2\Message $message, int $range=self::MESSAGE_ON_PAGE_COUNT)
const MESSAGE_ON_PAGE_COUNT
prepareParamsForSearch(Chat $chat, array $filter, array $order)
updateAction(\Bitrix\Im\V2\Message $message, array $fields=[], string $urlPreview='Y', int $botId=0)
markAction(\Bitrix\Im\V2\Message $message)
deleteAction(\Bitrix\Im\V2\Message $message)
disappearAction(\Bitrix\Im\V2\Message $message, int $hours)
pinAction(\Bitrix\Im\V2\Message $message)
searchAction(Chat $chat, array $filter=[], array $order=[], int $limit=50)
deleteRichUrlAction(\Bitrix\Im\V2\Message $message, CurrentUser $user)
unpinAction(\Bitrix\Im\V2\Message $message)
prepareParamsForTail(Chat $chat, array $filter, array $order)
const MAX_MESSAGES_COUNT_FOR_FORWARD
static disappearMessage(Message $message, int $hours)
convertKeysToCamelCase($data)