Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dialog.php
1<?php
2namespace Bitrix\Im;
3
7
8class Dialog
9{
10 public static function getTitle($dialogId, $userId = null):? string
11 {
12 if (Common::isChatId($dialogId))
13 {
14 if (!Dialog::hasAccess($dialogId, $userId))
15 {
16 return null;
17 }
18
19 $chatId = Dialog::getChatId($dialogId);
20
21 $chatData = ChatTable::getRow([
22 'select' => ['TITLE'],
23 'filter' => ['=ID' => $chatId],
24 ]);
25 if (!$chatData)
26 {
27 return null;
28 }
29
30 return $chatData['TITLE'];
31 }
32
33 $userId = Common::getUserId($userId);
34 $chatId = \CIMMessage::GetChatId($dialogId, $userId);
35 if (!$chatId)
36 {
37 return null;
38 }
39
40 $userNames = [
41 User::getInstance($dialogId)->getFullName(false),
42 User::getInstance($userId)->getFullName(false),
43 ];
44
45 return implode(" - ", $userNames);
46 }
47
48 public static function getDialogId(int $chatId, $userId = null): string
49 {
50 $userId = \Bitrix\Im\Common::getUserId($userId);
51 if (!$userId)
52 {
53 return false;
54 }
55
56 $chat = \Bitrix\Im\Chat::getById($chatId);
57
58 if (!$chat)
59 {
60 return false;
61 }
62
63 if ($chat['MESSAGE_TYPE'] !== Chat::TYPE_PRIVATE)
64 {
65 return "chat{$chat['ID']}";
66 }
67
68 $query = ChatTable::query()
69 ->setSelect(['DIALOG_ID' => 'RELATION.USER_ID'])
70 ->registerRuntimeField(
71 'RELATION',
72 (
73 new OneToMany('RELATION', RelationTable::class, 'CHAT')
74 )->configureJoinType('inner')
75 )
76 ->where('ID', (int)$chatId)
77 ->where('TYPE', Chat::TYPE_PRIVATE)
78 ->whereNot('RELATION.USER_ID', $userId)
79 ->setLimit(1)
80 ;
81
82 $queryResult = $query->fetch();
83 if (!$queryResult)
84 {
85 return false;
86 }
87
88 return $queryResult['DIALOG_ID'];
89 }
90
91 public static function getChatId($dialogId, $userId = null)
92 {
93 if (preg_match('/^chat[0-9]{1,}$/i', $dialogId))
94 {
95 $chatId = (int)mb_substr($dialogId, 4);
96 }
97 else if (preg_match('/^\d{1,}$/i', $dialogId))
98 {
99 $dialogId = intval($dialogId);
100 if (!$dialogId)
101 {
102 return false;
103 }
104
105 $userId = \Bitrix\Im\Common::getUserId($userId);
106 if (!$userId)
107 {
108 return false;
109 }
110
111 $chatId = \CIMMessage::GetChatId($dialogId, $userId);
112 if (!$chatId)
113 {
114 return false;
115 }
116 }
117 else if (preg_match('/^crm\|\w+?\|\d+?$/i', $dialogId))
118 {
119 $chatId = \CIMChat::GetCrmChatId(mb_substr($dialogId, 4));
120 }
121 else if (preg_match('/^sg[0-9]{1,}$/i', $dialogId))
122 {
123 $chatId = \CIMChat::GetSonetGroupChatId(mb_substr($dialogId, 2));
124 }
125 else
126 {
127 $chatId = 0;
128 }
129
130 return $chatId;
131 }
132
133 public static function getLink($dialogId, $userId = null):? string
134 {
135 if (!Dialog::hasAccess($dialogId, $userId))
136 {
137 return null;
138 }
139
140 return '/online/?IM_DIALOG='.$dialogId;
141 }
142
143 public static function hasAccess($dialogId, $userId = null)
144 {
145 $userId = \Bitrix\Im\Common::getUserId($userId);
146 if (!$userId)
147 {
148 return false;
149 }
150
151 if (\Bitrix\Im\Common::isChatId($dialogId))
152 {
153 $chatId = self::getChatId($dialogId, $userId);
154
155 return \Bitrix\Im\V2\Chat::getInstance($chatId)->hasAccess($userId);
156 }
157
158 return \Bitrix\Im\V2\Entity\User\User::getInstance($dialogId)->hasAccess($userId);
159 }
160
161 public static function read($dialogId, $messageId = null, $userId = null)
162 {
163 $userId = \Bitrix\Im\Common::getUserId($userId);
164 if (!$userId)
165 {
166 return false;
167 }
168
169 if (\Bitrix\Im\Common::isChatId($dialogId))
170 {
171 $chatId = self::getChatId($dialogId);
172
173 $chat = new \CIMChat($userId);
174 $result = $chat->SetReadMessage($chatId, $messageId);
175 }
176 else if ($dialogId === 'notify')
177 {
178 $notify = new \CIMNotify();
179 $notify->MarkNotifyRead(0, true);
180
181 return true;
182 }
183 else
184 {
185 $CIMMessage = new \CIMMessage($userId);
186 $result = $CIMMessage->SetReadMessage($dialogId, $messageId);
187 }
188
189 return $result;
190 }
191
192 public static function readAll($userId = null)
193 {
194 $userId = \Bitrix\Im\Common::getUserId($userId);
195 if (!$userId)
196 {
197 return false;
198 }
199
200 \Bitrix\Im\V2\Chat::readAllChats($userId);
201
202 return true;
203 }
204
205 public static function unread($dialogId, $messageId = null, $userId = null)
206 {
207 $userId = \Bitrix\Im\Common::getUserId($userId);
208 if (!$userId)
209 {
210 return false;
211 }
212
213 if (\Bitrix\Im\Common::isChatId($dialogId))
214 {
215 $chatId = self::getChatId($dialogId);
216
217 $chat = new \CIMChat($userId);
218 $chat->SetUnReadMessage($chatId, $messageId);
219 }
220 else
221 {
222 $CIMMessage = new \CIMMessage($userId);
223 $CIMMessage->SetUnReadMessage($dialogId, $messageId);
224 }
225
226 return false;
227 }
228
229 public static function getRelation($userId1, $userId2, $params = array())
230 {
231 $userId1 = intval($userId1);
232 $userId2 = intval($userId2);
233
234 if ($userId1 <= 0 || $userId2 <= 0)
235 {
236 return false;
237 }
238
239 $chatId = \CIMMessage::GetChatId($userId1, $userId2);
240 if (!$chatId)
241 {
242 return false;
243 }
244
245 return Chat::getRelation($chatId, $params);
246 }
247
248
249
250}
const TYPE_PRIVATE
Definition chat.php:21
static getRelation($chatId, $params=[])
Definition chat.php:84
static isChatId($id)
Definition common.php:64
static getUserId($userId=null)
Definition common.php:74
static getLink($dialogId, $userId=null)
Definition dialog.php:133
static getChatId($dialogId, $userId=null)
Definition dialog.php:91
static getRelation($userId1, $userId2, $params=array())
Definition dialog.php:229
static getDialogId(int $chatId, $userId=null)
Definition dialog.php:48
static hasAccess($dialogId, $userId=null)
Definition dialog.php:143
static readAll($userId=null)
Definition dialog.php:192
static unread($dialogId, $messageId=null, $userId=null)
Definition dialog.php:205
static getTitle($dialogId, $userId=null)
Definition dialog.php:10
static read($dialogId, $messageId=null, $userId=null)
Definition dialog.php:161
static getInstance($userId=null)
Definition user.php:44