6if (Loader::includeModule(
'replica'))
8 class StatusHandler
extends \Bitrix\Replica\Client\BaseHandler
10 public $insertIgnore =
true;
12 protected $tableName =
"b_im_status";
13 protected $moduleId =
"im";
14 protected $className =
"\\Bitrix\\Im\\Model\\StatusTable";
15 protected $primary = array(
16 "USER_ID" =>
"integer",
18 protected $predicates = array(
19 "USER_ID" =>
"b_im_status.USER_ID",
21 protected $translation = array(
22 "USER_ID" =>
"b_im_status.USER_ID",
24 protected $fields = array(
26 "DESKTOP_LAST_DATE" =>
"datetime",
27 "MOBILE_LAST_DATE" =>
"datetime",
28 "EVENT_UNTIL_DATE" =>
"datetime",
38 public function afterInsertTrigger(array $newRecord)
40 if ($newRecord[
"USER_ID"] && \CIMStatus::Enable())
42 \CPullStack::AddShared(Array(
43 'module_id' =>
'online',
44 'command' =>
'user_status',
47 "USER_ID" => $newRecord[
"USER_ID"],
48 "STATUS" => $newRecord[
"STATUS"],
62 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
64 if ($oldRecord[
"STATUS"] !== $newRecord[
"STATUS"])
66 if (\CIMStatus::Enable())
68 \CPullStack::AddShared(Array(
69 'module_id' =>
'online',
70 'command' =>
'user_status',
73 "USER_ID" => $newRecord[
"USER_ID"],
74 "STATUS" => $newRecord[
"STATUS"],
91 public function onUserSetLastActivityDate(\
Bitrix\Main\Event $event)
93 $users = $event->getParameter(0);
94 foreach ($users as $userId)
96 $cache = \Bitrix\Main\Data\Cache::createInstance();
97 if ($cache->startDataCache(60, $userId,
'/im/status'))
99 $mapper = \Bitrix\Replica\Mapper::getInstance();
100 $map = $mapper->getByPrimaryValue(
"b_im_status.USER_ID",
false, $userId);
103 $guid = \Bitrix\Replica\Client\User::getLocalUserGuid($userId).
'@'.getNameByDomain();
104 if ($guid && $map[$guid])
107 "operation" =>
"im_status_update",
109 "nodes" => $map[$guid],
113 \Bitrix\Replica\Log\Client::getInstance()->write($map[$guid], $event);
116 $cache->endDataCache(
true);
131 public function handleStatusUpdateOperation($event, $nodeFrom, $nodeTo)
161 public function onStartUserReplication(\
Bitrix\Main\Event $event)
163 $parameters = $event->getParameters();
166 $domain = $parameters[2];
168 $domainId = getNameByDomain($domain);
174 $mapper = \Bitrix\Replica\Mapper::getInstance();
175 $map = $mapper->getByPrimaryValue(
"b_user.ID",
false, $userId);
183 "operation" =>
"im_status_bind",
184 "guid" => $guid.
'@'.$domainId,
185 "nodes" => array($domainId),
189 \Bitrix\Replica\Log\Client::getInstance()->write(array($domainId), $event);
190 \Bitrix\Replica\Mapper::getInstance()->add(
"b_im_status.USER_ID", $userId, $domainId, $event[
"guid"]);
204 public function handleStatusBindOperation($event, $nodeFrom, $nodeTo)
206 if (!isset($event[
"guid"]))
211 list ($userGuid,) = explode(
'@', $event[
"guid"]);
217 $userId = \Bitrix\Replica\Client\User::getLocalUserId($userGuid);
223 $mapper = \Bitrix\Replica\Mapper::getInstance();
224 $mapper->add(
"b_im_status.USER_ID", $userId, $nodeFrom, $event[
"guid"]);
226 $res = \Bitrix\Im\Model\StatusTable::getById($userId);
230 \Bitrix\Replica\Db\Operation::writeInsert(
233 array(
"USER_ID" => $userId)
247 public function OnAfterRecentDelete(\
Bitrix\Main\Event $event)
249 $userId = $event->getParameter(
'user_id');
255 $mapper = \Bitrix\Replica\Mapper::getInstance();
256 $map = $mapper->getByPrimaryValue(
"b_im_status.USER_ID",
false, $userId);
263 list(, $targetNode) = explode(
"@", $guid, 2);
265 $domainId = getNameByDomain();
267 "operation" =>
"im_status_unbind",
269 "nodes" => array($domainId),
273 \Bitrix\Replica\Log\Client::getInstance()->write(array($targetNode), $event);
287 public function handleStatusUnbindOperation($event, $nodeFrom, $nodeTo)
289 if (!isset($event[
"guid"]))
294 list ($userGuid,) = explode(
'@', $event[
"guid"]);
295 $userId = \Bitrix\Replica\Client\User::getLocalUserId($userGuid);
301 $mapper = \Bitrix\Replica\Mapper::getInstance();
302 $mapper->deleteByGuid(
"b_im_status.USER_ID", $event[
"guid"], $nodeFrom);
314 public function OnAfterRecentAdd(\
Bitrix\Main\Event $event)
316 $userId = $event->getParameter(
'user_id');
322 $userGuid = \Bitrix\Replica\Client\User::getRemoteUserGuid($userId);
328 $mapper = \Bitrix\Replica\Mapper::getInstance();
329 $map = $mapper->getByPrimaryValue(
"b_user.ID",
false, $userId);
336 $targetNode = current($map[$guid]);
338 $domainId = getNameByDomain();
340 "operation" =>
"im_status_rebind",
341 "guid" => $userGuid.
"@".$targetNode,
342 "nodes" => array($domainId),
346 \Bitrix\Replica\Log\Client::getInstance()->write(array($targetNode), $event);
360 public function handleStatusRebindOperation($event, $nodeFrom, $nodeTo)
362 if (!isset($event[
"guid"]))
367 list ($userGuid,) = explode(
'@', $event[
"guid"]);
368 $userId = \Bitrix\Replica\Client\User::getLocalUserId($userGuid);
374 \Bitrix\Replica\Mapper::getInstance()->add(
"b_im_status.USER_ID", $userId, $nodeFrom, $event[
"guid"]);
376 $res = \Bitrix\Im\Model\StatusTable::getById($userId);
380 \Bitrix\Replica\Db\Operation::writeUpdate(
383 array(
"USER_ID" => $userId)