1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
DialogIds.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Sync\Entity;
4
5use Bitrix\Im\Model\RelationTable;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Common\ContextCustomer;
8use Bitrix\Im\V2\Sync\Entity;
9
10class DialogIds implements Entity
11{
12 use ContextCustomer;
13
14 private Chats $chats;
15 private array $dialogIds = [];
16 private array $chatsWithoutDialogIds = [];
17
18 public function __construct(Chats $chats)
19 {
20 $this->chats = $chats;
21 }
22
23 public function load(array $restData): self
24 {
25 $this
26 ->fillChatIds($restData)
27 ->loadFromFetchedChats($restData['chats'] ?? [])
28 ->loadFromNonPrivateChats()
29 ->loadFromPrivateChat()
30 ;
31
32 return $this;
33 }
34
35 private function fillChatIds($restData): self
36 {
37 foreach ($restData['messages'] as $message)
38 {
39 $this->chatsWithoutDialogIds[(int)$message['chat_id']] = (int)$message['chat_id'];
40 }
41
42 foreach ($restData['pinSync']['addedPins'] as $pin)
43 {
44 $this->chatsWithoutDialogIds[(int)$pin['chatId']] = (int)$pin['chatId'];
45 }
46
47 foreach ($restData['chats'] as $chat)
48 {
49 $this->chatsWithoutDialogIds[(int)$chat['id']] = (int)$chat['id'];
50 }
51
52 foreach ($this->chats->getShortInfoChatIds() as $chatId)
53 {
54 $this->chatsWithoutDialogIds[$chatId] = $chatId;
55 }
56
57 return $this;
58 }
59
60 private function loadFromFetchedChats(array $chats): self
61 {
62 if (!$this->needContinue())
63 {
64 return $this;
65 }
66
67 foreach ($chats as $chat)
68 {
69 $this->add($chat['id'], $chat['dialogId']);
70 }
71
72 return $this;
73 }
74
75 private function loadFromNonPrivateChats(): self
76 {
77 if (!$this->needContinue())
78 {
79 return $this;
80 }
81
82 foreach ($this->chatsWithoutDialogIds as $chatId)
83 {
84 $chat = Chat::getInstance($chatId);
85 if ($chat instanceof Chat\NullChat)
86 {
87 unset($this->chatsWithoutDialogIds[$chatId]);
88 continue;
89 }
90 if ($chat->getType() !== Chat::IM_TYPE_PRIVATE)
91 {
92 $this->add($chat->getId(), $chat->getDialogId());
93 }
94 }
95
96 return $this;
97 }
98
99 private function loadFromPrivateChat(): self
100 {
101 if (!$this->needContinue())
102 {
103 return $this;
104 }
105
106 $result = RelationTable::query()
107 ->setSelect(['CHAT_ID', 'USER_ID'])
108 ->whereIn('CHAT_ID', $this->chatsWithoutDialogIds)
109 ->where('MESSAGE_TYPE', Chat::IM_TYPE_PRIVATE)
110 ->whereNot('USER_ID', $this->getContext()->getUserId())
111 ->fetchAll()
112 ;
113
114 foreach ($result as $row)
115 {
116 $this->add((int)$row['CHAT_ID'], $row['USER_ID']);
117 }
118
119 return $this;
120 }
121
122 private function needContinue(): bool
123 {
124 return !empty($this->chatsWithoutDialogIds);
125 }
126
127 private function add(int $chatId, string $dialogId): void
128 {
129 if (isset($this->dialogIds[$chatId]))
130 {
131 return;
132 }
133
134 $this->dialogIds[$chatId] = $dialogId;
135 unset($this->chatsWithoutDialogIds[$chatId]);
136 }
137
138 public static function getRestEntityName(): string
139 {
140 return 'dialogIds';
141 }
142
143 public function toRestFormat(array $option = []): ?array
144 {
145 return $this->dialogIds;
146 }
147}
static getRestEntityName()
Определения DialogIds.php:138
load(array $restData)
Определения DialogIds.php:23
toRestFormat(array $option=[])
Определения DialogIds.php:143
__construct(Chats $chats)
Определения DialogIds.php:18
static getInstance()
Определения application.php:98
</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
int $chatId
Определения Param.php:36
Определения ufield.php:9
$message
Определения payment.php:8
$option
Определения options.php:1711