Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chatinvalidcounter.php
1<?php
2
3namespace Bitrix\Im\Update;
4
10
11final class ChatInvalidCounter extends Stepper
12{
13 private const ITERATION_COUNT = 5;
14
15 protected static $moduleId = 'im';
16
17 function execute(array &$option)
18 {
19 if (!Loader::includeModule(self::$moduleId))
20 {
22 }
23
25 for ($i = 0; $i < self::ITERATION_COUNT; ++$i)
26 {
27 $result = $this->makeMigrationIteration($option);
28
29 if ($result === self::FINISH_EXECUTION)
30 {
31 return $result;
32 }
33 }
34
35 return $result;
36 }
37
38 private function makeMigrationIteration(array &$option): bool
39 {
40 $lastId = $option['lastId'] ?? 0;
41 $userId = $this->getNextUser($lastId);
42
43 if ($userId === null || $userId === 0)
44 {
46 }
47
48 $option['lastId'] = $userId;
49
50 $chatIds = $this->getChatsWithInvalidCounterByUserId($userId);
51
52 if (empty($chatIds))
53 {
55 }
56
57 MessageUnreadTable::deleteByFilter(['=USER_ID' => $userId, '=CHAT_ID' => $chatIds]);
58 CounterService::clearCache($userId);
59
61 }
62
63 private function getChatsWithInvalidCounterByUserId(int $userId): array
64 {
65 $query = "
66 SELECT x1.CHAT_ID FROM (
67 SELECT bimu.CHAT_ID
68 FROM b_im_message_unread bimu
69 WHERE USER_ID = {$userId} GROUP BY CHAT_ID
70 ) x1
71
72 LEFT JOIN (
73
74 SELECT bir.CHAT_ID
75 FROM b_im_relation bir
76 WHERE
77 bir.USER_ID = {$userId} AND
78 bir.CHAT_ID IN (
79 SELECT bimu.CHAT_ID
80 FROM b_im_message_unread bimu
81 WHERE USER_ID = {$userId} GROUP BY CHAT_ID
82 )
83 ) x2 ON x1.CHAT_ID = x2.CHAT_ID
84
85 WHERE x2.CHAT_ID is null
86 ";
87 $result = Application::getConnection()->query($query);
88 $chatIds = [];
89
90 while ($row = $result->fetch())
91 {
92 $chatIds[] = (int)$row['CHAT_ID'];
93 }
94
95 return $chatIds;
96 }
97
98 private function getNextUser(?int $lastId = null): ?int
99 {
101 ->setSelect(['USER_ID'])
102 ->setGroup(['USER_ID'])
103 ->setOrder(['USER_ID'])
104 ->setLimit(1)
105 ;
106
107 if (isset($lastId) && $lastId > 0)
108 {
109 $query->where('USER_ID', '>', $lastId);
110 }
111
112 $result = $query->fetch();
113
114 if (!$result || !isset($result['USER_ID']))
115 {
116 return null;
117 }
118
119 return (int)$result['USER_ID'];
120 }
121}
static getConnection($name="")