Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sender.php
1<?php
2namespace Bitrix\Im\Disk;
3
4use Bitrix\Disk\File;
12use CPullOptions;
13
14Loc::loadMessages(__FILE__);
15
16class Sender
17{
18 public const SOURCE_DEFAULT = 'default';
19 public const SOURCE_CALL_RECORDING = 'call-recording';
20 public const SOURCE_CALL_DOCUMENT = 'call-document';
21
23 private $errorCollection;
25 private $chat;
27 private $file;
29 private $userId;
31 private $text;
33 private $params;
35 private $fileSource;
36
43 public static function hasFileInLastMessages(File $file, int $chatId, int $messageInterval = 10): bool
44 {
45 $result = \Bitrix\Im\Model\MessageTable::getList([
46 'select' => [
47 'ID',
48 'MESSAGE_FILE_ID' => 'FILE.PARAM_VALUE',
49 ],
50 'filter' => [
51 '=CHAT_ID' => $chatId
52 ],
53 'runtime' => [
54 new \Bitrix\Main\Entity\ReferenceField(
55 'FILE',
56 '\Bitrix\Im\Model\MessageParamTable',
57 [
58 "=ref.MESSAGE_ID" => "this.ID",
59 "=ref.PARAM_NAME" => new \Bitrix\Main\DB\SqlExpression("?s", "FILE_ID")
60 ],
61 ["join_type" => "LEFT"]
62 ),
63 ],
64 'order' => ['ID' => 'DESC'],
65 'limit' => $messageInterval,
66 ]);
67 while ($row = $result->fetch())
68 {
69 if ($row['MESSAGE_FILE_ID'] == $file->getId())
70 {
71 return false;
72 }
73 }
74
75 return true;
76 }
88 public static function sendFileToChat(
89 File $file,
90 int $chatId,
91 string $text,
92 $params = [],
93 $userId = null,
94 $fileSource = self::SOURCE_DEFAULT
95 ): Result
96 {
97 $result = new Result();
98 $sender = new self();
99
100 $initResult = $sender->init($file, $chatId, $text, $params, $userId, $fileSource);
101 if (!$initResult)
102 {
103 return $result->addErrors($sender->errorCollection->getValues());
104 }
105
106 $accessResult = $sender->checkAccess();
107 if (!$accessResult)
108 {
109 return $result->addErrors($sender->errorCollection->getValues());
110 }
111
112 $uploadResult = $sender->uploadFileToChatStorage();
113 if (!$uploadResult)
114 {
115 return $result->addErrors($sender->errorCollection->getValues());
116 }
117 $result->setData(['IM_FILE' => $uploadResult]);
118
119 $sender->sendEvent();
120
121 return $result;
122 }
123
137 public static function sendExistingFileToChat(
138 File $file,
139 int $chatId,
140 string $text,
141 $params = [],
142 int $userId
143 ): Result
144 {
145 $result = new Result();
146
147 $chat = ChatTable::getByPrimary($chatId, [
148 'select' => ['TYPE']
149 ])->fetch();
150 if (!$chat)
151 {
152 return $result->addError(new Error("Getting chat error"));
153 }
154
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'],
162 "ATTACH" => $attach,
163 "PARAMS" => $params
164 ]);
165
166 if (!$addResult)
167 {
168 return $result->addError(new Error("Adding message error"));
169 }
170
171 return $result;
172 }
173
174 private function __construct()
175 {
176 $this->errorCollection = new ErrorCollection();
177 }
178
179 private function init($file, $chatId, $text, $params, $userId, $fileSource): bool
180 {
181 $this->file = $file;
182 if ($chatId <= 0 || $this->file->getId() <= 0)
183 {
184 $this->errorCollection[] = new Error("Wrong CHAT_ID or FILE_ID");
185
186 return false;
187 }
188
189 $chat = ChatTable::getByPrimary($chatId, [
190 'select' => ['TITLE', 'ENTITY_TYPE', 'ENTITY_ID']
191 ])->fetch();
192 if (!$chat)
193 {
194 $this->errorCollection[] = new Error("Getting chat error");
195
196 return false;
197 }
198
199 $this->chat = $chat;
200 $this->chat['ID'] = $chatId;
201 $this->text = $text;
202 $this->params = $params;
203 $this->userId = $userId;
204 $this->fileSource = $fileSource;
205
206 if (!$this->loadModules())
207 {
208 $this->errorCollection[] = new Error("Loading modules error");
209
210 return false;
211 }
212
213 if (!\CIMChat::GetRelationById($this->chat['ID'], $this->userId, true, false))
214 {
215 $this->errorCollection[] = new Error("Getting chat relation error");
216
217 return false;
218 }
219
220 return true;
221 }
222
223 private function loadModules(): bool
224 {
225 if (!Loader::includeModule('pull') || !CPullOptions::GetNginxStatus())
226 {
227 return false;
228 }
229
230
231 if (!Loader::includeModule('disk'))
232 {
233 return false;
234 }
235
236
237 if (!\Bitrix\Disk\Driver::isSuccessfullyConverted())
238 {
239 return false;
240 }
241
242 return true;
243 }
244
245 private function uploadFileToChatStorage()
246 {
247 $fileIdWithPrefix = 'disk' . $this->file->getId();
248
249 $attach = new \CIMMessageParamAttach(null, \CIMMessageParamAttach::CHAT);
250 $attach->AddMessage($this->text);
251
252 $uploadResult = \CIMDisk::UploadFileFromDisk(
253 $this->chat['ID'],
254 [$fileIdWithPrefix],
255 '',
256 [
257 'SYMLINK' => true,
258 'PARAMS' => $this->params,
259 'ATTACH' => $attach
260 ]
261 );
262
263 if (!$uploadResult || !isset($uploadResult['FILE_MODELS'][$fileIdWithPrefix]))
264 {
265 $this->errorCollection[] = new Error("Uploading file to chat error");
266
267 return false;
268 }
269
270 return $uploadResult['FILE_MODELS'][$fileIdWithPrefix];
271 }
272
273 private function checkAccess(): bool
274 {
275 $storageModel = $this->file->getStorage();
276
277 $securityContext = null;
278 if (is_null($this->userId))
279 {
280 $securityContext = $storageModel->getCurrentUserSecurityContext();
281 }
282 else if ($this->userId > 0)
283 {
284 $securityContext = $storageModel->getSecurityContext($this->userId);
285 }
286
287 if ($securityContext && !$this->file->canRead($securityContext))
288 {
289 $this->errorCollection[] = new Error("Access denied");
290
291 return false;
292 }
293
294 return true;
295 }
296
297 private function sendEvent(): bool
298 {
299 if (empty($this->chat['ENTITY_TYPE']) || empty($this->chat['ENTITY_ID']))
300 {
301 return false;
302 }
303
304 $event = new Event('im', 'onDiskShare', [
305 'FILE_SOURCE' => $this->fileSource,
306 'DISK_ID' => $this->file->getId(),
307 'CHAT' => [
308 'ID' => $this->chat['ID'],
309 'TITLE' => $this->chat['TITLE'],
310 'ENTITY_TYPE' => $this->chat['ENTITY_TYPE'],
311 'ENTITY_ID' => $this->chat['ENTITY_ID'],
312 ],
313 'USER_ID' => $this->userId,
314 ]);
315 $event->send();
316
317 return true;
318 }
319}
const SOURCE_CALL_DOCUMENT
Definition sender.php:20
static hasFileInLastMessages(File $file, int $chatId, int $messageInterval=10)
Definition sender.php:43
static sendExistingFileToChat(File $file, int $chatId, string $text, $params=[], int $userId)
Definition sender.php:137
static sendFileToChat(File $file, int $chatId, string $text, $params=[], $userId=null, $fileSource=self::SOURCE_DEFAULT)
Definition sender.php:88
const SOURCE_CALL_RECORDING
Definition sender.php:19
static loadMessages($file)
Definition loc.php:64