3declare(strict_types=1);
5namespace Bitrix\Im\V2\Chat\Cleanup;
9use Bitrix\Disk\SystemUser;
10use Bitrix\Im\Model\AnchorTable;
11use Bitrix\Im\Model\BlockUserTable;
12use Bitrix\Im\Model\ChatIndexTable;
13use Bitrix\Im\Model\ChatParamTable;
14use Bitrix\Im\Model\ChatTable;
15use Bitrix\Im\Model\LastMessageTable;
16use Bitrix\Im\Model\LinkCalendarIndexTable;
17use Bitrix\Im\Model\LinkCalendarTable;
18use Bitrix\Im\Model\LinkFavoriteTable;
19use Bitrix\Im\Model\LinkFileTable;
20use Bitrix\Im\Model\LinkPinTable;
21use Bitrix\Im\Model\LinkReminderTable;
22use Bitrix\Im\Model\LinkTaskTable;
23use Bitrix\Im\Model\LinkUrlIndexTable;
24use Bitrix\Im\Model\LinkUrlTable;
25use Bitrix\Im\Model\MessageDisappearingTable;
26use Bitrix\Im\Model\MessageIndexTable;
27use Bitrix\Im\Model\MessageParamTable;
28use Bitrix\Im\Model\MessageTable;
29use Bitrix\Im\Model\MessageUnreadTable;
30use Bitrix\Im\Model\MessageUuidTable;
31use Bitrix\Im\Model\MessageViewedTable;
32use Bitrix\Im\Model\NoRelationPermissionDiskTable;
33use Bitrix\Im\Model\ReactionTable;
34use Bitrix\Im\Model\RecentTable;
35use Bitrix\Im\Model\RelationTable;
36use Bitrix\Im\V2\Anchor\DI\AnchorContainer;
38use Bitrix\Im\V2\Integration\HumanResources\Structure;
39use Bitrix\Im\V2\Message\CounterService;
40use Bitrix\Im\V2\Sync\Event;
41use Bitrix\Im\V2\Sync\Logger;
42use Bitrix\ImConnector\Model\ChatLastMessageTable;
43use Bitrix\Main\Application;
44use Bitrix\Main\ArgumentException;
45use Bitrix\Main\Entity\Query;
46use Bitrix\Main\ObjectPropertyException;
47use Bitrix\Main\ORM\Data\DataManager;
48use Bitrix\Main\SystemException;
54 private array $chatMembers;
58 if ($chat instanceof
Chat)
60 $this->chatId = $chat->getChatId();
64 $this->chatId = $chat;
79 private function collectByColumn(
82 int|
null $limit =
null,
83 string $columnName =
'CHAT_ID',
84 string|
array $selectColumnName =
'ID',
87 if (!is_a($tableClass, DataManager::class,
true))
92 $isMultiColumn = is_array($selectColumnName);
93 $indexColumn = $isMultiColumn ? reset($selectColumnName) : $selectColumnName;
96 'select' => $isMultiColumn ? $selectColumnName : [$selectColumnName],
97 'filter' => [(is_array($columnValue) ?
'@' :
'=') . $columnName => $columnValue],
108 foreach (
$result->fetchAll() as $row)
110 $data[$row[$indexColumn]] = $isMultiColumn ? $row : $row[$indexColumn];
124 private function deleteByColumn(
126 array $idColumnValues,
127 string $idColumnName =
'ID',
130 if (0 ===
count($idColumnValues))
135 if (!is_a($tableClass, DataManager::class,
true))
140 $whereSql = Query::buildFilterSql($tableClass::getEntity(), [
'@' . $idColumnName => $idColumnValues]);
141 $tableName = $tableClass::getTableName();
143 $connection->queryExecute($sql =
"DELETE FROM {$tableName} WHERE {$whereSql}");
146 private function getCleanupSequence():
array
151 'cleanupLastMessages',
152 'cleanupFavoritesLinks',
155 'cleanupCalendarLinks',
159 'cleanupNoRelationPermDisk',
163 private function getCurrentCleanupStep(?
string $previousCompletedStep =
null): ?string
165 $prevStepFound =
false;
167 foreach ($this->getCleanupSequence() as $step)
169 if ((
null === $previousCompletedStep) || $prevStepFound)
174 if ($step === $previousCompletedStep)
176 $prevStepFound =
true;
183 public function processCleanup(
int $limit, ?
string $previousCompletedStep =
null): ?string
185 $currentStep = $this->getCurrentCleanupStep($previousCompletedStep);
187 if (
null === $currentStep)
192 $isCompleted = !$this->{$currentStep}($limit);
194 return $isCompleted ? $currentStep : $previousCompletedStep;
204 $nextChildChat = $this->collectByColumn(
211 if (
count($nextChildChat) > 0)
213 return array_key_first($nextChildChat);
233 $this->chatMembers ??= $this->collectByColumn(
234 RelationTable::class,
236 selectColumnName:
'USER_ID',
239 return $this->chatMembers;
251 $logger = Logger::getInstance();
256 new Event(Event::COMPLETE_DELETE_EVENT, Event::CHAT_ENTITY, $this->chatId),
275 'command' =>
'chatDelete',
277 'chatId' => $this->chatId,
278 'parentChatId' => $chat->getParentChatId() ?? 0,
279 'dialogId' => $chat->getDialogId(),
280 'type' => $chat->getExtendedType(),
286 $this->deleteByColumn(RelationTable::class, [$this->chatId],
'CHAT_ID');
287 $this->deleteByColumn(ChatTable::class, [$this->chatId]);
289 $this->deleteByColumn(RecentTable::class, [$this->chatId],
'ITEM_CID');
290 $this->deleteByColumn(MessageUnreadTable::class, [$this->chatId],
'CHAT_ID');
291 $this->deleteByColumn(MessageUnreadTable::class, [$this->chatId],
'PARENT_ID');
293 $this->deleteByColumn(ChatIndexTable::class, [$this->chatId],
'CHAT_ID');
294 $this->deleteByColumn(AnchorTable::class, [$this->chatId],
'CHAT_ID');
299 CPullStack::AddShared($arMessage);
312 $provider = AnchorContainer::getInstance()->getAnchorProvider();
327 $this->deleteByColumn(ChatParamTable::class, [$this->chatId],
'CHAT_ID');
328 $this->deleteByColumn(ChatLastMessageTable::class, [$this->chatId],
'EXTERNAL_CHAT_ID');
329 $this->deleteByColumn(BlockUserTable::class, [$this->chatId],
'CHAT_ID');
341 $ids = $this->collectByColumn(LastMessageTable::class, $this->chatId, $limit);
343 if (
count($ids) === 0)
348 $this->deleteByColumn(LastMessageTable::class, $ids);
360 $ids = $this->collectByColumn(MessageTable::class, $this->chatId, $limit);
362 if (
count($ids) === 0)
368 $folderModel = Folder::getById($folderId);
369 $folderModel->deleteTree(SystemUser::SYSTEM_USER_ID);
375 $params = $this->collectByColumn(
376 MessageParamTable::class,
378 columnName:
'MESSAGE_ID',
385 $paramIds = array_keys(
$params);
390 if ($row[
'PARAM_NAME'] ===
'FILE_ID')
392 $fileIds = intval($row[
'PARAM_VALUE']);
396 $this->deleteByColumn(MessageTable::class, $ids);
397 $this->deleteByColumn(MessageUuidTable::class, $ids,
'MESSAGE_ID');
398 $this->deleteByColumn(MessageIndexTable::class, $ids,
'MESSAGE_ID');
399 $this->deleteByColumn(MessageViewedTable::class, $ids,
'MESSAGE_ID');
400 $this->deleteByColumn(MessageDisappearingTable::class, $ids,
'MESSAGE_ID');
401 $this->deleteByColumn(MessageParamTable::class, $paramIds);
402 $this->deleteByColumn(LinkPinTable::class, $ids,
'MESSAGE_ID');
404 foreach ($fileIds as $fileId)
406 File::getById($fileId)->delete(SystemUser::SYSTEM_USER_ID);
419 $ids = $this->collectByColumn(LinkFavoriteTable::class, $this->chatId, $limit);
421 if (
count($ids) === 0)
426 $this->deleteByColumn(LinkFavoriteTable::class, $ids);
438 $ids = $this->collectByColumn(LinkUrlTable::class, $this->chatId, $limit);
440 if (
count($ids) === 0)
445 $this->deleteByColumn(LinkUrlIndexTable::class, $ids,
'URL_ID');
446 $this->deleteByColumn(LinkUrlTable::class, $ids);
458 $ids = $this->collectByColumn(LinkTaskTable::class, $this->chatId, $limit);
460 if (
count($ids) === 0)
465 $this->deleteByColumn(LinkTaskTable::class, $ids);
477 $ids = $this->collectByColumn(LinkCalendarTable::class, $this->chatId, $limit);
479 if (
count($ids) === 0)
484 $this->deleteByColumn(LinkCalendarIndexTable::class, $ids);
485 $this->deleteByColumn(LinkCalendarTable::class, $ids);
497 $ids = $this->collectByColumn(LinkFileTable::class, $this->chatId, $limit);
499 if (
count($ids) === 0)
504 $this->deleteByColumn(LinkFileTable::class, $ids);
516 $ids = $this->collectByColumn(ReactionTable::class, $this->chatId, $limit);
518 if (
count($ids) === 0)
523 $this->deleteByColumn(ReactionTable::class, $ids);
535 $ids = $this->collectByColumn(LinkReminderTable::class, $this->chatId, $limit);
537 if (
count($ids) === 0)
542 $this->deleteByColumn(LinkReminderTable::class, $ids);
554 $ids = $this->collectByColumn(
555 NoRelationPermissionDiskTable::class,
560 if (
count($ids) === 0)
565 $this->deleteByColumn(NoRelationPermissionDiskTable::class, $ids);
if(!Loader::includeModule('messageservice')) $provider
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static register(int $chatId, ?int $currentChatId=null, ?int $userId=null,)
cleanupCalendarLinks(int $limit)
cleanupChatAddons($limit)
cleanupFavoritesLinks(int $limit)
cleanupLastMessages(int $limit)
cleanupReactions(int $limit)
cleanupUrlsLinks(int $limit)
cleanupReminder(int $limit)
processCleanup(int $limit, ?string $previousCompletedStep=null)
cleanupTasksLinks(int $limit)
cleanupFilesLinks(int $limit)
cleanupChatBasics(?int $userId=null)
__construct(Chat|int $chat)
cleanupNoRelationPermDisk(int $limit)
deleteChat(?int $userId=null)
static clearCache($moduleId)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']