40 $this->userId = $userId;
42 if (isset($sendingService))
44 $this->sendingService = $sendingService;
56 $chatData[
'ENTITY_TYPE'] =
58 ? self::IMPORT_PRIVATE_CHAT_ENTITY_TYPE
59 : self::IMPORT_GROUP_CHAT_ENTITY_TYPE
61 $chatData[
'SKIP_ADD_MESSAGE'] =
'Y';
63 $chatService = new \CIMChat(0);
64 $chatId = $chatService->Add($chatData);
73 'TYPE' => $chatData[
'TYPE']
79 return $this->sendingService->addMessages(
$messages);
84 return $this->sendingService->updateMessages(
$messages);
90 \CIMChat::deleteChat($this->chat);
97 return $this->commitCommon(
101 'ENTITY_TYPE' => self::IMPORT_GROUP_FINISH_ENTITY_TYPE,
112 $initUsers = $this->getInitUsers();
113 $originalChat = $this->getOriginalChat($initUsers);
114 $originalChatId =
null;
115 if ($originalChat !==
null)
117 $originalChatId = (int)$originalChat[
'ID'];
118 if ($this->hasRealMessages($originalChat))
124 $this->hideChat($originalChat, $initUsers);
128 $this->convertOriginalToGroup($originalChat, $initUsers);
133 $this->convertToGroup($initUsers);
139 $this->hideChat($originalChat, $initUsers);
147 'ENTITY_TYPE' => self::IMPORT_PRIVATE_FINISH_ENTITY_TYPE,
148 'ENTITY_ID' => $originalChatId,
155 $chatService = new \CIMChat(0);
157 foreach ($initUsers as
$user)
159 $managers[
$user] =
true;
161 $chatService->SetManagers((
int)$this->chat[
'ID'], $managers,
false);
167 private function hideChat(
array $chat,
array $users): void
169 $id = (int)$chat[
'ID'];
171 RelationTable::deleteByFilter([
'=CHAT_ID' => $id]);
175 ChatTable::update($id, [
'ENTITY_TYPE' => self::IMPORT_ARCHIVE_ENTITY_TYPE,
'ENTITY_ID' =>
"{$users[0]}|{$users[1]}"]);
178 private function getOriginalChat(
array $users): ?
array
181 if ($originalChatId === 0)
189 private function hasRealMessages(
array $chat): bool
191 if ((
int)$chat[
'MESSAGE_COUNT'] === 0)
196 if ((
int)$chat[
'MESSAGE_COUNT'] === 1)
198 return \CIMMessageParam::Get((
int)$chat[
'LAST_MESSAGE_ID'],
'USER_JOIN') !==
null;
204 private function getInitUsers():
array
207 [$initUsers[0], $initUsers[1]] = explode(
'|', $this->chat[
'ENTITY_DATA_1']);
209 return array_map(
'intval', $initUsers);
212 private function commitCommon(
array $users,
string $clientId,
array $finishEntityData = []): Result
215 $chatId = (int)$this->chat[
'ID'];
217 $users = array_map(
'intval', $users);
218 $folderMembers = $users;
219 if (!in_array($this->userId, $users,
true))
221 \CIMDisk::ChangeFolderMembers($chatId, $this->userId,
false);
225 foreach ($folderMembers as $index => $folderMember)
227 if ($folderMember === $this->userId)
229 unset($folderMembers[$index]);
234 $this->fillChatActualData();
235 $this->addUsersInChat($this->chat, $users);
236 \CIMDisk::ChangeFolderMembers($chatId, $users);
240 'ENTITY_TYPE' => $finishEntityData[
'ENTITY_TYPE'] ??
null,
241 'ENTITY_ID' => $finishEntityData[
'ENTITY_ID'] ??
null,
242 'ENTITY_DATA_1' => $finishEntityData[
'ENTITY_DATA_1'] ??
null,
243 'USER_COUNT' =>
count($users),
244 'MESSAGE_COUNT' => $this->chat[
'MESSAGE_COUNT'],
245 'LAST_MESSAGE_ID' => $this->chat[
'LAST_MESSAGE_ID'],
246 'PREV_MESSAGE_ID' => $this->chat[
'PREV_MESSAGE_ID'],
249 $this->showInRecent($this->chat);
250 $this->sendFinishMessage($users,
$clientId);
255 private function sendFinishMessage(
array $users,
string $clientId): void
257 $appName = $this->getRestAppName(
$clientId) ??
'';
259 'MESSAGE' => Loc::getMessage(
'IM_IMPORT_FINISH_MESSAGE', [
'#APP_NAME#' => $appName]),
260 'FROM_USER_ID' => $this->chat[
'MESSAGE_TYPE'] === \
IM_MESSAGE_PRIVATE ? $users[0] : 0,
261 'TO_CHAT_ID' => (
int)$this->chat[
'ID'],
262 'MESSAGE_TYPE' => $this->chat[
'MESSAGE_TYPE'],
267 private function fillChatActualData(): void
269 $lastMessageIds = $this->getLastMessageIds((
int)$this->chat[
'ID']);
270 $this->chat[
'MESSAGE_COUNT'] = $this->getMessageCount((
int)$this->chat[
'ID']);
271 $this->chat[
'LAST_MESSAGE_ID'] = $lastMessageIds[0] ?? 0;
272 $this->chat[
'PREV_MESSAGE_ID'] = $lastMessageIds[1] ?? 0;
275 private function getMessageCount(
int $chatId): int
277 return MessageTable::getCount(Query::filter()->where(
'CHAT_ID', $chatId));
280 private function getLastMessageIds(
int $chatId):
array
286 ->where(
'CHAT_ID', $chatId)
287 ->setOrder([
'DATE_CREATE' =>
'DESC'])
300 private function showInRecent(
array $chatData): Result
302 $relations =
Chat::getRelation((
int)$chatData[
'ID'], [
'WITHOUT_COUNTERS' =>
'Y']);
304 foreach ($relations as $userId => $relation)
308 ? $this->getEntityIdForPrivateChat($relations, (
int)$relation[
'USER_ID'])
309 : (int)$chatData[
'ID']
311 \CIMContactList::SetRecent(Array(
313 'MESSAGE_ID' => (
int)$chatData[
'LAST_MESSAGE_ID'],
314 'CHAT_TYPE' => $chatData[
'MESSAGE_TYPE'],
315 'USER_ID' => $relation[
'USER_ID'],
316 'CHAT_ID' => $relation[
'CHAT_ID'],
317 'RELATION_ID' => $relation[
'ID'],
324 private function addUsersInChat(
array $chatData,
array $users): void
326 $relationCollection =
new EO_Relation_Collection();
327 $lastRead =
new DateTime();
329 foreach ($users as
$user)
331 $relation =
new EO_Relation();
333 ->setChatId((
int)$chatData[
'ID'])
334 ->setMessageType($chatData[
'MESSAGE_TYPE'])
337 ->setLastId((
int)$chatData[
'LAST_MESSAGE_ID'])
338 ->setLastSendId((
int)$chatData[
'LAST_MESSAGE_ID'])
341 ->setLastRead($lastRead)
343 $relationCollection->add($relation);
346 $relationCollection->save(
true);
349 private function getEntityIdForPrivateChat(
array $relations,
int $userId): int
351 foreach ($relations as $relation)
353 if ((
int)$relation[
'USER_ID'] !== $userId)
355 return (
int)$relation[
'USER_ID'];
362 private function convertToGroup(
array $users): void
365 'IM_IMPORT_GROUP_FROM_PRIVATE_CHAT_TITLE',
374 private function convertOriginalToGroup(
array $originalChat,
array $users): void
376 $chatId = (int)$originalChat[
'ID'];
378 'IM_IMPORT_GROUP_FROM_ORIGINAL_PRIVATE_CHAT_TITLE',
388 $sqlUpdateRelation =
"UPDATE b_im_relation SET MESSAGE_TYPE= '" . \
IM_MESSAGE_CHAT .
"' WHERE CHAT_ID={$chatId}";
389 Application::getConnection()->query($sqlUpdateRelation);
390 $this->showInRecent($originalChat);
393 private function getRestAppName(
string $clientId): ?string
401 $appNamesByPriority = [
$app[
'MENU_NAME'],
$app[
'MENU_NAME_DEFAULT'],
$app[
'MENU_NAME_LICENSE'],
$app[
'APP_NAME']];
403 foreach ($appNamesByPriority as $appName)
416 return $this->isImportInProgress() &&
self::isAdmin($this->userId);
419 public static function isAdmin(
int $userId): bool
422 if (Loader::includeModule(
'bitrix24'))
425 $USER instanceof \CUser
426 &&
$USER->isAuthorized()
428 && (
int)
$USER->getId() === $userId
434 return \CBitrix24::isPortalAdmin($userId);
438 $USER instanceof \CUser
439 &&
$USER->isAuthorized()
440 && (
int)
$USER->getId() === $userId
443 return $USER->isAdmin();
447 $groups = UserTable::getUserGroupIds($userId);
450 if ((
int)$groupId === 1)
460 private function isImportInProgress(): bool
462 $entityType = $this->chat[
'ENTITY_TYPE'] ??
'';
465 $entityType === self::IMPORT_PRIVATE_CHAT_ENTITY_TYPE
466 || $entityType === self::IMPORT_GROUP_CHAT_ENTITY_TYPE