Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
UpdateState.php
1<?php
2
3namespace Bitrix\Im\V2;
4
7use CIMStatus;
8use CPullOptions;
9use CUser;
10use CUserCounter;
11use DateTimeInterface;
12
14{
15 public function getUpdateStateData(CurrentUser $user, ?string $siteId = null): array
16 {
17 $userId = (int)$user->getId();
18 $eventParams = $this->getEventParams();
19
20 isset($siteId)
21 ? $mainCounters = CUserCounter::GetValues($userId, $siteId)
22 : $mainCounters = CUserCounter::GetValues($userId)
23 ;
24
25 $chatCounters = \Bitrix\Im\Counter::get(null, ['JSON' => 'Y']);
26
27 return [
28 'revision' => \Bitrix\Im\Revision::getWeb(),
29 'mobileRevision' => \Bitrix\Im\Revision::getMobile(),
30 'counters' => $mainCounters,
31 'chatCounters' => $chatCounters,
32 'notifyLastId' => (new \Bitrix\Im\Notify())->getLastId(),
33 'desktopStatus' => $this->CheckDesktopStatusOnline($userId),
34 'serverTime' => time(),
35 'lastUpdate' => (new \Bitrix\Main\Type\DateTime())->format(DateTimeInterface::RFC3339),
36 'eventParams' => $eventParams,
37 ];
38 }
39
40 private function getEventParams(): array
41 {
42 $event = new Event('im', 'OnUpdateState');
43 $event->send();
44
45 $result = [];
46
47 if ($event->getResults())
48 {
49 foreach($event->getResults() as $eventResult)
50 {
51 if (\Bitrix\Main\EventResult::SUCCESS)
52 {
53 $result[$eventResult->getModuleId()] = $eventResult->getParameters();
54 }
55 }
56 }
57
58 return $result;
59 }
60
61 private function checkDesktopStatusOnline(int $userId): bool
62 {
63 $maxOnlineTime = 120;
64 if ($this->isPullEnable() && CPullOptions::GetNginxStatus())
65 {
66 $maxOnlineTime = $this->GetSessionLifeTime();
67 }
68
69 $status = CIMStatus::GetStatus($userId);
70 $desktopLastDateStatus = $status['DESKTOP_LAST_DATE'] ?? null;
71
72 if (
73 $desktopLastDateStatus instanceof \Bitrix\Main\Type\DateTime
74 && $desktopLastDateStatus->getTimestamp() + $maxOnlineTime + 60 > time()
75 )
76 {
77 return true;
78 }
79
80 return false;
81 }
82
83 public function getInterval(): ?int
84 {
85 $updateStateInterval = $this->getSessionLifeTime() - 60;
86
87 if ($updateStateInterval < 100)
88 {
89 $updateStateInterval = 100;
90 }
91 elseif ($updateStateInterval > 3600)
92 {
93 $updateStateInterval = 3600;
94 }
95
96 return $updateStateInterval;
97 }
98
99 private function getSessionLifeTime(): int
100 {
101 global $USER;
102
103 $sessTimeout = CUser::GetSecondsForLimitOnline();
104
105 if ($USER instanceof CUser)
106 {
107 $arPolicy = $USER->GetSecurityPolicy();
108
109 if($arPolicy["SESSION_TIMEOUT"] > 0)
110 {
111 $sessTimeout = min($arPolicy["SESSION_TIMEOUT"] * 60, $sessTimeout);
112 }
113 }
114
115 $sessTimeout = (int)$sessTimeout;
116
117 if ($sessTimeout <= 120)
118 {
119 $sessTimeout = 100;
120 }
121
122 return $sessTimeout;
123 }
124
125 private function isPullEnable(): bool
126 {
127 return \Bitrix\Main\Loader::includeModule('pull');
128 }
129}
getUpdateStateData(CurrentUser $user, ?string $siteId=null)