1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
CommentChat.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
5use Bitrix\Disk\Folder;
6use Bitrix\Im\Model\ChatTable;
7use Bitrix\Im\Recent;
8use Bitrix\Im\V2\Chat;
9use Bitrix\Im\V2\Message\Send\MentionService;
10use Bitrix\Im\V2\Message\Send\SendingConfig;
11use Bitrix\Im\V2\Message\Send\SendingService;
12use Bitrix\Im\V2\MessageCollection;
13use Bitrix\Im\V2\Relation;
14use Bitrix\Im\V2\RelationCollection;
15use Bitrix\Im\V2\Result;
16use Bitrix\Im\V2\Message;
17use Bitrix\Main\Application;
18use Bitrix\Main\Loader;
19use Bitrix\Main\Localization\Loc;
20use Bitrix\Main\Type\DateTime;
21use Bitrix\Pull\Event;
22
27{
28 protected const LOCK_TIMEOUT = 3;
29 protected const EXTRANET_CAN_SEE_HISTORY = true;
30 protected ?Chat $parentChat;
32
33 public static function get(Message $message, bool $createIfNotExists = true): Result
34 {
35 $result = new Result();
36 $chat = null;
37 $chatId = static::getIdByMessage($message);
38 if ($chatId)
39 {
40 $chat = Chat::getInstance($chatId);
41 }
42
43 if ($chat instanceof self)
44 {
45 $chat->parentMessage = $message;
46
47 return $result->setResult($chat);
48 }
49
50 if (!$createIfNotExists)
51 {
53
54 return $result;
55 }
56
57 return static::create($message);
58 }
59
65 {
66 $result = new Result();
67
68 $chatIds = static::getIdsByMessages($messages);
69 if (empty($chatIds))
70 {
71 return $result;
72 }
73
74 $chats = [];
75
76 foreach ($chatIds as $chatInfo)
77 {
78 $chat = Chat::getInstance((int)$chatInfo['ID']);
79 if ($chat instanceof self)
80 {
81 $chat->parentMessage = $messages[(int)$chatInfo['PARENT_MID']];
82 $chats[] = $chat;
83 }
84 }
85
86 return $result->setResult($chats);
87 }
88
93
94 public static function create(Message $message): Result
95 {
96 $result = new Result();
97 $parentChat = $message->getChat();
98
99 if (!$parentChat instanceof ChannelChat)
100 {
101 return $result->addError(new ChatError(ChatError::WRONG_PARENT_CHAT));
102 }
103
104 $isLocked = Application::getConnection()->lock(self::getLockName($message->getId()), self::LOCK_TIMEOUT);
105 if (!$isLocked)
106 {
107 return $result->addError(new ChatError(ChatError::CREATION_ERROR));
108 }
109
110 $chat = Chat::getInstance(static::getIdByMessage($message));
111 if ($chat instanceof self)
112 {
113 Application::getConnection()->unlock(self::getLockName($message->getId()));
114 $chat->parentMessage = $message;
115
116 return $result->setResult($chat);
117 }
118
119 $createResult = static::createInternal($message);
120 Application::getConnection()->unlock(self::getLockName($message->getId()));
121
122 return $createResult;
123 }
124
125 public function isAutoJoinEnabled(): bool
126 {
127 return true;
128 }
129
130 public function canUserAutoJoin(?int $userId = null): bool
131 {
132 return parent::canUserAutoJoin($userId) && $this->getParentChat()->getSelfRelation() !== null;
133 }
134
135 public function join(bool $withMessage = true): Chat
136 {
137 $this->getParentChat()->join();
138
139 return parent::join(false);
140 }
141
142 public function getRole(): string
143 {
144 if (isset($this->role))
145 {
146 return $this->role;
147 }
148
149 $role = parent::getRole();
150
151 if ($role === self::ROLE_MEMBER)
152 {
153 $role = $this->getParentChat()->getRole();
154 }
155
156 $this->role = $role;
157
158 return $role;
159 }
160
161 public function isCounterIncrementAllowed(): bool
162 {
163 return true;
164 }
165
167 {
168 return $this->subscribeUsers(true, $message->getUserIdsFromMention(), $message->getPrevId());
169 }
170
171 protected function onAfterMessageSend(Message $message, SendingService $sendingService): void
172 {
173 $this->subscribe(true, $message->getAuthorId());
174 $this->subscribeUsers(true, $message->getUserIdsFromMention(), $message->getPrevId());
176
177 if (!$sendingService->getConfig()->skipCounterIncrements())
178 {
180 }
181
182 parent::onAfterMessageSend($message, $sendingService);
183 }
184
185 public function filterUsersToMention(array $userIds): array
186 {
187 return $this->getParentChat()->filterUsersToMention($userIds);
188 }
189
191 {
192 $relations = parent::getRelations();
193 $userIds = $relations->getUserIds();
194 if (empty($userIds))
195 {
196 return $relations;
197 }
198
199 $parentRelations = $this->getParentChat()->getRelationsByUserIds($userIds);
200
201 return $relations->filter(
202 fn (Relation $relation) => $parentRelations->getByUserId($relation->getUserId(), $this->getParentChatId())
203 );
204 }
205
207 {
208 return parent::getRelationsForSendMessage()->filterNotifySubscribed();
209 }
210
212 {
213 $userIds = $this->getRelationsForSendMessage()->getUserIds();
214
215 return $this->getParentChat()->getRelationsByUserIds($userIds);
216 }
217
218 public function subscribe(bool $subscribe = true, ?int $userId = null): Result
219 {
220 $userId ??= $this->getContext()->getUserId();
221 $result = new Result();
222
223 $relation = $this->getRelationByUserId($userId);
224
225 if ($relation === null)
226 {
227 return $result->addError(new ChatError(ChatError::ACCESS_DENIED));
228 }
229
230 $relation->setNotifyBlock(!$subscribe)->save();
231 $this->sendSubscribePush($subscribe, [$userId]);
232
233 if (!$subscribe)
234 {
235 $this->read();
236 }
237
238 return $result;
239 }
240
241 protected function getValidUsersToAdd(array $userIds): array
242 {
243 $userIds = parent::getValidUsersToAdd($userIds);
244
245 return $this->getParentChat()->getRelationsByUserIds($userIds)->getUserIds();
246 }
247
248 public function subscribeUsers(bool $subscribe = true, array $userIds = [], ?int $lastId = null): Result
249 {
250 $result = new Result();
251
252 if (empty($userIds))
253 {
254 return $result;
255 }
256
257 $this->addUsers($userIds, new Relation\AddUsersConfig(hideHistory: false));
258 $relations = $this->getRelations();
259 $subscribedUsers = [];
260 foreach ($userIds as $userId)
261 {
262 $relation = $relations->getByUserId($userId, $this->getId());
263 if ($relation === null || !$relation->getNotifyBlock())
264 {
265 continue;
266 }
267 $relation->setNotifyBlock(false);
268 if ($lastId)
269 {
270 $relation->setLastId($lastId);
271 }
272 $subscribedUsers[] = $userId;
273 }
274
275 $relations->save(true);
276 $this->sendSubscribePush($subscribe, $subscribedUsers);
277
278 return $result;
279 }
280
281 protected function sendSubscribePush(bool $subscribe, array $userIds): void
282 {
283 if (!Loader::includeModule('pull') || empty($userIds))
284 {
285 return;
286 }
287 Event::add(
288 $userIds,
289 [
290 'module_id' => 'im',
291 'command' => 'commentSubscribe',
292 'params' => [
293 'dialogId' => $this->getDialogId(),
294 'subscribe' => $subscribe,
295 'messageId' => $this->getParentMessageId(),
296 ],
297 'extra' => \Bitrix\Im\Common::getPullExtra(),
298 ]
299 );
300 }
301
302 protected function createDiskFolder(): ?Folder
303 {
304 $parentFolder = $this->getParentChat()->getOrCreateDiskFolder();
305 if (!$parentFolder)
306 {
307 return null;
308 }
309
310 $folder = $parentFolder->addSubFolder(
311 [
312 'NAME' => "chat{$this->getId()}",
313 'CREATED_BY' => $this->getContext()->getUserId(),
314 ],
315 [],
316 true
317 );
318
319 if ($folder)
320 {
321 $this->setDiskFolderId($folder->getId())->save();
322 }
323
324 return $folder;
325 }
326
328 {
329 $notifyBlock = $userId !== $this->getParentMessage()?->getAuthorId();
330
331 return parent::createRelation($userId, $config)->setLastId(0)->setNotifyBlock($notifyBlock);
332 }
333
334 protected function getDefaultType(): string
335 {
336 return self::IM_TYPE_COMMENT;
337 }
338
339 public function setParentChat(?Chat $chat): self
340 {
341 $this->parentChat = $chat;
342
343 return $this;
344 }
345
346 public function getParentChat(): Chat
347 {
348 $this->parentChat ??= Chat::getInstance($this->getParentChatId());
349
350 return $this->parentChat;
351 }
352
353 public function setParentMessage(?Message $message): self
354 {
355 $this->parentMessage = $message;
356
357 return $this;
358 }
359
360 public function getParentMessage(): ?Message
361 {
362 $this->parentMessage ??= new Message($this->getParentMessageId());
363
365 }
366
367 protected function sendMessageUsersAdd(array $usersToAdd, Relation\AddUsersConfig $config): void
368 {
369 return;
370 }
371
372 protected function sendDescriptionMessage(?int $authorId = null): void
373 {
374 return;
375 }
376
378 {
379 return;
380 }
381
382 protected function sendGreetingMessage(?int $authorId = null)
383 {
384 $messageText = Loc::getMessage('IM_COMMENT_CREATE_V2');
385
387 'MESSAGE_TYPE' => $this->getType(),
388 'TO_CHAT_ID' => $this->getChatId(),
389 'FROM_USER_ID' => 0,
390 'MESSAGE' => $messageText,
391 'SYSTEM' => 'Y',
392 'PUSH' => 'N',
393 'SKIP_PULL' => 'Y', // todo: remove
394 'SKIP_COUNTER_INCREMENTS' => 'Y',
395 'PARAMS' => [
396 'NOTIFY' => 'N',
397 ],
398 ]);
399 }
400
401 protected function sendBanner(?int $authorId = null): void
402 {
403 return;
404 }
405
406 protected static function mirrorDataEntityFields(): array
407 {
408 $result = parent::mirrorDataEntityFields();
409 $result['PARENT_MESSAGE'] = [
410 'set' => 'setParentMessage',
411 'skipSave' => true,
412 ];
413 $result['PARENT_CHAT'] = [
414 'set' => 'setParentChat',
415 'skipSave' => true,
416 ];
417
418 return $result;
419 }
420
421 protected function prepareParams(array $params = []): Result
422 {
423 $result = new Result();
424
425 if (!isset($params['PARENT_CHAT']) || !$params['PARENT_CHAT'] instanceof Chat)
426 {
427 return $result->addError(new ChatError(ChatError::WRONG_PARENT_CHAT));
428 }
429
430 if (!isset($params['PARENT_MESSAGE']) || !$params['PARENT_MESSAGE'] instanceof Message)
431 {
433 }
434
435 $params['PARENT_ID'] = $params['PARENT_CHAT']->getId();
436 $params['PARENT_MID'] = $params['PARENT_MESSAGE']->getId();
437 $params['USERS'][] = $params['PARENT_MESSAGE']->getAuthorId();
438
439 return parent::prepareParams($params);
440 }
441
442 protected static function createInternal(Message $message): Result
443 {
444 $result = new Result();
445
446 $parentChat = $message->getChat();
447
448 $addResult = ChatFactory::getInstance()->addChat([
449 'TYPE' => self::IM_TYPE_COMMENT,
450 'PARENT_CHAT' => $parentChat,
451 'PARENT_MESSAGE' => $message,
452 'OWNER_ID' => $parentChat->getAuthorId(),
453 'AUTHOR_ID' => $parentChat->getAuthorId(),
454 ]);
455
456 if (!$addResult->isSuccess())
457 {
458 return $addResult;
459 }
460
462 $chat = $addResult->getResult()['CHAT'];
463 $chat->parentMessage = $message;
464 $chat->sendPushChatCreate();
465
466 return $result->setResult($chat);
467 }
468
469 protected static function getIdByMessage(Message $message): int
470 {
471 $row = ChatTable::query()
472 ->setSelect(['ID'])
473 ->where('PARENT_ID', $message->getChatId())
474 ->where('PARENT_MID', $message->getId())
475 ->fetch() ?: []
476 ;
477
478 return (int)($row['ID'] ?? 0);
479 }
480
482 {
483 $chatId = $messages->getCommonChatId();
484
485 if (!isset($chatId))
486 {
487 return [];
488 }
489
490 $result = ChatTable::query()
491 ->setSelect(['ID', 'PARENT_MID'])
492 ->where('PARENT_ID', $chatId)
493 ->whereIn('PARENT_MID', $messages->getIds())
494 ->fetchAll()
495 ;
496
497 return $result;
498 }
499
500 protected function sendPushChatCreate(): void
501 {
502 Event::add(
503 $this->getParentChat()->getRelations()->getUserIds(),
504 [
505 'module_id' => 'im',
506 'command' => 'chatCreate',
507 'params' => [
508 'id' => $this->getId(),
509 'parentChatId' => $this->getParentChatId(),
510 'parentMessageId' => $this->getParentMessageId(),
511 ],
512 'extra' => \Bitrix\Im\Common::getPullExtra(),
513 ]
514 );
515 }
516
517 protected function checkAccessInternal(int $userId): Result
518 {
519 return $this->getParentMessage()?->checkAccess($userId)
520 ?? (new Result())->addError(new ChatError(ChatError::ACCESS_DENIED))
521 ;
522 }
523
524 protected function addIndex(): Chat
525 {
526 return $this;
527 }
528
529 protected function updateIndex(): Chat
530 {
531 return $this;
532 }
533
534 protected static function getLockName(int $messageId): string
535 {
536 return 'com_create_' . $messageId;
537 }
538
539 protected function needToSendMessageUserDelete(): bool
540 {
541 return false;
542 }
543}
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
static getType($chatData, bool $camelCase=true)
Определения chat.php:45
static getPullExtra()
Определения common.php:127
static raiseChat(\Bitrix\Im\V2\Chat $chat, RelationCollection $relations, ?DateTime $lastActivity=null)
Определения recent.php:1582
const CREATION_ERROR
Определения ChatError.php:23
const ACCESS_DENIED
Определения ChatError.php:19
const NOT_FOUND
Определения ChatError.php:20
const WRONG_PARENT_CHAT
Определения ChatError.php:17
const WRONG_PARENT_MESSAGE
Определения ChatError.php:18
static getInstance()
Определения ChatFactory.php:37
const LOCK_TIMEOUT
Определения CommentChat.php:28
checkAccessInternal(int $userId)
Определения CommentChat.php:517
getValidUsersToAdd(array $userIds)
Определения CommentChat.php:241
onAfterMessageUpdate(Message $message)
Определения CommentChat.php:166
sendMessageUsersAdd(array $usersToAdd, Relation\AddUsersConfig $config)
Определения CommentChat.php:367
subscribe(bool $subscribe=true, ?int $userId=null)
Определения CommentChat.php:218
needToSendMessageUserDelete()
Определения CommentChat.php:539
static create(Message $message)
Определения CommentChat.php:94
static getChatsByMessages(MessageCollection $messages)
Определения CommentChat.php:64
getParentRelationsForRaiseChat()
Определения CommentChat.php:211
sendBanner(?int $authorId=null)
Определения CommentChat.php:401
sendSubscribePush(bool $subscribe, array $userIds)
Определения CommentChat.php:281
const EXTRANET_CAN_SEE_HISTORY
Определения CommentChat.php:29
subscribeUsers(bool $subscribe=true, array $userIds=[], ?int $lastId=null)
Определения CommentChat.php:248
static getIdByMessage(Message $message)
Определения CommentChat.php:469
static getIdsByMessages(MessageCollection $messages)
Определения CommentChat.php:481
prepareParams(array $params=[])
Определения CommentChat.php:421
setParentChat(?Chat $chat)
Определения CommentChat.php:339
sendMessageUserDelete(int $userId, Relation\DeleteUserConfig $config)
Определения CommentChat.php:377
onAfterMessageSend(Message $message, SendingService $sendingService)
Определения CommentChat.php:171
getMentionService(SendingConfig $config)
Определения CommentChat.php:89
sendDescriptionMessage(?int $authorId=null)
Определения CommentChat.php:372
Message $parentMessage
Определения CommentChat.php:31
setParentMessage(?Message $message)
Определения CommentChat.php:353
createRelation(int $userId, Relation\AddUsersConfig $config)
Определения CommentChat.php:327
sendGreetingMessage(?int $authorId=null)
Определения CommentChat.php:382
static getLockName(int $messageId)
Определения CommentChat.php:534
canUserAutoJoin(?int $userId=null)
Определения CommentChat.php:130
filterUsersToMention(array $userIds)
Определения CommentChat.php:185
getRelationsForSendMessage()
Определения CommentChat.php:206
static mirrorDataEntityFields()
Определения CommentChat.php:406
join(bool $withMessage=true)
Определения CommentChat.php:135
isCounterIncrementAllowed()
Определения CommentChat.php:161
static insert(Message $message)
Определения LastMessages.php:21
getAuthorId()
Определения Message.php:835
getUserIdsFromMention()
Определения Message.php:1695
static getInstance()
Определения application.php:98
Определения result.php:20
static Add($arFields)
Определения im_message.php:28
if(!\Bitrix\Main\Loader::includeModule('clouds')) $lastId
Определения sync.php:68
</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
Определения Uuid.php:3
$message
Определения payment.php:8
$config
Определения quickway.php:69
$messages
Определения template.php:8
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799