23 private $errorCollection;
45 $result = \Bitrix\Im\Model\MessageTable::getList([
48 'MESSAGE_FILE_ID' =>
'FILE.PARAM_VALUE',
56 '\Bitrix\Im\Model\MessageParamTable',
58 "=ref.MESSAGE_ID" =>
"this.ID",
61 [
"join_type" =>
"LEFT"]
64 'order' => [
'ID' =>
'DESC'],
65 'limit' => $messageInterval,
67 while ($row = $result->fetch())
69 if ($row[
'MESSAGE_FILE_ID'] == $file->getId())
94 $fileSource = self::SOURCE_DEFAULT
100 $initResult = $sender->init($file, $chatId, $text, $params, $userId, $fileSource);
103 return $result->addErrors($sender->errorCollection->getValues());
106 $accessResult = $sender->checkAccess();
109 return $result->addErrors($sender->errorCollection->getValues());
112 $uploadResult = $sender->uploadFileToChatStorage();
115 return $result->addErrors($sender->errorCollection->getValues());
117 $result->setData([
'IM_FILE' => $uploadResult]);
119 $sender->sendEvent();
147 $chat = ChatTable::getByPrimary($chatId, [
152 return $result->addError(
new Error(
"Getting chat error"));
155 $attach = new \CIMMessageParamAttach(
null, \CIMMessageParamAttach::CHAT);
156 $attach->AddMessage($text);
157 $addResult = \CIMMessenger::Add([
158 "TO_CHAT_ID" => $chatId,
159 "FROM_USER_ID" => $userId,
160 "FILES" => [(
int)$file->getId()],
161 "MESSAGE_TYPE" => $chat[
'TYPE'],
168 return $result->addError(
new Error(
"Adding message error"));
174 private function __construct()
179 private function init($file, $chatId, $text, $params, $userId, $fileSource): bool
182 if ($chatId <= 0 || $this->file->getId() <= 0)
184 $this->errorCollection[] =
new Error(
"Wrong CHAT_ID or FILE_ID");
189 $chat = ChatTable::getByPrimary($chatId, [
190 'select' => [
'TITLE',
'ENTITY_TYPE',
'ENTITY_ID']
194 $this->errorCollection[] =
new Error(
"Getting chat error");
200 $this->chat[
'ID'] = $chatId;
202 $this->params = $params;
203 $this->userId = $userId;
204 $this->fileSource = $fileSource;
206 if (!$this->loadModules())
208 $this->errorCollection[] =
new Error(
"Loading modules error");
213 if (!\CIMChat::GetRelationById($this->chat[
'ID'], $this->userId,
true,
false))
215 $this->errorCollection[] =
new Error(
"Getting chat relation error");
223 private function loadModules(): bool
225 if (!Loader::includeModule(
'pull') || !CPullOptions::GetNginxStatus())
231 if (!Loader::includeModule(
'disk'))
237 if (!\
Bitrix\Disk\Driver::isSuccessfullyConverted())
245 private function uploadFileToChatStorage()
247 $fileIdWithPrefix =
'disk' . $this->file->getId();
249 $attach = new \CIMMessageParamAttach(
null, \CIMMessageParamAttach::CHAT);
250 $attach->AddMessage($this->text);
252 $uploadResult = \CIMDisk::UploadFileFromDisk(
258 'PARAMS' => $this->params,
263 if (!$uploadResult || !isset($uploadResult[
'FILE_MODELS'][$fileIdWithPrefix]))
265 $this->errorCollection[] =
new Error(
"Uploading file to chat error");
270 return $uploadResult[
'FILE_MODELS'][$fileIdWithPrefix];
273 private function checkAccess(): bool
275 $storageModel = $this->file->getStorage();
277 $securityContext =
null;
278 if (is_null($this->userId))
280 $securityContext = $storageModel->getCurrentUserSecurityContext();
282 else if ($this->userId > 0)
284 $securityContext = $storageModel->getSecurityContext($this->userId);
287 if ($securityContext && !$this->file->canRead($securityContext))
289 $this->errorCollection[] =
new Error(
"Access denied");
297 private function sendEvent(): bool
299 if (empty($this->chat[
'ENTITY_TYPE']) || empty($this->chat[
'ENTITY_ID']))
304 $event =
new Event(
'im',
'onDiskShare', [
305 'FILE_SOURCE' => $this->fileSource,
306 'DISK_ID' => $this->file->getId(),
308 'ID' => $this->chat[
'ID'],
309 'TITLE' => $this->chat[
'TITLE'],
310 'ENTITY_TYPE' => $this->chat[
'ENTITY_TYPE'],
311 'ENTITY_ID' => $this->chat[
'ENTITY_ID'],
313 'USER_ID' => $this->userId,
static loadMessages($file)