Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chatreadrecount.php
1<?php
2namespace Bitrix\Im\Update;
3
10
11Loc::loadMessages(__FILE__);
12
13final class ChatReadRecount extends Stepper
14{
15 private const OPTION_NAME = 'im_chat_read_recount';
16
17 protected static $moduleId = 'im';
18
22 public function execute(array &$result): bool
23 {
24 global $DB;
25
26 if (!Loader::includeModule(self::$moduleId))
27 {
28 return false;
29 }
30
31 $return = false;
32
33 $params = Option::get(self::$moduleId, self::OPTION_NAME);
34 $params = ($params !== '' ? @unserialize($params, ['allowed_classes' => false]) : []);
35 $params = (is_array($params) ? $params : []);
36
37 if (empty($params))
38 {
39 $params = [
40 'lastId' => 0,
41 'number' => 0,
42 'count' => UserTable::getCount([
43 '=ACTIVE' => true,
44 '=IS_REAL_USER' => true,
45 ]),
46 ];
47 }
48
49 if ($params['count'] > 0)
50 {
51 $result['steps'] = '';
52 $result['count'] = $params['count'];
53
54 $cursor = UserTable::getList([
55 'select' => ['ID'],
56 'filter' => [
57 '>ID' => $params['lastId'],
58 '=ACTIVE' => true,
59 '=IS_REAL_USER' => true,
60 ],
61 'order' => ['ID' => 'ASC'],
62 'limit' => 500
63 ]);
64
65 $found = false;
66 $connection = \Bitrix\Main\Application::getInstance()->getConnection();
67
68 $users = [];
69 $batch = [];
70 while ($row = $cursor->fetch())
71 {
72 if (count($users) === 100)
73 {
74 $batch[] = $users;
75 $users = [];
76 }
77
78 $users[] = (int)$row['ID'];
79
80 $params['lastId'] = (int)$row['ID'];
81 $params['number']++;
82 $found = true;
83 }
84 if (!empty($users))
85 {
86 $batch[] = $users;
87 }
88
89 $sqlDate = $connection->getSqlHelper()->addDaysToDateTime(-30);
90 foreach ($batch as $users)
91 {
92 $sqlUserIds = implode(', ', $users);
93
94 $connection->query("
95 UPDATE
96 b_im_relation R USE INDEX (IX_IM_REL_2)
97 INNER JOIN b_im_chat C
98 INNER JOIN b_im_message M
99 SET
100 R.COUNTER = 0
101 WHERE
102 R.MESSAGE_TYPE = 'C'
103 and R.STATUS != 2
104 and R.USER_ID IN (".$sqlUserIds.")
105 and C.ID = R.CHAT_ID
106 and M.ID = C.LAST_MESSAGE_ID
107 and M.DATE_CREATE < {$sqlDate}
108 ");
109
110 $connection->query("
111 UPDATE
112 b_im_relation R USE INDEX (IX_IM_REL_2)
113 INNER JOIN b_im_chat C
114 INNER JOIN b_im_message M
115 SET
116 R.COUNTER = 0
117 WHERE
118 R.MESSAGE_TYPE = 'P'
119 and R.STATUS != 2
120 and R.USER_ID IN (".$sqlUserIds.")
121 and C.ID = R.CHAT_ID
122 and M.ID = C.LAST_MESSAGE_ID
123 and M.DATE_CREATE < {$sqlDate}
124 ");
125
126 $connection->query("
127 UPDATE
128 b_im_relation R USE INDEX (IX_IM_REL_2)
129 INNER JOIN b_im_chat C
130 INNER JOIN b_im_message M
131 SET
132 R.COUNTER = 0
133 WHERE
134 R.MESSAGE_TYPE = 'O'
135 and R.STATUS != 2
136 and R.USER_ID IN (".$sqlUserIds.")
137 and C.ID = R.CHAT_ID
138 and M.ID = C.LAST_MESSAGE_ID
139 and M.DATE_CREATE < {$sqlDate}
140 ");
141
142 foreach ($users as $userId)
143 {
144 Counter::clearCache($userId);
145 }
146 }
147
148 if ($found)
149 {
150 Option::set(self::$moduleId, self::OPTION_NAME, serialize($params));
151 $return = true;
152 }
153 else
154 {
155 Option::delete(self::$moduleId, ['name' => self::OPTION_NAME]);
156 }
157
158 $result['steps'] = $params['number'];
159 }
160
161 return $return;
162 }
163}
static clearCache($userId=null)
Definition counter.php:155
static loadMessages($file)
Definition loc.php:64