1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
CounterOverflowService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Message\Counter;
4
5use Bitrix\Im\Model\CounterOverflowTable;
6use Bitrix\Im\V2\Chat;
7
9{
10 protected const PARTIAL_INSERT_ROWS = 500;
11
15 protected static array $overflowInfoStaticCache = [];
16 protected static int $overflowValue = 100;
17 protected int $chatId;
18
19 public function __construct(int $chatId)
20 {
21 $this->chatId = $chatId;
22 }
23
24 public function insertOverflowed(array $counters): void
25 {
26 $overflowedCounters = $this->filterOverflowedCounters($counters);
27 $userIds = array_keys($overflowedCounters);
28 $this->insert($userIds);
29 foreach ($userIds as $userId)
30 {
31 self::cleanCacheByChatId($this->chatId, $userId, true);
32 }
33 }
34
35 public function getOverflowInfo(array $userIds): CounterOverflowInfo
36 {
37 $infoFromCache = $this->getOverflowInfoFromCache($userIds);
38 if ($infoFromCache)
39 {
40 return $infoFromCache;
41 }
42
43 $usersWithOverflowedCounters = $this->getUsersWithOverflow($userIds);
44 $usersWithoutOverflowedCounters = $this->filterUsersWithoutOverflow($usersWithOverflowedCounters, $userIds);
45 $overflowInfo = new CounterOverflowInfo(
46 $usersWithOverflowedCounters,
47 $usersWithoutOverflowedCounters
48 );
49 self::$overflowInfoStaticCache[$this->chatId] = $overflowInfo;
50
51 return $overflowInfo;
52 }
53
54 protected function getOverflowInfoFromCache(array $userIds): ?CounterOverflowInfo
55 {
56 if (!isset(self::$overflowInfoStaticCache[$this->chatId]))
57 {
58 return null;
59 }
60
61 $info = self::$overflowInfoStaticCache[$this->chatId];
62 foreach ($userIds as $userId)
63 {
64 if (!$info->has($userId))
65 {
66 return null;
67 }
68 }
69
70 return $info;
71 }
72
73 public static function getOverflowValue(): int
74 {
75 return self::$overflowValue;
76 }
77
78 public function delete(int $userId): void
79 {
80 CounterOverflowTable::deleteByFilter(['=CHAT_ID' => $this->chatId, '=USER_ID' => $userId]);
81 self::cleanCacheByChatId($this->chatId, $userId);
82 }
83
84 public static function deleteByChatIdForAll(int $chatId): void
85 {
86 CounterOverflowTable::deleteByFilter(['=CHAT_ID' => $chatId]);
88 }
89
90 public static function deleteByChatIds(array $chatIds, ?int $userId = null): void
91 {
92 if (empty($chatIds))
93 {
94 return;
95 }
96
97 $filter = ['=CHAT_ID' => $chatIds];
98 if (isset($userId))
99 {
100 $filter['=USER_ID'] = $userId;
101 }
102
103 CounterOverflowTable::deleteByFilter($filter);
104 foreach ($chatIds as $chatId)
105 {
107 }
108 }
109
110 public static function deleteAllByUserId(int $userId): void
111 {
112 CounterOverflowTable::deleteByFilter(['=USER_ID' => $userId]);
113 $chatIds = [];
114
115 foreach (self::$overflowInfoStaticCache as $chatId => $overflowInfo)
116 {
117 if ($overflowInfo->hasOverflow($userId))
118 {
119 $chatIds[] = $chatId;
120 }
121 }
122
123 foreach ($chatIds as $chatId)
124 {
125 unset(self::$overflowInfoStaticCache[$chatId]);
126 }
127 }
128
129 protected function getUsersWithOverflow(array $userIds): array
130 {
131 $result = [];
132 if (empty($userIds))
133 {
134 return [];
135 }
136
138 ->setSelect(['USER_ID'])
139 ->where('CHAT_ID', $this->chatId)
140 ->whereIn('USER_ID', $userIds)
141 ->exec()
142 ;
143
144 foreach ($raw as $row)
145 {
146 $userId = (int)$row['USER_ID'];
148 }
149
150 return $result;
151 }
152
153 protected function filterUsersWithoutOverflow(array $overflowedUsers, array $allUsers): array
154 {
155 return array_filter($allUsers, static fn (int $userId) => !isset($overflowedUsers[$userId]));
156 }
157
159 {
160 return array_filter($counters, static fn (int $counter) => $counter >= self::$overflowValue);
161 }
162
163 protected function insert(array $userIds): void
164 {
165 if (empty($userIds))
166 {
167 return;
168 }
169
170 $rows = $this->getRowsToInsert($userIds);
171
172 foreach (array_chunk($rows, self::PARTIAL_INSERT_ROWS, true) as $part)
173 {
174 CounterOverflowTable::multiplyInsertWithoutDuplicate(
175 $part,
176 [
177 'DEADLOCK_SAFE' => true,
178 'UNIQUE_FIELDS' => ['CHAT_ID', 'USER_ID'],
179 ]
180 );
181 }
182 }
183
184 protected function getRowsToInsert(array $userIds): array
185 {
186 return array_map(fn (int $userId) => $this->getRowToInsert($userId), $userIds);
187 }
188
189 protected function getRowToInsert(int $userId): array
190 {
191 return ['CHAT_ID' => $this->chatId, 'USER_ID' => $userId];
192 }
193
194 protected static function cleanCacheByChatId(int $chatId, ?int $userId = null, bool $hasOverflowNow = false): void
195 {
196 if (!isset(self::$overflowInfoStaticCache[$chatId]))
197 {
198 return;
199 }
200
201 if ($userId === null)
202 {
203 unset(self::$overflowInfoStaticCache[$chatId]);
204
205 return;
206 }
207
208 $wasOverflowed = self::$overflowInfoStaticCache[$chatId]->hasOverflow($userId);
209
210 if ($wasOverflowed !== $hasOverflowNow)
211 {
212 self::$overflowInfoStaticCache[$chatId]->changeOverflowStatus($userId, $hasOverflowNow);
213 }
214 }
215}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static deleteByChatIds(array $chatIds, ?int $userId=null)
Определения CounterOverflowService.php:90
static cleanCacheByChatId(int $chatId, ?int $userId=null, bool $hasOverflowNow=false)
Определения CounterOverflowService.php:194
filterUsersWithoutOverflow(array $overflowedUsers, array $allUsers)
Определения CounterOverflowService.php:153
</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
$filter
Определения iblock_catalog_list.php:54
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
Определения backup.php:924
$counter
Определения options.php:5
$counters
Определения options.php:100
$rows
Определения options.php:264