315 if (!$this->getChatId())
320 if (is_string($message))
322 $message = (
new Message)->setMessage($message);
324 elseif (!$message instanceof
Message)
326 $message =
new Message($message);
330 ->setRegistry($this->messageRegistry)
331 ->setContext($this->context)
332 ->setChatId($this->getChatId())
335 if (!$message->getNotifyModule())
337 $message->setNotifyModule(
'im');
339 if ($message->isSystem())
351 $sendingServiceConfig = $sendingConfig;
356 if (is_array($sendingConfig))
358 $sendingServiceConfig->fill($sendingConfig);
363 $sendService->setContext($this->context);
367 !$message->isSystem()
368 && $message->getUuid()
371 $checkUuidResult = $sendService->checkDuplicateByUuid($message);
372 if (!$checkUuidResult->isSuccess())
374 return $result->addErrors($checkUuidResult->getErrors());
376 $data = $checkUuidResult->getResult();
377 if (!empty($data[
'messageId']))
379 return $result->setResult($checkUuidResult->getResult());
385 !$message->getAuthorId()
386 && !$message->isSystem()
389 $message->setAuthorId($this->getContext()->getUserId());
390 if (!$message->getAuthorId())
399 && $message->getAuthorId()
403 $message->markAsSystem(
false);
408 !$sendingServiceConfig->skipUserCheck()
409 && !$sendingServiceConfig->convertMode()
410 && !$message->isSystem()
413 if (!$this->
hasAccess($message->getAuthorId()))
420 $eventResult = $sendService->fireEventBeforeMessageSend($this, $message);
421 if (!$eventResult->isSuccess())
424 return $result->addErrors($eventResult->getErrors());
429 !$message->getMessage()
430 && !$message->hasFiles()
431 && !$message->getParams()->isSet(Params::ATTACH)
434 return $result->addError(
new MessageError(MessageError::EMPTY_MESSAGE));
438 if ($sendingServiceConfig->generateUrlPreview())
440 $message->parseDates();
444 $message->checkEmoji();
448 $message->uploadFileFromText();
451 $message->formatFilesMessageOut();
454 $saveResult = $message->save();
455 if (!$saveResult->isSuccess())
457 return $result->addErrors($saveResult->getErrors());
461 $readService =
new ReadService($message->getAuthorId());
462 $readService->markMessageUnread($message, $this->getRelations());
466 ->setLastMessageId($message->getMessageId())
467 ->incrementMessageCount()
472 if ($sendingServiceConfig->addRecent())
474 $readService->markRecentUnread($message);
479 if ($sendingServiceConfig->sendPush())
481 $counters = $readService->getCountersForUsers($message, $this->getRelations());
485 $sendService->fireEventAfterMessageSend($this, $message);
489 $sendingServiceConfig->addRecent()
490 && !$sendingServiceConfig->skipAuthorAddRecent()
493 $this->riseInRecent($message);
497 if ($sendingServiceConfig->generateUrlPreview())
500 $message->generateUrlPreview();
503 if ($this->getParentMessageId())
505 $this->updateParentMessageCount();
509 if ($sendingServiceConfig->sendPush())
511 $pushService =
new PushService($sendingServiceConfig);
512 $pushService->sendPushPrivateChat($this, $message, $counters);
516 if (!$message->isSystem())
519 $mentionService->setContext($this->context);
520 $mentionService->sendMentions($this, $message);
524 $botService =
new BotService($sendingServiceConfig);
525 $botService->setContext($this->context);
526 $botService->runMessageCommand($this, $message);
529 (
new UrlService())->saveUrlsFromMessage($message);
532 $message->updateSearchIndex();
534 $result->setResult([
'messageId' => $message->getMessageId()]);
631 if (!$paramsResult->isSuccess())
633 return $result->addErrors($paramsResult->getErrors());
636 $params = $paramsResult->getResult();
643 if ($params[
'FROM_USER_ID'] == $params[
'TO_USER_ID'])
649 if ($chatResult->isSuccess() && $chatResult->hasResult())
651 $chatParams = $chatResult->getResult();
653 return $result->setResult([
654 'CHAT_ID' => (
int)$chatParams[
'ID'],
655 'CHAT' => self::load($chatParams),
659 $chat =
new static($params);
662 if ($chat->getChatId() <= 0)
667 \Bitrix\Im\Model\RelationTable::add([
668 'CHAT_ID' => $chat->getChatId(),
669 'MESSAGE_TYPE' => \IM_MESSAGE_PRIVATE,
670 'USER_ID' => $params[
'FROM_USER_ID'],
671 'STATUS' => \IM_STATUS_READ,
673 \Bitrix\Im\Model\RelationTable::add([
674 'CHAT_ID' => $chat->getChatId(),
675 'MESSAGE_TYPE' => \IM_MESSAGE_PRIVATE,
676 'USER_ID' => $params[
'TO_USER_ID'],
677 'STATUS' => \IM_STATUS_READ,
681 'CHAT_TYPE' => \IM_MESSAGE_PRIVATE,
682 'MESSAGE_TYPE' => \IM_MESSAGE_PRIVATE
689 $botJoinFields[
'BOT_ID'] = $params[
'TO_USER_ID'];
690 $botJoinFields[
'USER_ID'] = $params[
'FROM_USER_ID'];
691 $botJoinFields[
'TO_USER_ID'] = $params[
'TO_USER_ID'];
692 $botJoinFields[
'FROM_USER_ID'] = $params[
'FROM_USER_ID'];
693 \Bitrix\Im\Bot::onJoinChat($params[
'FROM_USER_ID'], $botJoinFields);
700 $botJoinFields[
'BOT_ID'] = $params[
'FROM_USER_ID'];
701 $botJoinFields[
'USER_ID'] = $params[
'TO_USER_ID'];
702 $botJoinFields[
'TO_USER_ID'] = $params[
'TO_USER_ID'];
703 $botJoinFields[
'FROM_USER_ID'] = $params[
'FROM_USER_ID'];
704 \Bitrix\Im\Bot::onJoinChat($params[
'TO_USER_ID'], $botJoinFields);
707 return $result->setResult([
708 'CHAT_ID' => $chat->getChatId(),