7if (Loader::includeModule(
'replica'))
9 class MessageHandler
extends \Bitrix\Replica\Client\BaseHandler
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",
17 protected $predicates = array(
18 "AUTHOR_ID" =>
"b_user.ID",
19 "CHAT_ID" =>
"b_im_chat.ID",
21 protected $translation = array(
22 "ID" =>
"b_im_message.ID",
23 "CHAT_ID" =>
"b_im_chat.ID",
24 "AUTHOR_ID" =>
"b_user.ID",
26 protected $fields = array(
27 "DATE_CREATE" =>
"datetime",
29 "MESSAGE_OUT" =>
"text",
32 const LOADER_PLACEHOLDER =
'[B][/B]';
40 public function beforeLogInsert(array $record)
42 if ($record[
"NOTIFY_TYPE"] <= 0)
61 public function beforeInsertTrigger(array &$newRecord)
63 if ($newRecord[
"CHAT_ID"] <= 0)
65 return array(
"ID" => 0);
68 $newRecord[
"MESSAGE"] = $this->fixMessage($newRecord[
"MESSAGE"]);
69 if ($newRecord[
"MESSAGE"] ==
"")
71 $newRecord[
"MESSAGE"] = self::LOADER_PLACEHOLDER;
84 public function beforeUpdateTrigger(array $oldRecord, array &$newRecord)
86 if (array_key_exists(
"MESSAGE", $newRecord))
88 $newRecord[
"MESSAGE"] = $this->fixMessage($newRecord[
"MESSAGE"]);
99 protected function fixMessage($message)
101 $fixed = preg_replace(
"/\\[CHAT=[0-9]+\\](.*?)\\[\\/CHAT\\]/",
"\\1", $message);
106 $fixed = preg_replace(
"/\\[USER=[0-9]+\\](.*?)\\[\\/USER\\]/",
"\\1", $fixed);
123 public function afterInsertTrigger(array $newRecord)
127 $chatId = $newRecord[
'CHAT_ID'];
128 $arRel = \CIMChat::GetRelationById($chatId);
130 $arFields[
'MESSAGE_TYPE'] =
'';
131 foreach ($arRel as $rel)
133 $arFields[
'MESSAGE_TYPE'] = $rel[
"MESSAGE_TYPE"];
136 $arFields[
'PARAMS'] = Array();
137 $arFields[
'FILES'] = Array();
139 if ($arFields[
'MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
141 foreach ($arRel as $rel)
143 if ($rel[
'USER_ID'] == $newRecord[
'AUTHOR_ID'])
144 $arFields[
'FROM_USER_ID'] = $rel[
'USER_ID'];
146 $arFields[
'TO_USER_ID'] = $rel[
'USER_ID'];
149 foreach ($arRel as $rel)
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']
161 if (\CModule::IncludeModule(
'pull'))
163 $pullMessage = Array(
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'],
178 'extra' => \
Bitrix\Im\Common::getPullExtra()
180 $relations = \Bitrix\Im\Chat::getRelation($chatId, Array(
181 'REAL_COUNTERS' =>
'Y',
184 $pullMessage[
'params'][
'dialogId'] = $arFields[
'FROM_USER_ID'];
185 $pullMessage[
'params'][
'counter'] = $relations[$arFields[
'TO_USER_ID']][
'COUNTER'];
187 $pullMessageTo = $pullMessage;
189 if (\CPullOptions::GetPushStatus())
191 if (\CIMSettings::GetNotifyAccess($arFields[
"TO_USER_ID"],
'im',
'message', \CIMSettings::CLIENT_PUSH))
193 $pushParams = $pullMessage;
194 $pushParams[
'params'][
'message'][
'text_push'] = $newRecord[
'MESSAGE'];
195 $pushParams = \CIMMessenger::PreparePushForPrivate($pushParams);
196 $pullMessageTo = array_merge($pullMessage, $pushParams);
200 \Bitrix\Pull\Event::add($arFields[
'TO_USER_ID'], $pullMessageTo);
202 \CPushManager::DeleteFromQueueBySubTag($arFields[
'FROM_USER_ID'],
'IM_MESS');
205 else if ($arFields[
'MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields[
'MESSAGE_TYPE'] == IM_MESSAGE_OPEN || $arFields[
'MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
207 $chat = \Bitrix\Im\Model\ChatTable::getById($chatId);
208 $chatData = $chat->fetch();
210 foreach ($arRel as $relation)
212 if ($relation[
"EXTERNAL_AUTH_ID"] == \
Bitrix\Im\Bot::EXTERNAL_AUTH_ID)
216 if ($chatData[
'ENTITY_TYPE'] ==
"LINES" && $relation[
"EXTERNAL_AUTH_ID"] ==
'imconnector')
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'],
231 if (\CModule::IncludeModule(
'pull'))
233 $pullMessage = Array(
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'],
248 'extra' => \
Bitrix\Im\Common::getPullExtra()
251 if ($chatData && \CPullOptions::GetPushStatus())
253 $pushParams = $pullMessage;
254 $pushParams[
'params'][
'message'][
'text_push'] = $newRecord[
'MESSAGE'];
255 $pushParams = \CIMMessenger::PreparePushForChat($pushParams);
256 $pullMessage = array_merge($pullMessage, $pushParams);
259 $pullUsers = Array();
260 $pullUsersSkip = Array();
261 foreach ($arRel as $rel)
263 if ($chatData[
'ENTITY_TYPE'] ==
"LINES" && $rel[
"EXTERNAL_AUTH_ID"] ==
'imconnector')
266 if ($rel[
'USER_ID'] == $newRecord[
'AUTHOR_ID'])
268 $pullUsers[] = $rel[
'USER_ID'];
269 $pullUsersSkip[] = $rel[
'USER_ID'];
270 \CPushManager::DeleteFromQueueBySubTag($newRecord[
'AUTHOR_ID'],
'IM_MESS');
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))
277 $pullUsersSkip[] = $rel[
'USER_ID'];
281 $pullMessage[
'push'][
'skip_users'] = $pullUsersSkip;
283 \Bitrix\Pull\Event::add($pullUsers, $pullMessage);
285 if ($arFields[
'MESSAGE_TYPE'] == IM_MESSAGE_OPEN || $arFields[
'MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
287 \CPullWatch::AddToStack(
'IM_PUBLIC_'.$chatId, $pullMessage);
302 foreach(\GetModuleEvents(
"im",
"OnAfterMessagesAdd",
true) as $arEvent)
303 \ExecuteModuleEventEx($arEvent, array($newRecord[
'ID'], $newRecord));
316 public function afterUpdateTrigger(array $oldRecord, array $newRecord)
318 if (!\
Bitrix\Main\Loader::includeModule(
'pull'))
321 if ($oldRecord[
"MESSAGE"] == self::LOADER_PLACEHOLDER && $newRecord[
"MESSAGE"] ==
"")
324 $arFields = \CIMMessenger::GetById($newRecord[
'ID'], Array(
'WITH_FILES' =>
'Y'));
328 $relations = \CIMChat::GetRelationById($arFields[
'CHAT_ID']);
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']),
336 if ($arFields[
'MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
338 $arFields[
'FROM_USER_ID'] = $arFields[
'AUTHOR_ID'];
339 foreach ($relations as $rel)
341 if ($rel[
'USER_ID'] != $arFields[
'AUTHOR_ID'])
342 $arFields[
'TO_USER_ID'] = $rel[
'USER_ID'];
345 $arPullMessage[
'fromUserId'] = $arFields[
'FROM_USER_ID'];
346 $arPullMessage[
'toUserId'] = $arFields[
'TO_USER_ID'];
350 $arPullMessage[
'chatId'] = $arFields[
'CHAT_ID'];
351 $arPullMessage[
'senderId'] = $arFields[
'AUTHOR_ID'];
353 if ($arFields[
'CHAT_ENTITY_TYPE'] ==
'LINES')
355 foreach ($relations as $rel)
357 if ($rel[
"EXTERNAL_AUTH_ID"] ==
'imconnector')
359 unset($relations[$rel[
"USER_ID"]]);
365 \Bitrix\Pull\Event::add(array_keys($relations), $p=Array(
367 'command' => $arFields[
'PARAMS'][
'IS_DELETED']===
'Y'?
'messageDelete':
'messageUpdate',
368 'params' => $arPullMessage,
369 'extra' => \
Bitrix\Im\Common::getPullExtra()
371 foreach ($relations as $rel)
373 $obCache = new \CPHPCache();
374 $obCache->CleanDir(
'/bx/imc/recent'.\CIMMessenger::GetCachePath($rel[
'USER_ID']));
376 if ($newRecord[
'MESSAGE_TYPE'] == IM_MESSAGE_OPEN || $newRecord[
'MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
378 \CPullWatch::AddToStack(
'IM_PUBLIC_'.$arFields[
'CHAT_ID'], Array(
380 'command' => $arFields[
'PARAMS'][
'IS_DELETED']===
'Y'?
'messageDelete':
'messageUpdate',
381 'params' => $arPullMessage,
382 'extra' => \
Bitrix\Im\Common::getPullExtra()
386 $updateFlags = Array(
387 'ID' => $newRecord[
'ID'],
388 'TEXT' => $newRecord[
"MESSAGE"],
389 'URL_PREVIEW' =>
true,
391 'USER_ID' => $arFields[
'AUTHOR_ID'],
395 foreach(\GetModuleEvents(
"im",
"OnAfterMessagesUpdate",
true) as $arEvent)
396 \ExecuteModuleEventEx($arEvent, array(intval($newRecord[
'ID']), $arFields, $updateFlags));
406 public function beforeLogFormat(array &$record)
410 if (!$record[
'MESSAGE'])
415 if (\
Bitrix\Im\User::getInstance($record[
'AUTHOR_ID'])->isBot())
417 $record[
'MESSAGE'] =
"[b]".\Bitrix\Im\User::getInstance($record[
'AUTHOR_ID'])->getFullName().
"[/b] \n ".$record[
'MESSAGE'];
418 $record[
'AUTHOR_ID'] = 0;
static getMessage($code, $replace=null, $language=null)