1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
GuestCounter.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat\Collab;
4
5use Bitrix\Im\Common;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Entity\User\UserCollaber;
8use Bitrix\Im\V2\RelationCollection;
9use Bitrix\Main\Application;
10use Bitrix\Pull\Event;
11
13{
14 protected const CACHE_TTL = 18144000;
15
16 protected static array $staticCache = [];
17
18 protected Chat $chat;
19 protected ?RelationCollection $relations = null;
20
21 public function __construct(Chat $chat)
22 {
23 $this->chat = $chat;
24 }
25
26 public function getGuestCount(): int
27 {
28 if (isset(self::$staticCache[$this->chat->getId()]))
29 {
30 return self::$staticCache[$this->chat->getId()];
31 }
32
33 $cache = Application::getInstance()->getCache();
34 if ($cache->initCache(self::CACHE_TTL, $this->getCacheId(), $this->getCacheDir()))
35 {
36 $cachedGuestCount = $cache->getVars();
37
38 if (!is_array($cachedGuestCount))
39 {
40 $cachedGuestCount = [];
41 }
42
43 return self::$staticCache[$this->chat->getId()] = $cachedGuestCount['guestCount'] ?? 0;
44 }
45
46 self::$staticCache[$this->chat->getId()] = $this->getGuestCountByRelation();
47
48 $cache->startDataCache();
49 $cache->endDataCache(['guestCount' => self::$staticCache[$this->chat->getId()]]);
50
51 return self::$staticCache[$this->chat->getId()];
52 }
53
54 public function sendPushGuestCount(): void
55 {
56 $relations = $this->relations ?? $this->chat->getRelations();
57
58 $push = [
59 'module_id' => 'im',
60 'command' => 'updateCollabGuestCount',
61 'params' => [
62 'dialogId' => $this->chat->getDialogId(),
63 'chatId' => $this->chat->getId(),
64 'guestCount' => $this->getGuestCount(),
65 ],
66 'extra' => Common::getPullExtra()
67 ];
68 Event::add($relations->getUserIds(), $push);
69 }
70
71 protected function getGuestCountByRelation(): int
72 {
73 $guestCount = 0;
74
75 $relations = $this->relations ?? $this->chat->getRelations();
76 foreach ($relations->getUsers() as $user)
77 {
78 if ($user instanceof UserCollaber)
79 {
80 $guestCount++;
81 }
82 }
83
84 return $guestCount;
85 }
86
88 {
89 $this->relations = $relations;
90 return $this;
91 }
92
93 private function getCacheId(): string
94 {
95 return "chat_collab_guest_count_{$this->chat->getId()}";
96 }
97
98 private function getCacheDir(): string
99 {
100 return static::getCacheDirByChatId($this->chat->getId());
101 }
102
103 private static function getCacheDirByChatId(int $chatId): string
104 {
105 $cacheSubDir = $chatId % 100;
106
107 return "/bx/im/collab/guest_count/v1/{$cacheSubDir}/{$chatId}";
108 }
109
110 public static function cleanCache(int $chatId): void
111 {
112 unset(self::$staticCache[$chatId]);
113 Application::getInstance()->getCache()->cleanDir(static::getCacheDirByChatId($chatId));
114 }
115}
static getPullExtra()
Определения common.php:127
setRelations(RelationCollection $relations)
Определения GuestCounter.php:87
RelationCollection $relations
Определения GuestCounter.php:19
static cleanCache(int $chatId)
Определения GuestCounter.php:110
__construct(Chat $chat)
Определения GuestCounter.php:21
static array $staticCache
Определения GuestCounter.php:16
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
$user
Определения mysql_to_pgsql.php:33