Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ChatFactory.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
6use Bitrix\Im\V2\Common\ContextCustomer;
10
12{
13 use ContextCustomer;
14
15 public const NON_CACHED_FIELDS = ['MESSAGE_COUNT', 'USER_COUNT', 'LAST_MESSAGE_ID'];
16
17 protected static self $instance;
18
19 private function __construct()
20 {
21 }
22
27 public static function getInstance(): self
28 {
29 if (isset(self::$instance))
30 {
31 return self::$instance;
32 }
33
34 self::$instance = new static();
35
36 return self::$instance;
37 }
38
39
40
41 //region Chat actions
42
47 public function getChat($params): ?Chat
48 {
49 $type = $params['TYPE'] ?? $params['MESSAGE_TYPE'] ?? '';
50
51 if (empty($params))
52 {
53 return null;
54 }
55 if (is_numeric($params))
56 {
57 $params = ['CHAT_ID' => (int)$params];
58 }
59 elseif (is_string($params))
60 {
61 $params = ['DIALOG_ID' => $params];
62 if (\Bitrix\Im\Common::isChatId($params['DIALOG_ID']))
63 {
64 $params['CHAT_ID'] = \Bitrix\Im\Dialog::getChatId($params['DIALOG_ID']);
65 }
66 }
67
68 $findResult = $this->findChat($params);
69
70 if ($findResult->hasResult())
71 {
72 $chatParams = $findResult->getResult();
73
74 return $this->initChat($chatParams);
75 }
76
77 if (
78 $type === Chat::IM_TYPE_SYSTEM
79 || $type === Chat::IM_TYPE_PRIVATE
80 )
81 {
82 $addResult = $this->addChat($params);
83 if ($addResult->hasResult())
84 {
85 $chat = $addResult->getResult()['CHAT'];
86 $chat->setContext($this->context);
87
88 return $chat;
89 }
90 }
91
92 return null;
93 }
94
98 public function getNotifyFeed($userId = null): ?NotifyChat
99 {
100 if (!$userId)
101 {
102 $userId = $this->getContext()->getUserId();
103 }
104
105 $params = [
106 'TYPE' => Chat::IM_TYPE_SYSTEM,
107 'TO_USER_ID' => $userId,
108 ];
109
110 return $this->getChat($params);
111 }
112
118 public function getEntityChat(string $entityType, $entityId): ?EntityChat
119 {
120 $params = [
121 'TYPE' => Chat::IM_TYPE_CHAT,
122 'ENTITY_TYPE' => $entityType,
123 'ENTITY_ID' => $entityId,
124 ];
125
126 return $this->getChat($params);
127 }
128
134 public function getGeneralChat(): ?GeneralChat
135 {
136 return GeneralChat::get();
137 }
138
142 public function getPrivateChat($fromUserId, $toUserId): ?Chat\PrivateChat
143 {
144 $params = [
145 'TYPE' => Chat::IM_TYPE_PRIVATE,
146 'FROM_USER_ID' => $fromUserId,
147 'TO_USER_ID' => $toUserId,
148 ];
149
150 return $this->getChat($params);
151 }
152
156 public function getPersonalChat($userId = null): ?Chat\FavoriteChat
157 {
158 if (!$userId)
159 {
160 $userId = $this->getContext()->getUserId();
161 }
162
163 $params = [
164 'TYPE' => Chat::IM_TYPE_PRIVATE,
165 'FROM_USER_ID' => $userId,
166 'TO_USER_ID' => $userId,
167 ];
168
169 return $this->getChat($params);
170 }
171 //endregion
172
173 //region Chat Create
178 public function initChat(?array $params = null): Chat
179 {
180 $type = $params['TYPE'] ?? $params['MESSAGE_TYPE'] ?? '';
181 $entityType = $params['ENTITY_TYPE'] ?? '';
182 switch (true)
183 {
184 case $entityType === Chat::ENTITY_TYPE_FAVORITE:
185 case $entityType === 'PERSONAL':
186 $chat = new FavoriteChat($params);
187 break;
188
189 case $entityType === Chat::ENTITY_TYPE_GENERAL:
190 $chat = new GeneralChat($params);
191 break;
192
193 case $type === Chat::IM_TYPE_OPEN_LINE:
194 case $entityType === Chat::ENTITY_TYPE_LINE:
195 $chat = new OpenLineChat($params);
196 break;
197
198 case $entityType === Chat::ENTITY_TYPE_LIVECHAT:
199 $chat = new OpenLineLiveChat($params);
200 break;
201
202 case $entityType === Chat::ENTITY_TYPE_VIDEOCONF:
203 $chat = new VideoConfChat($params);
204 break;
205
206 case $entityType === Chat::IM_TYPE_CHANNEL:
207 $chat = new ChannelChat($params);
208 break;
209
210 case $type === Chat::IM_TYPE_OPEN:
211 $chat = new OpenChat($params);
212 break;
213
214 case $type === Chat::IM_TYPE_SYSTEM:
215 $chat = new NotifyChat($params);
216 break;
217
218 case $type === Chat::IM_TYPE_PRIVATE:
219 $chat = new PrivateChat($params);
220 break;
221
222 case $type === Chat::IM_TYPE_CHAT:
223 $chat = new GroupChat($params);
224 break;
225
226 case $type === Chat::IM_TYPE_COMMENT:
227 $chat = new CommentChat($params);
228 break;
229
230 case $type === Chat::IM_TYPE_COPILOT:
231 $chat = new CopilotChat($params);
232 break;
233
234 default:
235 $chat = new NullChat();
236 break;
237 }
238
239 $chat->setContext($this->context);
240
241 return $chat;
242 }
243
248 public function createNotifyFeed(?array $params = null): Chat
249 {
250 $params = $params ?? [];
251 $params['TYPE'] = Chat::IM_TYPE_SYSTEM;
252
253 return $this->initChat($params);
254 }
255
260 public function createPersonalChat(?array $params = null): Chat
261 {
262 $params = $params ?? [];
263 $params['ENTITY_TYPE'] = Chat::ENTITY_TYPE_FAVORITE;
264
265 return $this->initChat($params);
266 }
267
272 public function createPrivateChat(?array $params = null): Chat
273 {
274 $params = $params ?? [];
275 $params['TYPE'] = Chat::IM_TYPE_PRIVATE;
276
277 return $this->initChat($params);
278 }
279
284 public function createOpenChat(?array $params = null): Chat
285 {
286 $params = $params ?? [];
287 $params['TYPE'] = Chat::IM_TYPE_OPEN;
288
289 return $this->initChat($params);
290 }
291
296 public function createOpenLineChat(?array $params = null): Chat
297 {
298 $params = $params ?? [];
299 $params['TYPE'] = Chat::IM_TYPE_OPEN_LINE;
300
301 return $this->initChat($params);
302 }
303
304 //endregion
305
306 //region Chat Find
307
308
313 public function getChatById(int $chatId): Chat
314 {
315 $findResult = $this->findChat(['CHAT_ID' => $chatId]);
316 if ($findResult->hasResult())
317 {
318 $chatParams = $findResult->getResult();
319
321 $chat = $this->initChat($chatParams);
322 }
323 else
324 {
325 $chat = new NullChat();
326 }
327
328 return $chat;
329 }
330
331
356 public function findChat(array $params): Result
357 {
358 $result = new Result;
359
360 if (isset($params['TYPE']))
361 {
362 $params['MESSAGE_TYPE'] = $params['TYPE'];
363 }
364
365 if (empty($params['CHAT_ID']) && !empty($params['DIALOG_ID']))
366 {
367 if (\Bitrix\Im\Common::isChatId($params['DIALOG_ID']))
368 {
369 $params['CHAT_ID'] = \Bitrix\Im\Dialog::getChatId($params['DIALOG_ID']);
370 if (!isset($params['MESSAGE_TYPE']))
371 {
372 $params['MESSAGE_TYPE'] = Chat::IM_TYPE_CHAT;
373 }
374 }
375 else
376 {
377 $params['TO_USER_ID'] = (int)$params['DIALOG_ID'];
378 $params['MESSAGE_TYPE'] = Chat::IM_TYPE_PRIVATE;
379 }
380 }
381
382 if (!empty($params['CHAT_ID']) && (int)$params['CHAT_ID'] > 0)
383 {
384 $chatId = (int)$params['CHAT_ID'];
385 $cache = $this->getCache($chatId);
386 $cachedChat = $cache->getVars();
387
388 if ($cachedChat !== false)
389 {
390 return $result->setResult($this->filterNonCachedFields($cachedChat));
391 }
392
393 $chat = \Bitrix\Im\Model\ChatTable::getByPrimary((int)$params['CHAT_ID'])->fetch();
394
395 if ($chat)
396 {
397 $cache->startDataCache();
398 $cache->endDataCache($chat);
399 }
400 else
401 {
402 $chat = null;
403 }
404
405 return $result->setResult($chat);
406 }
407
408 switch ($params['MESSAGE_TYPE'] ?? '')
409 {
411 $result = NotifyChat::find($params, $this->context);
412 break;
413
415 if (
416 isset($params['TO_USER_ID'], $params['FROM_USER_ID'])
417 && $params['TO_USER_ID'] == $params['FROM_USER_ID']
418 )
419 {
420 $result = FavoriteChat::find($params, $this->context);
421 }
422 else
423 {
424 $result = PrivateChat::find($params, $this->context);
425 }
426 break;
427
430 if (
431 isset($params['ENTITY_TYPE'])
432 && $params['ENTITY_TYPE'] == Chat::ENTITY_TYPE_GENERAL
433 )
434 {
435 $result = GeneralChat::find($params);
436 break;
437 }
439 $result = Chat::find($params, $this->context);
440 break;
441
442 default:
443 return $result->addError(new ChatError(ChatError::WRONG_TYPE));
444 }
445
446 return $result;
447 }
448
449 private function filterNonCachedFields(array $chat): array
450 {
451 foreach (self::NON_CACHED_FIELDS as $key)
452 {
453 unset($chat[$key]);
454 }
455
456 return $chat;
457 }
458
459 //endregion
460
461 //region Add new chat
462
467 public function addChat(array $params): Result
468 {
469 $addResult = new Result();
470
471 $params['ENTITY_TYPE'] = $params['ENTITY_TYPE'] ?? '';
472
473 $params['TYPE'] = $params['TYPE'] ?? Chat::IM_TYPE_CHAT;
474
475 // Temporary workaround for Open chat type
476 if ($params['SEARCHABLE'] === 'Y' && $params['TYPE'] === Chat::IM_TYPE_CHAT)
477 {
478 $params['TYPE'] = Chat::IM_TYPE_OPEN;
479 }
480 else
481 {
482 $params['SEARCHABLE'] = 'N';
483 }
484
485 switch ($params['ENTITY_TYPE'])
486 {
488 $addResult = (new FavoriteChat)->add($params);
489 break;
491 $addResult = (new VideoConfChat)->add($params);
492 break;
494 $addResult = (new GeneralChat())->add($params);
495 break;
497 $addResult = (new OpenLineLiveChat())->add($params);
498 break;
499 default:
500 switch ($params['TYPE'])
501 {
503 if ($params['ENTITY_TYPE'])
504 {
505 $addResult = (new EntityChat())->add($params);
506 break;
507 }
508 $addResult = (new GroupChat())->add($params);
509 break;
511 $addResult = (new OpenChat())->add($params);
512 break;
514 $addResult = (new ChannelChat())->add($params);
515 break;
517 $addResult = (new PrivateChat)->add($params);
518 break;
520 $addResult = (new NotifyChat)->add($params);
521 break;
523 $addResult = (new CommentChat())->add($params);
524 break;
526 $addResult = (new OpenLineChat())->add($params);
527 break;
529 $addResult = (new CopilotChat())->add($params);
530 break;
531 default:
532 $addResult->addError(new ChatError(ChatError::CREATION_ERROR));
533 }
534 }
535
536 return $addResult;
537 }
538
539 //endregion
540
541 //region Cache
542
543 public function cleanCache(int $id): void
544 {
545 Application::getInstance()->getCache()->cleanDir($this->getCacheDir($id));
546 }
547
548 protected function getCache(int $id): Cache
549 {
550 $cache = Application::getInstance()->getCache();
551
552 $cacheTTL = defined("BX_COMP_MANAGED_CACHE") ? 18144000 : 1800;
553 $cacheId = "chat_data_{$id}";
554 $cacheDir = $this->getCacheDir($id);
555
556 $cache->initCache($cacheTTL, $cacheId, $cacheDir);
557
558 return $cache;
559 }
560
561 private function getCacheDir(int $id): string
562 {
563 $cacheSubDir = $id % 100;
564
565 return "/bx/imc/chatdata/5/{$cacheSubDir}/{$id}";
566 }
567
568 //endregion
569}
static isChatId($id)
Definition common.php:64
createOpenLineChat(?array $params=null)
createPrivateChat(?array $params=null)
getEntityChat(string $entityType, $entityId)
getPrivateChat($fromUserId, $toUserId)
initChat(?array $params=null)
createPersonalChat(?array $params=null)
createNotifyFeed(?array $params=null)
createOpenChat(?array $params=null)
static find(array $params=[], ?Context $context=null)
static find(array $params=[], ?Context $context=null)
static find(array $params=[], ?Context $context=null)
static find(array $params, ?Context $context=null)
const ENTITY_TYPE_LIVECHAT
Definition Chat.php:98
const IM_TYPE_CHANNEL
Definition Chat.php:60
const IM_TYPE_PRIVATE
Definition Chat.php:55
const ENTITY_TYPE_GENERAL
Definition Chat.php:91
const IM_TYPE_OPEN_LINE
Definition Chat.php:58
const ENTITY_TYPE_VIDEOCONF
Definition Chat.php:90
const IM_TYPE_COMMENT
Definition Chat.php:57
const IM_TYPE_OPEN
Definition Chat.php:61
const IM_TYPE_SYSTEM
Definition Chat.php:59
const IM_TYPE_CHAT
Definition Chat.php:56
const ENTITY_TYPE_LINE
Definition Chat.php:97
const IM_TYPE_COPILOT
Definition Chat.php:62
static find(array $params, ?Context $context=null)
Definition Chat.php:1346
const ENTITY_TYPE_FAVORITE
Definition Chat.php:92