- Модуль: im
- Путь к файлу: ~/bitrix/modules/im/lib/V2/Chat/NotifyChat.php
- Класс: BitrixImV2ChatNotifyChat
- Вызов: NotifyChat::sendMessage
public function sendMessage($message, $sendingConfig = null): Result
{
$result = new Result;
if (!$this->getChatId())
{
return $result->addError(new ChatError(ChatError::WRONG_TARGET_CHAT));
}
if (is_string($message))
{
$message = (new Message)->setMessage($message);
}
elseif (!$message instanceof Message)
{
$message = new Message($message);
}
$message
->setRegistry($this->messageRegistry)
->setContext($this->context)
->setChatId($this->getChatId())
;
if (!$message->getNotifyModule())
{
$message->setNotifyModule('im');
}
if (!$message->getNotifyEvent())
{
$message->setNotifyEvent(Notify::EVENT_DEFAULT);
}
if (!$message->getNotifyType())
{
if ($message->getAuthorId())
{
$message->setNotifyType(IM_NOTIFY_FROM);
}
else
{
$message->setNotifyType(IM_NOTIFY_SYSTEM);
}
}
if ($message->allowNotifyAnswer())
{
$message->getParams()->get(Params::CAN_ANSWER)->setValue(true);
}
// config for sending process
if ($sendingConfig instanceof SendingConfig)
{
$sendingServiceConfig = $sendingConfig;
}
else
{
$sendingServiceConfig = new SendingConfig();
if (is_array($sendingConfig))
{
$sendingServiceConfig->fill($sendingConfig);
}
}
// sending process
$sendService = new SendingService($sendingServiceConfig);
$sendService->setContext($this->context);
// fire event `im:OnBeforeMessageNotifyAdd` before message send
$eventResult = $sendService->fireEventBeforeNotifySend($this, $message);
if (!$eventResult->isSuccess())
{
// cancel sending by event
return $result->addErrors($eventResult->getErrors());
}
$checkResult = $this->validateMessage($message, $sendingServiceConfig);
if (!$checkResult->isSuccess())
{
return $result->addErrors($checkResult->getErrors());
}
$skipAdd = false;
$skipFlash = false;
if ($message->getNotifyType() != IM_NOTIFY_CONFIRM)
{
$skipAdd = !CIMSettings::GetNotifyAccess($this->getAuthorId(), $message->getNotifyModule(), $message->getNotifyEvent(), CIMSettings::CLIENT_SITE);
$skipFlash = $skipAdd;
}
if (!$skipAdd && $message->isNotifyFlash() === true)
{
$skipAdd = true;
}
if ($skipAdd)
{
$message
->markNotifyRead(true)
->markNotifyFlash(true)
;
}
// fill message param USERS with authorIds and drop other notify by tag
$this->dropOtherUserNotificationByTag($message);
if ($message->getNotifyType() == IM_NOTIFY_CONFIRM)
{
$this->prepareConfirm($message);
$this->dropAllConfirmByTag($message);
}
$counter = 0;
if ($skipAdd)
{
$message->setMessageId(time());
}
else
{
// Save + Save Params
$saveResult = $message->save();
if (!$saveResult->isSuccess())
{
return $result->addErrors($saveResult->getErrors());
}
$messageCount = MessageTable::getCount(['=CHAT_ID' => $this->getChatId()]);
$this
->setMessageCount($messageCount)
->setLastMessageId($message->getMessageId())
->save()
;
// Unread
$readService = new ReadService($this->getAuthorId());
$readService->markNotificationUnread($message, $this->getRelations());
$counter = $readService->getCounterService()->getByChat($this->getChatId());
}
// fire event `im:OnAfterNotifyAdd`
$sendService->fireEventAfterNotifySend($this, $message);
// send Push
if ($sendingServiceConfig->sendPush())
{
$pushService = new PushService($sendingServiceConfig);
$pushService->sendPushNotification($this, $message, $counter, !$skipFlash);
}
// search
if (!$skipAdd)
{
$message->updateSearchIndex();
}
$result->setResult(['messageId' => $message->getMessageId()]);
return $result;
}