1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
DeletionStrategy.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Message\Delete\Strategy;
4
5use Bitrix\Disk\SystemUser;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Common\ContextCustomer;
8use Bitrix\Im\V2\Entity\File\FileItem;
9use Bitrix\Im\V2\Link\File\FileService;
10use Bitrix\Im\V2\Message\Delete\DeletionMode;
11use Bitrix\Im\V2\MessageCollection;
12use Bitrix\Im\V2\Result;
13use Bitrix\Im\V2\Sync\Event;
14use Bitrix\Im\V2\Sync\Logger;
15
16abstract class DeletionStrategy
17{
18 use ContextCustomer;
19
21 protected Chat $chat;
22
23 final protected function __construct(MessageCollection $messages)
24 {
25 $this->messages = $messages;
26 $chatId = $messages->getCommonChatId();
27 $this->chat = Chat::getInstance($chatId);
28 }
29
31 {
32 return match ($deletionMode)
33 {
34 DeletionMode::Soft => (new SoftDeletionStrategy($messages)),
36 default => (new NoneDeletionStrategy($messages)),
37 };
38 }
39
43 abstract protected function onBeforeDelete(): void;
44
48 abstract protected function onAfterDelete(): void;
49
53 abstract protected function execute(): void;
54
55 protected function logToSync(string $event): void
56 {
57 $ids = $this->messages->getIds();
58
59 foreach ($ids as $id)
60 {
61 Logger::getInstance()->add(
62 new Event($event, Event::MESSAGE_ENTITY, $id),
63 fn () => $this->chat->getRelations()->getUserIds(),
64 $this->chat
65 );
66 }
67 }
68
69 protected function deleteFiles(): void
70 {
71 $messageIdsToDeleteLinks = [];
72
73 foreach ($this->messages as $message)
74 {
75 if ($message->getId() === null || $message->getFiles()->isEmpty())
76 {
77 continue;
78 }
79
80 $needToDeleteLink = true;
81
85 foreach ($message->getFiles() as $file)
86 {
87 $diskFile = $file->getDiskFile();
88 $contextUserId = $this->getContext()->getUserId();
89
90 if (!isset($diskFile))
91 {
92 continue;
93 }
94
95 if (
96 (int)$diskFile->getCreatedBy() === $contextUserId
97 && (int)$diskFile->getParentId() === $this->chat->getDiskFolderId()
98 && $diskFile->delete(SystemUser::SYSTEM_USER_ID)
99 )
100 {
105 $needToDeleteLink = false;
106 }
107 }
108
109 if ($needToDeleteLink)
110 {
111 $messageIdsToDeleteLinks[] = $message->getId();
112 }
113 }
114
115 (new FileService())->deleteFilesByMessageIds($messageIdsToDeleteLinks);
116 }
117
121 protected function checkResult(Result $result): void
122 {
123 if (!$result->isSuccess())
124 {
126 }
127 }
128
129 final public function delete(): Result
130 {
131 $result = new Result();
132
133 try
134 {
135 $this->onBeforeDelete();
136 $this->execute();
137 $this->onAfterDelete();
138 }
139 catch (InterruptedExecutionException $exception)
140 {
141 $result = $exception->getResult();
142 }
143 finally
144 {
145 return $result;
146 }
147 }
148}
static getInstance(MessageCollection $messages, DeletionMode $deletionMode)
Определения DeletionStrategy.php:30
__construct(MessageCollection $messages)
Определения DeletionStrategy.php:23
static getInstance()
Определения application.php:98
Определения result.php:20
$result
Определения get_property_values.php:14
$message
Определения payment.php:8
$event
Определения prolog_after.php:141