Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
VideoConfChat.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
8use Bitrix\IM\Model\AliasTable;
10use Bitrix\IM\Model\ConferenceTable;
11use Bitrix\IM\Model\ConferenceUserRoleTable;
19use CGlobalCounter;
20use CIMMessageParamAttach;
21
23{
24 private const MAX_CONF_NUMBER = 999;
25
26 public function getDefaultEntityType(): ?string
27 {
28 return self::ENTITY_TYPE_VIDEOCONF;
29 }
30
31 protected function needToSendGreetingMessages(): bool
32 {
33 return true;
34 }
35
36 public function add(array $params, ?Context $context = null): Result
37 {
38 $addResult = parent::add($params, $context);
39 if (!$addResult->isSuccess() || !$addResult->hasResult())
40 {
41 return $addResult;
42 }
43
44 $chatResult = $addResult->getResult();
46 $chat = $chatResult['CHAT'];
47
48
49 if (
50 !isset($params['VIDEOCONF']['ALIAS_DATA'])
51 || !isset($params['VIDEOCONF']['ALIAS_DATA']['ID'])
52 || !isset($params['VIDEOCONF']['ALIAS_DATA']['LINK'])
53 )
54 {
55 $aliasData = Alias::addUnique([
56 "ENTITY_TYPE" => Alias::ENTITY_TYPE_VIDEOCONF,
57 "ENTITY_ID" => $chat->getChatId(),
58 ]);
59 }
60 else
61 {
62 $aliasData = $params['VIDEOCONF']['ALIAS_DATA'];
63
64 AliasTable::update($aliasData['ID'], ['ENTITY_ID' => $chat->getChatId()]);
65 }
66
67 $conferenceData = [
68 'ALIAS_ID' => $aliasData['ID']
69 ];
70
71 if (isset($params['VIDEOCONF']['PASSWORD']))
72 {
73 $conferenceData['PASSWORD'] = $params['VIDEOCONF']['PASSWORD'];
74 }
75 else
76 {
77 $conferenceData['PASSWORD'] = $params['CONFERENCE_PASSWORD'] ?? '';
78 }
79
80 if (isset($params['VIDEOCONF']['INVITATION']))
81 {
82 $conferenceData['INVITATION'] = $params['VIDEOCONF']['INVITATION'];
83 }
84
85 $conferenceData['IS_BROADCAST'] = isset($params['VIDEOCONF']['IS_BROADCAST']) && $params['VIDEOCONF']['IS_BROADCAST'] === 'Y'? 'Y': 'N';
86
87 $creationResult = ConferenceTable::add($conferenceData);
88 if (isset($params['VIDEOCONF']['PRESENTERS']))
89 {
90 foreach ($params['VIDEOCONF']['PRESENTERS'] as $presenter)
91 {
92 ConferenceUserRoleTable::add([
93 'CONFERENCE_ID' => $creationResult->getId(),
94 'USER_ID' => $presenter,
95 'ROLE' => Conference::ROLE_PRESENTER
96 ]);
97 }
98 }
99
100 $message = ''
101 . GetMessage("IM_VIDEOCONF_LINK_TITLE") . ': [URL]' . $aliasData['LINK'] . '[/URL][BR]'
102 ;
103 $attach = new CIMMessageParamAttach(null, Color::getColor($chat->getColor()));
104 $attach->SetDescription(CIMMessageParamAttach::FIRST_MESSAGE);
105 $attach->AddMessage($message);
106
107 $keyboard = new \Bitrix\Im\Bot\Keyboard();
108 $keyboard->addButton(
109 [
110 "TEXT" => GetMessage("IM_VIDEOCONF_COPY_LINK"),
111 "ACTION" => "COPY",
112 "ACTION_VALUE" => $aliasData['LINK'],
113 "DISPLAY" => "LINE",
114 "BG_COLOR" => "#A4C31E",
115 "TEXT_COLOR" => "#FFF"
116 ]
117 );
118
119 \CIMChat::AddMessage([
120 "TO_CHAT_ID" => $chat->getChatId(),
121 "FROM_USER_ID" => $chat->getAuthorId(),
122 "MESSAGE" => GetMessage('IM_VIDEOCONF_CREATE_WELCOME'),
123 "SYSTEM" => 'Y',
124 "ATTACH" => $attach,
125 "KEYBOARD" => $keyboard,
126 'PARAMS' => [
127 'COMPONENT_ID' => 'ConferenceCreationMessage',
128 ],
129 'SKIP_USER_CHECK' => 'Y',
130 ]);
131
132 $addResult->setResult([
133 'CHAT_ID' => $chat->getChatId(),
134 'CHAT' => $chat,
135 'ALIAS' => $aliasData['ALIAS'],
136 'LINK' => $aliasData['LINK'],
137 ]);
138
139 return $addResult;
140 }
141
142 protected function getCodeGreetingMessage(\Bitrix\Im\V2\Entity\User\User $author): string
143 {
144 return 'IM_VIDEOCONF_CREATE_' . $author->getGender();
145 }
146
147 protected function prepareParams(array $params = []): Result
148 {
149 if (!isset($params['TITLE']))
150 {
151 $params['TITLE'] = $this->generateTitle();
152 }
153
154 if (isset($params['OWNER_ID']))
155 {
156 $params['OWNER_ID'] = (int)$params['OWNER_ID'];
157 }
158
159 if (!isset($params['VIDEOCONF']['PASSWORD']) && isset($params['CONFERENCE_PASSWORD']))
160 {
161 $params['PASSWORD'] = $params['CONFERENCE_PASSWORD'];
162 }
163
164 $params['SEARCHABLE'] = 'N';
165
166 $params['MANAGE_UI'] = $params['MANAGE_UI'] ?? $this->getDefaultManageUI();
167 $params['MANAGE_SETTINGS'] = $params['MANAGE_SETTINGS'] ?? $this->getDefaultManageSettings();
168 $params['MANAGE_USERS_ADD'] = $params['MANAGE_USERS_ADD'] ?? $this->getDefaultManageUsersAdd();
169 $params['MANAGE_USERS_DELETE'] = $params['MANAGE_USERS_DELETE'] ?? $this->getDefaultManageUsersDelete();
170 $params['CAN_POST'] = $params['CAN_POST'] ?? $this->getDefaultCanPost();
171
172 $params = parent::prepareParams($params);
173 if (!$params->isSuccess())
174 {
175 return $params;
176 }
177
178 $paramData = $params->getResult();
179
180 //todo: drag method into this class
181 $confParams = Conference::prepareParamsForAdd($paramData);
182 if (!$confParams->isSuccess())
183 {
184 return $confParams;
185 }
186 $confParams = $confParams->getData()['FIELDS'];
187
188 return $params->setResult(array_merge($paramData, $confParams));
189 }
190
191 public function generateTitle(): string
192 {
193 CGlobalCounter::Increment('im_videoconf_count', CGlobalCounter::ALL_SITES, false);
194 $videoconfCount = CGlobalCounter::GetValue('im_videoconf_count', CGlobalCounter::ALL_SITES);
195
196 if ($videoconfCount === self::MAX_CONF_NUMBER)
197 {
198 CGlobalCounter::Set('im_videoconf_count', 1, CGlobalCounter::ALL_SITES, '', false);
199 }
200
201 return Loc::GetMessage('IM_VIDEOCONF_NAME_FORMAT_NEW', [
202 '#NUMBER#' => $videoconfCount
203 ]);
204 }
205
206 public function setExtranet(?bool $extranet): \Bitrix\Im\V2\Chat
207 {
208 return $this;
209 }
210
211 public function getExtranet(): ?bool
212 {
213 return false;
214 }
215
216 protected function updateStateAfterUsersAdd(array $usersToAdd): self
217 {
218 parent::updateStateAfterUsersAdd($usersToAdd);
219
220 $wasUserBlocked = BlockUserTable::query()
221 ->setSelect(['ID'])
222 ->where('CHAT_ID', $this->getId())
223 ->whereIn('USER_ID', $usersToAdd)
224 ->fetchCollection()
225 ->getIdList()
226 ;
227
228 if (empty($wasUserBlocked))
229 {
230 return $this;
231 }
232
233 BlockUserTable::deleteByFilter(['=USER_ID' => $wasUserBlocked]);
234
235 return $this;
236 }
237
238 protected function updateStateAfterUserDelete(int $deletedUserId): Chat
239 {
240 parent::updateStateAfterUserDelete($deletedUserId);
241
242 $externalAuthId = User::getInstance($deletedUserId)->getExternalAuthId();
243 if ($externalAuthId === 'call')
244 {
245 BlockUserTable::add(
246 [
247 'CHAT_ID' => $this->getId(),
248 'USER_ID' => $deletedUserId,
249 'BLOCK_DATE' => new SqlExpression(Application::getConnection()->getSqlHelper()->getCurrentDateTimeFunction())
250 ]
251 );
252 }
253
254 return $this;
255 }
256}
const ENTITY_TYPE_VIDEOCONF
Definition alias.php:13
static addUnique(array $fields)
Definition alias.php:55
static getColor($code)
Definition color.php:121
add(array $params, ?Context $context=null)
Definition GroupChat.php:69
getCodeGreetingMessage(\Bitrix\Im\V2\Entity\User\User $author)
updateStateAfterUserDelete(int $deletedUserId)
updateStateAfterUsersAdd(array $usersToAdd)
static getConnection($name="")