1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ChatRelations.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Relation;
4
5use Bitrix\Im\V2\Entity\User\User;
6use Bitrix\Im\V2\Relation;
7use Bitrix\Im\V2\RelationCollection;
8
10{
11 protected const NEED_ADDITIONAL_FILTER_BY_ACCESS = false;
12
16 private static array $instances = [];
17
18 protected int $chatId;
22 private array $relationByUserId = [];
26 private array $relationsByUserIds = [];
27 private RelationCollection $fullRelations;
28 private RelationCollection $rawFullRelations;
29
30 private function __construct(int $chatId)
31 {
32 $this->chatId = $chatId;
33 }
34
35 public static function getInstance(int $chatId): static
36 {
37 self::$instances[$chatId] ??= new static($chatId);
38
39 return self::$instances[$chatId];
40 }
41
42 public function forceRelations(RelationCollection $relations): void
43 {
44 $this->cleanCache();
45 $this->fullRelations = $relations;
46 }
47
48 public function filterUserIdsByAccess(array $userIds): array
49 {
50 return $userIds;
51 }
52
54 {
55 if (!static::NEED_ADDITIONAL_FILTER_BY_ACCESS)
56 {
57 return $relations;
58 }
59
60 $usersWithAccess = $this->filterUserIdsByAccess($relations->getUserIds());
61
62 return $relations->filter(fn (Relation $relation) => in_array($relation->getUserId(), $usersWithAccess, true));
63 }
64
65 public function preloadUserRelation(int $userId, ?Relation $relation): void
66 {
67 $this->relationByUserId[$userId] = $relation ?? false;
68 }
69
70 public function get(): RelationCollection
71 {
72 if (!isset($this->fullRelations))
73 {
74 $this->fullRelations = $this->loadFullRelations();
75 }
76
77 return $this->fullRelations;
78 }
79
81 {
82 $this->rawFullRelations ??= RelationCollection::find(['CHAT_ID' => $this->chatId]);
83
84 return $this->rawFullRelations;
85 }
86
88 {
89 return $this->filterRelationsByAccess($this->getRawFullRelations());
90 }
91
93 {
94 if (isset($this->fullRelations))
95 {
96 return $this->fullRelations->filter(fn (Relation $relation) => $relation->getManager());
97 }
98
99 return $this->loadManagersOnly();
100 }
101
103 {
104 return $this->filterRelationsByAccess(RelationCollection::find(['CHAT_ID' => $this->chatId, 'MANAGER' => 'Y']));
105 }
106
107 public function getByUserId(int $userId): ?Relation
108 {
109 if (isset($this->relationByUserId[$userId]))
110 {
111 return $this->relationByUserId[$userId] ?: null;
112 }
113
114 if (isset($this->fullRelations))
115 {
116 return $this->fullRelations->getByUserId($userId, $this->chatId);
117 }
118
119 $relations = $this->loadByUserId($userId);
120 $this->relationByUserId[$userId] = $relations->getByUserId($userId, $this->chatId) ?? false;
121
122 return $this->relationByUserId[$userId] ?: null;
123 }
124
125 protected function loadByUserId(int $userId): RelationCollection
126 {
127 return $this->filterRelationsByAccess(RelationCollection::find(['CHAT_ID' => $this->chatId, 'USER_ID' => $userId]));
128 }
129
130 public function getByUserIds(array $userIds): RelationCollection
131 {
132 if (empty($userIds))
133 {
134 return new RelationCollection();
135 }
136
137 sort($userIds);
138
139 $userIdsString = implode('|', $userIds);
140
141 if (isset($this->relationsByUserIds[$userIdsString]))
142 {
143 return $this->relationsByUserIds[$userIdsString];
144 }
145
146 if (isset($this->fullRelations))
147 {
148 return $this->fullRelations->filter(fn (Relation $relation) => in_array($relation->getUserId(), $userIds, true));
149 }
150
151 $this->relationsByUserIds[$userIdsString] = $this->loadByUserIds($userIds);
152
153 return $this->relationsByUserIds[$userIdsString];
154 }
155
156 protected function loadByUserIds(array $userIds): RelationCollection
157 {
158 if (empty($userIds))
159 {
160 return new RelationCollection();
161 }
162
163 return $this->filterRelationsByAccess(RelationCollection::find(['CHAT_ID' => $this->chatId, 'USER_ID' => $userIds]));
164 }
165
166 public function getByReason(Reason $reason): RelationCollection
167 {
168 if (isset($this->fullRelations))
169 {
170 return $this->fullRelations->filter(fn (Relation $relation) => $relation->getReason() === $reason);
171 }
172
173 return $this->loadByReason($reason);
174 }
175
176 protected function loadByReason(Reason $reason): RelationCollection
177 {
178 return $this->filterRelationsByAccess(RelationCollection::find(['CHAT_ID' => $this->chatId, 'REASON' => $reason->value]));
179 }
180
181 public function cleanCache(): void
182 {
183 unset($this->fullRelations, $this->rawFullRelations);
184 $this->relationByUserId = [];
185 $this->relationsByUserIds = [];
186 }
187
188 public function onAfterRelationAdd(array $usersToAdd): void
189 {
190 //TODO: change to update cache for optimization
191 $this->cleanCache();
192 }
193
194 public function onAfterRelationDelete(int $deletedUserId): void
195 {
196 $this->fullRelations->onAfterRelationDelete($this->chatId, $deletedUserId);
197 $this->rawFullRelations->onAfterRelationDelete($this->chatId, $deletedUserId);
198 unset($this->relationsByUserIds[$deletedUserId]);
199 $this->relationsByUserIds = [];
200 }
201
202 public function getUserCount(): int
203 {
204 $fullRelations = $this->get();
205
206 $count = 0;
207 foreach ($fullRelations as $relation)
208 {
209 if (!$relation->isHidden() && $relation->getUser()->isActive())
210 {
211 $count++;
212 }
213 }
214
215 return $count;
216 }
217}
$count
Определения admin_tab.php:4
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
filter(callable $predicate)
Определения Registry.php:37
filterRelationsByAccess(RelationCollection $relations)
Определения ChatRelations.php:53
getByUserIds(array $userIds)
Определения ChatRelations.php:130
getByUserId(int $userId)
Определения ChatRelations.php:107
onAfterRelationAdd(array $usersToAdd)
Определения ChatRelations.php:188
loadByReason(Reason $reason)
Определения ChatRelations.php:176
preloadUserRelation(int $userId, ?Relation $relation)
Определения ChatRelations.php:65
loadByUserIds(array $userIds)
Определения ChatRelations.php:156
onAfterRelationDelete(int $deletedUserId)
Определения ChatRelations.php:194
const NEED_ADDITIONAL_FILTER_BY_ACCESS
Определения ChatRelations.php:11
loadByUserId(int $userId)
Определения ChatRelations.php:125
filterUserIdsByAccess(array $userIds)
Определения ChatRelations.php:48
getByReason(Reason $reason)
Определения ChatRelations.php:166
static getInstance(int $chatId)
Определения ChatRelations.php:35
forceRelations(RelationCollection $relations)
Определения ChatRelations.php:42
static find(array $filter, array $order=[], ?int $limit=null, ?Context $context=null, array $select=self::COMMON_FIELDS)
Определения RelationCollection.php:50
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804