Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messagehandler.php
1<?php
2namespace Bitrix\Im\Replica;
3
6
7if (Loader::includeModule('replica'))
8{
9 class MessageHandler extends \Bitrix\Replica\Client\BaseHandler
10 {
11 protected $tableName = "b_im_message";
12 protected $moduleId = "im";
13 protected $className = "\\Bitrix\\Im\\Model\\MessageTable";
14 protected $primary = array(
15 "ID" => "auto_increment",
16 );
17 protected $predicates = array(
18 "AUTHOR_ID" => "b_user.ID",
19 "CHAT_ID" => "b_im_chat.ID",
20 );
21 protected $translation = array(
22 "ID" => "b_im_message.ID",
23 "CHAT_ID" => "b_im_chat.ID",
24 "AUTHOR_ID" => "b_user.ID",
25 );
26 protected $fields = array(
27 "DATE_CREATE" => "datetime",
28 "MESSAGE" => "text",
29 "MESSAGE_OUT" => "text",
30 );
31
32 const LOADER_PLACEHOLDER = '[B][/B]';
33
40 public function beforeLogInsert(array $record)
41 {
42 if ($record["NOTIFY_TYPE"] <= 0)
43 {
44 return true;
45 }
46 else
47 {
48 return false;
49 }
50 }
51
61 public function beforeInsertTrigger(array &$newRecord)
62 {
63 if ($newRecord["CHAT_ID"] <= 0)
64 {
65 return array("ID" => 0);
66 }
67
68 $newRecord["MESSAGE"] = $this->fixMessage($newRecord["MESSAGE"]);
69 if ($newRecord["MESSAGE"] == "")
70 {
71 $newRecord["MESSAGE"] = self::LOADER_PLACEHOLDER;
72 }
73 return null;
74 }
75
84 public function beforeUpdateTrigger(array $oldRecord, array &$newRecord)
85 {
86 if (array_key_exists("MESSAGE", $newRecord))
87 {
88 $newRecord["MESSAGE"] = $this->fixMessage($newRecord["MESSAGE"]);
89 }
90 }
91
99 protected function fixMessage($message)
100 {
101 $fixed = preg_replace("/\\[CHAT=[0-9]+\\](.*?)\\[\\/CHAT\\]/", "\\1", $message);
102 if ($fixed == null)
103 {
104 return $message;
105 }
106 $fixed = preg_replace("/\\[USER=[0-9]+\\](.*?)\\[\\/USER\\]/", "\\1", $fixed);
107
108 if ($fixed == null)
109 {
110 return $message;
111 }
112
113 return $fixed;
114 }
115
123 public function afterInsertTrigger(array $newRecord)
124 {
125 $arParams = array();
126
127 $chatId = $newRecord['CHAT_ID'];
128 $arRel = \CIMChat::GetRelationById($chatId);
129
130 $arFields['MESSAGE_TYPE'] = '';
131 foreach ($arRel as $rel)
132 {
133 $arFields['MESSAGE_TYPE'] = $rel["MESSAGE_TYPE"];
134 break;
135 }
136 $arFields['PARAMS'] = Array();
137 $arFields['FILES'] = Array();
138
139 if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
140 {
141 foreach ($arRel as $rel)
142 {
143 if ($rel['USER_ID'] == $newRecord['AUTHOR_ID'])
144 $arFields['FROM_USER_ID'] = $rel['USER_ID'];
145 else
146 $arFields['TO_USER_ID'] = $rel['USER_ID'];
147 }
148
149 foreach ($arRel as $rel)
150 {
151 \CIMContactList::SetRecent(Array(
152 'ENTITY_ID' => $rel['USER_ID'] == $arFields['TO_USER_ID']? $arFields['FROM_USER_ID']: $arFields['TO_USER_ID'],
153 'MESSAGE_ID' => $newRecord['ID'],
154 'CHAT_TYPE' => IM_MESSAGE_PRIVATE,
155 'USER_ID' => $rel['USER_ID'],
156 'CHAT_ID' => $chatId,
157 'RELATION_ID' => $rel['ID']
158 ));
159 }
160
161 if (\CModule::IncludeModule('pull'))
162 {
163 $pullMessage = Array(
164 'module_id' => 'im',
165 'command' => 'message',
166 'params' => \CIMMessage::GetFormatMessage(Array(
167 'ID' => $newRecord['ID'],
168 'CHAT_ID' => $chatId,
169 'TO_USER_ID' => $arFields['TO_USER_ID'],
170 'FROM_USER_ID' => $arFields['FROM_USER_ID'],
171 'SYSTEM' => $newRecord['NOTIFY_EVENT'] == 'private_system'? 'Y': 'N',
172 'MESSAGE' => $newRecord['MESSAGE'],
173 'DATE_CREATE' => time(),
174 'PARAMS' => $arFields['PARAMS'],
175 'FILES' => $arFields['FILES'],
176 'NOTIFY' => true
177 )),
178 'extra' => \Bitrix\Im\Common::getPullExtra()
179 );
180 $relations = \Bitrix\Im\Chat::getRelation($chatId, Array(
181 'REAL_COUNTERS' => 'Y',
182 'USER_DATA' => 'Y',
183 ));
184 $pullMessage['params']['dialogId'] = $arFields['FROM_USER_ID'];
185 $pullMessage['params']['counter'] = $relations[$arFields['TO_USER_ID']]['COUNTER'];
186
187 $pullMessageTo = $pullMessage;
188
189 if (\CPullOptions::GetPushStatus())
190 {
191 if (\CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], 'im', 'message', \CIMSettings::CLIENT_PUSH))
192 {
193 $pushParams = $pullMessage;
194 $pushParams['params']['message']['text_push'] = $newRecord['MESSAGE'];
195 $pushParams = \CIMMessenger::PreparePushForPrivate($pushParams);
196 $pullMessageTo = array_merge($pullMessage, $pushParams);
197 }
198 }
199
200 \Bitrix\Pull\Event::add($arFields['TO_USER_ID'], $pullMessageTo);
201
202 \CPushManager::DeleteFromQueueBySubTag($arFields['FROM_USER_ID'], 'IM_MESS');
203 }
204 }
205 else if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
206 {
207 $chat = \Bitrix\Im\Model\ChatTable::getById($chatId);
208 $chatData = $chat->fetch();
209
210 foreach ($arRel as $relation)
211 {
212 if ($relation["EXTERNAL_AUTH_ID"] == \Bitrix\Im\Bot::EXTERNAL_AUTH_ID)
213 {
214 continue;
215 }
216 if ($chatData['ENTITY_TYPE'] == "LINES" && $relation["EXTERNAL_AUTH_ID"] == 'imconnector')
217 {
218 continue;
219 }
220
221 \CIMContactList::SetRecent(Array(
222 'ENTITY_ID' => $relation['CHAT_ID'],
223 'MESSAGE_ID' => $newRecord['ID'],
224 'CHAT_TYPE' => $relation['MESSAGE_TYPE'],
225 'USER_ID' => $relation['USER_ID'],
226 'CHAT_ID' => $relation['CHAT_ID'],
227 'RELATION_ID' => $relation['ID'],
228 ));
229 }
230
231 if (\CModule::IncludeModule('pull'))
232 {
233 $pullMessage = Array(
234 'module_id' => 'im',
235 'command' => 'messageChat',
236 'params' => \CIMMessage::GetFormatMessage(Array(
237 'ID' => $newRecord['ID'],
238 'CHAT_ID' => $chatId,
239 'TO_CHAT_ID' => $chatId,
240 'FROM_USER_ID' => $newRecord['AUTHOR_ID'],
241 'MESSAGE' => $newRecord['MESSAGE'],
242 'SYSTEM' => $newRecord['AUTHOR_ID'] > 0? 'N': 'Y',
243 'DATE_CREATE' => time(),
244 'PARAMS' => $arFields['PARAMS'],
245 'FILES' => $arFields['FILES'],
246 'NOTIFY' => true
247 )),
248 'extra' => \Bitrix\Im\Common::getPullExtra()
249 );
250
251 if ($chatData && \CPullOptions::GetPushStatus())
252 {
253 $pushParams = $pullMessage;
254 $pushParams['params']['message']['text_push'] = $newRecord['MESSAGE'];
255 $pushParams = \CIMMessenger::PreparePushForChat($pushParams);
256 $pullMessage = array_merge($pullMessage, $pushParams);
257 }
258
259 $pullUsers = Array();
260 $pullUsersSkip = Array();
261 foreach ($arRel as $rel)
262 {
263 if ($chatData['ENTITY_TYPE'] == "LINES" && $rel["EXTERNAL_AUTH_ID"] == 'imconnector')
264 {
265 }
266 if ($rel['USER_ID'] == $newRecord['AUTHOR_ID'])
267 {
268 $pullUsers[] = $rel['USER_ID'];
269 $pullUsersSkip[] = $rel['USER_ID'];
270 \CPushManager::DeleteFromQueueBySubTag($newRecord['AUTHOR_ID'], 'IM_MESS');
271 }
272 else
273 {
274 $pullUsers[] = $rel['USER_ID'];
275 if ($rel['NOTIFY_BLOCK'] == 'Y' || !\CIMSettings::GetNotifyAccess($rel['USER_ID'], 'im', ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN? 'openChat': 'chat'), \CIMSettings::CLIENT_PUSH))
276 {
277 $pullUsersSkip[] = $rel['USER_ID'];
278 }
279 }
280 }
281 $pullMessage['push']['skip_users'] = $pullUsersSkip;
282
283 \Bitrix\Pull\Event::add($pullUsers, $pullMessage);
284
285 if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
286 {
287 \CPullWatch::AddToStack('IM_PUBLIC_'.$chatId, $pullMessage);
288 }
289
290 /*
291 \CIMMessenger::SendMention(Array(
292 'CHAT_ID' => $chatId,
293 'CHAT_TITLE' => $chatData['TITLE'],
294 'CHAT_RELATION' => $arRel,
295 'CHAT_TYPE' => $chatData['TYPE'],
296 'MESSAGE' => $newRecord['MESSAGE'],
297 'FILES' => $arFields['FILES'],
298 'FROM_USER_ID' => $newRecord['AUTHOR_ID'],
299 ));
300 */
301
302 foreach(\GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent)
303 \ExecuteModuleEventEx($arEvent, array($newRecord['ID'], $newRecord));
304 }
305 }
306 }
307
316 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
317 {
318 if (!\Bitrix\Main\Loader::includeModule('pull'))
319 return;
320
321 if ($oldRecord["MESSAGE"] == self::LOADER_PLACEHOLDER && $newRecord["MESSAGE"] == "")
322 return;
323
324 $arFields = \CIMMessenger::GetById($newRecord['ID'], Array('WITH_FILES' => 'Y'));
325 if (!$arFields)
326 return;
327
328 $relations = \CIMChat::GetRelationById($arFields['CHAT_ID']);
329
330 $arPullMessage = Array(
331 'id' => $arFields['ID'],
332 'type' => $arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE? 'private': 'chat',
333 'text' => \Bitrix\Im\Text::parse($arFields['MESSAGE']),
334 'date' => \Bitrix\Main\Type\DateTime::createFromTimestamp($arFields['DATE_CREATE']),
335 );
336 if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
337 {
338 $arFields['FROM_USER_ID'] = $arFields['AUTHOR_ID'];
339 foreach ($relations as $rel)
340 {
341 if ($rel['USER_ID'] != $arFields['AUTHOR_ID'])
342 $arFields['TO_USER_ID'] = $rel['USER_ID'];
343 }
344
345 $arPullMessage['fromUserId'] = $arFields['FROM_USER_ID'];
346 $arPullMessage['toUserId'] = $arFields['TO_USER_ID'];
347 }
348 else
349 {
350 $arPullMessage['chatId'] = $arFields['CHAT_ID'];
351 $arPullMessage['senderId'] = $arFields['AUTHOR_ID'];
352
353 if ($arFields['CHAT_ENTITY_TYPE'] == 'LINES')
354 {
355 foreach ($relations as $rel)
356 {
357 if ($rel["EXTERNAL_AUTH_ID"] == 'imconnector')
358 {
359 unset($relations[$rel["USER_ID"]]);
360 }
361 }
362 }
363 }
364
365 \Bitrix\Pull\Event::add(array_keys($relations), $p=Array(
366 'module_id' => 'im',
367 'command' => $arFields['PARAMS']['IS_DELETED']==='Y'? 'messageDelete': 'messageUpdate',
368 'params' => $arPullMessage,
369 'extra' => \Bitrix\Im\Common::getPullExtra()
370 ));
371 foreach ($relations as $rel)
372 {
373 $obCache = new \CPHPCache();
374 $obCache->CleanDir('/bx/imc/recent'.\CIMMessenger::GetCachePath($rel['USER_ID']));
375 }
376 if ($newRecord['MESSAGE_TYPE'] == IM_MESSAGE_OPEN || $newRecord['MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
377 {
378 \CPullWatch::AddToStack('IM_PUBLIC_'.$arFields['CHAT_ID'], Array(
379 'module_id' => 'im',
380 'command' => $arFields['PARAMS']['IS_DELETED']==='Y'? 'messageDelete': 'messageUpdate',
381 'params' => $arPullMessage,
382 'extra' => \Bitrix\Im\Common::getPullExtra()
383 ));
384 }
385
386 $updateFlags = Array(
387 'ID' => $newRecord['ID'],
388 'TEXT' => $newRecord["MESSAGE"],
389 'URL_PREVIEW' => true,
390 'EDIT_FLAG' => true,
391 'USER_ID' => $arFields['AUTHOR_ID'],
392 'BY_EVENT' => false,
393 );
394
395 foreach(\GetModuleEvents("im", "OnAfterMessagesUpdate", true) as $arEvent)
396 \ExecuteModuleEventEx($arEvent, array(intval($newRecord['ID']), $arFields, $updateFlags));
397 }
398
406 public function beforeLogFormat(array &$record)
407 {
408 global $USER;
409
410 if (!$record['MESSAGE'])
411 {
412 $record['MESSAGE'] = Loc::getMessage('IM_REPLICA_FILE');
413 }
414
415 if (\Bitrix\Im\User::getInstance($record['AUTHOR_ID'])->isBot())
416 {
417 $record['MESSAGE'] = "[b]".\Bitrix\Im\User::getInstance($record['AUTHOR_ID'])->getFullName()."[/b] \n ".$record['MESSAGE'];
418 $record['AUTHOR_ID'] = 0;
419 }
420 }
421 }
422}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29