Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
UrlService.php
1<?php
2
4
6use Bitrix\Im\V2\Common\ContextCustomer;
12
14{
15 use ContextCustomer;
16
17 protected const ADD_URL_EVENT = 'urlAdd';
18 protected const DELETE_URL_EVENT = 'urlDelete';
19
20 private bool $isBackgroundMode = true;
21 private int $quotaOfFetchMetadata = 5;
22
23 public function getCount(int $chatId, ?int $startId = null): int
24 {
25 $filter = Query::filter()->where('CHAT_ID', $chatId);
26
27 if (isset($startId) && $startId > 0)
28 {
29 $filter->where('MESSAGE_ID', '>=', $startId);
30 }
31
32 return LinkUrlTable::getCount($filter);
33 }
34
39 public function saveUrlsFromMessage(Message $message): Result
40 {
41 if ($this->isBackgroundMode)
42 {
43 Application::getInstance()->addBackgroundJob(fn () => $this->saveUrlsFromMessageInternal($message));
44
45 return new Result();
46 }
47
48 return $this->saveUrlsFromMessageInternal($message);
49 }
50
55 public function updateUrlsFromMessage(Message $message): Result
56 {
57 $result = new Result();
58
59 $deleteResult = $this->deleteUrlsByMessage($message);
60
61 if (!$deleteResult->isSuccess())
62 {
63 return $result->addErrors($deleteResult->getErrors());
64 }
65
66 $saveResult = $this->saveUrlsFromMessage($message);
67
68 if (!$saveResult->isSuccess())
69 {
70 return $result->addErrors($saveResult->getErrors());
71 }
72
73 return $result;
74 }
75
76 public function deleteUrlsByMessage(Message $message): Result
77 {
78 $result = new Result();
79
80 $urls = $this->getUrlsByMessage($message);
81
82 if (count($urls) === 0)
83 {
84 return $result;
85 }
86
87 return $this->deleteUrls($urls);
88 }
89
90 public function deleteUrls(UrlCollection $urls): Result
91 {
92 $deleteResult = $urls->delete();
93
94 if (!$deleteResult->isSuccess())
95 {
96 return $deleteResult;
97 }
98
99 foreach ($urls as $url)
100 {
102 ->setContext($this->context)
103 ->sendIdOnly($url, UrlService::DELETE_URL_EVENT, ['CHAT_ID' => $url->getChatId()])
104 ;
105 }
106
107 return $deleteResult;
108 }
109
110 public function setBackgroundMode(bool $isBackgroundMode): self
111 {
112 $this->isBackgroundMode = $isBackgroundMode;
113
114 return $this;
115 }
116
117 public function setQuotaOfFetchMetadata(int $quota): self
118 {
119 $this->quotaOfFetchMetadata = $quota;
120
121 return $this;
122 }
123
124 protected function saveUrlsFromMessageInternal(Message $message): Result
125 {
126 $result = new Result();
127 $urlCollection = $this->initUrlsByMessage($message);
128
129 if ($urlCollection->hasUnsaved())
130 {
131 $saveResult = $this->saveUrls($urlCollection);
132 if ($saveResult->isSuccess())
133 {
134 $this->sendAddPush($urlCollection);
135 }
136 else
137 {
138 $result->addErrors($saveResult->getErrors());
139 }
140 }
141
142 return $result;
143 }
144
145 protected function saveUrls(UrlCollection $urls): Result
146 {
147 return $urls->save();
148 }
149
150 protected function getUrlsByMessage(Message $message): UrlCollection
151 {
152 $urlEntities = LinkUrlTable::query()
153 ->setSelect(['*'])
154 ->where('MESSAGE_ID', $message->getMessageId())
155 ->fetchCollection();
156
157 return (new UrlCollection($urlEntities))->fillMetadata();
158 }
159
160 protected function initUrlsByMessage(Message $message): UrlCollection
161 {
162 $urls = \Bitrix\Im\V2\Entity\Url\UrlItem::getUrlsFromText($message->getMessage());
163 $urlCollection = new \Bitrix\Im\V2\Entity\Url\UrlCollection();
164 $countUrlsWithMetadata = 0;
165
166 foreach ($urls as $url)
167 {
168 $withFetchMetadata = $countUrlsWithMetadata < $this->quotaOfFetchMetadata;
169 $urlCollection[] = new \Bitrix\Im\V2\Entity\Url\UrlItem($url, $withFetchMetadata);
170 $countUrlsWithMetadata++;
171 }
172
173 return UrlCollection::linkEntityToMessage($urlCollection, $message);
174 }
175
176 protected function sendAddPush(UrlCollection $urls): void
177 {
178 foreach ($urls as $url)
179 {
180 $recipient = $url->getEntity()->getRichData()->getAllowedUsers();
181 if ($recipient === null)
182 {
184 ->setContext($this->context)
185 ->sendFull($url, self::ADD_URL_EVENT, ['CHAT_ID' => $url->getChatId()])
186 ;
187 }
188 else
189 {
191 ->setContext($this->context)
192 ->sendFull($url, self::ADD_URL_EVENT, ['RECIPIENT' => $recipient])
193 ;
194 }
195 }
196 }
197}