6if (Loader::includeModule(
'replica'))
8 class MessageParamHandler
extends \Bitrix\Replica\Client\BaseHandler
10 protected $tableName =
"b_im_message_param";
11 protected $moduleId =
"im";
12 protected $className =
"\\Bitrix\\Im\\Model\\MessageParamTable";
13 protected $primary = array(
14 "ID" =>
"auto_increment",
16 protected $predicates = array(
17 "MESSAGE_ID" =>
"b_im_message.ID",
22 $this->translation = array(
23 "MESSAGE_ID" =>
"b_im_message.ID",
24 "PARAM_VALUE" => array($this,
"paramValueTranslation"),
35 public function beforeLogInsert(array $record)
37 return $record[
"PARAM_NAME"] ===
"KEYBOARD"?
false:
true;
49 public function beforeInsertTrigger(array &$newRecord)
51 if ($newRecord[
"MESSAGE_ID"] <= 0)
53 return array(
"ID" => 0);
65 public static function paramValueTranslation($record)
67 if ($record[
"PARAM_NAME"] ===
"LIKE" && $record[
"PARAM_VALUE"])
71 elseif ($record[
"PARAM_NAME"] ===
"URL_ID" && $record[
"PARAM_VALUE"])
73 return "b_urlpreview_metadata.ID";
85 public function beforeLogFormat(array &$record)
88 if ($record[
"PARAM_NAME"] !==
"FILE_ID" || $record[
"PARAM_VALUE"] <= 0)
93 if (!\
Bitrix\Main\Loader::includeModule(
'disk'))
95 AddMessage2Log(
'MessageParamHandler::beforeLogFormat: failed to load disk module.');
99 if (!is_object($USER) || $USER->GetID() < 0)
101 AddMessage2Log(
'MessageParamHandler::beforeLogFormat: no user provided.');
106 $fileId = $record[
"PARAM_VALUE"];
108 $file = \Bitrix\Disk\File::loadById($fileId);
111 AddMessage2Log(
'MessageParamHandler::beforeLogFormat: file ('.$fileId.
') not found for user ('.
$userId.
').');
115 $url = \CIMDisk::GetFileLink($file);
118 AddMessage2Log(
'MessageParamHandler::beforeLogFormat: failed to get external link for file ('.$fileId.
').');
119 AddMessage2Log($file->getErrors());
123 $fileName = $file->getName();
124 $fileSize = $file->getSize();
126 $attach = new \CIMMessageParamAttach(
null, \CIMMessageParamAttach::CHAT);
127 if (\
Bitrix\Disk\TypeFile::isImage($file))
129 $source = $file->getFile();
132 $attach->AddImages([[
135 "WIDTH" => (
int)$source[
"WIDTH"],
136 "HEIGHT" => (
int)$source[
"HEIGHT"],
141 if ($attach->IsEmpty())
150 $record[
"PARAM_NAME"] =
'ATTACH';
151 $record[
"PARAM_VALUE"] = 1;
152 $record[
"PARAM_JSON"] = $attach->GetJSON();
162 public function afterInsertTrigger(array $newRecord)
164 $id = intval($newRecord[
'MESSAGE_ID']);
166 if (!\
Bitrix\Main\Loader::includeModule(
'pull'))
169 $message = \CIMMessenger::GetById($id, Array(
'WITH_FILES' =>
'Y'));
173 if ($newRecord[
'PARAM_NAME'] ===
'LIKE' && $newRecord[
"PARAM_VALUE"])
175 $like = $message[
'PARAMS'][
'LIKE'];
177 $result = \Bitrix\Im\Model\ChatTable::getList(Array(
179 '=ID' => $message[
'CHAT_ID']
182 $chat = $result->fetch();
184 $relations = \CIMMessenger::GetRelationById($id);
185 if (!isset($relations[$newRecord[
"PARAM_VALUE"]]))
188 if ($message[
'AUTHOR_ID'] > 0 && $message[
'AUTHOR_ID'] != $newRecord[
"PARAM_VALUE"])
190 $message[
'MESSAGE'] = str_replace(
'<br />',
' ', \
Bitrix\Im\Text::parse($message[
'MESSAGE']));
191 $message[
'MESSAGE'] = preg_replace(
"/\[s\].*?\[\/s\]/i",
"", $message[
'MESSAGE']);
192 $message[
'MESSAGE'] = preg_replace(
"/\[[bui]\](.*?)\[\/[bui]\]/i",
"$1", $message[
'MESSAGE']);
193 $message[
'MESSAGE'] = preg_replace(
"/\[USER=([0-9]{1,})\](.*?)\[\/USER\]/i",
"$2", $message[
'MESSAGE']);
194 $message[
'MESSAGE'] = preg_replace(
"/\[SEND(?:=(.+?))?\](.+?)?\[\/SEND\]/i",
"$2", $message[
'MESSAGE']);
195 $message[
'MESSAGE'] = preg_replace(
"/\[PUT(?:=(.+?))?\](.+?)?\[\/PUT\]/i",
"$2", $message[
'MESSAGE']);
196 $message[
'MESSAGE'] = preg_replace(
"/\[CALL(?:=(.+?))?\](.+?)?\[\/CALL\]/i",
"$2", $message[
'MESSAGE']);
197 $message[
'MESSAGE'] = preg_replace(
"/------------------------------------------------------(.*)------------------------------------------------------/mi",
" [".GetMessage(
'IM_QUOTE').
"] ", str_replace(array(
"#BR#"), Array(
" "), $message[
'MESSAGE']));
199 if (count($message[
'FILES']) > 0 && mb_strlen($message[
'MESSAGE']) < 200)
201 foreach ($message[
'FILES'] as $file)
203 $file =
" [".GetMessage(
'IM_MESSAGE_FILE').
": ".$file[
'name'].
"]";
204 if (mb_strlen($message[
'MESSAGE'].$file) > 200)
207 $message[
'MESSAGE'] .= $file;
209 $message[
'MESSAGE'] = trim($message[
'MESSAGE']);
212 $isChat = $chat && $chat[
'TITLE'] <>
'';
214 $dot = mb_strlen($message[
'MESSAGE']) >= 200?
'...':
'';
215 $message[
'MESSAGE'] = mb_substr($message[
'MESSAGE'], 0, 199).$dot;
216 $message[
'MESSAGE'] = $message[
'MESSAGE'] <>
''? $message[
'MESSAGE']:
'-';
218 $arMessageFields = array(
219 "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
220 "TO_USER_ID" => $message[
'AUTHOR_ID'],
221 "FROM_USER_ID" => $newRecord[
"PARAM_VALUE"],
222 "NOTIFY_TYPE" => IM_NOTIFY_FROM,
223 "NOTIFY_MODULE" =>
"im",
224 "NOTIFY_EVENT" =>
"like",
225 "NOTIFY_TAG" =>
"RATING|IM|".($isChat?
'G':
'P').
"|".($isChat? $chat[
'ID']: $newRecord[
"PARAM_VALUE"]).
"|".$id,
226 "NOTIFY_MESSAGE" => GetMessage($isChat?
'IM_MESSAGE_LIKE':
'IM_MESSAGE_LIKE_PRIVATE', Array(
227 '#MESSAGE#' => $message[
'MESSAGE'],
228 '#TITLE#' => $chat[
'TITLE']
231 \CIMNotify::Add($arMessageFields);
234 $arPullMessage = Array(
236 'chatId' => $relations[$newRecord[
"PARAM_VALUE"]][
'CHAT_ID'],
237 'senderId' => $newRecord[
"PARAM_VALUE"],
241 foreach ($relations as $rel)
243 \Bitrix\Pull\Event::add($rel[
'USER_ID'], Array(
245 'command' =>
'messageLike',
246 'params' => $arPullMessage,
247 'extra' => \
Bitrix\Im\Common::getPullExtra()
251 else if (in_array($newRecord[
'PARAM_NAME'], Array(
'ATTACH',
'URL_ID',
'IS_DELETED',
'IS_EDITED')))
253 \CIMMessageParam::SendPull($id, Array($newRecord[
'PARAM_NAME']));
257 public function afterDeleteTrigger(array $oldRecord)
259 \CIMMessageParam::SendPull($oldRecord[
'MESSAGE_ID'], Array($oldRecord[
'PARAM_NAME']));