Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
statushandler.php
1<?php
2namespace Bitrix\Im\Replica;
3
5
6if (Loader::includeModule('replica'))
7{
8 class StatusHandler extends \Bitrix\Replica\Client\BaseHandler
9 {
10 public $insertIgnore = true;
11
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",
17 );
18 protected $predicates = array(
19 "USER_ID" => "b_im_status.USER_ID",
20 );
21 protected $translation = array(
22 "USER_ID" => "b_im_status.USER_ID",
23 );
24 protected $fields = array(
25 "IDLE" => "datetime",
26 "DESKTOP_LAST_DATE" => "datetime",
27 "MOBILE_LAST_DATE" => "datetime",
28 "EVENT_UNTIL_DATE" => "datetime",
29 );
30
38 public function afterInsertTrigger(array $newRecord)
39 {
40 if ($newRecord["USER_ID"] && \CIMStatus::Enable())
41 {
42 \CPullStack::AddShared(Array(
43 'module_id' => 'online',
44 'command' => 'user_status',
45 'expiry' => 120,
46 'params' => array(
47 "USER_ID" => $newRecord["USER_ID"],
48 "STATUS" => $newRecord["STATUS"],
49 ),
50 ));
51 }
52 }
53
62 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
63 {
64 if ($oldRecord["STATUS"] !== $newRecord["STATUS"])
65 {
66 if (\CIMStatus::Enable())
67 {
68 \CPullStack::AddShared(Array(
69 'module_id' => 'online',
70 'command' => 'user_status',
71 'expiry' => 120,
72 'params' => array(
73 "USER_ID" => $newRecord["USER_ID"],
74 "STATUS" => $newRecord["STATUS"],
75 ),
76 ));
77 }
78 }
79 }
80
91 public function onUserSetLastActivityDate(\Bitrix\Main\Event $event)
92 {
93 $users = $event->getParameter(0);
94 foreach ($users as $userId)
95 {
96 $cache = \Bitrix\Main\Data\Cache::createInstance();
97 if ($cache->startDataCache(60, $userId, '/im/status'))
98 {
99 $mapper = \Bitrix\Replica\Mapper::getInstance();
100 $map = $mapper->getByPrimaryValue("b_im_status.USER_ID", false, $userId);
101 if ($map)
102 {
103 $guid = \Bitrix\Replica\Client\User::getLocalUserGuid($userId).'@'.getNameByDomain();
104 if ($guid && $map[$guid])
105 {
106 $event = array(
107 "operation" => "im_status_update",
108 "guid" => $guid,
109 "nodes" => $map[$guid],
110 "ts" => time(),
111 "ip" => \Bitrix\Main\Application::getInstance()->getContext()->getServer()->get('REMOTE_ADDR'),
112 );
113 \Bitrix\Replica\Log\Client::getInstance()->write($map[$guid], $event);
114 }
115 }
116 $cache->endDataCache(true);
117 }
118 }
119 }
120
131 public function handleStatusUpdateOperation($event, $nodeFrom, $nodeTo)
132 {
133 /*
134 global $USER;
135 if (!isset($event["guid"]))
136 {
137 return;
138 }
139
140 $mapper = \Bitrix\Replica\Mapper::getInstance();
141 $userId = $mapper->getByGuid("b_im_status.USER_ID", $event["guid"], $nodeFrom);
142 if (!$userId)
143 {
144 return;
145 }
146
147 $USER->setLastActivityDate($userId);
148 */
149 }
150
161 public function onStartUserReplication(\Bitrix\Main\Event $event)
162 {
163 $parameters = $event->getParameters();
164
165 $userId = $parameters[0];
166 $domain = $parameters[2];
167
168 $domainId = getNameByDomain($domain);
169 if (!$domainId)
170 {
171 return;
172 }
173
174 $mapper = \Bitrix\Replica\Mapper::getInstance();
175 $map = $mapper->getByPrimaryValue("b_user.ID", false, $userId);
176 if (!$map)
177 {
178 return;
179 }
180
181 $guid = key($map);
182 $event = array(
183 "operation" => "im_status_bind",
184 "guid" => $guid.'@'.$domainId,
185 "nodes" => array($domainId),
186 "ts" => time(),
187 "ip" => \Bitrix\Main\Application::getInstance()->getContext()->getServer()->get('REMOTE_ADDR'),
188 );
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"]);
191 }
192
204 public function handleStatusBindOperation($event, $nodeFrom, $nodeTo)
205 {
206 if (!isset($event["guid"]))
207 {
208 return;
209 }
210
211 list ($userGuid,) = explode('@', $event["guid"]);
212 if (!$userGuid)
213 {
214 return;
215 }
216
217 $userId = \Bitrix\Replica\Client\User::getLocalUserId($userGuid);
218 if (!$userId)
219 {
220 return;
221 }
222
223 $mapper = \Bitrix\Replica\Mapper::getInstance();
224 $mapper->add("b_im_status.USER_ID", $userId, $nodeFrom, $event["guid"]);
225
226 $res = \Bitrix\Im\Model\StatusTable::getById($userId);
227 if ($res->fetch())
228 {
229 //Insert operation
230 \Bitrix\Replica\Db\Operation::writeInsert(
231 "b_im_status",
232 $this->getPrimary(),
233 array("USER_ID" => $userId)
234 );
235 }
236 }
237
247 public function OnAfterRecentDelete(\Bitrix\Main\Event $event)
248 {
249 $userId = $event->getParameter('user_id');
250 if (!$userId)
251 {
252 return;
253 }
254
255 $mapper = \Bitrix\Replica\Mapper::getInstance();
256 $map = $mapper->getByPrimaryValue("b_im_status.USER_ID", false, $userId);
257 if (!$map)
258 {
259 return;
260 }
261
262 $guid = key($map);
263 list(, $targetNode) = explode("@", $guid, 2);
264
265 $domainId = getNameByDomain();
266 $event = array(
267 "operation" => "im_status_unbind",
268 "guid" => $guid,
269 "nodes" => array($domainId),
270 "ts" => time(),
271 "ip" => \Bitrix\Main\Application::getInstance()->getContext()->getServer()->get('REMOTE_ADDR'),
272 );
273 \Bitrix\Replica\Log\Client::getInstance()->write(array($targetNode), $event);
274
275 }
276
287 public function handleStatusUnbindOperation($event, $nodeFrom, $nodeTo)
288 {
289 if (!isset($event["guid"]))
290 {
291 return;
292 }
293
294 list ($userGuid,) = explode('@', $event["guid"]);
295 $userId = \Bitrix\Replica\Client\User::getLocalUserId($userGuid);
296 if (!$userId > 0)
297 {
298 return;
299 }
300
301 $mapper = \Bitrix\Replica\Mapper::getInstance();
302 $mapper->deleteByGuid("b_im_status.USER_ID", $event["guid"], $nodeFrom);
303 }
304
314 public function OnAfterRecentAdd(\Bitrix\Main\Event $event)
315 {
316 $userId = $event->getParameter('user_id');
317 if (!$userId)
318 {
319 return;
320 }
321
322 $userGuid = \Bitrix\Replica\Client\User::getRemoteUserGuid($userId);
323 if (!$userGuid)
324 {
325 return;
326 }
327
328 $mapper = \Bitrix\Replica\Mapper::getInstance();
329 $map = $mapper->getByPrimaryValue("b_user.ID", false, $userId);
330 if (!$map)
331 {
332 return;
333 }
334
335 $guid = key($map);
336 $targetNode = current($map[$guid]);
337
338 $domainId = getNameByDomain();
339 $event = array(
340 "operation" => "im_status_rebind",
341 "guid" => $userGuid."@".$targetNode,
342 "nodes" => array($domainId),
343 "ts" => time(),
344 "ip" => \Bitrix\Main\Application::getInstance()->getContext()->getServer()->get('REMOTE_ADDR'),
345 );
346 \Bitrix\Replica\Log\Client::getInstance()->write(array($targetNode), $event);
347 }
348
360 public function handleStatusRebindOperation($event, $nodeFrom, $nodeTo)
361 {
362 if (!isset($event["guid"]))
363 {
364 return;
365 }
366
367 list ($userGuid,) = explode('@', $event["guid"]);
368 $userId = \Bitrix\Replica\Client\User::getLocalUserId($userGuid);
369 if (!$userId)
370 {
371 return;
372 }
373
374 \Bitrix\Replica\Mapper::getInstance()->add("b_im_status.USER_ID", $userId, $nodeFrom, $event["guid"]);
375
376 $res = \Bitrix\Im\Model\StatusTable::getById($userId);
377 if ($res->fetch())
378 {
379 //Update operation
380 \Bitrix\Replica\Db\Operation::writeUpdate(
381 "b_im_status",
382 $this->getPrimary(),
383 array("USER_ID" => $userId)
384 );
385 }
386 }
387 }
388}