1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
im_status.php
См. документацию.
1<?
2use Bitrix\Im as IM;
3
5{
6 public static $AVAILABLE_STATUSES = Array('online', 'dnd', 'away', 'break');
7 public static $CACHE_USERS = null;
8 public static $CACHE_RECENT = null;
9
13 protected static array $status = [];
14
15 const CACHE_TTL = 31536000;
16 const CACHE_PATH = '/bx/im/status/';
17
18 const CACHE_ONLINE_TTL = 60;
19 const CACHE_ONLINE_PATH = '/bx/im/online/';
20
21 public static function Set($userId, $params)
22 {
23 global $CACHE_MANAGER;
24
25 $userId = intval($userId);
26 if ($userId <= 0)
27 return false;
28
29 if (isset($params['STATUS']))
30 {
31 $params['IDLE'] = null;
32 }
33
34 if (isset($params['STATUS']) || isset($params['COLOR']))
35 {
36 $CACHE_MANAGER->ClearByTag("USER_NAME_".$userId);
37 }
38
39 $previousStatus = Array(
40 'USER_ID' => $userId,
41 'STATUS' => '',
42 'COLOR' => '',
43 'IDLE' => false,
44 'MOBILE_LAST_DATE' => false,
45 'DESKTOP_LAST_DATE' => false,
46 );
47 $needToUpdate = false;
48
49 $params = self::PrepareFields($params);
50 $res = IM\Model\StatusTable::getById($userId);
51 if ($status = $res->fetch())
52 {
53 $status['IDLE'] ??= false;
54 $status['MOBILE_LAST_DATE'] ??= false;
55 $status['DESKTOP_LAST_DATE'] ??= false;
56 //$status = CIMStatus::prepareLastDate($status);
57 $previousStatus = Array(
58 'USER_ID' => $status['USER_ID'],
59 'STATUS' => (string)$status['STATUS'],
60 'COLOR' => (string)$status['COLOR'],
61 'IDLE' => $status['IDLE'],
62 'MOBILE_LAST_DATE' => $status['MOBILE_LAST_DATE'],
63 'DESKTOP_LAST_DATE' => $status['DESKTOP_LAST_DATE'],
64 );
65
66 foreach ($params as $key => $value)
67 {
68 $oldValue = is_object($status[$key])? $status[$key]->toString(): $status[$key];
69 $newValue = is_object($value)? $value->toString(): $value;
70 if ($oldValue != $newValue)
71 {
72 $status[$key] = $value;
73 $needToUpdate = true;
74 }
75 }
76
77 if ($needToUpdate)
78 {
79 IM\Model\StatusTable::update($userId, $params);
80 }
81 }
82 else
83 {
84 $params['USER_ID'] = $userId;
85 $update = $params;
86 IM\Model\StatusTable::merge($params, $update);
87
88 $needToUpdate = true;
90 }
91
92 $cache = \Bitrix\Main\Data\Cache::createInstance();
93 $cache->cleanDir(self::CACHE_PATH.$userId.'/');
94 unset(self::$status[$userId]);
95
96 if ($needToUpdate && self::Enable())
97 {
98 $push = static::getPushParams($status, $userId);
99 //\CPullWatch::AddToStack(static::getPushTag($userId), $push);
100
101 if (isset($params['STATUS']))
102 {
104 }
105 }
106
107 $cache->CleanDir(self::CACHE_ONLINE_PATH);
108
109 $event = new \Bitrix\Main\Event("im", "onStatusSet", array(
110 'USER_ID' => $userId,
111 'STATUS' => $status['STATUS'],
112 'COLOR' => $status['COLOR']? $status['COLOR']: '',
113 'IDLE' => $status['IDLE'] instanceof \Bitrix\Main\Type\DateTime? $status['IDLE']: false,
114 'MOBILE_LAST_DATE' => $status['MOBILE_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime? $status['MOBILE_LAST_DATE']: false,
115 'DESKTOP_LAST_DATE' => $status['DESKTOP_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime? $status['DESKTOP_LAST_DATE']: false,
116 'PREVIOUS_VALUES' => $previousStatus
117 ));
118 $event->send();
119
120 return true;
121 }
122
123 private static function getPushParams(array $status, int $userId): array
124 {
125 return [
126 'module_id' => 'online',
127 'command' => 'userStatus',
128 'expiry' => 1,
129 'params' => [
130 'users' => [
131 $userId => [
132 'id' => $userId,
133 'status' => $status['STATUS'] ?? null,
134 'color' => ($status['COLOR'] ?? null)
135 ? \Bitrix\Im\Color::getColor($status['COLOR'])
137 ,
138 'idle' => $status['IDLE'] instanceof \Bitrix\Main\Type\DateTime
139 ? date('c', $status['IDLE']->getTimestamp())
140 : false
141 ,
142 'mobile_last_date' => ($status['MOBILE_LAST_DATE'] ?? null) instanceof \Bitrix\Main\Type\DateTime
143 ? date('c', $status['MOBILE_LAST_DATE']->getTimestamp())
144 : false
145 ,
146 'desktop_last_date' => $status['DESKTOP_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime
147 ? date('c', $status['DESKTOP_LAST_DATE']->getTimestamp())
148 : false
149 ,
150 'last_activity_date' => date('c', time())
151 ]
152 ]
153 ]
154 ];
155 }
156
157 public static function getPushTag(int $userId): string
158 {
159 return "IM_USER_ONLINE_{$userId}";
160 }
161
162 public static function SetIdle($userId, $result = true, $ago = 10)
163 {
164 $date = null;
165 $ago = intval($ago);
166 if ($result && $ago > 0)
167 {
168 $date = new Bitrix\Main\Type\DateTime();
169 $date->add('-'.$ago.' MINUTE');
170 }
171
172 CIMStatus::Set($userId, Array('IDLE' => $date));
173 }
174
175 public static function SetMobile($userId, $result = true)
176 {
177 $date = null;
178 if ($result)
179 {
180 $date = new Bitrix\Main\Type\DateTime();
181 }
182 CIMStatus::Set($userId, Array('MOBILE_LAST_DATE' => $date));
183 }
184
185 public static function SetColor($userId, $color)
186 {
187 CIMStatus::Set($userId, Array('COLOR' => $color));
188
189 if(defined("BX_COMP_MANAGED_CACHE"))
190 {
191 global $CACHE_MANAGER;
192 $CACHE_MANAGER->ClearByTag('IM_CONTACT_LIST');
193 $CACHE_MANAGER->ClearByTag('USER_NAME_'.$userId);
194 }
195 }
196
197 private static function PrepareToPush($params)
198 {
199 foreach($params as $key => $value)
200 {
201 if ($key == 'STATUS')
202 {
203 $params[$key] = in_array($value, self::$AVAILABLE_STATUSES)? $value: 'online';
204 }
205 else if (in_array($key, Array('IDLE', 'DESKTOP_LAST_DATE', 'MOBILE_LAST_DATE', 'EVENT_UNTIL_DATE')))
206 {
207 $params[$key] = is_object($value)? $value->getTimestamp(): 0;
208 }
209 else if ($key == 'COLOR')
210 {
211 $params[$key] = IM\Color::getColor($value);
212 if (!$params[$key])
213 {
214 unset($params[$key]);
215 }
216 }
217 else
218 {
220 }
221 }
222
223 return $params;
224 }
225
226 private static function PrepareFields($params)
227 {
228 $arValues = Array();
229
230 $arFields = IM\Model\StatusTable::getMap();
231 foreach($params as $key => $value)
232 {
233 if (!isset($arFields[$key]))
234 continue;
235
236 if ($key == 'STATUS')
237 {
238 $arValues[$key] = in_array($value, self::$AVAILABLE_STATUSES)? $value: 'online';
239 }
240 else if ($key == 'COLOR')
241 {
242 $colors = IM\Color::getSafeColors();
243 if (isset($colors[$value]))
244 {
246 }
247 }
248 else
249 {
251 }
252 }
253
254 return $arValues;
255 }
256
257 public static function GetList($params = Array())
258 {
259 if (!is_array($params))
260 $params = Array();
261
262 $userIds = Array();
263 if (isset($params['ID']) && is_array($params['ID']) && !empty($params['ID']))
264 {
265 foreach ($params['ID'] as $key => $value)
266 {
267 $userIds[] = intval($value);
268 }
269 }
270 else if (isset($params['ID']) && intval($params['ID']) > 0)
271 {
272 $userIds[] = intval($params['ID']);
273 }
274
275 if (isset($params['CLEAR_CACHE']) && $params['CLEAR_CACHE'] == 'Y')
276 {
277 $obCache = new CPHPCache();
278 $obCache->CleanDir(self::CACHE_ONLINE_PATH);
279 }
280
281 global $USER;
282 $userId = is_object($USER)? intval($USER->GetID()): 0;
283
284 $users = Array();
285 $loadFromDb = true;
286 $loadRecent = false;
287 if (empty($userIds))
288 {
289 $loadRecent = true;
290 if (!is_null(self::$CACHE_RECENT))
291 {
292 $loadFromDb = false;
293 $users = self::$CACHE_RECENT;
294 }
295 }
296 else if (!empty($userIds))
297 {
298 foreach($userIds as $id => $uid)
299 {
300 if (isset(self::$CACHE_USERS[$uid]))
301 {
302 unset($userIds[$id]);
303 $users[$uid] = self::$CACHE_USERS[$uid];
304 }
305 }
306 if (empty($userIds))
307 {
308 $loadFromDb = false;
309 }
310 }
311
312 if ($loadFromDb)
313 {
314 if ($loadRecent)
315 {
316 $orm = \Bitrix\Im\Model\RecentTable::getList(array(
317 'select' => Array(
318 'ID' => 'U.ID',
319 'EXTERNAL_AUTH_ID' => 'U.EXTERNAL_AUTH_ID',
320 'LAST_ACTIVITY_DATE' => 'U.LAST_ACTIVITY_DATE',
321 'PERSONAL_GENDER' => 'U.PERSONAL_GENDER',
322 'COLOR' => 'ST.COLOR',
323 'STATUS' => 'ST.STATUS',
324 'IDLE' => 'ST.IDLE',
325 'MOBILE_LAST_DATE' => 'ST.MOBILE_LAST_DATE',
326 'DESKTOP_LAST_DATE' => 'ST.DESKTOP_LAST_DATE',
327 ),
328 'runtime' => Array(
329 new \Bitrix\Main\Entity\ReferenceField(
330 'ST',
331 '\Bitrix\Im\Model\StatusTable',
332 array("=ref.USER_ID" => "this.ITEM_ID",),
333 array("join_type"=>"LEFT")
334 ),
335 new \Bitrix\Main\Entity\ReferenceField(
336 'U',
337 '\Bitrix\Main\UserTable',
338 array("=ref.ID" => "this.ITEM_ID",),
339 array("join_type"=>"LEFT")
340 )
341 ),
342 'filter' => Array(
343 '=USER_ID' => $userId,
344 "=ITEM_TYPE" => IM_MESSAGE_PRIVATE,
345 )
346 ));
347 }
348 else
349 {
351 'select' => Array(
352 'ID',
353 'EXTERNAL_AUTH_ID',
354 'LAST_ACTIVITY_DATE',
355 'PERSONAL_GENDER',
356 'COLOR' => 'ST.COLOR',
357 'STATUS' => 'ST.STATUS',
358 'IDLE' => 'ST.IDLE',
359 'MOBILE_LAST_DATE' => 'ST.MOBILE_LAST_DATE',
360 'DESKTOP_LAST_DATE' => 'ST.DESKTOP_LAST_DATE',
361 ),
362 'runtime' => Array(
363 new \Bitrix\Main\Entity\ReferenceField(
364 'ST',
365 '\Bitrix\Im\Model\StatusTable',
366 array(
367 "=ref.USER_ID" => "this.ID",
368 ),
369 array("join_type"=>"LEFT")
370 )
371 ),
372 'filter' => Array(
373 '=ID' => $userIds,
374 )
375 ));
376 }
377
378 while ($user = $orm->fetch())
379 {
380 $color = null;
381 if (isset($user['COLOR']) && $user['COLOR'] <> '')
382 {
383 $color = IM\Color::getColor($user['COLOR']);
384 }
385 if (!$color)
386 {
387 $color = \CIMContactList::GetUserColor($user["ID"], $user['PERSONAL_GENDER'] == 'M'? 'M': 'F');
388 }
389
390 $user['LAST_ACTIVITY_DATE'] = $user['LAST_ACTIVITY_DATE'] instanceof \Bitrix\Main\Type\DateTime? $user['LAST_ACTIVITY_DATE']: false;
391 $user = CIMStatus::prepareLastDate($user);
392
393 $users[$user["ID"]] = Array(
394 'id' => $user["ID"],
395 //'status' => in_array($user['STATUS'], self::$AVAILABLE_STATUSES)? $user['STATUS']: 'online',
396 'status' => 'online',
397 'color' => $color,
398 //'idle' => $user['IDLE']?: false,
399 'idle' => false,
400 'last_activity_date' => $user['LAST_ACTIVITY_DATE']?: false,
401 //'mobile_last_date' => $user['MOBILE_LAST_DATE']?: false,
402 'mobile_last_date' => false,
403 'absent' => \CIMContactList::formatAbsentResult($user["ID"]),
404 );
405
406 self::$CACHE_USERS[$user["ID"]] = $users[$user["ID"]];
407 }
408
409 if ($loadRecent)
410 {
411 self::$CACHE_RECENT = self::$CACHE_USERS;
412 }
413 }
414
415 return Array('users' => $users);
416 }
417
418 public static function GetOnline()
419 {
420 global $USER;
421 $userId = is_object($USER)? intval($USER->GetID()): 0;
422
423 $obCLCache = new CPHPCache;
424 $cache_id = 'im_user_online_v1';
425 $cache_dir = self::CACHE_ONLINE_PATH.$userId.'/';
426 if($obCLCache->InitCache(self::CACHE_ONLINE_TTL, $cache_id, $cache_dir))
427 {
428 $arOnline = $obCLCache->GetVars();
429 }
430 else
431 {
432 $arOnline = self::GetList();
433
434 if($obCLCache->StartDataCache())
435 {
436 $obCLCache->EndDataCache($arOnline);
437 }
438 }
439
440 return $arOnline;
441 }
442
443 public static function GetStatus($userId = null)
444 {
445 $userId = IM\Common::getUserId($userId);
446 if (!$userId)
447 {
448 return null;
449 }
450
451 $userStatus = null;
452 $cache = \Bitrix\Main\Data\Cache::createInstance();
453
454 if (isset(self::$status[$userId]))
455 {
456 return self::getStatusFromStaticCache($userId);
457 }
458
459 if($cache->initCache(self::CACHE_TTL, 'list_v2', self::CACHE_PATH.$userId.'/'))
460 {
461 $userStatus = $cache->getVars();
462 }
463 else
464 {
465 $res = IM\Model\StatusTable::getList(Array(
466 'select' => Array(
467 'STATUS',
468 'MOBILE_LAST_DATE',
469 'DESKTOP_LAST_DATE',
470 'IDLE',
471 'EXTERNAL_AUTH_ID' => 'USER.EXTERNAL_AUTH_ID'
472 ),
473 'runtime' => Array(
474 new \Bitrix\Main\Entity\ReferenceField(
475 'USER',
476 '\Bitrix\Main\UserTable',
477 array("=ref.ID" => "this.USER_ID",),
478 array("join_type"=>"LEFT")
479 )
480 ),
481 'filter' => Array('=USER_ID' => $userId),
482 ));
483 if ($status = $res->fetch())
484 {
485 $userStatus = $status;
486 $cache->startDataCache();
487 $cache->endDataCache($userStatus);
488 }
489 }
490
491 self::$status[$userId] = $userStatus ?? [];
492
493 if ($userStatus)
494 {
495 $userStatus = CIMStatus::prepareLastDate($userStatus);
496 }
497
498 return $userStatus;
499 }
500
501 protected static function getStatusFromStaticCache(int $userId): ?array
502 {
503 if (!empty(self::$status[$userId]))
504 {
505 return self::prepareLastDate(self::$status[$userId]);
506 }
507
508 return null;
509 }
510
511 public static function OnUserOnlineStatusGetCustomStatus($userId, $lastseen, $now, $mode)
512 {
513 $result = false;
514 $status = self::GetStatus($userId);
515 if (!$status)
516 {
517 return $result;
518 }
519
520 $result = [];
521 $externalUser = \Bitrix\Main\UserTable::getExternalUserTypes();
522 $externalUser[] = 'network';
523
524 if (in_array($status['EXTERNAL_AUTH_ID'], $externalUser))
525 {
526 $result['STATUS'] = 'online';
527 $result['STATUS_TEXT'] = GetMessage('IM_STATUS_EAID_'.mb_strtoupper($status['EXTERNAL_AUTH_ID']));
528 $result['LAST_SEEN_TEXT'] = '';
529
530 return $result;
531 }
532
534 $mobileLastDate = $status['MOBILE_LAST_DATE'];
535 if ($mobileLastDate)
536 {
537 if (
538 $now - $mobileLastDate->getTimestamp() < CUser::GetSecondsForLimitOnline()
539 && $lastseen - $mobileLastDate->getTimestamp() < 300
540 )
541 {
542 $result['STATUS'] = 'mobile';
543 $result['STATUS_TEXT'] = GetMessage('IM_STATUS_MOBILE');
544 $result['LAST_SEEN'] = $mobileLastDate->getTimestamp();
545 $result['LAST_SEEN_TEXT'] = CUser::FormatLastActivityDate($mobileLastDate->getTimestamp(), $now);
546 }
547 }
548
549 if ($mode == CUser::STATUS_OFFLINE)
550 {
551 return $result;
552 }
553
554 if ($result && $result['STATUS'] === 'mobile')
555 {
556 }
557 else if (in_array($status['STATUS'], Array('dnd', 'away', 'break', 'video')))
558 {
559 $result['STATUS'] = $status['STATUS'];
560 $result['STATUS_TEXT'] = GetMessage('IM_STATUS_'.mb_strtoupper($status['STATUS']));
561 }
562
564 $idleDate = $status['IDLE'];
565 if ($idleDate)
566 {
567 $result['STATUS'] = 'idle';
568 $result['STATUS_TEXT'] = GetMessage('IM_STATUS_IDLE');
569 $result['LAST_SEEN'] = $idleDate->getTimestamp();
570 $result['LAST_SEEN_TEXT'] = CUser::FormatLastActivityDate($idleDate, $now);
571 }
572
573 return $result;
574 }
575
576 public static function getDesktopStatus($dates)
577 {
578 $result = [
579 'ONLINE' => false,
580 'IDLE' => false,
581 ];
582
583 if (!($dates['DESKTOP_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime))
584 {
585 return $result;
586 }
587
588 $maxOnlineTime = 120;
589 if (\Bitrix\Main\Loader::includeModule('pull') && CPullOptions::GetNginxStatus())
590 {
591 $maxOnlineTime = CIMMessenger::GetSessionLifeTime();
592 }
593
594 if ($dates['DESKTOP_LAST_DATE']->getTimestamp()+$maxOnlineTime+60 > time())
595 {
596 $result['ONLINE'] = true;
597 }
598
599 if (!$result['ONLINE'])
600 {
601 return $result;
602 }
603
604 if (
605 $dates['IDLE'] instanceof \Bitrix\Main\Type\DateTime
606 && $dates['IDLE']->getTimestamp() > 0
607 )
608 {
609 $result['IDLE'] = true;
610 }
611
612 return $result;
613 }
614
615 public static function prepareLastDate($dates)
616 {
617 if (!($dates['MOBILE_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime))
618 {
619 $dates['MOBILE_LAST_DATE'] = false;
620 }
621
622 if (!($dates['DESKTOP_LAST_DATE'] instanceof \Bitrix\Main\Type\DateTime))
623 {
624 $dates['DESKTOP_LAST_DATE'] = false;
625 }
626
627 $status = self::getDesktopStatus($dates);
628 if (!$status['IDLE'])
629 {
630 $dates['IDLE'] = false;
631 }
632
633 return $dates;
634 }
635
636 public static function Enable()
637 {
638 return CModule::IncludeModule('pull') && CPullOptions::GetNginxStatus()? true: false;
639 }
640}
641?>
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getColorByNumber($number)
Определения color.php:144
static getList(array $parameters=array())
Определения datamanager.php:431
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
static FormatLastActivityDate($timestamp, $now=false)
Определения user.php:5271
Определения im_status.php:5
static SetColor($userId, $color)
Определения im_status.php:185
static getDesktopStatus($dates)
Определения im_status.php:576
static GetOnline()
Определения im_status.php:418
static GetList($params=Array())
Определения im_status.php:257
static $CACHE_RECENT
Определения im_status.php:8
const CACHE_PATH
Определения im_status.php:16
static GetStatus($userId=null)
Определения im_status.php:443
static SetIdle($userId, $result=true, $ago=10)
Определения im_status.php:162
static Set($userId, $params)
Определения im_status.php:21
const CACHE_ONLINE_TTL
Определения im_status.php:18
static array $status
Определения im_status.php:13
const CACHE_TTL
Определения im_status.php:15
static getPushTag(int $userId)
Определения im_status.php:157
static $CACHE_USERS
Определения im_status.php:7
static Enable()
Определения im_status.php:636
static $AVAILABLE_STATUSES
Определения im_status.php:6
static getStatusFromStaticCache(int $userId)
Определения im_status.php:501
static prepareLastDate($dates)
Определения im_status.php:615
const CACHE_ONLINE_PATH
Определения im_status.php:19
static SetMobile($userId, $result=true)
Определения im_status.php:175
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$arValues
Определения component_props.php:25
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$uid
Определения hot_keys_act.php:8
const IM_MESSAGE_PRIVATE
Определения include.php:22
global $USER
Определения csv_new_run.php:40
$status
Определения session.php:10
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$value
Определения Param.php:39
$event
Определения prolog_after.php:141
return false
Определения prolog_main_admin.php:185
if(empty($signedUserToken)) $key
Определения quickway.php:257
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799