1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
dialog.php
См. документацию.
1<?php
2namespace Bitrix\Im;
3
4use Bitrix\Im\Model\ChatTable;
5use Bitrix\Im\Model\RelationTable;
6use Bitrix\Main\ORM\Fields\Relations\OneToMany;
7use Bitrix\Main\ORM\Fields\Relations\Reference;
8use Bitrix\Main\ORM\Query\Join;
9
10class Dialog
11{
12 public static function getTitle($dialogId, $userId = null):? string
13 {
14 if (Common::isChatId($dialogId))
15 {
16 if (!Dialog::hasAccess($dialogId, $userId))
17 {
18 return null;
19 }
20
21 $chatId = Dialog::getChatId($dialogId);
22
23 $chatData = ChatTable::getRow([
24 'select' => ['TITLE'],
25 'filter' => ['=ID' => $chatId],
26 ]);
27 if (!$chatData)
28 {
29 return null;
30 }
31
32 return $chatData['TITLE'];
33 }
34
36 $chatId = \CIMMessage::GetChatId($dialogId, $userId);
37 if (!$chatId)
38 {
39 return null;
40 }
41
42 $userNames = [
43 User::getInstance($dialogId)->getFullName(false),
44 User::getInstance($userId)->getFullName(false),
45 ];
46
47 return implode(" - ", $userNames);
48 }
49
50 public static function getDialogId(int $chatId, $userId = null): string
51 {
53 if (!$userId)
54 {
55 return '';
56 }
57
58 $chat = \Bitrix\Im\Chat::getById($chatId, ['USER_ID' => $userId]);
59
60 if (!$chat)
61 {
62 return '';
63 }
64
65 if ($chat['MESSAGE_TYPE'] !== Chat::TYPE_PRIVATE)
66 {
67 return "chat{$chat['ID']}";
68 }
69
70 $query = ChatTable::query()
71 ->setSelect(['DIALOG_ID' => 'RELATION.USER_ID'])
72 ->registerRuntimeField(
73 'RELATION',
74 (
75 new OneToMany('RELATION', RelationTable::class, 'CHAT')
76 )->configureJoinType('inner')
77 )
78 ->where('ID', (int)$chatId)
79 ->where('TYPE', Chat::TYPE_PRIVATE)
80 ->whereNot('RELATION.USER_ID', $userId)
81 ->setLimit(1)
82 ;
83
84 $queryResult = $query->fetch();
85 if (!$queryResult)
86 {
87 return '';
88 }
89
90 return $queryResult['DIALOG_ID'];
91 }
92
93 public static function getChatId($dialogId, $userId = null)
94 {
95 if (preg_match('/^chat[0-9]{1,}$/i', $dialogId))
96 {
97 $chatId = (int)mb_substr($dialogId, 4);
98 }
99 else if (preg_match('/^\d{1,}$/i', $dialogId))
100 {
101 $dialogId = intval($dialogId);
102 if (!$dialogId)
103 {
104 return false;
105 }
106
108 if (!$userId)
109 {
110 return false;
111 }
112
113 $chatId = \CIMMessage::GetChatId($dialogId, $userId);
114 if (!$chatId)
115 {
116 return false;
117 }
118 }
119 else if (preg_match('/^crm\|\w+?\|\d+?$/i', $dialogId))
120 {
121 $chatId = \CIMChat::GetCrmChatId(mb_substr($dialogId, 4));
122 }
123 else if (preg_match('/^sg[0-9]{1,}$/i', $dialogId))
124 {
125 $chatId = \CIMChat::GetSonetGroupChatId(mb_substr($dialogId, 2));
126 }
127 else
128 {
129 $chatId = 0;
130 }
131
132 return $chatId;
133 }
134
135 public static function getChatIds(array $dialogIds, int $currentUserId): array
136 {
137 $dialogIds = array_filter($dialogIds, function($dialogId) use ($currentUserId) {
138 return (int)$dialogId !== $currentUserId;
139 });
140
141 if (empty($dialogIds))
142 {
143 return [];
144 }
145
146 $chatIds = [];
147
148 $query = RelationTable::query()
149 ->setSelect(['CHAT_ID', 'USER_ID'])
150 ->registerRuntimeField(
151 'RELATION',
152 new Reference(
153 'RELATION',
154 RelationTable::class,
155 Join::on('this.CHAT_ID', 'ref.CHAT_ID')
156 ->where('ref.USER_ID', $currentUserId)
157 ->where('ref.MESSAGE_TYPE', Chat::TYPE_PRIVATE),
158 ['join_type' => Join::TYPE_INNER]
159 )
160 )
161 ->whereIn('USER_ID', $dialogIds)
162 ->where('MESSAGE_TYPE', Chat::TYPE_PRIVATE)
163 ->exec()
164 ;
165
166 while ($row = $query->Fetch())
167 {
168 $chatIds[(int)$row['USER_ID']] = (int)$row['CHAT_ID'];
169 }
170
171 return $chatIds;
172 }
173
174 public static function getLink($dialogId, $userId = null):? string
175 {
176 if (!Dialog::hasAccess($dialogId, $userId))
177 {
178 return null;
179 }
180
181 return '/online/?IM_DIALOG='.$dialogId;
182 }
183
184 public static function hasAccess($dialogId, $userId = null)
185 {
187 if (!$userId)
188 {
189 return false;
190 }
191
192 if (\Bitrix\Im\Common::isChatId($dialogId))
193 {
194 $chatId = self::getChatId($dialogId, $userId);
195
196 return \Bitrix\Im\V2\Chat::getInstance($chatId)->checkAccess($userId)->isSuccess();
197 }
198
199 return \Bitrix\Im\V2\Entity\User\User::getInstance($dialogId)->checkAccess($userId)->isSuccess();
200 }
201
202 public static function read($dialogId, $messageId = null, $userId = null)
203 {
205 if (!$userId)
206 {
207 return false;
208 }
209
210 if (\Bitrix\Im\Common::isChatId($dialogId))
211 {
212 $chatId = self::getChatId($dialogId);
213
214 $chat = new \CIMChat($userId);
215 $result = $chat->SetReadMessage($chatId, $messageId);
216 }
217 else if ($dialogId === 'notify')
218 {
219 $notify = new \CIMNotify();
220 $notify->MarkNotifyRead(0, true);
221
222 return true;
223 }
224 else
225 {
226 $CIMMessage = new \CIMMessage($userId);
227 $result = $CIMMessage->SetReadMessage($dialogId, $messageId);
228 }
229
230 return $result;
231 }
232
233 public static function readAll($userId = null)
234 {
236 if (!$userId)
237 {
238 return false;
239 }
240
241 \Bitrix\Im\V2\Chat::readAllChats($userId);
242
243 return true;
244 }
245
246 public static function unread($dialogId, $messageId = null, $userId = null)
247 {
249 if (!$userId)
250 {
251 return false;
252 }
253
254 if (\Bitrix\Im\Common::isChatId($dialogId))
255 {
256 $chatId = self::getChatId($dialogId);
257
258 $chat = new \CIMChat($userId);
259 $chat->SetUnReadMessage($chatId, $messageId);
260 }
261 else
262 {
263 $CIMMessage = new \CIMMessage($userId);
264 $CIMMessage->SetUnReadMessage($dialogId, $messageId);
265 }
266
267 return false;
268 }
269
270 public static function getRelation($userId1, $userId2, $params = array())
271 {
272 $userId1 = intval($userId1);
273 $userId2 = intval($userId2);
274
275 if ($userId1 <= 0 || $userId2 <= 0)
276 {
277 return false;
278 }
279
280 $chatId = \CIMMessage::GetChatId($userId1, $userId2);
281 if (!$chatId)
282 {
283 return false;
284 }
285
286 return Chat::getRelation($chatId, $params);
287 }
288
289
290
291}
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
const TYPE_PRIVATE
Определения chat.php:25
static getById($id, $params=array())
Определения chat.php:800
static getRelation($chatId, $params=[])
Определения chat.php:93
static isChatId($id)
Определения common.php:58
static getUserId($userId=null)
Определения common.php:73
Определения dialog.php:11
static getLink($dialogId, $userId=null)
Определения dialog.php:174
static getChatId($dialogId, $userId=null)
Определения dialog.php:93
static getRelation($userId1, $userId2, $params=array())
Определения dialog.php:270
static getDialogId(int $chatId, $userId=null)
Определения dialog.php:50
static hasAccess($dialogId, $userId=null)
Определения dialog.php:184
static readAll($userId=null)
Определения dialog.php:233
static getChatIds(array $dialogIds, int $currentUserId)
Определения dialog.php:135
static unread($dialogId, $messageId=null, $userId=null)
Определения dialog.php:246
static getTitle($dialogId, $userId=null)
Определения dialog.php:12
static read($dialogId, $messageId=null, $userId=null)
Определения dialog.php:202
static getInstance($userId=null)
Определения user.php:45
static GetChatId($fromUserId, $toUserId, $createIfNotExists=true)
Определения im_message.php:1459
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799