40 $this->userId = $userId;
42 if (isset($sendingService))
44 $this->sendingService = $sendingService;
56 $chatData[
'ENTITY_TYPE'] =
57 $chatData[
'TYPE'] === \IM_MESSAGE_PRIVATE
62 $chatService = new \CIMChat(0);
63 $chatId = $chatService->Add($chatData);
70 return $result->setResult([
72 'TYPE' => $chatData[
'TYPE']
78 return $this->sendingService->addMessages($messages);
83 return $this->sendingService->updateMessages($messages);
89 \CIMChat::deleteChat($this->chat);
96 return $this->commitCommon(
100 'ENTITY_TYPE' => self::IMPORT_GROUP_FINISH_ENTITY_TYPE,
102 'ENTITY_DATA_1' => $clientId
111 $initUsers = $this->getInitUsers();
112 $originalChat = $this->getOriginalChat($initUsers);
113 $originalChatId =
null;
114 if ($originalChat !==
null)
116 $originalChatId = (int)$originalChat[
'ID'];
117 if ($this->hasRealMessages($originalChat))
123 $this->hideChat($originalChat, $initUsers);
127 $this->convertOriginalToGroup($originalChat, $initUsers);
132 $this->convertToGroup($initUsers);
133 $this->chat[
'MESSAGE_TYPE'] = \IM_MESSAGE_CHAT;
138 $this->hideChat($originalChat, $initUsers);
146 'ENTITY_TYPE' => self::IMPORT_PRIVATE_FINISH_ENTITY_TYPE,
147 'ENTITY_ID' => $originalChatId,
148 'ENTITY_DATA_1' => $clientId
152 if ($this->chat[
'MESSAGE_TYPE'] === \IM_MESSAGE_CHAT)
154 $chatService = new \CIMChat(0);
156 foreach ($initUsers as $user)
158 $managers[$user] =
true;
160 $chatService->SetManagers((
int)$this->chat[
'ID'], $managers,
false);
166 private function hideChat(array $chat, array $users): void
168 $id = (int)$chat[
'ID'];
170 RelationTable::deleteByFilter([
'=CHAT_ID' => $id]);
174 ChatTable::update($id, [
'ENTITY_TYPE' => self::IMPORT_ARCHIVE_ENTITY_TYPE,
'ENTITY_ID' =>
"{$users[0]}|{$users[1]}"]);
177 private function getOriginalChat(array $users): ?array
179 $originalChatId = \CIMMessage::GetChatId($users[0], $users[1],
false);
180 if ($originalChatId === 0)
188 private function hasRealMessages(array $chat): bool
190 if ((
int)$chat[
'MESSAGE_COUNT'] === 0)
195 if ((
int)$chat[
'MESSAGE_COUNT'] === 1)
197 return \CIMMessageParam::Get((
int)$chat[
'LAST_MESSAGE_ID'],
'USER_JOIN') !==
null;
203 private function getInitUsers(): array
206 [$initUsers[0], $initUsers[1]] = explode(
'|', $this->chat[
'ENTITY_DATA_1']);
208 return array_map(
'intval', $initUsers);
211 private function commitCommon(array $users,
string $clientId, array $finishEntityData = []): Result
213 $result =
new Result();
214 $chatId = (int)$this->chat[
'ID'];
216 $users = array_map(
'intval', $users);
217 $folderMembers = $users;
218 if (!in_array($this->userId, $users,
true))
220 \CIMDisk::ChangeFolderMembers($chatId, $this->userId,
false);
224 foreach ($folderMembers as $index => $folderMember)
226 if ($folderMember === $this->userId)
228 unset($folderMembers[$index]);
233 $this->fillChatActualData();
234 $this->addUsersInChat($this->chat, $users);
235 \CIMDisk::ChangeFolderMembers($chatId, $users);
239 'ENTITY_TYPE' => $finishEntityData[
'ENTITY_TYPE'] ??
null,
240 'ENTITY_ID' => $finishEntityData[
'ENTITY_ID'] ??
null,
241 'ENTITY_DATA_1' => $finishEntityData[
'ENTITY_DATA_1'] ??
null,
242 'USER_COUNT' => count($users),
243 'MESSAGE_COUNT' => $this->chat[
'MESSAGE_COUNT'],
244 'LAST_MESSAGE_ID' => $this->chat[
'LAST_MESSAGE_ID'],
245 'PREV_MESSAGE_ID' => $this->chat[
'PREV_MESSAGE_ID'],
248 $this->showInRecent($this->chat);
249 $this->sendFinishMessage($users, $clientId);
254 private function sendFinishMessage(array $users,
string $clientId): void
256 $appName = $this->getRestAppName($clientId) ??
'';
258 'MESSAGE' =>
Loc::getMessage(
'IM_IMPORT_FINISH_MESSAGE', [
'#APP_NAME#' => $appName]),
259 'FROM_USER_ID' => $this->chat[
'MESSAGE_TYPE'] === \IM_MESSAGE_PRIVATE ? $users[0] : 0,
260 'TO_CHAT_ID' => (int)$this->chat[
'ID'],
261 'MESSAGE_TYPE' => $this->chat[
'MESSAGE_TYPE'],
266 private function fillChatActualData(): void
268 $lastMessageIds = $this->getLastMessageIds((
int)$this->chat[
'ID']);
269 $this->chat[
'MESSAGE_COUNT'] = $this->getMessageCount((
int)$this->chat[
'ID']);
270 $this->chat[
'LAST_MESSAGE_ID'] = $lastMessageIds[0] ?? 0;
271 $this->chat[
'PREV_MESSAGE_ID'] = $lastMessageIds[1] ?? 0;
274 private function getMessageCount(
int $chatId): int
276 return MessageTable::getCount(Query::filter()->where(
'CHAT_ID', $chatId));
279 private function getLastMessageIds(
int $chatId): array
283 $messages = MessageTable::query()
285 ->where(
'CHAT_ID', $chatId)
286 ->setOrder([
'DATE_CREATE' =>
'DESC'])
291 foreach ($messages as $message)
293 $result[] = $message->getId();
299 private function showInRecent(array $chatData): Result
301 $relations =
Chat::getRelation((
int)$chatData[
'ID'], [
'WITHOUT_COUNTERS' =>
'Y']);
303 foreach ($relations as $userId => $relation)
306 $chatData[
'MESSAGE_TYPE'] === \IM_MESSAGE_PRIVATE
307 ? $this->getEntityIdForPrivateChat($relations, (
int)$relation[
'USER_ID'])
308 : (int)$chatData[
'ID']
310 \CIMContactList::SetRecent(Array(
311 'ENTITY_ID' => $entityId,
312 'MESSAGE_ID' => (
int)$chatData[
'LAST_MESSAGE_ID'],
313 'CHAT_TYPE' => $chatData[
'MESSAGE_TYPE'],
314 'USER_ID' => $relation[
'USER_ID'],
315 'CHAT_ID' => $relation[
'CHAT_ID'],
316 'RELATION_ID' => $relation[
'ID'],
323 private function addUsersInChat(array $chatData, array $users): void
325 $relationCollection =
new EO_Relation_Collection();
326 $lastRead =
new DateTime();
328 foreach ($users as $user)
330 $relation =
new EO_Relation();
332 ->setChatId((
int)$chatData[
'ID'])
333 ->setMessageType($chatData[
'MESSAGE_TYPE'])
336 ->setLastId((
int)$chatData[
'LAST_MESSAGE_ID'])
337 ->setLastSendId((
int)$chatData[
'LAST_MESSAGE_ID'])
340 ->setLastRead($lastRead)
342 $relationCollection->add($relation);
345 $relationCollection->save(
true);
348 private function getEntityIdForPrivateChat(array $relations,
int $userId): int
350 foreach ($relations as $relation)
352 if ((
int)$relation[
'USER_ID'] !== $userId)
354 return (
int)$relation[
'USER_ID'];
361 private function convertToGroup(array $users): void
364 'IM_IMPORT_GROUP_FROM_PRIVATE_CHAT_TITLE',
370 ChatTable::update((
int)$this->chat[
'ID'], [
'TYPE' => \IM_MESSAGE_CHAT,
'TITLE' => $title]);
373 private function convertOriginalToGroup(array $originalChat, array $users): void
375 $chatId = (int)$originalChat[
'ID'];
377 'IM_IMPORT_GROUP_FROM_ORIGINAL_PRIVATE_CHAT_TITLE',
385 ChatTable::update((
int)$originalChat[
'ID'], [
'TYPE' => \IM_MESSAGE_CHAT,
'TITLE' => $title]);
386 $originalChat[
'MESSAGE_TYPE'] = \IM_MESSAGE_CHAT;
387 $sqlUpdateRelation =
"UPDATE b_im_relation SET MESSAGE_TYPE= '" . \IM_MESSAGE_CHAT .
"' WHERE CHAT_ID={$chatId}";
389 $this->showInRecent($originalChat);
392 private function getRestAppName(
string $clientId): ?string
394 $app = AppTable::getByClientId($clientId);
400 $appNamesByPriority = [$app[
'MENU_NAME'], $app[
'MENU_NAME_DEFAULT'], $app[
'MENU_NAME_LICENSE'], $app[
'APP_NAME']];
402 foreach ($appNamesByPriority as $appName)
415 return $this->isImportInProgress() &&
self::isAdmin($this->userId);
418 public static function isAdmin(
int $userId): bool
421 if (Loader::includeModule(
'bitrix24'))
424 $USER instanceof \CUser
425 && $USER->isAuthorized()
427 && (
int)$USER->getId() === $userId
433 return \CBitrix24::isPortalAdmin($userId);
437 $USER instanceof \CUser
438 && $USER->isAuthorized()
439 && (
int)$USER->getId() === $userId
442 return $USER->isAdmin();
446 $groups = UserTable::getUserGroupIds($userId);
447 foreach ($groups as $groupId)
449 if ((
int)$groupId === 1)
459 private function isImportInProgress(): bool
461 $entityType = $this->chat[
'ENTITY_TYPE'] ??
'';
464 $entityType === self::IMPORT_PRIVATE_CHAT_ENTITY_TYPE
465 || $entityType === self::IMPORT_GROUP_CHAT_ENTITY_TYPE
static getMessage($code, $replace=null, $language=null)