Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
MentionService.php
1<?php
2
4
8use Bitrix\Im\V2\Common\ContextCustomer;
9
11{
12 use ContextCustomer;
13
14 private SendingConfig $sendingConfig;
15
19 public function __construct(?SendingConfig $sendingConfig = null)
20 {
21 if ($sendingConfig === null)
22 {
23 $sendingConfig = new SendingConfig();
24 }
25 $this->sendingConfig = $sendingConfig;
26 }
27
28 public function isPullEnable(): bool
29 {
30 static $enable;
31 if ($enable === null)
32 {
33 $enable = \Bitrix\Main\Loader::includeModule('pull');
34 }
35 return $enable;
36 }
37
38 public function sendMentions(Chat $chat, Message $message): void
39 {
40 if (
41 !$chat->allowMention()
42 || !$chat->getChatId()
43 || !$message->getMessage()
44 || !$message->getAuthorId()
45 )
46 {
47 return;
48 }
49
50 $userName = $message->getAuthor()->getFullName(false);
51 if (!$userName)
52 {
53 return;
54 }
55
56 $userGender = $message->getAuthor()->getGender() == 'F' ? 'F' : 'M';
57 $chatTitle = mb_substr(htmlspecialcharsback($chat->getTitle()), 0, 32);
58
59
60 $relations = [];
61 foreach ($chat->getRelations() as $relation)
62 {
63 $relations[$relation->getUserId()] = $relation->getNotifyBlock();
64 }
65
66 $forUsers = [];
67 if (preg_match_all("/\[USER=([0-9]+)( REPLACE)?](.*?)\[\/USER]/i", $message->getMessage(), $matches))
68 {
69 if ($chat->getType() == Chat::IM_TYPE_OPEN)
70 {
71 foreach ($matches[1] as $userId)
72 {
73 if (!\CIMSettings::GetNotifyAccess($userId, 'im', 'mention', \CIMSettings::CLIENT_SITE))
74 {
75 continue;
76 }
77
78 if (
79 !isset($relations[$userId])
80 || $relations[$userId] === true
81 )
82 {
83 $forUsers[$userId] = $userId;
84 }
85 }
86 }
87 else
88 {
89 foreach ($matches[1] as $userId)
90 {
91 if (!\CIMSettings::GetNotifyAccess($userId, 'im', 'mention', \CIMSettings::CLIENT_SITE))
92 {
93 continue;
94 }
95
96 if (
97 isset($relations[$userId])
98 && $relations[$userId] === true
99 )
100 {
101 $forUsers[$userId] = $userId;
102 }
103 }
104 }
105 }
106
107 foreach ($forUsers as $userId)
108 {
109 if ($message->getAuthorId() == $userId)
110 {
111 continue;
112 }
113
114 $arMessageFields = array(
115 "TO_USER_ID" => $userId,
116 "FROM_USER_ID" => $message->getAuthorId(),
117 "NOTIFY_TYPE" => \IM_NOTIFY_FROM,
118 "NOTIFY_MODULE" => "im",
119 "NOTIFY_EVENT" => "mention",
120 "NOTIFY_TAG" => 'IM|MENTION|'.$chat->getChatId(),
121 "NOTIFY_SUB_TAG" => 'IM_MESS_'.$chat->getChatId().'_'.$userId,
122 "NOTIFY_MESSAGE" => $this->prepareNotifyMessage($chatTitle, $chat->getChatId(), $userGender),
123 "NOTIFY_MESSAGE_OUT" => $this->prepareNotifyMail($chatTitle, $userGender),
124 );
125 \CIMNotify::Add($arMessageFields);//todo: Replace with new sending functional
126
127 if ($this->isPullEnable())
128 {
129 \Bitrix\Pull\Push::add(
130 $userId,
131 $this->preparePushForMentionInChat(
132 $this->preparePushMessage($message, $chatTitle, $userName, $userGender),
133 $message,
134 $chat,
135 $chatTitle
136 )
137 );
138 }
139 }
140 }
141
142
143 private function preparePushForMentionInChat(string $pushText, Message $message, Chat $chat, string $chatTitle): array
144 {
145 $avatarUser = $message->getAuthor()->getAvatar();
146 if ($avatarUser && mb_strpos($avatarUser, 'http') !== 0)
147 {
148 $avatarUser = \Bitrix\Im\Common::getPublicDomain(). $avatarUser;
149 }
150
151 $avatarChat = \CIMChat::GetAvatarImage($chat->getAvatarId(), 200, false);
152 if ($avatarChat && mb_strpos($avatarChat, 'http') !== 0)
153 {
154 $avatarChat = \Bitrix\Im\Common::getPublicDomain(). $avatarChat;
155 }
156
157 $result = [];
158 $result['push'] = [];
159
160 $result['module_id'] = 'im';
161 $result['push']['params'] = [
162 'TAG' => 'IM_CHAT_'.$chat->getChatId(),
163 'CHAT_TYPE' => $chat->getType(),
164 'CATEGORY' => 'ANSWER',
165 'URL' => SITE_DIR.'mobile/ajax.php?mobile_action=im_answer',
166 'PARAMS' => [
167 'RECIPIENT_ID' => 'chat'.$chat->getChatId()
168 ]
169 ];
170 $result['push']['type'] = ($chat->getType() == Chat::IM_TYPE_OPEN ? 'openChat' : 'chat');
171 $result['push']['tag'] = 'IM_CHAT_'.$chat->getChatId();
172 $result['push']['sub_tag'] = 'IM_MESS';
173 $result['push']['app_id'] = 'Bitrix24';
174 $result['push']['message'] = $pushText;
175 $result['push']['advanced_params'] = [
176 'group' => ($chat->getEntityType() == Chat::ENTITY_TYPE_LINE ? 'im_lines_message' : 'im_message'),
177 'avatarUrl' => $avatarChat ?: $avatarUser,
178 'senderName' => $chatTitle,
179 'senderMessage' => $pushText,
180 ];
181
182 return $result;
183 }
184
185 private function preparePushMessage(Message $message, string $chatTitle, string $userName, string $userGender): string
186 {
188
189 $pushMessage = $message->getMessage();
190
191 $pushFiles = '';
192 if ($message->hasFiles())
193 {
194 foreach ($message->getFiles() as $file)
195 {
196 $pushFiles .= " [".Loc::getMessage('IM_MESSAGE_FILE').": ".$file->getDiskFile()->getName()."]";
197 }
198 $pushMessage .= $pushFiles;
199 }
200
201 $hasAttach = mb_strpos($pushMessage, '[ATTACH=') !== false;
202
203 $pushMessage = preg_replace("/\[CODE\](.*?)\[\/CODE\]/si", " [".Loc::getMessage('IM_MESSAGE_CODE')."] ", $pushMessage);
204 $pushMessage = preg_replace("/\[s\].*?\[\/s\]/i", "-", $pushMessage);
205 $pushMessage = preg_replace("/\[[bui]\](.*?)\[\/[bui]\]/i", "$1", $pushMessage);
206 $pushMessage = preg_replace("/\\[url\\](.*?)\\[\\/url\\]/i".\BX_UTF_PCRE_MODIFIER, "$1", $pushMessage);
207 $pushMessage = preg_replace("/\\[url\\s*=\\s*((?:[^\\[\\]]++|\\[ (?: (?>[^\\[\\]]+) | (?:\\1) )* \\])+)\\s*\\](.*?)\\[\\/url\\]/ixs".\BX_UTF_PCRE_MODIFIER, "$2", $pushMessage);
208 $pushMessage = preg_replace_callback("/\[USER=([0-9]{1,})\]\[\/USER\]/i", ['\Bitrix\Im\Text', 'modifyShortUserTag'], $pushMessage);
209 $pushMessage = preg_replace("/\[USER=([0-9]+)( REPLACE)?](.+?)\[\/USER]/i", "$3", $pushMessage);
210 $pushMessage = preg_replace("/\[CHAT=([0-9]{1,})\](.*?)\[\/CHAT\]/i", "$2", $pushMessage);
211 $pushMessage = preg_replace_callback("/\[SEND(?:=(?:.+?))?\](?:.+?)?\[\/SEND]/i", ['\Bitrix\Im\Text', 'modifySendPut'], $pushMessage);
212 $pushMessage = preg_replace_callback("/\[PUT(?:=(?:.+?))?\](?:.+?)?\[\/PUT]/i", ['\Bitrix\Im\Text', 'modifySendPut'], $pushMessage);
213 $pushMessage = preg_replace("/\[CALL(?:=(.+?))?\](.+?)?\[\/CALL\]/i", "$2", $pushMessage);
214 $pushMessage = preg_replace("/\[PCH=([0-9]{1,})\](.*?)\[\/PCH\]/i", "$2", $pushMessage);
215 $pushMessage = preg_replace_callback("/\[ICON\=([^\]]*)\]/i", ['\Bitrix\Im\Text', 'modifyIcon'], $pushMessage);
216 $pushMessage = preg_replace('#\-{54}.+?\-{54}#s', " [".Loc::getMessage('IM_MESSAGE_QUOTE')."] ", str_replace('#BR#', ' ', $pushMessage));
217 $pushMessage = preg_replace('/^(>>(.*)(\n)?)/mi', " [".Loc::getMessage('IM_MESSAGE_QUOTE')."] ", str_replace('#BR#', ' ', $pushMessage));
218
219 if (!$pushFiles && !$hasAttach && $message->getParams()->isSet('ATTACH'))
220 {
221 $pushMessage .= " [".Loc::getMessage('IM_MESSAGE_ATTACH')."]";
222 }
223
224 return
225 Loc::getMessage('IM_MESSAGE_MENTION_PUSH_2_'.$userGender, ['#USER#' => $userName, '#TITLE#' => $chatTitle])
226 . ': '
227 . $pushMessage;
228 }
229
230 private function prepareNotifyMail(string $chatTitle, string $userGender): callable
231 {
232 return fn (?string $languageId = null) => Loc::getMessage(
233 'IM_MESSAGE_MENTION_'.$userGender,
234 ['#TITLE#' => $chatTitle],
235 $languageId
236 );
237 }
238
239 private function prepareNotifyMessage(string $chatTitle, int $chatId, string $userGender): callable
240 {
241 return fn (?string $languageId = null) => Loc::getMessage(
242 'IM_MESSAGE_MENTION_'.$userGender,
243 ['#TITLE#' => '[CHAT='.$chatId.']'.$chatTitle.'[/CHAT]'],
244 $languageId
245 );
246 }
247}
static getType($chatData)
Definition chat.php:41
const IM_TYPE_OPEN
Definition Chat.php:61
const ENTITY_TYPE_LINE
Definition Chat.php:97
sendMentions(Chat $chat, Message $message)
__construct(?SendingConfig $sendingConfig=null)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29