1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
NotifyChat.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
5use Bitrix\Im\V2\Message\Send\SendResult;
6use Bitrix\Main\Loader;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Im\Notify;
9use Bitrix\Im\User;
10use Bitrix\Im\Model\UserTable;
11use Bitrix\Im\Model\MessageTable;
12use Bitrix\Im\V2\Chat;
13use Bitrix\Im\V2\Message;
14use Bitrix\Im\V2\Message\MessageError;
15use Bitrix\Im\V2\Result;
16use Bitrix\Im\V2\Service\Context;
17use Bitrix\Im\V2\Service\Locator;
18use Bitrix\Im\V2\Message\Send\SendingConfig;
19use Bitrix\Im\V2\Message\Send\SendingService;
20use Bitrix\Im\V2\Message\Send\PushService;
21use Bitrix\Im\V2\Message\Params;
22use Bitrix\Im\V2\Message\ReadService;
23
24class NotifyChat extends Chat
25{
26 protected function getDefaultType(): string
27 {
28 return self::IM_TYPE_SYSTEM;
29 }
30
31 protected function checkAccessInternal(int $userId): Result
32 {
33 return (new Result())->addError(new ChatError(ChatError::ACCESS_DENIED));
34 }
35
40 public function allowMention(): bool
41 {
42 return false;
43 }
44
45
46 public function getStartId(?int $userId = null): int
47 {
48 return 0;
49 }
50
51 protected function prepareParams(array $params = []): Result
52 {
53 $result = new Result();
54
55 if (!isset($params['AUTHOR_ID']))
56 {
57 if (!isset($params['TO_USER_ID']))
58 {
59 return $result->addError(new ChatError(ChatError::WRONG_RECIPIENT));
60 }
61
62 $params['AUTHOR_ID'] = $params['TO_USER_ID'];
63 }
64
65 $result->setResult($params);
66
67 return $result;
68 }
69
70 public static function getByUser(?int $userId = null): ?NotifyChat
71 {
72 return ChatFactory::getInstance()->getNotifyFeed($userId);
73 }
74
86 public static function find(array $params = [], ?Context $context = null): Result
87 {
88 $result = new Result;
89
90 if (empty($params['TO_USER_ID']))
91 {
92 $context = $context ?? Locator::getContext();
93 $params['TO_USER_ID'] = $context->getUserId();
94 }
95
96 $params['TO_USER_ID'] = (int)$params['TO_USER_ID'];
97 if ($params['TO_USER_ID'] <= 0)
98 {
99 return $result->addError(new ChatError(ChatError::WRONG_RECIPIENT));
100 }
101
102 $blockedExternalAuthId = \Bitrix\Im\Model\UserTable::filterExternalUserTypes(['replica']);
104 if (
105 !($userData = $res->fetch())
106 || $userData['ACTIVE'] == 'N'
107 || in_array($userData['EXTERNAL_AUTH_ID'], $blockedExternalAuthId, true)
108 )
109 {
110 return $result->addError(new ChatError(ChatError::WRONG_RECIPIENT));
111 }
112
114
115 $res = $connection->query("
116 SELECT *
117 FROM b_im_chat
118 WHERE AUTHOR_ID = " . $params['TO_USER_ID'] . " AND TYPE = '" . self::IM_TYPE_SYSTEM . "'
119 ORDER BY ID ASC
120 ");
121 if ($row = $res->fetch())
122 {
123 $result->setResult([
124 'ID' => (int)$row['ID'],
125 'TYPE' => $row['TYPE'],
126 'ENTITY_TYPE' => $row['ENTITY_TYPE'],
127 'ENTITY_ID' => $row['ENTITY_ID'],
128 ]);
129 }
130
131 return $result;
132 }
133
134 public function add(array $params, ?Context $context = null): Result
135 {
136 $result = new Result;
137
138 $paramsResult = $this->prepareParams($params);
139 if ($paramsResult->isSuccess())
140 {
141 $params = $paramsResult->getResult();
142 }
143 else
144 {
145 return $result->addErrors($paramsResult->getErrors());
146 }
147
148 $blockedExternalAuthId = \Bitrix\Im\Model\UserTable::filterExternalUserTypes(['replica']);
150 if (
151 !($userData = $res->fetch())
152 || $userData['ACTIVE'] == 'N'
153 || in_array($userData['EXTERNAL_AUTH_ID'], $blockedExternalAuthId, true)
154 )
155 {
156 return $result->addError(new ChatError(ChatError::WRONG_RECIPIENT));
157 }
158
159 $chat = new static($params);
160 $chat->save();
161
162 if ($chat->getChatId() <= 0)
163 {
164 return $result->addError(new ChatError(ChatError::CREATION_ERROR));
165 }
166
167 \Bitrix\Im\Model\RelationTable::add([
168 'CHAT_ID' => $chat->getChatId(),
169 'MESSAGE_TYPE' => \IM_MESSAGE_SYSTEM,
170 'USER_ID' => $params['AUTHOR_ID'],
171 ]);
172
173 $chat->isFilledNonCachedData = false;
174
175 return $result->setResult([
176 'CHAT_ID' => $chat->getChatId(),
177 'CHAT' => $chat,
178 ]);
179 }
180
188 public function sendMessage($message, $sendingConfig = null): SendResult
189 {
190 return new SendResult();
191 /*$result = new Result;
192
193 if (!$this->getChatId())
194 {
195 return $result->addError(new ChatError(ChatError::WRONG_TARGET_CHAT));
196 }
197
198 if (is_string($message))
199 {
200 $message = (new Message)->setMessage($message);
201 }
202 elseif (!$message instanceof Message)
203 {
204 $message = new Message($message);
205 }
206 $message
207 ->setRegistry($this->messageRegistry)
208 ->setContext($this->context)
209 ->setChatId($this->getChatId())
210 ;
211
212 if (!$message->getNotifyModule())
213 {
214 $message->setNotifyModule('im');
215 }
216 if (!$message->getNotifyEvent())
217 {
218 $message->setNotifyEvent(Notify::EVENT_DEFAULT);
219 }
220 if (!$message->getNotifyType())
221 {
222 if ($message->getAuthorId())
223 {
224 $message->setNotifyType(\IM_NOTIFY_FROM);
225 }
226 else
227 {
228 $message->setNotifyType(\IM_NOTIFY_SYSTEM);
229 }
230 }
231 if ($message->allowNotifyAnswer())
232 {
233 $message->getParams()->get(Params::CAN_ANSWER)->setValue(true);
234 }
235
236 // config for sending process
237 if ($sendingConfig instanceof SendingConfig)
238 {
239 $sendingServiceConfig = $sendingConfig;
240 }
241 else
242 {
243 $sendingServiceConfig = new SendingConfig();
244 if (is_array($sendingConfig))
245 {
246 $sendingServiceConfig->fill($sendingConfig);
247 }
248 }
249 // sending process
250 $sendService = new SendingService($sendingServiceConfig);
251 $sendService->setContext($this->context);
252
253
254 // fire event `im:OnBeforeMessageNotifyAdd` before message send
255 //$eventResult = $sendService->fireEventBeforeNotifySend($this, $message);
256 if (!$eventResult->isSuccess())
257 {
258 // cancel sending by event
259 return $result->addErrors($eventResult->getErrors());
260 }
261
262 $checkResult = $this->validateMessage($message, $sendingServiceConfig);
263 if (!$checkResult->isSuccess())
264 {
265 return $result->addErrors($checkResult->getErrors());
266 }
267
268 $skipAdd = false;
269 $skipFlash = false;
270 if ($message->getNotifyType() != \IM_NOTIFY_CONFIRM)
271 {
272 $skipAdd = !\CIMSettings::GetNotifyAccess($this->getAuthorId(), $message->getNotifyModule(), $message->getNotifyEvent(), \CIMSettings::CLIENT_SITE);
273 $skipFlash = $skipAdd;
274 }
275 if (!$skipAdd && $message->isNotifyFlash() === true)
276 {
277 $skipAdd = true;
278 }
279 if ($skipAdd)
280 {
281 $message
282 ->markNotifyRead(true)
283 ->markNotifyFlash(true)
284 ;
285 }
286
287 // fill message param USERS with authorIds and drop other notify by tag
288 $this->dropOtherUserNotificationByTag($message);
289
290 if ($message->getNotifyType() == \IM_NOTIFY_CONFIRM)
291 {
292 $this->prepareConfirm($message);
293 $this->dropAllConfirmByTag($message);
294 }
295
296 $counter = 0;
297 if ($skipAdd)
298 {
299 $message->setMessageId(time());
300 }
301 else
302 {
303 // Save + Save Params
304 $saveResult = $message->save();
305 if (!$saveResult->isSuccess())
306 {
307 return $result->addErrors($saveResult->getErrors());
308 }
309
310 $messageCount = MessageTable::getCount(['=CHAT_ID' => $this->getChatId()]);
311
312 $this
313 ->setMessageCount($messageCount)
314 ->setLastMessageId($message->getMessageId())
315 ->save()
316 ;
317
318 // Unread
319 $readService = new ReadService($this->getAuthorId());
320 $readService->markNotificationUnread($message, $this->getRelations());
321
322 $counter = $readService->getCounterService()->getByChat($this->getChatId());
323 }
324
325 // fire event `im:OnAfterNotifyAdd`
326 $sendService->fireEventAfterNotifySend($this, $message);
327
328 // send Push
329 if ($sendingServiceConfig->sendPush())
330 {
331 $pushService = new PushService($sendingServiceConfig);
332 $pushService->sendPushNotification($this, $message, $counter, !$skipFlash);
333 }
334
335 // search
336 if (!$skipAdd)
337 {
338 $message->updateSearchIndex();
339 }
340
341 $result->setResult(['messageId' => $message->getMessageId()]);
342
343 return $result;*/
344 }
345
351 public function validateMessage(Message $message, SendingConfig $sendingServiceConfig): Result
352 {
353 $result = new Result;
354
355 if (!$this->getAuthorId())
356 {
357 return $result->addError(new ChatError(ChatError::WRONG_RECIPIENT));
358 }
359
360 $blockedExternalAuthId = UserTable::filterExternalUserTypes(['replica']);
361 $recipient = User::getInstance($this->getAuthorId());
362 if (
363 !$recipient->isActive()
364 || in_array($recipient->getExternalAuthId(), $blockedExternalAuthId, true)
365 )
366 {
367 return $result->addError(new ChatError(ChatError::WRONG_RECIPIENT));
368 }
369
370 if (
371 !$message->getMessage()
372 && !$message->getParams()->isSet(Params::ATTACH)
373 )
374 {
375 return $result->addError(new MessageError(MessageError::EMPTY_MESSAGE));
376 }
377
378 if (
379 !$message->getNotifyType()
380 || !in_array($message->getNotifyType(), [\IM_NOTIFY_CONFIRM, \IM_NOTIFY_SYSTEM, \IM_NOTIFY_FROM], true)
381 )
382 {
383 $result->addError(new MessageError(MessageError::NOTIFY_TYPE));
384 }
385 if (!$message->getNotifyModule())
386 {
387 $result->addError(new MessageError(MessageError::NOTIFY_MODULE));
388 }
389 if (!$message->getNotifyEvent())
390 {
391 $result->addError(new MessageError(MessageError::NOTIFY_EVENT));
392 }
393 if(
394 $message->getNotifyType() === \IM_NOTIFY_CONFIRM
395 && !$message->getNotifyButtons()
396 )
397 {
398 $result->addError(new MessageError(MessageError::NOTIFY_BUTTONS));
399 }
400 if(
401 $message->getNotifyType() === \IM_NOTIFY_FROM
402 && !$message->getAuthorId()
403 )
404 {
405 $result->addError(new MessageError(MessageError::WRONG_SENDER));
406 }
407
408 return $result;
409 }
410
411 public function dropAll(): void
412 {
413 $chatId = $this->getChatId();
414
415 if ($chatId === null || $chatId === 0)
416 {
417 return;
418 }
419
420 Message\MessageService::deleteByChatId($chatId, $this->getContext()->getUserId());
421 $this->setMessageCount(0)->save();
422
423 $this->sendPushDropAll();
424 }
425
434 {
435 if (
436 $this->getChatId()
437 && $message->getAuthorId()
438 && $message->getNotifyTag()
439 )
440 {
441 $lastMessages = MessageTable::getList([
442 'select' => ['ID', 'AUTHOR_ID'],
443 'filter' => [
444 '=NOTIFY_TAG' => $message->getNotifyTag(),
445 '=CHAT_ID' => $this->getChatId(),
446 ]
447 ]);
448 $users = [];
449 while ($lastMessage = $lastMessages->fetch())
450 {
451 $lastMessageParams = new Params();
452 $lastMessageParams->loadByMessageId($lastMessage['ID']);
453
454 if ($lastMessageParams->isSet(Params::USERS))
455 {
456 $users = array_merge($users, $lastMessageParams->get(Params::USERS)->getValue());
457 }
458 $users[] = (int)$lastMessage['AUTHOR_ID'];
459
460 \CIMNotify::Delete($lastMessage['ID']);
461 }
462 $message->getParams()
463 ->get(Params::USERS)
464 ->setValue(array_unique($users))
465 ->unsetValue($message->getAuthorId())
466 ;
467 }
468 }
469
470 protected function prepareConfirm(Message $message): void
471 {
472 if ($message->getNotifyType() == \IM_NOTIFY_CONFIRM)
473 {
474 if (!empty($message->getNotifyButtons()))
475 {
476 $buttons = $message->getNotifyButtons();
477 foreach ($buttons as $index => $button)
478 {
479 if (
480 is_array($button)
481 && !empty($button['TITLE'])
482 && !empty($button['VALUE'])
483 && !empty($button['TYPE'])
484 )
485 {
486 $button['TITLE'] = htmlspecialcharsbx($button['TITLE']);
487 $button['VALUE'] = htmlspecialcharsbx($button['VALUE']);
488 $button['TYPE'] = htmlspecialcharsbx($button['TYPE']);
489 $buttons[$index] = $button;
490 }
491 else
492 {
493 unset($buttons[$index]);
494 }
495 }
496 }
497 else
498 {
499 $buttons = [
500 [
501 'TITLE' => Loc::getMessage('IM_NOTIFY_CONFIRM_BUTTON_ACCEPT'),
502 'VALUE' => 'Y',
503 'TYPE' => 'accept'
504 ],
505 [
506 'TITLE' => Loc::getMessage('IM_NOTIFY_CONFIRM_BUTTON_CANCEL'),
507 'VALUE' => 'N',
508 'TYPE' => 'cancel'
509 ],
510 ];
511 }
512
513 $message->setNotifyButtons($buttons);
514 }
515 }
516
517 protected function dropAllConfirmByTag(Message $message): void
518 {
519 if (
520 $message->getNotifyType() == \IM_NOTIFY_CONFIRM
521 && !empty($message->getNotifyTag())
522 )
523 {
524 \CIMNotify::DeleteByTag($message->getNotifyTag());
525 }
526 }
527
528 protected function sendPushDropAll(): void
529 {
530 if (Loader::includeModule('pull'))
531 {
533 $this->getContext()->getUserId(),
534 [
535 'module_id' => 'im',
536 'command' => 'notifyDeleteAll',
537 'params' => [
538 'chatId' => $this->getChatId(),
539 ],
540 'extra' => \Bitrix\Im\Common::getPullExtra()
541 ]
542 );
543 \Bitrix\Pull\MobileCounter::send($this->getContext()->getUserId());
544 }
545 }
546
547 protected function addIndex(): Chat
548 {
549 return $this;
550 }
551
552 protected function updateIndex(): Chat
553 {
554 return $this;
555 }
556
561}
$connection
Определения actionsdefinitions.php:38
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getPullExtra()
Определения common.php:127
static filterExternalUserTypes(array $skipTypes=[])
Определения user.php:31
const CREATION_ERROR
Определения ChatError.php:23
const WRONG_RECIPIENT
Определения ChatError.php:14
const ACCESS_DENIED
Определения ChatError.php:19
static getInstance()
Определения ChatFactory.php:37
checkAccessInternal(int $userId)
Определения NotifyChat.php:31
add(array $params, ?Context $context=null)
Определения NotifyChat.php:134
dropAllConfirmByTag(Message $message)
Определения NotifyChat.php:517
getPushService(Message $message, SendingConfig $config)
Определения NotifyChat.php:557
static getByUser(?int $userId=null)
Определения NotifyChat.php:70
sendMessage($message, $sendingConfig=null)
Определения NotifyChat.php:188
static find(array $params=[], ?Context $context=null)
Определения NotifyChat.php:86
validateMessage(Message $message, SendingConfig $sendingServiceConfig)
Определения NotifyChat.php:351
getStartId(?int $userId=null)
Определения NotifyChat.php:46
prepareParams(array $params=[])
Определения NotifyChat.php:51
prepareConfirm(Message $message)
Определения NotifyChat.php:470
dropOtherUserNotificationByTag(Message $message)
Определения NotifyChat.php:433
static deleteByChatId(int $chatId, int $userId)
Определения MessageService.php:76
static getInstance()
Определения application.php:98
static getConnection($name="")
Определения application.php:638
Определения result.php:20
static getById($id)
Определения datamanager.php:364
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
static send($userId=null, $appId=self::MOBILE_APP)
Определения mobilecounter.php:187
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
const IM_MESSAGE_SYSTEM
Определения include.php:21
const IM_NOTIFY_SYSTEM
Определения include.php:38
const IM_NOTIFY_CONFIRM
Определения include.php:36
const IM_NOTIFY_FROM
Определения include.php:37
$context
Определения csv_new_setup.php:223
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
Определения Uuid.php:3
Определения culture.php:9
$message
Определения payment.php:8
$config
Определения quickway.php:69
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799