1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
AnchorProvider.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Im\V2\Anchor;
6
7use Bitrix\Im\Model\ChatTable;
8use Bitrix\Im\V2\Common\ContextCustomer;
9use Bitrix\Main\Application;
10use Bitrix\Main\Data\Cache;
11
13{
14 use ContextCustomer;
15
16 private const CACHE_ID = 'user_anchors_';
17 private const CACHE_DIR = '/bx/imc/anchors/';
18 private const CACHE_TTL = 60 * 60 * 24 * 30; // 1 month
19
20 public function getUserAnchors(?int $userId = null): array
21 {
22 $userId ??= $this->getContext()->getUserId();
23 if ($userId <= 0)
24 {
25 return [];
26 }
27
28 return $this->tryInitFromCache($userId);
29 }
30
31 public function cleanUsersCache(array $userIds): void
32 {
33 foreach ($userIds as $userId)
34 {
35 $this->cleanCache($userId);
36 }
37 }
38
39 public function cleanCache(int $userId = 0): void
40 {
41 $cache = Application::getInstance()->getCache();
42
43 if ($userId > 0)
44 {
45 $cacheId = self::CACHE_ID . $userId;
46 $cacheDir = self::CACHE_DIR . $cacheId;
47
48 $cache->clean($cacheId, $cacheDir);
49 }
50 else
51 {
52 $cache->cleanDir(self::CACHE_DIR);
53 }
54 }
55
56 public function getParentMap(array $chatIds): array
57 {
58 if (empty($chatIds))
59 {
60 return [];
61 }
62
63 $map = array_fill_keys($chatIds, ['PARENT_CHAT_ID' => 0, 'PARENT_MESSAGE_ID' => 0]);
64
65 $rows = ChatTable::query()
66 ->setSelect(['ID', 'PARENT_ID', 'PARENT_MID'])
67 ->whereIn('ID', $chatIds)
68 ->fetchAll()
69 ;
70
71 foreach ($rows as $row)
72 {
73 $chatId = (int)$row['ID'];
74 $chatParentId = (int)($row['PARENT_ID'] ?? 0);
75 $messageParentId = (int)($row['PARENT_MID'] ?? 0);
76
77 $map[$chatId] = [
78 'PARENT_CHAT_ID' => $chatParentId,
79 'PARENT_MESSAGE_ID' => $messageParentId
80 ];
81 }
82
83 return $map;
84 }
85
86 public function getUserCache(int $userId): Cache
87 {
88 $cache = Application::getInstance()->getCache();
89
90 $cacheId = self::CACHE_ID . $userId;
91 $cacheDir = self::CACHE_DIR . $cacheId;
92
93 $cache->initCache(self::CACHE_TTL, $cacheId, $cacheDir);
94
95 return $cache;
96 }
97
98 private function tryInitFromCache(int $userId): array
99 {
100 $cache = $this->getUserCache($userId);
101 $anchors = $cache->getVars();
102 if ($anchors === false)
103 {
104 $anchorCollection = AnchorCollection::find(['USER_ID' => $userId]);
105
106 $parents = $this->getParentMap((array)$anchorCollection->getChatIdList());
107 $anchorCollection->fillParents($parents);
108
109 $anchors = $anchorCollection->toRestFormat();
110
111 $cache->startDataCache();
112 $cache->endDataCache($anchors);
113 }
114
115 return $anchors;
116 }
117}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static find(array $filter, array $order=['MESSAGE_ID'=> 'desc'], ?int $limit=null, ?Context $context=null)
Определения AnchorCollection.php:35
cleanUsersCache(array $userIds)
Определения AnchorProvider.php:31
getUserAnchors(?int $userId=null)
Определения AnchorProvider.php:20
getParentMap(array $chatIds)
Определения AnchorProvider.php:56
cleanCache(int $userId=0)
Определения AnchorProvider.php:39
getUserCache(int $userId)
Определения AnchorProvider.php:86
static getInstance()
Определения servicelocator.php:33
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$map
Определения config.php:5
$rows
Определения options.php:264