Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messageparamhandler.php
1<?php
2namespace Bitrix\Im\Replica;
3
5
6if (Loader::includeModule('replica'))
7{
8 class MessageParamHandler extends \Bitrix\Replica\Client\BaseHandler
9 {
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",
15 );
16 protected $predicates = array(
17 "MESSAGE_ID" => "b_im_message.ID",
18 );
19
20 public function __construct()
21 {
22 $this->translation = array(
23 "MESSAGE_ID" => "b_im_message.ID",
24 "PARAM_VALUE" => array($this, "paramValueTranslation"),
25 );
26 }
27
35 public function beforeLogInsert(array $record)
36 {
37 return $record["PARAM_NAME"] === "KEYBOARD"? false: true;
38 }
39
49 public function beforeInsertTrigger(array &$newRecord)
50 {
51 if ($newRecord["MESSAGE_ID"] <= 0)
52 {
53 return array("ID" => 0);
54 }
55
56 return null;
57 }
58
65 public static function paramValueTranslation($record)
66 {
67 if ($record["PARAM_NAME"] === "LIKE" && $record["PARAM_VALUE"])
68 {
69 return "b_user.ID";
70 }
71 elseif ($record["PARAM_NAME"] === "URL_ID" && $record["PARAM_VALUE"])
72 {
73 return "b_urlpreview_metadata.ID";
74 }
75 return false;
76 }
77
85 public function beforeLogFormat(array &$record)
86 {
87 global $USER;
88 if ($record["PARAM_NAME"] !== "FILE_ID" || $record["PARAM_VALUE"] <= 0)
89 {
90 return;
91 }
92
93 if (!\Bitrix\Main\Loader::includeModule('disk'))
94 {
95 AddMessage2Log('MessageParamHandler::beforeLogFormat: failed to load disk module.');
96 return;
97 }
98
99 if (!is_object($USER) || $USER->GetID() < 0)
100 {
101 AddMessage2Log('MessageParamHandler::beforeLogFormat: no user provided.');
102 return;
103 }
104
106 $fileId = $record["PARAM_VALUE"];
107 $userId = $USER->GetID();
108 $file = \Bitrix\Disk\File::loadById($fileId);
109 if (!$file)
110 {
111 AddMessage2Log('MessageParamHandler::beforeLogFormat: file ('.$fileId.') not found for user ('.$userId.').');
112 return;
113 }
114
115 $url = \CIMDisk::GetFileLink($file);
116 if (!$url)
117 {
118 AddMessage2Log('MessageParamHandler::beforeLogFormat: failed to get external link for file ('.$fileId.').');
119 AddMessage2Log($file->getErrors());
120 return;
121 }
122
123 $fileName = $file->getName();
124 $fileSize = $file->getSize();
125
126 $attach = new \CIMMessageParamAttach(null, \CIMMessageParamAttach::CHAT);
127 if (\Bitrix\Disk\TypeFile::isImage($file))
128 {
129 $source = $file->getFile();
130 if ($source)
131 {
132 $attach->AddImages([[
133 "NAME" => $fileName,
134 "LINK" => $url,
135 "WIDTH" => (int)$source["WIDTH"],
136 "HEIGHT" => (int)$source["HEIGHT"],
137 ]]);
138 }
139 }
140
141 if ($attach->IsEmpty())
142 {
143 $attach->AddFiles([[
144 "NAME" => $fileName,
145 "LINK" => $url,
146 "SIZE" => $fileSize,
147 ]]);
148 }
149
150 $record["PARAM_NAME"] = 'ATTACH';
151 $record["PARAM_VALUE"] = 1;
152 $record["PARAM_JSON"] = $attach->GetJSON();
153 }
154
162 public function afterInsertTrigger(array $newRecord)
163 {
164 $id = intval($newRecord['MESSAGE_ID']);
165
166 if (!\Bitrix\Main\Loader::includeModule('pull'))
167 return;
168
169 $message = \CIMMessenger::GetById($id, Array('WITH_FILES' => 'Y'));
170 if (!$message)
171 return;
172
173 if ($newRecord['PARAM_NAME'] === 'LIKE' && $newRecord["PARAM_VALUE"])
174 {
175 $like = $message['PARAMS']['LIKE'];
176
177 $result = \Bitrix\Im\Model\ChatTable::getList(Array(
178 'filter'=>Array(
179 '=ID' => $message['CHAT_ID']
180 )
181 ));
182 $chat = $result->fetch();
183
184 $relations = \CIMMessenger::GetRelationById($id);
185 if (!isset($relations[$newRecord["PARAM_VALUE"]]))
186 return;
187
188 if ($message['AUTHOR_ID'] > 0 && $message['AUTHOR_ID'] != $newRecord["PARAM_VALUE"])
189 {
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']));
198
199 if (count($message['FILES']) > 0 && mb_strlen($message['MESSAGE']) < 200)
200 {
201 foreach ($message['FILES'] as $file)
202 {
203 $file = " [".GetMessage('IM_MESSAGE_FILE').": ".$file['name']."]";
204 if (mb_strlen($message['MESSAGE'].$file) > 200)
205 break;
206
207 $message['MESSAGE'] .= $file;
208 }
209 $message['MESSAGE'] = trim($message['MESSAGE']);
210 }
211
212 $isChat = $chat && $chat['TITLE'] <> '';
213
214 $dot = mb_strlen($message['MESSAGE']) >= 200? '...': '';
215 $message['MESSAGE'] = mb_substr($message['MESSAGE'], 0, 199).$dot;
216 $message['MESSAGE'] = $message['MESSAGE'] <> ''? $message['MESSAGE']: '-';
217
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']
229 ))
230 );
231 \CIMNotify::Add($arMessageFields);
232 }
233
234 $arPullMessage = Array(
235 'id' => $id,
236 'chatId' => $relations[$newRecord["PARAM_VALUE"]]['CHAT_ID'],
237 'senderId' => $newRecord["PARAM_VALUE"],
238 'users' => $like
239 );
240
241 foreach ($relations as $rel)
242 {
243 \Bitrix\Pull\Event::add($rel['USER_ID'], Array(
244 'module_id' => 'im',
245 'command' => 'messageLike',
246 'params' => $arPullMessage,
247 'extra' => \Bitrix\Im\Common::getPullExtra()
248 ));
249 }
250 }
251 else if (in_array($newRecord['PARAM_NAME'], Array('ATTACH', 'URL_ID', 'IS_DELETED', 'IS_EDITED')))
252 {
253 \CIMMessageParam::SendPull($id, Array($newRecord['PARAM_NAME']));
254 }
255 }
256
257 public function afterDeleteTrigger(array $oldRecord)
258 {
259 \CIMMessageParam::SendPull($oldRecord['MESSAGE_ID'], Array($oldRecord['PARAM_NAME']));
260 }
261 }
262}