1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
UpdateService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat\Update;
4
5use Bitrix\Im\V2\Analytics\ChatAnalytics;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Chat\Converter;
8use Bitrix\Im\V2\Entity\File\ChatAvatar;
9use Bitrix\Im\V2\Integration\HumanResources\Structure;
10use Bitrix\Im\V2\Integration\Socialnetwork\Collab\Collab;
11use Bitrix\Im\V2\Integration\Socialnetwork\Group;
12use Bitrix\Im\V2\Relation\AddUsersConfig;
13use Bitrix\Im\V2\Relation\DeleteUserConfig;
14use Bitrix\Im\V2\Result;
15
17{
20 protected ?string $newChatType = null;
21
23 {
24 $this->chat = $chat;
25 $this->updateFields = $updateFields;
26 }
27
28 public function updateChat(): Result
29 {
30 $prevAnalyticsData = $this->getAnalyticsData();
31
32 $convertResult = $this->convertChat();
33
34 if (!$convertResult->isSuccess())
35 {
36 return $convertResult;
37 }
38
40
41 $this->chat->fill($this->getArrayToSave());
42 $result = $this->chat->save();
43
44 if (!$result->isSuccess())
45 {
46 return $result->setResult($this->chat);
47 }
48
49 $svc = $this
50 ->sendMessageAfterUpdateAvatar()
51 ->deleteUsers()
52 ->addUsers()
53 ->deleteManagers()
54 ->addManagers()
55 ;
56
57 ChatAnalytics::blockSingleUserEvents($this->chat);
58
59 $svc
60 ->deleteDepartments()
61 ->addDepartments()
62 ;
63
64 $this->sendPushUpdateChat();
65 $this->compareAnalyticsData($prevAnalyticsData);
66
67 ChatAnalytics::unblockSingleUserEventsByChat($this->chat);
68
69 return $result->setResult($this->chat);
70 }
71
72 protected function convertChat(): Result
73 {
74 $result = new Result();
75
76 $newType = $this->getConvertType();
77
78 if (!isset($newType) || $this->chat->getType() === $newType)
79 {
80 return $result;
81 }
82
83 $convertResult = (new Converter($this->chat->getId(), $newType))->convert();
84
85 if (!$convertResult->isSuccess())
86 {
87 return $result->addErrors($convertResult->getErrors());
88 }
89
90 $this->newChatType = $newType;
91
92 // replace object after conversion
93 $this->chat = Chat\GroupChat::getInstance($this->chat->getChatId());
94
95 return $result;
96 }
97
98 protected function getConvertType(): ?string
99 {
100 $searchable = $this->updateFields->getSearchable();
101 $currentType = $this->chat->getType();
102 $newType = $this->updateFields->getType();
103
104 return match (true)
105 {
106 $currentType === Chat::IM_TYPE_CHAT && $searchable === 'Y' => \Bitrix\Im\V2\Chat::IM_TYPE_OPEN,
107 $currentType === Chat::IM_TYPE_OPEN && $searchable === 'N' => \Bitrix\Im\V2\Chat::IM_TYPE_CHAT,
108 $currentType === Chat::IM_TYPE_CHANNEL && $searchable === 'Y' => \Bitrix\Im\V2\Chat::IM_TYPE_OPEN_CHANNEL,
109 $currentType === Chat::IM_TYPE_OPEN_CHANNEL && $searchable === 'N' => \Bitrix\Im\V2\Chat::IM_TYPE_CHANNEL,
110 default => $newType,
111 };
112
113 }
114
115 protected function addUsers(): self
116 {
118
119 $addedUsers = array_unique(array_merge(
120 $updateFields->getAddedUsers(),
121 $updateFields->getAddedManagers(),
122 [$updateFields->getOwnerId()]
123 ));
124
125 $this->chat->addUsers($addedUsers, new AddUsersConfig($updateFields->getAddedManagers(), $updateFields->shouldHideHistory()));
126
127 return $this;
128 }
129
130 protected function deleteUsers(): self
131 {
132 $deletedUsers = $this->updateFields->getDeletedUsers();
133
134 foreach ($deletedUsers as $userId)
135 {
136 $this->chat->deleteUser((int)$userId, new DeleteUserConfig(false));
137 }
138
139 return $this;
140 }
141
142 protected function addManagers(): self
143 {
144 $addManagers = $this->updateFields->getAddedManagers();
145
146 if (empty($addManagers))
147 {
148 return $this;
149 }
150
151 $this->chat->addManagers($addManagers, false);
152
153 return $this;
154 }
155
156 protected function deleteManagers(): self
157 {
158 $deleteManagers = $this->updateFields->getDeletedManagers();
159
160 if (empty($deleteManagers))
161 {
162 return $this;
163 }
164
165 $this->chat->deleteManagers($deleteManagers, false);
166
167 return $this;
168 }
169
170 protected function addDepartments(): self
171 {
172 $addNodes = $this->updateFields->getAddedDepartments();
173
174 if (empty($addNodes))
175 {
176 return $this;
177 }
178
179 (new Structure($this->chat))->link($addNodes);
180
181 foreach ($addNodes as $node)
182 {
183 (new ChatAnalytics($this->chat))->addAddDepartment();
184 }
185
186 return $this;
187 }
188
189 protected function deleteDepartments(): self
190 {
191 $deleteNodes = $this->updateFields->getDeletedDepartments();
192
193 if (empty($deleteNodes))
194 {
195 return $this;
196 }
197
198 $currentNodes = (new Structure($this->chat))->getNodesAccessCodes();
199
200 foreach ($deleteNodes as $key => $node)
201 {
202 if (!in_array($node, $currentNodes, true))
203 {
204 unset($deleteNodes[$key]);
205 }
206 }
207
208 (new Structure($this->chat))->unlink($deleteNodes);
209
210 foreach ($deleteNodes as $node)
211 {
212 (new ChatAnalytics($this->chat))->addDeleteDepartment();
213 }
214
215 return $this;
216 }
217
218 protected function updateAvatarBeforeSave(): self
219 {
220 $avatarId = $this->updateFields->getAvatar();
221 if (!isset($avatarId))
222 {
223 return $this;
224 }
225
226 (new ChatAvatar($this->chat))->update($avatarId, false, false, true);
227
228 return $this;
229 }
230
231 protected function sendMessageAfterUpdateAvatar(): self
232 {
233 $avatarId = $this->updateFields->getAvatar();
234 if (!isset($avatarId))
235 {
236 return $this;
237 }
238
239 $this->chat->sendMessageUpdateAvatar();
240
241 return $this;
242 }
243
244 protected function sendPushUpdateChat(): void
245 {
246 if (!\Bitrix\Main\Loader::includeModule("pull"))
247 {
248 return;
249 }
250
251 $pushMessage = [
252 'module_id' => 'im',
253 'command' => 'chatUpdate',
254 'expiry' => 3600,
255 'params' => [
256 'chat' => $this->chat->toPullFormat(),
257 ],
259 ];
260
261 \Bitrix\Pull\Event::add($this->chat->getRelations()->getUserIds(), $pushMessage);
262 if ($this->chat->needToSendPublicPull())
263 {
264 \CPullWatch::AddToStack('IM_PUBLIC_' . $this->chat->getId(), $pushMessage);
265 }
266 }
267
268 protected function getArrayToSave(): array
269 {
270 $fields = $this->filterFieldsByDifference($this->updateFields->getArrayToSave());
271
272 if (isset($this->newChatType))
273 {
274 $fields['TYPE'] = $this->newChatType;
275 }
276
277 return $fields;
278 }
279
281 {
282 if ($this->chat->getDescription() === $fields['DESCRIPTION'])
283 {
284 unset($fields['DESCRIPTION']);
285 }
286
287 return $fields;
288 }
289
290 protected function compareAnalyticsData(array $prevData): void
291 {
292 $currentData = $this->getAnalyticsData();
293 $analytics = new ChatAnalytics($this->chat);
294 $diff = fn(string $key) => $currentData[$key] !== $prevData[$key];
295
296 if ($diff('description'))
297 {
298 $analytics->addEditDescription();
299 }
300
301 if ($diff('type'))
302 {
303 $analytics->addSetType();
304 }
305
306 if (
307 $diff('owner') ||
308 $diff('manageUI') ||
309 $diff('manageUsersAdd') ||
310 $diff('manageUsersDelete') ||
311 $diff('manageMessages')
312 )
313 {
314 $analytics->addEditPermissions();
315 }
316 }
317
318 protected function getAnalyticsData(): array
319 {
320 return [
321 'description' => $this->chat->getDescription(),
322 'type' => $this->chat->getType(),
323 'owner' => $this->chat->getAuthorId(),
324 'manageUI' => $this->chat->getManageUI(),
325 'manageUsersAdd' => $this->chat->getManageUsersAdd(),
326 'manageUsersDelete' => $this->chat->getManageUsersDelete(),
327 'manageMessages' => $this->chat->getManageMessages(),
328 ];
329 }
330}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getPullExtra()
Определения common.php:127
compareAnalyticsData(array $prevData)
Определения UpdateService.php:290
__construct(Chat\GroupChat $chat, UpdateFields $updateFields)
Определения UpdateService.php:22
UpdateFields $updateFields
Определения UpdateService.php:18
filterFieldsByDifference(array $fields)
Определения UpdateService.php:280
Chat GroupChat $chat
Определения UpdateService.php:19
Определения result.php:20
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
</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
if(empty($signedUserToken)) $key
Определения quickway.php:257
$fields
Определения yandex_run.php:501