Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
startwritinghandler.php
1<?php
2namespace Bitrix\Im\Replica;
3
5
6if (Loader::includeModule('replica'))
7{
8 class StartWritingHandler extends \Bitrix\Replica\Client\BaseHandler
9 {
10 protected $moduleId = "im";
11
12 public function initDataManagerEvents()
13 {
14 \Bitrix\Main\EventManager::getInstance()->addEventHandler(
15 "im",
16 "OnStartWriting",
17 array($this, "OnStartWriting")
18 );
19 \Bitrix\Main\EventManager::getInstance()->addEventHandler(
20 "replica",
21 "OnExecuteStartWriting",
22 array($this, "OnExecuteStartWriting")
23 );
24 }
25
26 function onStartWriting($params)
27 {
28 $userId = intval($params['USER_ID']);
29 if ($userId <= 0)
30 {
31 return false;
32 }
33
34 if (\Bitrix\Im\User::getInstance($userId)->isBot())
35 {
36 return true;
37 }
38
39 $dialogId = $params['DIALOG_ID'];
40
41 $operation = new \Bitrix\Replica\Db\Execute();
42 if (mb_substr($dialogId, 0, 4) === "chat")
43 {
44 $chatId = intval(mb_substr($dialogId, 4));
45 $operation->writeToLog(
46 "StartWriting",
47 array(
48 array(
49 "relation" => "b_user.ID",
50 "value" => $userId,
51 ),
52 array(
53 "value" => "chat",
54 ),
55 array(
56 "relation" => "b_im_chat.ID",
57 "value" => $chatId,
58 ),
59 )
60 );
61 }
62 else
63 {
64 $dialogId = intval($dialogId);
65 $operation->writeToLog(
66 "StartWriting",
67 array(
68 array(
69 "relation" => "b_user.ID",
70 "value" => $userId,
71 ),
72 array(
73 "value" => "",
74 ),
75 array(
76 "relation" => "b_user.ID",
77 "value" => $dialogId,
78 ),
79 )
80 );
81 }
82
83 return true;
84 }
85
86 function onExecuteStartWriting(\Bitrix\Main\Event $event)
87 {
88 $parameters = $event->getParameters();
89 $userId = intval($parameters[0]);
90 $dialogId = $parameters[1].$parameters[2];
91
92 if ($userId > 0)
93 {
94 if (!\Bitrix\Main\Loader::includeModule('pull'))
95 return;
96
97 $userName = \Bitrix\Im\User::getInstance($userId)->getFullName();
98
99
100 if (mb_substr($dialogId, 0, 4) == 'chat')
101 {
102 $chatId = mb_substr($dialogId, 4);
103 $arRelation = \CIMChat::GetRelationById($chatId);
104 unset($arRelation[$userId]);
105
106 $chat = \Bitrix\Im\Model\ChatTable::getById($chatId);
107 $chatData = $chat->fetch();
108
109 $pullMessage = Array(
110 'module_id' => 'im',
111 'command' => 'startWriting',
112 'expiry' => 60,
113 'params' => Array(
114 'dialogId' => $dialogId,
115 'userId' => $userId,
116 'userName' => $userName
117 ),
118 'extra' => \Bitrix\Im\Common::getPullExtra()
119 );
120 if ($chatData['ENTITY_TYPE'] == 'LINES')
121 {
122 foreach ($arRelation as $rel)
123 {
124 if ($rel["EXTERNAL_AUTH_ID"] == 'imconnector')
125 {
126 unset($arRelation[$rel["USER_ID"]]);
127 }
128 }
129 }
130 \Bitrix\Pull\Event::add(array_keys($arRelation), $pullMessage);
131
132 $orm = \Bitrix\Im\Model\ChatTable::getById($chatId);
133 $chat = $orm->fetch();
134 if ($chat['TYPE'] == IM_MESSAGE_OPEN || $chat['TYPE'] == IM_MESSAGE_OPEN_LINE)
135 {
136 \CPullWatch::AddToStack('IM_PUBLIC_'.$chatId, $pullMessage);
137 }
138 }
139 else if (intval($dialogId) > 0)
140 {
141 \Bitrix\Pull\Event::add($dialogId, Array(
142 'module_id' => 'im',
143 'command' => 'startWriting',
144 'expiry' => 60,
145 'params' => Array(
146 'dialogId' => $userId,
147 'userId' => $userId,
148 'userName' => $userName
149 ),
150 'extra' => \Bitrix\Im\Common::getPullExtra()
151 ));
152 }
153 }
154 }
155 }
156}