72 $skipAddMessage = ($params[
'SKIP_ADD_MESSAGE'] ??
'N') ===
'Y';
73 $skipMessageUsersAdd = ($params[
'SKIP_MESSAGE_USERS_ADD'] ??
'N') ===
'Y';
75 if ($paramsResult->isSuccess())
77 $params = $paramsResult->getResult();
81 return $result->addErrors($paramsResult->getErrors());
84 $chat =
new static($params);
85 $chat->setExtranet($chat->checkIsExtranet())->setContext($context);
88 if (!$chat->getChatId())
93 $chat->addUsersToRelation([$chat->getAuthorId()], $params[
'MANAGERS'] ?? [],
false);
94 if (!$skipAddMessage && $chat->needToSendGreetingMessages())
96 $chat->sendGreetingMessage($this->getContext()->getUserId());
97 $chat->sendBanner($this->getContext()->getUserId());
100 $usersToInvite = $chat->filterUsersToAdd($chat->getUserIds());
101 $addedUsers = $usersToInvite;
102 $addedUsers[$chat->getAuthorId()] = $chat->getAuthorId();
104 $chat->addUsersToRelation($usersToInvite, $params[
'MANAGERS'] ?? [],
false);
105 if (!$skipAddMessage && !$skipMessageUsersAdd)
107 $chat->sendMessageUsersAdd($usersToInvite);
109 $chat->sendEventUsersAdd($addedUsers);
110 $chat->sendDescriptionMessage($this->getContext()->getUserId());
114 'CHAT_ID' => $chat->getChatId(),
118 self::cleanCache($chat->getChatId());
180 if (!isset($params[
'AUTHOR_ID']))
182 $params[
'AUTHOR_ID'] = $this->getContext()->getUserId();
185 if (!isset($params[
'OWNER_ID']))
187 $params[
'OWNER_ID'] = $this->getContext()->getUserId();
190 $params[
'MANAGERS'] ??= [];
191 $params[
'MANAGERS'] = array_unique(array_merge($params[
'MANAGERS'], [$params[
'AUTHOR_ID']]));
193 $params[
'USERS'] = array_unique(array_merge($params[
'USERS'], $params[
'MANAGERS']));
194 $params[
'USER_COUNT'] = count($params[
'USERS']);
197 isset($params[
'AVATAR'])
199 && !is_numeric((
string)$params[
'AVATAR'])
202 $params[
'AVATAR'] = \CRestUtil::saveFile($params[
'AVATAR']);
203 $imageCheck = (new \Bitrix\Main\File\Image($params[
'AVATAR'][
"tmp_name"]))->getInfo();
206 || !$imageCheck->getWidth()
207 || $imageCheck->getWidth() > 5000
208 || !$imageCheck->getHeight()
209 || $imageCheck->getHeight() > 5000
212 $params[
'AVATAR'] =
null;
215 if (!$params[
'AVATAR'] || mb_strpos($params[
'AVATAR'][
'type'],
"image/") !== 0)
217 $params[
'AVATAR'] =
null;
221 $params[
'AVATAR'] = \CFile::saveFile($params[
'AVATAR'],
'im');
225 return $result->setResult($params);
247 $colorCodeKey =
'im_chat_color_' . $this->getColor();
248 $colorCodeCount = \CGlobalCounter::GetValue($colorCodeKey, \CGlobalCounter::ALL_SITES);
252 \CGlobalCounter::Set($colorCodeKey, 1, \CGlobalCounter::ALL_SITES,
'',
false);
257 '#NUMBER#' => ++$colorCodeCount,
259 \CGlobalCounter::Set($colorCodeKey, $colorCodeCount, \CGlobalCounter::ALL_SITES,
'',
false);
264 if ($this->getUserIds() && count($this->getUserIds()))
266 $userIds = $this->getUserIds();
268 $userIds = \CIMContactList::PrepareUserIds($userIds);
269 $users = \CIMContactList::GetUserData([
270 'ID' => array_values($userIds),
276 foreach ($users[
'users'] as $user)
278 $usersNames[] = htmlspecialcharsback($user[
'name']);
282 '#USERS_NAMES#' => implode(
', ', $usersNames),
286 return mb_substr($chatTitle, 0, 255);
390 $authorId = $this->getAuthorId();
392 $author = \Bitrix\Im\V2\Entity\User\User::getInstance($authorId);
394 $userIds = array_unique($this->getUserIds());
395 if (count($userIds) < 2)
400 $userIds = \CIMContactList::PrepareUserIds($userIds);
401 $users = \CIMContactList::GetUserData([
402 'ID' => array_values($userIds),
407 if (!isset($users[
'users']) || count($users[
'users']) < 2)
414 if ($authorId !== $this->getAuthorId())
416 $usersNames[] = htmlspecialcharsback($this->getAuthor()->getName());
419 foreach ($users[
'users'] as $user)
421 if ($user[
'name'] !== $author->getName())
423 $usersNames[] = htmlspecialcharsback($user[
'name']);
428 'IM_CHAT_JOIN_' . $author->getGender(),
430 '#USER_1_NAME#' => htmlspecialcharsback($author->getName()),
431 '#USER_2_NAME#' => implode(
', ', array_unique($usersNames))
436 'MESSAGE_TYPE' => $this->
getType(),
437 'TO_CHAT_ID' => $this->getChatId(),
438 'FROM_USER_ID' => $author->getId(),
439 'MESSAGE' => $messageText,
455 if (!$this->getChatId())
460 if (is_string($message))
462 $message = (
new Message)->setMessage($message);
464 elseif (!$message instanceof
Message)
466 $message =
new Message($message);
470 ->setRegistry($this->messageRegistry)
471 ->setContext($this->context)
472 ->setChatId($this->getChatId())
473 ->setNotifyModule(
'im')
480 $sendingServiceConfig = $sendingConfig;
485 if (is_array($sendingConfig))
487 $sendingServiceConfig->fill($sendingConfig);
492 $sendService->setContext($this->context);
497 !$message->isSystem()
498 && $message->getUuid()
501 $checkUuidResult = $sendService->checkDuplicateByUuid($message);
502 if (!$checkUuidResult->isSuccess())
504 return $result->addErrors($checkUuidResult->getErrors());
506 $data = $checkUuidResult->getResult();
507 if (!empty($data[
'messageId']))
509 return $result->setResult($checkUuidResult->getResult());
515 !$message->getAuthorId()
516 && !$message->isSystem()
519 $message->setAuthorId($this->getContext()->getUserId());
525 && $message->getAuthorId()
529 $message->markAsSystem(
false);
534 !$sendingServiceConfig->skipUserCheck()
535 && !$sendingServiceConfig->convertMode()
536 && !$message->isSystem()
539 if (!$this->
hasAccess($message->getAuthorId()))
546 $eventResult = $sendService->fireEventBeforeMessageSend($this, $message);
547 if (!$eventResult->isSuccess())
550 return $result->addErrors($eventResult->getErrors());
555 !$message->getMessage()
556 && !$message->hasFiles()
557 && !$message->getParams()->isSet(Params::ATTACH)
560 return $result->addError(
new MessageError(MessageError::EMPTY_MESSAGE));
563 if ($sendingServiceConfig->keepConnectorSilence())
565 $message->getParams()->get(Params::STYLE_CLASS)->setValue(
'bx-messenger-content-item-system');
570 if ($sendingServiceConfig->generateUrlPreview())
572 $message->parseDates();
576 $message->checkEmoji();
579 $message->uploadFileFromText();
582 $message->formatFilesMessageOut();
585 $saveResult = $message->save();
586 if (!$saveResult->isSuccess())
588 return $result->addErrors($saveResult->getErrors());
591 $sendService->updateMessageUuid($message);
594 $readService =
new ReadService($message->getAuthorId());
595 $readService->markMessageUnread($message, $this->getRelations());
599 ->setLastMessageId($message->getMessageId())
600 ->incrementMessageCount()
605 if ($sendingServiceConfig->addRecent())
607 $readService->markRecentUnread($message);
612 if ($sendingServiceConfig->sendPush())
614 $counters = $readService->getCountersForUsers($message, $this->getRelations());
618 $sendService->fireEventAfterMessageSend($this, $message);
622 $sendingServiceConfig->addRecent()
623 && !$sendingServiceConfig->skipAuthorAddRecent()
626 $this->riseInRecent($message);
630 if ($sendingServiceConfig->generateUrlPreview())
633 $message->generateUrlPreview();
636 if ($this->getParentMessageId())
638 $this->updateParentMessageCount();
642 if ($sendingServiceConfig->sendPush())
644 $pushService =
new PushService($sendingServiceConfig);
645 $pushService->sendPushGroupChat($this, $message, $counters);
649 if (!$message->isSystem())
652 $mentionService->setContext($this->context);
653 $mentionService->sendMentions($this, $message);
657 $botService =
new BotService($sendingServiceConfig);
658 $botService->setContext($this->context);
659 $botService->runMessageCommand($this, $message);
662 (
new UrlService())->saveUrlsFromMessage($message);
665 $message->updateSearchIndex();
667 $result->setResult([
'messageId' => $message->getMessageId()]);