89 if (empty($params[
'TO_USER_ID']))
91 $context = $context ?? Locator::getContext();
92 $params[
'TO_USER_ID'] = $context->getUserId();
95 $params[
'TO_USER_ID'] = (int)$params[
'TO_USER_ID'];
96 if ($params[
'TO_USER_ID'] <= 0)
101 $blockedExternalAuthId = \Bitrix\Im\Model\UserTable::filterExternalUserTypes([
'replica']);
102 $res = \Bitrix\Im\Model\UserTable::getById($params[
'TO_USER_ID']);
104 !($userData = $res->fetch())
105 || $userData[
'ACTIVE'] ==
'N'
106 || in_array($userData[
'EXTERNAL_AUTH_ID'], $blockedExternalAuthId,
true)
112 $connection = \Bitrix\Main\Application::getConnection();
114 $res = $connection->query(
"
117 WHERE AUTHOR_ID = " . $params[
'TO_USER_ID'] .
" AND TYPE = '" . self::IM_TYPE_SYSTEM .
"'
120 if ($row = $res->fetch())
123 'ID' => (
int)$row[
'ID'],
124 'TYPE' => $row[
'TYPE'],
125 'ENTITY_TYPE' => $row[
'ENTITY_TYPE'],
126 'ENTITY_ID' => $row[
'ENTITY_ID'],
138 if ($paramsResult->isSuccess())
140 $params = $paramsResult->getResult();
144 return $result->addErrors($paramsResult->getErrors());
147 $blockedExternalAuthId = \Bitrix\Im\Model\UserTable::filterExternalUserTypes([
'replica']);
148 $res = \Bitrix\Im\Model\UserTable::getById($params[
'AUTHOR_ID']);
150 !($userData = $res->fetch())
151 || $userData[
'ACTIVE'] ==
'N'
152 || in_array($userData[
'EXTERNAL_AUTH_ID'], $blockedExternalAuthId,
true)
158 $chat =
new static($params);
161 if ($chat->getChatId() <= 0)
166 \Bitrix\Im\Model\RelationTable::add([
167 'CHAT_ID' => $chat->getChatId(),
168 'MESSAGE_TYPE' => \IM_MESSAGE_SYSTEM,
169 'USER_ID' => $params[
'AUTHOR_ID'],
172 return $result->setResult([
173 'CHAT_ID' => $chat->getChatId(),
189 if (!$this->getChatId())
194 if (is_string($message))
196 $message = (
new Message)->setMessage($message);
198 elseif (!$message instanceof
Message)
200 $message =
new Message($message);
203 ->setRegistry($this->messageRegistry)
204 ->setContext($this->context)
205 ->setChatId($this->getChatId())
208 if (!$message->getNotifyModule())
210 $message->setNotifyModule(
'im');
212 if (!$message->getNotifyEvent())
216 if (!$message->getNotifyType())
218 if ($message->getAuthorId())
220 $message->setNotifyType(\IM_NOTIFY_FROM);
224 $message->setNotifyType(\IM_NOTIFY_SYSTEM);
227 if ($message->allowNotifyAnswer())
229 $message->getParams()->get(Params::CAN_ANSWER)->setValue(
true);
235 $sendingServiceConfig = $sendingConfig;
240 if (is_array($sendingConfig))
242 $sendingServiceConfig->fill($sendingConfig);
247 $sendService->setContext($this->context);
251 $eventResult = $sendService->fireEventBeforeNotifySend($this, $message);
252 if (!$eventResult->isSuccess())
255 return $result->addErrors($eventResult->getErrors());
258 $checkResult = $this->
validateMessage($message, $sendingServiceConfig);
259 if (!$checkResult->isSuccess())
261 return $result->addErrors($checkResult->getErrors());
266 if ($message->getNotifyType() != \IM_NOTIFY_CONFIRM)
268 $skipAdd = !\CIMSettings::GetNotifyAccess($this->getAuthorId(), $message->getNotifyModule(), $message->getNotifyEvent(), \CIMSettings::CLIENT_SITE);
269 $skipFlash = $skipAdd;
271 if (!$skipAdd && $message->isNotifyFlash() ===
true)
278 ->markNotifyRead(
true)
279 ->markNotifyFlash(
true)
286 if ($message->getNotifyType() == \IM_NOTIFY_CONFIRM)
295 $message->setMessageId(time());
300 $saveResult = $message->save();
301 if (!$saveResult->isSuccess())
303 return $result->addErrors($saveResult->getErrors());
306 $messageCount = MessageTable::getCount([
'=CHAT_ID' => $this->getChatId()]);
309 ->setMessageCount($messageCount)
310 ->setLastMessageId($message->getMessageId())
315 $readService =
new ReadService($this->getAuthorId());
316 $readService->markNotificationUnread($message, $this->getRelations());
318 $counter = $readService->getCounterService()->getByChat($this->getChatId());
322 $sendService->fireEventAfterNotifySend($this, $message);
325 if ($sendingServiceConfig->sendPush())
327 $pushService =
new PushService($sendingServiceConfig);
328 $pushService->sendPushNotification($this, $message, $counter, !$skipFlash);
334 $message->updateSearchIndex();
337 $result->setResult([
'messageId' => $message->getMessageId()]);
351 if (!$this->getAuthorId())
356 $blockedExternalAuthId = UserTable::filterExternalUserTypes([
'replica']);
359 !$recipient->isActive()
360 || in_array($recipient->getExternalAuthId(), $blockedExternalAuthId,
true)
368 && !$message->
getParams()->isSet(Params::ATTACH)
371 return $result->addError(
new MessageError(MessageError::EMPTY_MESSAGE));
376 || !in_array($message->
getNotifyType(), [\IM_NOTIFY_CONFIRM, \IM_NOTIFY_SYSTEM, \IM_NOTIFY_FROM],
true)
379 $result->addError(
new MessageError(MessageError::NOTIFY_TYPE));
383 $result->addError(
new MessageError(MessageError::NOTIFY_MODULE));
387 $result->addError(
new MessageError(MessageError::NOTIFY_EVENT));
394 $result->addError(
new MessageError(MessageError::NOTIFY_BUTTONS));
401 $result->addError(
new MessageError(MessageError::WRONG_SENDER));
437 $lastMessages = MessageTable::getList([
438 'select' => [
'ID',
'AUTHOR_ID'],
441 '=CHAT_ID' => $this->getChatId(),
445 while ($lastMessage = $lastMessages->fetch())
447 $lastMessageParams =
new Params();
448 $lastMessageParams->loadByMessageId($lastMessage[
'ID']);
450 if ($lastMessageParams->isSet(Params::USERS))
452 $users = array_merge($users, $lastMessageParams->get(Params::USERS)->getValue());
454 $users[] = (int)$lastMessage[
'AUTHOR_ID'];
456 \CIMNotify::Delete($lastMessage[
'ID']);
460 ->setValue(array_unique($users))