1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
im_notify.php
См. документацию.
1<?
3
4use Bitrix\Im as IM;
12
14{
15 private $user_id = 0;
16 private $bHideLink = false;
17
18 function __construct($user_id = false, $arParams = Array())
19 {
20 global $USER;
21 $this->user_id = intval($user_id);
22 if ($this->user_id == 0)
23 $this->user_id = intval($USER->GetID());
24 if (isset($arParams['HIDE_LINK']) && $arParams['HIDE_LINK'] == 'Y')
25 $this->bHideLink = true;
26 }
27
28 public static function Add($arFields)
29 {
30 $locFields = [
31 'MESSAGE',
32 'NOTIFY_MESSAGE',
33 'MESSAGE_OUT',
34 'NOTIFY_MESSAGE_OUT',
35 'TITLE',
36 'NOTIFY_TITLE',
37 'PUSH_MESSAGE',
38 ];
39
40 foreach ($arFields as $key => $field)
41 {
42 if (in_array($key, $locFields, true))
43 {
44 if (isset($field) && is_callable($field))
45 {
46 $localizeText = self::getTextMessageByLang((int)$arFields['TO_USER_ID'], $field);
47 $arFields[$key] = $localizeText;
48 }
49 }
50 }
51
52 if (isset($arFields['NOTIFY_BUTTONS']) && is_array($arFields['NOTIFY_BUTTONS']))
53 {
54 foreach ($arFields['NOTIFY_BUTTONS'] as $key => $button)
55 {
56 if (isset($button['TITLE']) && is_callable($button['TITLE']))
57 {
58 $localizeText = self::getTextMessageByLang((int)$arFields['TO_USER_ID'], $button['TITLE']);
59 $arFields['NOTIFY_BUTTONS'][$key]['TITLE'] = $localizeText;
60 }
61 }
62 }
63
64 $arFields['MESSAGE_TYPE'] = IM_MESSAGE_SYSTEM;
65
66 return CIMMessenger::Add($arFields);
67 }
68
69 public static function getTextMessageByLang(int $userId, callable $locFunction): ?string
70 {
71 if (!($locFunction instanceof \Closure))
72 {
73 return null;
74 }
75
76 if (!Loader::includeModule('bitrix24'))
77 {
78 return $locFunction(Loc::getCurrentLang(), $userId);
79 }
80
81 $user = IM\V2\Entity\User\User::getInstance($userId);
82 $langId = $user->getLanguageId();
83
84 return $locFunction($langId, $userId);
85 }
86
87 public function GetNotifyList($arParams = array())
88 {
89 global $DB;
90
91 $iNumPage = 1;
92 if (isset($arParams['PAGE']) && intval($arParams['PAGE']) >= 0)
93 $iNumPage = intval($arParams['PAGE']);
94
95 $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N'? false: true;
96
97 $sqlStr = "
98 SELECT COUNT(M.ID) as CNT, M.CHAT_ID
99 FROM b_im_relation R
100 INNER JOIN b_im_message M ON M.CHAT_ID = R.CHAT_ID
101 WHERE R.USER_ID = ".$this->user_id." AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."'
102 GROUP BY M.CHAT_ID
103 ORDER BY M.CHAT_ID
104 ";
105 $res_cnt = $DB->Query($sqlStr);
106 $res_cnt = $res_cnt->Fetch();
107 $cnt = $res_cnt["CNT"] ?? 0;
108 $chatId = $res_cnt["CHAT_ID"] ?? -1;
109
110 $arNotify = Array();
111 if ($cnt > 0)
112 {
113 if (!$bTimeZone)
114 CTimeZone::Disable();
115
116 $strSql ="
117 SELECT
118 M.ID,
119 M.CHAT_ID,
120 M.MESSAGE,
121 M.MESSAGE_OUT,
122 ".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." as DATE_CREATE,
123 M.NOTIFY_TYPE,
124 M.NOTIFY_MODULE,
125 M.NOTIFY_EVENT,
126 M.NOTIFY_TITLE,
127 M.NOTIFY_BUTTONS,
128 M.NOTIFY_TAG,
129 M.NOTIFY_SUB_TAG,
130 M.NOTIFY_READ,
131 $this->user_id as TO_USER_ID,
132 M.AUTHOR_ID as FROM_USER_ID
133 FROM b_im_message M
134 WHERE M.CHAT_ID = ".$chatId." #LIMIT#
135 ORDER BY M.DATE_CREATE DESC, M.ID DESC
136 ";
137 if (!$bTimeZone)
138 CTimeZone::Enable();
139
140 if ($iNumPage == 0)
141 {
142 if ($DB->type == "MSSQL")
143 {
144 $sqlLimit = " AND M.DATE_CREATE > dateadd(day, -30, getdate())";
145 }
146 elseif ($DB->type == "ORACLE")
147 {
148 $sqlLimit = " AND M.DATE_CREATE > SYSDATE-30";
149 }
150 else
151 {
153 $helper = $connection->getSqlHelper();
154 $sqlLimit = " AND M.DATE_CREATE > ". $helper->addDaysToDateTime(-30);
155 }
156
157 $strSql = $DB->TopSql($strSql, 100);
158 $dbRes = $DB->Query(str_replace("#LIMIT#", $sqlLimit, $strSql));
159 }
160 else
161 {
162 $dbRes = new CDBResult();
163 $dbRes->NavQuery(str_replace("#LIMIT#", "", $strSql), $cnt, Array('iNumPage' => $iNumPage, 'nPageSize' => 20));
164 }
165
166 $arGetUsers = Array();
167 $arNotifyId = Array();
168 $arChatId = Array();
169 while ($arRes = $dbRes->Fetch())
170 {
171 if ($this->bHideLink)
172 $arRes['HIDE_LINK'] = 'Y';
173
174 $arNotify[$arRes['ID']] = $arRes;
175 $arNotifyId[$arRes['ID']] = $arRes['ID'];
176 $arGetUsers[] = $arRes['FROM_USER_ID'];
177 $arChatId[$arRes['CHAT_ID']] = $arRes['CHAT_ID'];
178 }
179 if (empty($arNotify))
180 return $arNotify;
181
182 $counters = self::GetCounters($arChatId);
183
184 $params = CIMMessageParam::Get(array_keys($arNotifyId));
185 foreach ($params as $notifyId => $param)
186 {
187 $arNotify[$notifyId]['PARAMS'] = $param;
188 }
189
190 $arUsers = CIMContactList::GetUserData(Array('ID' => $arGetUsers, 'DEPARTMENT' => 'N', 'USE_CACHE' => 'Y', 'CACHE_TTL' => 86400));
191 $arGetUsers = $arUsers['users'];
192
193 $arNotify = $this->fillReadStatuses($arNotify);
194 foreach ($arNotify as $key => $value)
195 {
196 $value['COUNTER'] = $counters[$value['CHAT_ID']];
197 $value['FROM_USER_DATA'] = $arGetUsers;
198 $arNotify[$key] = self::GetFormatNotify($value);
199 }
200 }
201 return $arNotify;
202 }
203
204 public function GetUnreadNotify($arParams = Array())
205 {
206 global $DB;
207
208 $order = isset($arParams['ORDER']) && $arParams['ORDER'] == 'ASC'? 'ASC': 'DESC';
209 $bSpeedCheck = isset($arParams['SPEED_CHECK']) && $arParams['SPEED_CHECK'] == 'N'? false: true;
210 $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N'? false: true;
211 $bGetOnlyFlash = isset($arParams['GET_ONLY_FLASH']) && $arParams['GET_ONLY_FLASH'] == 'Y'? true: false;
212
213 $arNotify['result'] = false;
214 $arNotify['notify'] = Array();
215 $arNotify['unreadNotify'] = Array();
216 $arNotify['loadNotify'] = false;
217 $arNotify['countNotify'] = 0;
218 $arNotify['maxNotify'] = 0;
219
220 $bLoadNotify = $bSpeedCheck? !CIMMessenger::SpeedFileExists($this->user_id, IM_SPEED_NOTIFY): true;
221 if ($bLoadNotify)
222 {
223 /*$strSql =
224 "SELECT
225 CHAT_ID,
226 STATUS
227 FROM
228 b_im_relation
229 WHERE
230 USER_ID = ".$this->user_id."
231 AND MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."'
232 ORDER BY ID ASC";
233 $dbRes = $DB->Query($strSql);
234 if ($arRes = $dbRes->Fetch())
235 {
236 $chatId = intval($arRes['CHAT_ID']);
237 $chatStatus = $arRes['STATUS'];
238 }
239 else
240 return $arNotify;*/
241
242 $result = IM\Model\MessageUnreadTable::query()
243 ->setSelect(['MESSAGE_ID'])
244 ->where('USER_ID', $this->user_id)
245 ->where('CHAT_TYPE', \IM_MESSAGE_SYSTEM) //todo add index
246 ->setLimit(100)
247 ->fetchAll()
248 ;
249
250 $messageIds = array_column($result, 'MESSAGE_ID');
251
252 if (empty($messageIds))
253 {
254 return $arNotify;
255 }
256
257 $implodeMessageIds = implode(',', $messageIds);
258
259 if (!$bTimeZone)
260 CTimeZone::Disable();
261 $strSql ="
262 SELECT
263 M.ID,
264 M.CHAT_ID,
265 M.MESSAGE,
266 M.MESSAGE_OUT,
267 ".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." as DATE_CREATE,
268 M.NOTIFY_TYPE,
269 M.NOTIFY_MODULE,
270 M.NOTIFY_EVENT,
271 M.NOTIFY_TITLE,
272 M.NOTIFY_BUTTONS,
273 M.NOTIFY_TAG,
274 M.NOTIFY_SUB_TAG,
275 M.NOTIFY_READ,
276 $this->user_id as TO_USER_ID,
277 M.AUTHOR_ID as FROM_USER_ID
278 FROM b_im_message M
279 WHERE M.ID IN ({$implodeMessageIds})
280 ORDER BY ID DESC
281 ";
282 if (!$bTimeZone)
283 CTimeZone::Enable();
284 $strSql = $DB->TopSql($strSql, 100);
285 $dbRes = $DB->Query($strSql);
286
287 //$arMark = Array();
288 $arGetUsers = Array();
289 $arNotifyId = Array();
290 $arChatId = Array();
291 while ($arRes = $dbRes->Fetch())
292 {
293 if ($this->bHideLink)
294 $arRes['HIDE_LINK'] = 'Y';
295
296 $arNotifyId[$arRes['ID']] = $arRes['ID'];
297 $arNotify['original_notify'][$arRes['ID']] = $arRes;
298 $arNotify['notify'][$arRes['ID']] = $arRes;
299 $arNotify['unreadNotify'][$arRes['ID']] = $arRes['ID'];
300
301 /*if ($chatStatus == IM_STATUS_UNREAD && (!isset($arMark[$arRes["CHAT_ID"]]) || $arMark[$arRes["CHAT_ID"]] < $arRes["ID"]))
302 $arMark[$arRes["CHAT_ID"]] = $arRes["ID"];*/
303
304 if ($arNotify['maxNotify'] < $arRes['ID'])
305 $arNotify['maxNotify'] = $arRes['ID'];
306
307 $arGetUsers[] = $arRes['FROM_USER_ID'];
308 $arChatId[$arRes["CHAT_ID"]] = $arRes["CHAT_ID"];
309 }
310
311 $params = CIMMessageParam::Get(array_keys($arNotifyId));
312 foreach ($params as $notifyId => $param)
313 {
314 $arNotify['notify'][$notifyId]['PARAMS'] = $param;
315 }
316
317 /*foreach ($arMark as $chatId => $lastSendId)
318 CIMNotify::SetLastSendId($chatId, $lastSendId);*/
319
320 $arNotify['countNotify'] = $this->GetNotifyCounter($arNotify);
321 CIMMessenger::SpeedFileCreate($this->user_id, array('counter' => $arNotify['countNotify'], 'maxId' => $arNotify['maxNotify']), IM_SPEED_NOTIFY);
322
323 $arUsers = CIMContactList::GetUserData(Array('ID' => $arGetUsers, 'DEPARTMENT' => 'N', 'USE_CACHE' => 'Y', 'CACHE_TTL' => 86400));
324 $arGetUsers = $arUsers['users'] ?? null;
325
326 $counters = self::GetCounters($arChatId);
327
328 $arNotify['notify'] = $this->fillReadStatuses($arNotify['notify']);
329 foreach ($arNotify['notify'] as $id => $value)
330 {
331 $value['FROM_USER_DATA'] = $arGetUsers;
332 $value['COUNTER'] = $counters[$value['CHAT_ID']];
333 if (!$bTimeZone)
334 {
335 $dateTime = \Bitrix\Main\Type\DateTime::createFromTimestamp($value['DATE_CREATE']);
336 $value['DATE_CREATE'] = $dateTime->toUserTime()->getTimestamp();
337 }
338 $arNotify['notify'][$id] = self::GetFormatNotify($value);
339 }
340
341 $arNotify['result'] = true;
342 }
343 else
344 {
345 $cache = CIMMessenger::SpeedFileGet($this->user_id, IM_SPEED_NOTIFY);
346 $arNotify['countNotify'] = $cache? $cache['counter']: 0;
347 $arNotify['maxNotify'] = $cache? $cache['maxId']: 0;
348 if ($arNotify['countNotify'] > 0)
349 $arNotify['loadNotify'] = true;
350 }
351
352 return $arNotify;
353 }
354
355 public static function GetUnsendNotify() //todo refactor mail send
356 {
357 global $DB;
358
359 $mailService = new IM\V2\Mail();
360 $unsendIds = $mailService->getNotificationIdsToSend(5000);
361
362 if (empty($unsendIds))
363 {
364 return [];
365 }
366
367 $implodeUnsendIds = implode(',', $unsendIds);
368
369 CTimeZone::Disable();
370 $strSql ="
371 SELECT
372 M.ID,
373 M.CHAT_ID,
374 M.MESSAGE,
375 M.MESSAGE_OUT,
376 ".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." as DATE_CREATE,
377 M.NOTIFY_TYPE,
378 M.NOTIFY_MODULE,
379 M.NOTIFY_EVENT,
380 M.NOTIFY_TITLE,
381 M.NOTIFY_BUTTONS,
382 M.NOTIFY_TAG,
383 M.NOTIFY_SUB_TAG,
384 M.EMAIL_TEMPLATE,
385 M.AUTHOR_ID as FROM_USER_ID,
386 U2.LOGIN as FROM_USER_LOGIN,
387 U2.NAME as FROM_USER_NAME,
388 U2.LAST_NAME as FROM_USER_LAST_NAME,
389 U2.SECOND_NAME as FROM_USER_SECOND_NAME,
390 U2.EXTERNAL_AUTH_ID as FROM_EXTERNAL_AUTH_ID,
391 C.AUTHOR_ID as TO_USER_ID,
392 U1.LOGIN as TO_USER_LOGIN,
393 U1.NAME as TO_USER_NAME,
394 U1.LAST_NAME as TO_USER_LAST_NAME,
395 U1.SECOND_NAME as TO_USER_SECOND_NAME,
396 U1.EMAIL as TO_USER_EMAIL,
397 U1.ACTIVE as TO_USER_ACTIVE,
398 U1.LID as TO_USER_LID,
399 U1.TIME_ZONE as TIME_ZONE,
400 U1.EXTERNAL_AUTH_ID as TO_EXTERNAL_AUTH_ID
401 FROM b_im_message M
402 LEFT JOIN b_user U2 ON U2.ID = M.AUTHOR_ID
403 LEFT JOIN b_im_chat C ON M.CHAT_ID = C.ID
404 LEFT JOIN b_user U1 ON U1.ID = C.AUTHOR_ID
405 WHERE M.ID IN ({$implodeUnsendIds})
406 ORDER BY M.ID DESC
407 ";
408 $dbRes = $DB->Query($strSql);
409 CTimeZone::Enable();
410
411 $arNotify = Array();
412 while ($arRes = $dbRes->Fetch())
413 {
414 $arRes["DATE_CREATE"] = $arRes["DATE_CREATE"] + CIMMail::GetUserOffset($arRes);
415 $arNotify[$arRes['ID']] = $arRes;
416 }
417
418 return $arNotify;
419 }
420
421 public function GetNotify($ID)
422 {
423 global $DB;
424
425 $ID = intval($ID);
426
427 $strSql = "SELECT M.* FROM b_im_relation R, b_im_message M WHERE M.ID = ".$ID." AND R.USER_ID = ".$this->user_id." AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."' AND R.CHAT_ID = M.CHAT_ID";
428 $dbRes = $DB->Query($strSql);
429 if ($arRes = $dbRes->Fetch())
430 return $arRes;
431
432 return false;
433 }
434
435 public static function GetFormatNotify(array $arFields): array
436 {
437 $messageText = \Bitrix\Im\Text::parse(
438 \Bitrix\Im\Text::convertHtmlToBbCode($arFields['MESSAGE']),
439 [
440 'LINK' => (isset($arFields['HIDE_LINK']) && $arFields['HIDE_LINK'] === 'Y') ? 'N' : 'Y',
441 'LINK_TARGET_SELF' => 'Y',
442 'SAFE' => 'N',
443 'FONT' => 'Y',
444 'SMILES' => 'N',
445 ]
446 );
447 if (!isset($arFields["FROM_USER_DATA"]))
448 {
449 $arUsers = CIMContactList::GetUserData(Array('ID' => $arFields['FROM_USER_ID'], 'DEPARTMENT' => 'N', 'USE_CACHE' => 'Y', 'CACHE_TTL' => 86400));
450 $arFields["FROM_USER_DATA"] = $arUsers['users'] ?? null;
451 }
452 $users = [];
453 if ((int)$arFields['FROM_USER_ID'] > 0)
454 {
455 $user = User::getInstance((int)$arFields['FROM_USER_ID'])->getArray([
456 'JSON' => 'Y',
457 'SKIP_ONLINE' => 'Y'
458 ]);
459 if (isset($arFields['FROM_USER_DATA'][$arFields['FROM_USER_ID']]))
460 {
461 $user['idle'] = $arFields['FROM_USER_DATA'][$arFields['FROM_USER_ID']]['idle'] ?? false;
462 $user['last_activity_date'] = $arFields['FROM_USER_DATA'][$arFields['FROM_USER_ID']]['last_activity_date'] ?? false;
463 $user['mobile_last_date'] = $arFields['FROM_USER_DATA'][$arFields['FROM_USER_ID']]['mobile_last_date'] ?? false;
464 }
465 $users[] = $user;
466 }
467
468 $arNotify = [
469 'id' => $arFields['ID'],
470 'type' => $arFields['NOTIFY_TYPE'],
472 'silent' => ($arFields['NOTIFY_SILENT'] ?? null) ? 'Y' : 'N',
473 'onlyFlash' => (bool)($arFields['NOTIFY_ONLY_FLASH'] ?? false),
474 'link' => (string)($arFields['NOTIFY_LINK'] ?? ''),
475 'text' => $messageText,
476 'tag' => $arFields['NOTIFY_TAG'] != '' ? md5($arFields['NOTIFY_TAG']): '',
477 'originalTag' => $arFields['NOTIFY_TAG'],
478 'original_tag' => $arFields['NOTIFY_TAG'],
479 'read' => $arFields['NOTIFY_READ'] ?? null,
480 'settingName' => $arFields['NOTIFY_MODULE'] . '|' . $arFields['NOTIFY_EVENT'],
481 'params' => $arFields['PARAMS'] ?? [],
482 'counter' => isset($arFields['COUNTER']) ? (int)$arFields['COUNTER'] : 0,
483 'users' => $users
484 ];
485
486 $arNotify['userId'] = $arFields["FROM_USER_ID"];
487 $arNotify['userName'] = $arFields["FROM_USER_DATA"][$arFields["FROM_USER_ID"]]['name'] ?? null;
488 $arNotify['userColor'] = $arFields["FROM_USER_DATA"][$arFields["FROM_USER_ID"]]['color'] ?? null;
489 $arNotify['userAvatar'] = $arFields["FROM_USER_DATA"][$arFields["FROM_USER_ID"]]['avatar'] ?? null;
490 $arNotify['userLink'] = $arFields["FROM_USER_DATA"][$arFields["FROM_USER_ID"]]['profile'] ?? null;
491
492 if ($arFields['NOTIFY_TYPE'] == IM_NOTIFY_CONFIRM)
493 {
494 $arNotify['buttons'] = unserialize($arFields['NOTIFY_BUTTONS'], ['allowed_classes' => false]);
495 }
496 else
497 {
498 $arNotify['title'] = htmlspecialcharsbx($arFields['NOTIFY_TITLE']);
499 }
500
501 return $arNotify;
502 }
503
504 public function MarkNotifyRead($id = 0, $setThisAndHigher = false, $appId = 'Bitrix24')
505 {
506 $id = intval($id);
507 if ($id < 0)
508 return false;
509 /*global $DB;
510
511
512
513 if ($id === 0)
514 {
515 $query = [
516 'select' => ['CHAT_ID'],
517 'filter' => [
518 '=USER_ID' => $this->user_id,
519 '=MESSAGE_TYPE' => IM_MESSAGE_SYSTEM
520 ]
521 ];
522
523 $chatResult = \Bitrix\Im\Model\RelationTable::getList($query)->fetch();
524 $chatId = (int)$chatResult['CHAT_ID'];
525 if (!$chatId)
526 {
527 return false;
528 }
529 }
530 else
531 {
532 $query = [
533 'select' => ['ID', 'CHAT_ID', 'NOTIFY_TAG'],
534 'filter' => [
535 '=RELATION.MESSAGE_TYPE' => IM_MESSAGE_SYSTEM,
536 '=RELATION.USER_ID' => $this->user_id,
537 '=ID' => $id
538 ]
539 ];
540
541 $message = \Bitrix\Im\Model\MessageTable::getList($query)->fetch();
542 if (!$message)
543 {
544 return false;
545 }
546
547 $chatId = (int)$message['CHAT_ID'];
548 }
549
550 $messages = array();
551
552 $filter = [
553 '=CHAT_ID' => $chatId,
554 '=NOTIFY_READ' => 'N',
555 ];
556 if ($id > 0)
557 {
558 $filterId = ($setThisAndHigher? '>=': '=').'ID';
559 $filter[$filterId] = $id;
560 }
561 $orm = \Bitrix\Im\Model\MessageTable::getList(Array(
562 'select' => ['ID', 'CHAT_ID', 'NOTIFY_TAG'],
563 'filter' => $filter
564 ));
565 while ($row = $orm->fetch())
566 {
567 $messages[$row['ID']] = $row;
568 }
569
570 if ($messages)
571 {
572 $DB->Query("UPDATE b_im_message SET NOTIFY_READ = 'Y' WHERE ID IN (".implode(',', array_keys($messages)).")");
573 }
574
575 if (!empty($message['NOTIFY_TAG']))
576 {
577 $orm = \Bitrix\Im\Model\MessageTable::getList(Array(
578 'select' => Array('ID', 'CHAT_ID', 'NOTIFY_TYPE'),
579 'filter' => Array(
580 '=CHAT_ID' => $chatId,
581 '=NOTIFY_READ' => 'N',
582 '=NOTIFY_TAG' => $message['NOTIFY_TAG'],
583 )
584 ));
585 $tagIds = Array();
586 while ($row = $orm->fetch())
587 {
588 $messages[$row['ID']] = $row;
589 $tagIds[] = $row['ID'];
590 }
591 if ($tagIds)
592 {
593 $DB->Query("UPDATE b_im_message SET NOTIFY_READ = 'Y' WHERE ID IN (".implode(',', $tagIds).")");
594 }
595 }
596
597 if (!$messages)
598 {
599 self::SetLastId($chatId, $this->user_id);
600 return false;
601 }
602
603 $lastId = max(array_keys($messages));
604
605 self::SetLastId($chatId, $this->user_id, $lastId);*/
606
607 $findResult = NotifyChat::find(['TO_USER_ID' => $this->user_id]);
608
609 if (!$findResult->isSuccess())
610 {
611 return false;
612 }
613
614 $chatId = (int)$findResult->getResult()['ID'];
615
616 $readService = new IM\V2\Message\ReadService($this->user_id);
617 $counterService = $readService->getCounterService();
618
619 if ($id === 0)
620 {
621 $readService->readAllInChat($chatId);
622 if (CModule::IncludeModule("pull"))
623 {
624 \Bitrix\Pull\Event::add($this->user_id, Array(
625 'module_id' => 'im',
626 'command' => 'notifyReadAll',
627 'params' => Array(
628 'chatId' => $chatId,
629 ),
630 'extra' => \Bitrix\Im\Common::getPullExtra()
631 ));
632
633 \Bitrix\Pull\MobileCounter::send($this->user_id, $appId);
634 }
635
636 return true;
637 }
638
639 $messages = [];
640
641 $operator = $setThisAndHigher ? '>=' : '=';
642 $query = IM\Model\MessageUnreadTable::query()
643 ->setSelect(['MESSAGE_ID'])
644 ->where('USER_ID', $this->user_id)
645 ->where('CHAT_ID', $chatId)
646 ->where('MESSAGE_ID', $operator, $id)
647 ->exec()
648 ;
649
650 $messageCollection = new IM\V2\MessageCollection();
651
652 while ($row = $query->fetch())
653 {
654 $messages[] = (int)$row['MESSAGE_ID'];
656 $message->setMessageId((int)$row['MESSAGE_ID'])->setChatId($chatId)->setAuthorId(0);
657 $messageCollection->add($message);
658 }
659
660 if (empty($messages))
661 {
662 return false;
663 }
664
665 $counter = $readService->readNotifications($messageCollection, [$chatId => $this->user_id])->getResult()['COUNTERS'][$chatId];
666
667 if (CModule::IncludeModule("pull"))
668 {
669 \Bitrix\Pull\Event::add($this->user_id, Array(
670 'module_id' => 'im',
671 'command' => 'notifyRead',
672 'params' => Array(
673 'chatId' => $chatId,
674 'list' => $messages,
675 'counter' => $counter
676 ),
677 'extra' => \Bitrix\Im\Common::getPullExtra()
678 ));
679
680 \Bitrix\Pull\MobileCounter::send($this->user_id, $appId);
681 }
682
683 CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_NOTIFY);
684
685 return true;
686 }
687
688 public function MarkNotifyReadBySubTag($subTagList = array())
689 {
690 global $DB;
691
692 if (empty($subTagList))
693 {
694 return false;
695 }
696
697 if (!is_array($subTagList))
698 {
699 $subTagList = array($subTagList);
700 }
701
702 $result = IM\Model\MessageTable::query()
703 ->setSelect(['ID', 'CHAT_ID', 'USER_ID' => 'RELATION.USER_ID'])
704 ->whereIn('NOTIFY_SUB_TAG', $subTagList)
705 ->withUnreadOnly()
706 ->exec()
707 ;
708
709 /*$sqlTags = Array();
710 foreach ($subTagList as $value)
711 {
712 $value = (string)$value;
713 $sqlTags[] = "'".$DB->ForSQL($value)."'";
714 }*/
715
716 $users = array();
717 $chats = array();
718 $messages = array();
719 $messagesByUser = array();
720
721 $messageCollection = new IM\V2\MessageCollection();
722
723 while ($row = $result->fetch())
724 {
725 $messages[] = (int)$row['ID'];
726 $users[$row['CHAT_ID']] = $row['USER_ID'];
727 $chats[$row['CHAT_ID']] = $row['CHAT_ID'];
728 $messagesByUser[$row['CHAT_ID']][] = $row['ID'];
729 $message = new IM\V2\Message();
730 $message->setMessageId((int)$row['ID'])->setChatId((int)$row['CHAT_ID']);
731 $messageCollection->add($message);
732 }
733
734 if (empty($messages))
735 {
736 return true;
737 }
738
739 $readService = new IM\V2\Message\ReadService();
740 $counters = $readService->readNotifications($messageCollection, $users)->getResult()['COUNTERS'];
741
742 $isLoadPull = Loader::includeModule("pull");
743 foreach ($messagesByUser as $chatId => $messagesList)
744 {
745 //\Bitrix\Im\Counter::clearCache($users[$chatId]);
746 CIMMessenger::SpeedFileDelete($users[$chatId], IM_SPEED_NOTIFY);
747
748 if ($isLoadPull)
749 {
750 \Bitrix\Pull\Event::add($users[$chatId], Array(
751 'module_id' => 'im',
752 'command' => 'notifyRead',
753 'params' => Array(
754 'chatId' => $chatId,
755 'list' => array_values($messagesList),
756 'counter' => (int)$counters[$chatId]
757 ),
758 'extra' => \Bitrix\Im\Common::getPullExtra()
759 ));
760 }
761 }
762
763 /*$strSql ="
764 SELECT M.ID, M.CHAT_ID, R.USER_ID
765 FROM b_im_message M
766 LEFT JOIN b_im_relation R ON R.CHAT_ID = M.CHAT_ID
767 WHERE
768 M.NOTIFY_SUB_TAG IN (".implode(",", $sqlTags).")
769 AND M.NOTIFY_READ='N'";
770 $res = $DB->Query($strSql);
771 while ($message = $res->fetch())
772 {
773 $messages[$message["ID"]] = $message;
774 $messagesByUser[$message["CHAT_ID"]][] = $message["ID"];
775 $users[$message["CHAT_ID"]] = $message["USER_ID"];
776 $chats[$message["CHAT_ID"]] = $message["CHAT_ID"];
777 }
778
779 if (!empty($messages))
780 {
781 $strSql ="UPDATE b_im_message SET NOTIFY_READ = 'Y' WHERE ID IN (".implode(",", array_keys($messages)).")";
782 $DB->Query($strSql);
783
784 $counters = \CIMNotify::GetRealCounters($chats);
785 foreach ($counters as $chatId => $counter)
786 {
787 $DB->Query("UPDATE b_im_relation SET COUNTER = {$counter} WHERE CHAT_ID = {$chatId}");
788 }
789
790 $isLoadPull = Loader::includeModule("pull");
791 foreach ($messagesByUser as $chatId => $messagesList)
792 {
793 \Bitrix\Im\Counter::clearCache($users[$chatId]);
794 CIMMessenger::SpeedFileDelete($users[$chatId], IM_SPEED_NOTIFY);
795
796 if ($isLoadPull)
797 {
798 \Bitrix\Pull\Event::add($users[$chatId], Array(
799 'module_id' => 'im',
800 'command' => 'notifyRead',
801 'params' => Array(
802 'chatId' => $chatId,
803 'list' => array_values($messagesList),
804 'counter' => (int)$counters[$chatId]
805 ),
806 'extra' => \Bitrix\Im\Common::getPullExtra()
807 ));
808 }
809 }
810 }*/
811
812 return true;
813 }
814
815 public function MarkNotifyUnRead($id = 0, $setThisAndHigher = false, $appId = 'Bitrix24')
816 {
817 global $DB;
818
819 $id = intval($id);
820 if ($id <= 0)
821 return false;
822
823 $startNotify = \Bitrix\Im\Model\MessageTable::getList(Array(
824 'select' => Array('ID', 'CHAT_ID', 'NOTIFY_TAG'),
825 'filter' => Array(
826 '=ID' => $id,
827 '=RELATION.MESSAGE_TYPE' => IM_MESSAGE_SYSTEM,
828 '=RELATION.USER_ID' => $this->user_id,
829 )
830 ))->fetch();
831 if (!$startNotify)
832 {
833 return false;
834 }
835
836 $notifyTag = $startNotify['NOTIFY_TAG'] ?? null;
837
838 $chatId = intval($startNotify['CHAT_ID']);
839
840 $operator = $setThisAndHigher ? '>=' : '=';
841 $notifyByIdResult = IM\Model\MessageTable::query()
842 ->setSelect(['ID'])
843 ->where('CHAT_ID', $chatId)
844 ->where('ID', $operator, $id)
845 ->withReadOnly()
846 ->exec()
847 ;
848
849 $messages = new IM\V2\MessageCollection();
850
851 while ($notifyById = $notifyByIdResult->fetch())
852 {
853 $message = new IM\V2\Message();
854 $message->setMessageId((int)$notifyById['ID'])->setChatId($chatId)->setAuthorId(0);
855 $messages->add($message);
856 }
857
858 if ($notifyTag !== null && $notifyTag !== '')
859 {
860 $notifyByTagResult = IM\Model\MessageTable::query()
861 ->setSelect(['ID'])
862 ->where('CHAT_ID', $chatId)
863 ->where('NOTIFY_TAG', $notifyTag)
864 ->withReadOnly()
865 ->exec()
866 ;
867
868 while ($notifyByTag = $notifyByTagResult->fetch())
869 {
870 $message = new IM\V2\Message();
871 $message->setMessageId((int)$notifyByTag['ID'])->setChatId($chatId)->setAuthorId(0);
872 $messages->add($message);
873 }
874 }
875
876 $readService = new IM\V2\Message\ReadService($this->user_id);
877
878 $relation = new IM\V2\Relation();
879 $relation->setChatId($chatId)->setUserId($this->user_id)->setMessageType('S')->setNotifyBlock(false);
880
881 $counter = $readService->unreadNotifications($messages, $relation)->getResult()['COUNTER'];
882
883/* $filterId = ($setThisAndHigher? '>=': '=').'ID';
884 $orm = \Bitrix\Im\Model\MessageTable::getList(Array(
885 'select' => Array('ID', 'CHAT_ID', 'NOTIFY_TAG'),
886 'filter' => Array(
887 '=CHAT_ID' => $chatId,
888 '=NOTIFY_READ' => 'Y',
889 $filterId => $id,
890 )
891 ));
892 while ($row = $orm->fetch())
893 {
894 $messages[$row['ID']] = $row;
895 }
896
897 if ($messages)
898 {
899 $DB->Query("UPDATE b_im_message SET NOTIFY_READ = 'N' WHERE ID IN (".implode(',', array_keys($messages)).")");
900 }
901
902 if (!empty($startNotify['NOTIFY_TAG']))
903 {
904 $orm = \Bitrix\Im\Model\MessageTable::getList(Array(
905 'select' => Array('ID', 'CHAT_ID', 'NOTIFY_TYPE', 'NOTIFY_READ'),
906 'filter' => Array(
907 '=CHAT_ID' => $chatId,
908 '=NOTIFY_READ' => 'Y',
909 '=NOTIFY_TAG' => $startNotify['NOTIFY_TAG'],
910 )
911 ));
912 $tagIds = Array();
913 while ($row = $orm->fetch())
914 {
915 $messages[$row['ID']] = $row;
916 $tagIds[] = $row['ID'];
917 }
918
919 if ($tagIds)
920 {
921 $DB->Query("UPDATE b_im_message SET NOTIFY_READ = 'N' WHERE ID IN (".implode(',', $tagIds).")");
922 }
923 }
924
925 if (!$messages)
926 {
927 return false;
928 }
929
930 $lastId = max(array_keys($messages));
931
932 self::SetLastId($chatId, $this->user_id, $lastId);*/
933
934 if (CModule::IncludeModule("pull"))
935 {
936 \Bitrix\Pull\Event::add($this->user_id, Array(
937 'module_id' => 'im',
938 'command' => 'notifyUnread',
939 'params' => Array(
940 'chatId' => $chatId,
941 'list' => $messages->getIds(),
942 'counter' => $counter
943 ),
944 'extra' => \Bitrix\Im\Common::getPullExtra()
945 ));
946
947 \Bitrix\Pull\MobileCounter::send($this->user_id, $appId);
948 }
949
950 CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_NOTIFY);
951
952 return true;
953 }
954
955 public static function SetLastId($chatId, $userId, $lastId = null)
956 {
957 /*global $DB;
958
959 if (intval($chatId) <= 0 || intval($userId) <= 0)
960 return false;
961
962 $ssqlLastId = "";
963 if (!is_null($lastId))
964 {
965 $ssqlLastId = "LAST_ID = (case when LAST_ID < ".intval($lastId)." then ".intval($lastId)." else LAST_ID end),";
966 }
967
968 $counter = \CIMNotify::GetRealCounter($chatId);
969
970 $status = "STATUS = ".IM_STATUS_READ.", ";
971 if ($counter > 0)
972 {
973 $status = "STATUS = (case when STATUS = ".IM_STATUS_READ." then ".IM_STATUS_UNREAD." else STATUS end), ";
974 }
975
976 $DB->Query("UPDATE b_im_relation SET ".$ssqlLastId." ".$status." COUNTER = ".$counter." WHERE CHAT_ID = ".intval($chatId));
977
978 //if (!is_null($lastId))
979 //{
980 // CIMNotify::SetLastSendId($chatId, $lastId);
981 //}
982
983 \Bitrix\Im\Counter::clearCache($userId);*/
984
985 return true;
986 }
987
995 public static function SetLastSendId($chatId, $lastSendId)
996 {
997 /*global $DB;
998
999 if (intval($chatId) <= 0 || intval($lastSendId) <= 0)
1000 return false;
1001
1002 $strSql = "
1003 UPDATE b_im_relation SET
1004 LAST_SEND_ID = (case when LAST_SEND_ID < ".intval($lastSendId)." then ".intval($lastSendId)." else LAST_SEND_ID end),
1005 STATUS = ".IM_STATUS_NOTIFY."
1006 WHERE CHAT_ID = ".intval($chatId);
1007 $DB->Query($strSql);*/
1008
1009 return true;
1010 }
1011
1012 public function Confirm($id, $value)
1013 {
1014 global $DB;
1015
1016 $id = intval($id);
1017
1018 $strSql = "
1019 SELECT M.*
1020 FROM b_im_relation R, b_im_message M
1021 WHERE M.ID = ".$id." AND R.USER_ID = ".$this->user_id." AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."' AND R.CHAT_ID = M.CHAT_ID AND M.NOTIFY_TYPE = ".IM_NOTIFY_CONFIRM;
1022 $dbRes = $DB->Query($strSql);
1023 if (!($arRes = $dbRes->Fetch()))
1024 return false;
1025
1026 $arRes['RELATION_USER_ID'] = $this->user_id;
1027 $arRes['NOTIFY_BUTTONS'] = unserialize($arRes['NOTIFY_BUTTONS'], ['allowed_classes' => false]);
1028
1029 $resultMessages = Array();
1030 if ($arRes['NOTIFY_TAG'] <> '')
1031 {
1032 $CBXSanitizer = new CBXSanitizer;
1033 $CBXSanitizer->AddTags(array(
1034 'a' => array('href','style', 'target'),
1035 'b' => array(), 'u' => array(),
1036 'i' => array(), 'br' => array(),
1037 'span' => array('style'),
1038 ));
1039
1040 foreach(GetModuleEvents("im", "OnBeforeConfirmNotify", true) as $arEvent)
1041 {
1042 $resultEvent = ExecuteModuleEventEx($arEvent, Array($arRes['NOTIFY_MODULE'], $arRes['NOTIFY_TAG'], $value, $arRes));
1043 if($resultEvent===false || is_array($resultEvent) && $resultEvent['result'] === false)
1044 {
1045 $resultMessages = Array();
1046 if (is_array($resultEvent) && $resultEvent['text'])
1047 {
1048 $resultMessages[] = $CBXSanitizer->SanitizeHtml($resultEvent['text']);
1049 }
1050 break;
1051 }
1052 else if (is_array($resultEvent) && $resultEvent['text'] || is_string($resultEvent) && $resultEvent <> '')
1053 {
1054 $resultMessages[] = $CBXSanitizer->SanitizeHtml(is_string($resultEvent)? $resultEvent: $resultEvent['text']);
1055 }
1056 }
1057 }
1058 if (empty($resultMessages))
1059 {
1060 foreach ($arRes['NOTIFY_BUTTONS'] as $button)
1061 {
1062 if ($button['VALUE'] == $value)
1063 {
1064 $resultMessages[] = GetMessage('IM_CONFIRM_CHOICE', Array('#BUTTON#' => $button['TITLE']));
1065 break;
1066 }
1067 }
1068 }
1069
1070 self::Delete($id);
1071
1072 if ($arRes['NOTIFY_TAG'] <> '')
1073 {
1074 foreach(GetModuleEvents("im", "OnAfterConfirmNotify", true) as $arEvent)
1075 ExecuteModuleEventEx($arEvent, array($arRes['NOTIFY_MODULE'], $arRes['NOTIFY_TAG'], $value, $arRes, $resultMessages));
1076 }
1077
1078 if (CModule::IncludeModule("pull"))
1079 {
1080 \Bitrix\Pull\Event::add($this->user_id, Array(
1081 'module_id' => 'im',
1082 'command' => 'notifyConfirm',
1083 'params' => Array(
1084 'id' => $id,
1085 'chatId' => intval($arRes['CHAT_ID']),
1086 'confirmMessages' => $resultMessages,
1087 'counter' => self::GetRealCounter($arRes['CHAT_ID']),
1088 ),
1089 'extra' => \Bitrix\Im\Common::getPullExtra()
1090 ));
1091 }
1092
1093 CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_NOTIFY);
1094
1095 return $resultMessages;
1096 }
1097
1098 public function Answer($id, $text)
1099 {
1100 global $DB;
1101
1102 $id = intval($id);
1103 $text = trim($text);
1104 if ($id <= 0 || $text == '')
1105 return false;
1106
1107 $strSql = "
1108 SELECT M.*
1109 FROM b_im_relation R, b_im_message M
1110 WHERE M.ID = ".$id." AND R.USER_ID = ".$this->user_id." AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."' AND R.CHAT_ID = M.CHAT_ID
1111 ";
1112 $dbRes = $DB->Query($strSql);
1113 if (!($arRes = $dbRes->Fetch()))
1114 return false;
1115
1116 $mention = \Bitrix\Im\User::getInstance($arRes['AUTHOR_ID']);
1117 if ($mention->isExists())
1118 {
1119 $text = '[USER='.$mention->getId().']'.$mention->getFullName(false).'[/USER] '.$text;
1120 }
1121
1122 $CBXSanitizer = new CBXSanitizer;
1123 $CBXSanitizer->AddTags(array(
1124 'a' => array('href','style', 'target'),
1125 'b' => array(), 'u' => array(),
1126 'i' => array(), 'br' => array(),
1127 'span' => array('style'),
1128 ));
1129
1130 foreach(GetModuleEvents("im", "OnAnswerNotify", true) as $arEvent)
1131 {
1132 $resultEvent = ExecuteModuleEventEx($arEvent, Array($arRes['NOTIFY_MODULE'], $arRes['NOTIFY_TAG'], $text, $arRes));
1133 if($resultEvent===false || is_array($resultEvent) && $resultEvent['result'] === false)
1134 {
1135 $resultMessages = Array();
1136 if (is_array($resultEvent) && $resultEvent['text'])
1137 {
1138 $resultMessages[] = $CBXSanitizer->SanitizeHtml($resultEvent['text']);
1139 }
1140 break;
1141 }
1142 else if (is_array($resultEvent) && $resultEvent['text'] || is_string($resultEvent) && $resultEvent <> '')
1143 {
1144 $resultMessages[] = $CBXSanitizer->SanitizeHtml(is_string($resultEvent)? $resultEvent: $resultEvent['text']);
1145 }
1146 }
1147
1148 if (empty($resultMessages))
1149 {
1150 $resultMessages[] = GetMessage('IM_ANSWER_DONE');
1151 }
1152
1153 return $resultMessages;
1154 }
1155
1156 public static function Delete($id)
1157 {
1158 $id = (int)$id;
1159 $notification = self::getNotificationById($id);
1160 if (!$notification)
1161 {
1162 return false;
1163 }
1164
1165 $message = (new IM\V2\Message())->setMessageId($id)->setChatId((int)$notification['CHAT_ID']);
1166 self::deleteInternal($message, (int)$notification['RELATION_USER_ID']);
1167 $counter = self::GetRealCounter($notification['CHAT_ID']);
1168 $chatId = (int)$notification['CHAT_ID'];
1169
1170 self::updateStateAfterDelete($chatId, $notification['RELATION_USER_ID']);
1171
1172 foreach(GetModuleEvents("im", "OnAfterDeleteNotify", true) as $arEvent)
1173 {
1174 ExecuteModuleEventEx($arEvent, [$id, $notification]);
1175 }
1176
1177 if (CModule::IncludeModule("pull"))
1178 {
1179
1181 $notification['RELATION_USER_ID'],
1182 [
1183 'module_id' => 'im',
1184 'command' => 'notifyDelete',
1185 'params' => [
1186 'id' => [$id => $notification['NOTIFY_TYPE']],
1187 'counter' => $counter
1188 ],
1189 'extra' => \Bitrix\Im\Common::getPullExtra()
1190 ]
1191 );
1192 }
1193
1194 return true;
1195 }
1196
1197 public static function deleteList(array $ids, array $params = []): bool
1198 {
1199 if (empty($ids))
1200 {
1201 return false;
1202 }
1203
1204 $chatIds = [];
1205 $relationUserIds = [];
1206 $resultDataForPushAndPull = [];
1207 $messageCollection = new IM\V2\MessageCollection();
1208
1209 $notifyList = self::getNotificationList($ids);
1210 foreach ($notifyList as $notify)
1211 {
1212 $notifyId = (int)$notify['ID'];
1213
1214 $message = (new IM\V2\Message())
1215 ->setMessageId($notifyId)
1216 ->setChatId((int)$notify['CHAT_ID'])
1217 ;
1218 $messageCollection->add($message);
1219
1220 $chatIds[(int)$notify['CHAT_ID']] = (int)$notify['CHAT_ID'];
1221 $relationUserIds[(int)$notify['CHAT_ID']] = (int)$notify['RELATION_USER_ID'];
1222 $resultDataForPushAndPull[$notifyId] = $notify['NOTIFY_TYPE'];
1223 }
1224
1225 self::deleteListInternal($messageCollection, $relationUserIds);
1226
1227 foreach ($notifyList as $notify)
1228 {
1229 $notifyId = (int)$notify['ID'];
1230 foreach(GetModuleEvents("im", "OnAfterDeleteNotify", true) as $arEvent)
1231 {
1232 ExecuteModuleEventEx($arEvent, [$notifyId, $notify]);
1233 }
1234 }
1235
1236 if (empty($resultDataForPushAndPull))
1237 {
1238 return false;
1239 }
1240
1241 $counters = self::GetCounters(array_values($chatIds));
1242
1243 foreach ($chatIds as $chatId)
1244 {
1245 $userId = $relationUserIds[$chatId];
1246 $counter = $counters[$chatId] ?? 0;
1248 self::updateStateAfterDelete($chatId, $userId);
1249
1250 if (CModule::IncludeModule("pull"))
1251 {
1252 if (isset($params['NOTIFY_TAG']))
1253 {
1254 CPushManager::DeleteFromQueueByTag($userId, $params['NOTIFY_TAG']);
1255 }
1256 elseif (isset($params['NOTIFY_SUB_TAG']))
1257 {
1258 CPushManager::DeleteFromQueueBySubTag($userId, $params['NOTIFY_SUB_TAG']);
1259 }
1260
1262 $userId,
1263 [
1264 'module_id' => 'im',
1265 'command' => 'notifyDelete',
1266 'params' => [
1267 'id' => $resultDataForPushAndPull,
1268 'counter' => $counter
1269 ],
1270 'extra' => \Bitrix\Im\Common::getPullExtra()
1271 ]
1272 );
1274 }
1275 }
1276
1277 return true;
1278 }
1279
1280 /*
1281 * Updates counters (unread and total) and last message for recent list.
1282 */
1283 private static function updateStateAfterDelete(int $chatId, int $userId): void
1284 {
1285 CIMMessenger::SpeedFileDelete($userId, IM_SPEED_NOTIFY);
1286
1287 // update total amount of notifications
1288 $date = new DateTime();
1289 $date->add('-60 days'); // sync with \Bitrix\Im\Notify::cleanNotifyAgent
1290 $messageCount = \Bitrix\Im\Model\MessageTable::getCount([
1291 '=CHAT_ID' => $chatId,
1292 '>DATE_CREATE' => $date
1293 ]);
1294
1295 IM\Model\ChatTable::update($chatId, ['MESSAGE_COUNT' => $messageCount]);
1296 }
1297
1298 private static function deleteInternal(IM\V2\Message $message, int $userId): void
1299 {
1301 \Bitrix\Im\Model\MessageTable::delete($message->getId());
1302 (new IM\V2\Message\ReadService())->deleteByMessage($message, [$userId]);
1303 }
1304
1305 private static function deleteListInternal(IM\V2\MessageCollection $messages, array $userIds): void
1306 {
1307 $messageIds = $messages->getIds();
1308 if (empty($messageIds))
1309 {
1310 return;
1311 }
1312
1313 IM\Model\MessageParamTable::deleteBatch(['=MESSAGE_ID' => $messageIds]);
1314 IM\Model\MessageTable::deleteBatch(['=ID' => $messageIds]);
1315 (new IM\V2\Message\ReadService())->deleteByMessages($messages, $userIds);
1316 }
1317
1318 private static function getNotificationById(int $id): ?array
1319 {
1320 global $DB;
1321
1322 $sqlQuery = "
1323 SELECT M.*, R.USER_ID as RELATION_USER_ID
1324 FROM b_im_message M
1325 LEFT JOIN b_im_relation R ON R.CHAT_ID = M.CHAT_ID
1326 WHERE M.ID = ".$id." AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."'
1327 ";
1328 $dbRes = $DB->Query($sqlQuery);
1329 $result = $dbRes->Fetch();
1330 if (!$result)
1331 {
1332 return null;
1333 }
1334
1335 return $result;
1336 }
1337
1338 private static function getNotificationList(array $messageIds): array
1339 {
1340 if (empty($messageIds))
1341 {
1342 return [];
1343 }
1344
1345 return IM\Model\MessageTable::query()
1346 ->setSelect(['*', 'RELATION_USER_ID' => 'RELATION.USER_ID'])
1347 ->registerRuntimeField(new Reference(
1348 'RELATION',
1349 IM\Model\RelationTable::class,
1350 Join::on('this.CHAT_ID', 'ref.CHAT_ID')
1351 ->where('ref.MESSAGE_TYPE', IM_MESSAGE_SYSTEM),
1352 ['join_type' => Join::TYPE_INNER]
1353 ))
1354 ->whereIn('ID', $messageIds)
1355 ->fetchAll()
1356 ;
1357 }
1358
1359 private function fillReadStatuses(array $notifications): array
1360 {
1361 $messageIds = array_keys($notifications);
1362
1363 $readStatuses = (new IM\V2\Message\ReadService($this->user_id))->getReadStatusesByMessageIds($messageIds);
1364
1365 foreach ($notifications as $id => $notification)
1366 {
1367 $notifications[$id]['NOTIFY_READ'] = $readStatuses[$id] ? 'Y' : 'N';
1368 }
1369
1370 return $notifications;
1371 }
1372
1373 public function DeleteWithCheck($id)
1374 {
1375 global $DB;
1376
1377 if (is_array($id) && count($id) === 1)
1378 {
1379 $id = $id[0];
1380 }
1381
1382 if (is_array($id) && count($id) > 1)
1383 {
1384 $ids = array_map(static function($item) {
1385 return (int)$item;
1386 }, $id);
1387
1388 $sqlWhere = "M.ID IN (" . implode(',', $ids) . ")";
1389 }
1390 else
1391 {
1392 $id = (int)$id;
1393 $sqlWhere = "M.ID = " . $id;
1394 }
1395
1396 $strSql = "
1397 SELECT M.* FROM b_im_relation R, b_im_message M
1398 WHERE ". $sqlWhere ."
1399 AND R.USER_ID = ".$this->user_id."
1400 AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."'
1401 AND R.CHAT_ID = M.CHAT_ID
1402 ";
1403 $dbRes = $DB->Query($strSql);
1404
1405 $notificationsToDelete = [];
1406 while ($arRes = $dbRes->Fetch())
1407 {
1408 $notificationsToDelete[] = (int)$arRes['ID'];
1409 }
1410
1411 if (count($notificationsToDelete) === 0)
1412 {
1413 return false;
1414 }
1415
1416 if (count($notificationsToDelete) === 1)
1417 {
1418 self::Delete($notificationsToDelete[0]);
1419 }
1420 else
1421 {
1422 self::deleteList($notificationsToDelete);
1423 }
1424
1425 return true;
1426 }
1427
1428 public static function DeleteByTag($notifyTag, $authorId = false)
1429 {
1430 global $DB;
1431
1432 $notifyTag = (string)$notifyTag;
1433 if ($notifyTag == '')
1434 {
1435 return false;
1436 }
1437
1438 $sqlUser = "";
1439 if ($authorId !== false)
1440 {
1441 $sqlUser = " AND M.AUTHOR_ID = " . (int)$authorId;
1442 }
1443
1444 $dbRes = $DB->Query("
1445 SELECT M.ID
1446 FROM b_im_message M
1447 WHERE M.NOTIFY_TAG = '" . $DB->ForSQL($notifyTag) . "'" . $sqlUser)
1448 ;
1449
1450 $messages = [];
1451 while ($row = $dbRes->Fetch())
1452 {
1453 $messages[] = (int)$row['ID'];
1454 }
1455
1456 if (count($messages) > 0)
1457 {
1458 self::deleteList($messages, ['NOTIFY_TAG' => $notifyTag]);
1459 }
1460
1461 return true;
1462 }
1463
1464 public static function ConfirmBySubTag($notifySubTag, $resultMessages)
1465 {
1466 global $DB;
1467
1468 $notifySubTag = (string)$notifySubTag;
1469 if ($notifySubTag == '')
1470 return false;
1471
1472 $dbRes = $DB->Query("
1473 SELECT M.ID, M.NOTIFY_TYPE, R.USER_ID, R.STATUS, R.CHAT_ID
1474 FROM b_im_relation R, b_im_message M
1475 WHERE M.CHAT_ID = R.CHAT_ID AND M.NOTIFY_SUB_TAG = '".$DB->ForSQL($notifySubTag)."'");
1476 $arUsers = Array();
1477 $arChatId = Array();
1478 $messages = Array();
1479 while ($row = $dbRes->Fetch())
1480 {
1481 $messages[$row['ID']] = $row;
1482 $arUsers[$row['USER_ID']] = $row['USER_ID'];
1483 $arChatId[$row['CHAT_ID']] = $row['CHAT_ID'];
1484 }
1485
1486 $pullActive = false;
1487 if (CModule::IncludeModule("pull"))
1488 $pullActive = true;
1489
1490 $arUsersSend = Array();
1491 foreach ($arUsers as $userId => $count)
1492 {
1493 CIMMessenger::SpeedFileDelete($userId, IM_SPEED_NOTIFY);
1494 if ($count > 0)
1495 {
1496 $arUsersSend[] = $userId;
1497 }
1498 if ($pullActive)
1499 {
1500 CPushManager::DeleteFromQueueBySubTag($userId, $notifySubTag);
1501 }
1502 }
1503
1504 $counters = self::GetCounters(array_keys($arChatId));
1505
1506 if (count($messages) > 0)
1507 {
1508 foreach ($messages as $messageId => $message)
1509 {
1510 self::Delete($messageId);
1511 if ($pullActive)
1512 {
1513 \Bitrix\Pull\Event::add($message['USER_ID'], Array(
1514 'module_id' => 'im',
1515 'command' => 'notifyConfirm',
1516 'params' => Array(
1517 'id' => $messageId,
1518 'chatId' => $message['CHAT_ID'],
1519 'confirmMessages' => $resultMessages,
1520 'counter' => $counters[$message['CHAT_ID']],
1521 ),
1522 'extra' => \Bitrix\Im\Common::getPullExtra()
1523 ));
1524 }
1525 }
1526 }
1527
1528 return true;
1529 }
1530
1531 public static function DeleteBySubTag($notifySubTag, $authorId = false, $pullActive = true)
1532 {
1533 global $DB;
1534
1535 $notifySubTag = (string)$notifySubTag;
1536 if ($notifySubTag == '')
1537 {
1538 return false;
1539 }
1540
1541 $sqlUser = "";
1542 if ($authorId !== false)
1543 {
1544 $sqlUser = " AND M.AUTHOR_ID = ".intval($authorId);
1545 }
1546
1547 $dbRes = $DB->Query("
1548 SELECT M.ID
1549 FROM b_im_message M
1550 WHERE M.NOTIFY_SUB_TAG = '".$DB->ForSQL($notifySubTag)."'".$sqlUser)
1551 ;
1552
1553 $messages = [];
1554 while ($row = $dbRes->Fetch())
1555 {
1556 $messages[] = $row['ID'];
1557 }
1558
1559 if (!$pullActive)
1560 {
1561 $notifySubTag = null;
1562 }
1563
1564 return self::deleteList($messages, ['NOTIFY_SUB_TAG' => $notifySubTag]);
1565 }
1566
1567 public static function DeleteByModule($moduleId, $moduleEvent = '')
1568 {
1569 global $DB;
1570 $moduleId = (string)$moduleId;
1571 if ($moduleId == '')
1572 return false;
1573
1574 $sqlEvent = '';
1575 $moduleEvent = (string)$moduleEvent;
1576 if ($moduleEvent <> '')
1577 {
1578 $sqlEvent = " AND NOTIFY_EVENT = '".$DB->ForSQL($moduleEvent)."'";
1579 }
1580
1581 if ($DB->type == 'MYSQL')
1582 {
1583 $strSql = "
1584 DELETE U
1585 FROM b_im_message M INNER JOIN b_im_message_unread U ON M.ID = U.MESSAGE_ID
1586 WHERE M.NOTIFY_MODULE = '".$DB->ForSQL($moduleId)."'".$sqlEvent;
1587 }
1588 elseif ($DB->type == 'PGSQL')
1589 {
1590 $strSql = "
1591 DELETE FROM b_im_message_unread U
1592 USING b_im_message M
1593 WHERE M.ID = U.MESSAGE_ID
1594 AND M.NOTIFY_MODULE = '".$DB->ForSQL($moduleId)."'".$sqlEvent;
1595 }
1596 $DB->Query($strSql);
1597
1598 if ($DB->type == 'MYSQL')
1599 {
1600 $strSql = "
1601 DELETE V
1602 FROM b_im_message M INNER JOIN b_im_message_viewed V ON M.ID = V.MESSAGE_ID
1603 WHERE M.NOTIFY_MODULE = '".$DB->ForSQL($moduleId)."'".$sqlEvent;
1604 }
1605 elseif ($DB->type == 'PGSQL')
1606 {
1607 $strSql = "
1608 DELETE FROM b_im_message_viewed V
1609 USING b_im_message M
1610 WHERE M.ID = V.MESSAGE_ID
1611 AND M.NOTIFY_MODULE = '".$DB->ForSQL($moduleId)."'".$sqlEvent;
1612 }
1613 $DB->Query($strSql);
1614
1615 $strSql = "DELETE FROM b_im_message WHERE NOTIFY_MODULE = '".$DB->ForSQL($moduleId)."'".$sqlEvent;
1616 $DB->Query($strSql);
1617
1618 return true;
1619 }
1620
1621 public function GetNotifyCounter($arNotify = Array())
1622 {
1623 $count = 0;
1624 if (isset($arNotify['unreadNotify']) && !empty($arNotify['unreadNotify']) && isset($arNotify['notify']))
1625 {
1626 foreach ($arNotify['unreadNotify'] as $key => $value)
1627 {
1628 if (!isset($arNotify['notify'][$key]))
1629 continue;
1630
1631 $count++;
1632 }
1633 }
1634 else
1635 {
1636 $cache = CIMMessenger::SpeedFileGet($this->user_id, IM_SPEED_NOTIFY);
1637 $count = $cache? $cache['counter']: 0;
1638 }
1639 return intval($count);
1640 }
1641
1642 public static function GetRealCounter($chatId)
1643 {
1644 return \Bitrix\Im\Notify::getRealCounter($chatId);
1645 }
1646
1647 public static function GetRealCounters($chatId)
1648 {
1649 return \Bitrix\Im\Notify::getRealCounters($chatId);
1650 }
1651
1652 public static function GetCounter($chatId)
1653 {
1654 return \Bitrix\Im\Notify::getCounter($chatId);
1655 }
1656
1657 public static function GetCounters($chatIds)
1658 {
1659 return \Bitrix\Im\Notify::getCounters($chatIds);
1660 }
1661}
$arParams
Определения access_dialog.php:21
$connection
Определения actionsdefinitions.php:38
$count
Определения admin_tab.php:4
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static clearCache($userId=null)
Определения counter.php:156
static parse($text, $params=Array())
Определения text.php:28
static getInstance($userId=null)
Определения user.php:45
static getInstance()
Определения application.php:98
Определения loader.php:13
static createFromTimestamp($timestamp)
Определения datetime.php:246
static send()
Определения event.php:387
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
static send($userId=null, $appId=self::MOBILE_APP)
Определения mobilecounter.php:187
Определения sanitizer.php:23
AddTags($arTags)
Определения sanitizer.php:136
Определения dbresult.php:88
static GetUserOffset(array $params)
Определения im_mail.php:451
static DeleteAll($messageId)
Определения im_message_param.php:328
Определения im_notify.php:14
GetNotifyCounter($arNotify=Array())
Определения im_notify.php:1621
GetNotify($ID)
Определения im_notify.php:421
static DeleteByTag($notifyTag, $authorId=false)
Определения im_notify.php:1428
static DeleteBySubTag($notifySubTag, $authorId=false, $pullActive=true)
Определения im_notify.php:1531
static Delete($id)
Определения im_notify.php:1156
static ConfirmBySubTag($notifySubTag, $resultMessages)
Определения im_notify.php:1464
Answer($id, $text)
Определения im_notify.php:1098
static GetRealCounters($chatId)
Определения im_notify.php:1647
static deleteList(array $ids, array $params=[])
Определения im_notify.php:1197
static getTextMessageByLang(int $userId, callable $locFunction)
Определения im_notify.php:69
static Add($arFields)
Определения im_notify.php:28
static SetLastSendId($chatId, $lastSendId)
Определения im_notify.php:995
static GetFormatNotify(array $arFields)
Определения im_notify.php:435
static GetCounters($chatIds)
Определения im_notify.php:1657
DeleteWithCheck($id)
Определения im_notify.php:1373
MarkNotifyUnRead($id=0, $setThisAndHigher=false, $appId='Bitrix24')
Определения im_notify.php:815
static GetUnsendNotify()
Определения im_notify.php:355
static GetCounter($chatId)
Определения im_notify.php:1652
MarkNotifyRead($id=0, $setThisAndHigher=false, $appId='Bitrix24')
Определения im_notify.php:504
MarkNotifyReadBySubTag($subTagList=array())
Определения im_notify.php:688
GetUnreadNotify($arParams=Array())
Определения im_notify.php:204
static SetLastId($chatId, $userId, $lastId=null)
Определения im_notify.php:955
Confirm($id, $value)
Определения im_notify.php:1012
static GetRealCounter($chatId)
Определения im_notify.php:1642
static DeleteByModule($moduleId, $moduleEvent='')
Определения im_notify.php:1567
GetNotifyList($arParams=array())
Определения im_notify.php:87
__construct($user_id=false, $arParams=Array())
Определения im_notify.php:18
if(!\Bitrix\Main\Loader::includeModule('clouds')) $lastId
Определения sync.php:68
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
if($ajaxMode) $ID
Определения get_user.php:27
$moduleId
const IM_MESSAGE_SYSTEM
Определения include.php:21
const IM_NOTIFY_CONFIRM
Определения include.php:36
const IM_SPEED_NOTIFY
Определения include.php:65
global $DB
Определения cron_frame.php:29
global $USER
Определения csv_new_run.php:40
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$order
Определения payment.php:8
$message
Определения payment.php:8
$counter
Определения options.php:5
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$text
Определения template_pdf.php:79
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$messages
Определения template.php:8
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$counters
Определения options.php:100
$arRes
Определения options.php:104
$dbRes
Определения yandex_detail.php:168