Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PushService.php
1<?php
2
4
8
10{
11 private const ADD_REACTION_EVENT = 'addReaction';
12 private const DELETE_REACTION_EVENT = 'deleteReaction';
13
14 public function add(ReactionItem $reaction): void
15 {
16 $this->send(self::ADD_REACTION_EVENT, $reaction);
17 }
18
19 public function delete(ReactionItem $reaction): void
20 {
21 $this->send(self::DELETE_REACTION_EVENT, $reaction);
22 }
23
24 private function send(string $eventName, ReactionItem $reaction): void
25 {
26 $converter = new Converter(Converter::KEYS | Converter::VALUES | Converter::TO_LOWER | Converter::LC_FIRST);
27 $messageId = $reaction->getMessageId();
28 $reactionMessage = (new ReactionMessages([$messageId], false))->getReactionMessage($messageId);
29 $params = [
30 'userId' => $reaction->getUserId(),
31 'reaction' => $converter->process($reaction->getReaction()),
32 'actualReactions' => (new RestAdapter($reactionMessage))->toRestFormat(),
33 'dialogId' => Chat::getInstance($reaction->getChatId())->getDialogId(),
34 ];
35 $chat = Chat::getInstance($reaction->getChatId());
36
37 if ($chat instanceof Chat\PrivateChat)
38 {
39 $this->sendToPrivateChat($params, $eventName, $reaction);
40
41 return;
42 }
43
44 \Bitrix\Pull\Event::add($this->getRecipient($reaction), [
45 'module_id' => 'im',
46 'command' => $eventName,
47 'params' => $params,
48 'extra' => \Bitrix\Im\Common::getPullExtra()
49 ]);
50
51 if ($chat->getType() === Chat::IM_TYPE_OPEN || $chat->getType() === Chat::IM_TYPE_OPEN_LINE)
52 {
53 \CPullWatch::AddToStack('IM_PUBLIC_'.$chat->getChatId(), Array(
54 'module_id' => 'im',
55 'command' => $eventName,
56 'params' => $params,
57 'extra' => \Bitrix\Im\Common::getPullExtra()
58 ));
59 }
60 }
61
62 private function sendToPrivateChat(array $params, string $eventName, ReactionItem $reaction): void
63 {
65 $chat = Chat::getInstance($reaction->getChatId());
66 foreach ($this->getRecipient($reaction) as $recipient)
67 {
68 $params['dialogId'] = $chat->getCompanion($recipient)->getId();
69 \Bitrix\Pull\Event::add($recipient, [
70 'module_id' => 'im',
71 'command' => $eventName,
72 'params' => $params,
73 'extra' => \Bitrix\Im\Common::getPullExtra()
74 ]);
75 }
76 }
77
78 private function getRecipient(ReactionItem $reaction): array
79 {
80 $userIds = [];
81 $relations = Chat::getInstance($reaction->getChatId())->getRelations();
82
83 foreach ($relations as $relation)
84 {
85 if ($relation->getStartId() <= $reaction->getMessageId())
86 {
87 $userIds[] = $relation->getUserId();
88 }
89 }
90
91 return $userIds;
92 }
93}
static getPullExtra()
Definition common.php:128
const IM_TYPE_OPEN_LINE
Definition Chat.php:58
const IM_TYPE_OPEN
Definition Chat.php:61