1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
BaseController.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Controller;
4
5use Bitrix\Im\Dialog;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Chat\CommentChat;
8use Bitrix\Im\V2\Controller\Filter\ActionUuidHandler;
9use Bitrix\Im\V2\Controller\Filter\AuthorizationPrefilter;
10use Bitrix\Im\V2\Controller\Filter\AutoJoinToChat;
11use Bitrix\Im\V2\Controller\Filter\CheckChatAccess;
12use Bitrix\Im\V2\Controller\Filter\SameChatMessageFilter;
13use Bitrix\Im\V2\Controller\Filter\UpdateStatus;
14use Bitrix\Im\V2\Link\Pin\PinCollection;
15use Bitrix\Im\V2\Message;
16use Bitrix\Im\V2\Message\MessageError;
17use Bitrix\Im\V2\Message\MessageService;
18use Bitrix\Im\V2\MessageCollection;
19use Bitrix\Im\V2\Rest\RestAdapter;
20use Bitrix\Im\V2\Rest\RestConvertible;
21use Bitrix\Main\Application;
22use Bitrix\Main\Engine\ActionFilter\CloseSession;
23use Bitrix\Main\Engine\AutoWire\ExactParameter;
24use Bitrix\Main\Engine\Controller;
25use Bitrix\Main\Engine\Response\Converter;
26use Bitrix\Main\Text\Encoding;
27use Bitrix\Main\Type\ParameterDictionary;
28
29abstract class BaseController extends Controller
30{
31 protected const MAX_LIMIT = 200;
32 protected const DEFAULT_LIMIT = 50;
33 protected const MAX_MESSAGES_COUNT = 100;
34
35 public function getAutoWiredParameters()
36 {
37 return [
39 Chat::class,
40 'chat',
41 function($className, string $dialogId) {
42 $chatId = Dialog::getChatId($dialogId);
43
44 return Chat::getInstance((int)$chatId);
45 }
46 ),
48 Chat::class,
49 'chat',
50 function($className, int $chatId) {
51 return Chat::getInstance($chatId);
52 }
53 ),
55 Message::class,
56 'message',
57 function ($className, int $messageId) {
58 return new Message($messageId);
59 }
60 ),
62 MessageCollection::class,
63 'messages',
64 function ($className, array $messageIds) {
65 return $this->getMessagesByIds($messageIds);
66 }
67 ),
69 Message::class,
70 'message',
71 function ($className, int $commentChatId) {
72 return new Message(\Bitrix\Im\V2\Chat::getInstance($commentChatId)->getParentMessageId());
73 }
74 ),
76 \Bitrix\Im\V2\Chat::class,
77 'chat',
78 function($className, int $postId, string $createIfNotExists = 'N') {
79 return $this->getChatByPostId($postId, $createIfNotExists === 'Y');
80 }
81 ),
82 ];
83 }
84
85 protected function getDefaultPreFilters()
86 {
87 return array_merge(
88 [
90 new UpdateStatus(),
91 ],
92 parent::getDefaultPreFilters(),
93 [
94 new CloseSession(true),
96 new CheckChatAccess(),
98 new AutoJoinToChat(),
99 ]
100 );
101 }
102
103 protected function getLimit(int $limit): int
104 {
105 return $limit > 0 && $limit <= static::MAX_LIMIT ? $limit : static::DEFAULT_LIMIT;
106 }
107
108 protected function toRestFormat(RestConvertible ...$entities): array
109 {
110 return (new RestAdapter(...$entities))->toRestFormat();
111 }
112
113 protected function load(\Bitrix\Im\V2\Chat $chat, int $messageLimit, int $pinLimit, bool $ignoreMark = false, ?Message $targetMessage = null): array
114 {
115 $messageLimit = $this->getLimit($messageLimit);
116 $pinLimit = $this->getLimit($pinLimit);
117 $messageService = new MessageService($targetMessage ?? $chat->getLoadContextMessage($ignoreMark));
118 $messages = $messageService->getMessageContext($messageLimit, Message::REST_FIELDS)->getResult();
119 $pins = PinCollection::find(
120 ['CHAT_ID' => $chat->getChatId(), 'START_ID' => $chat->getStartId() ?: null],
121 ['ID' => 'DESC'],
122 $pinLimit
123 );
124
125 $restAdapter = new RestAdapter($chat, $messages, $pins);
126
127 $rest = $restAdapter->toRestFormat();
128
129 return $messageService->fillContextPaginationData($rest, $messages, $messageLimit);
130 }
131
132 protected function toRestFormatWithPaginationData(array $entities, int $needCount, int $realCount): array
133 {
134 $rest = (new RestAdapter(...$entities))->toRestFormat();
135 $rest['hasNextPage'] = $realCount >= $needCount;
136
137 return $rest;
138 }
139
140 public static function recursiveWhiteList($fields, $whiteList, bool $sanitizeOnly = false)
141 {
142 $data = [];
143 $converter = new Converter(Converter::TO_SNAKE | Converter::TO_UPPER);
144 foreach ($fields as $field => $value)
145 {
146 if (is_array($value))
147 {
148 $data[$converter->process($field)] = self::recursiveWhiteList($value, $whiteList[$field], true);
149 }
150 elseif ((is_array($whiteList) && in_array($field, $whiteList)) || $sanitizeOnly)
151 {
152 $data[$converter->process($field)] = $value;
153 }
154 }
155
156 return $data;
157 }
158
159 //todo: think about recursion in method.
160 protected function checkWhiteList(array $fields, array $whiteList): array
161 {
162 $filteredFields = [];
163
164 foreach ($whiteList as $allowedField)
165 {
166 if (isset($fields[$allowedField]))
167 {
168 $filteredFields[$allowedField] = $fields[$allowedField];
169 }
170 }
171
172 return $filteredFields;
173 }
174
175 protected function getChatByPostId(int $postId, bool $createIfNotExists): ?Chat
176 {
177 $message = new Message($postId);
178
179 if (!$message->checkAccess()->isSuccess())
180 {
181 $this->addError(new MessageError(MessageError::ACCESS_DENIED));
182
183 return null;
184 }
185
186 $result = CommentChat::get($message, $createIfNotExists);
187
188 if (!$result->isSuccess())
189 {
190 $this->addErrors($result->getErrors());
191
192 return null;
193 }
194
195 return $result->getResult();
196 }
197
198 protected function getMessagesByIds(array $ids): ?MessageCollection
199 {
200 if (count($ids) > static::MAX_MESSAGES_COUNT)
201 {
202 $this->addError(new MessageError(MessageError::TOO_MANY_MESSAGES));
203
204 return null;
205 }
206 $ids = array_map('intval', $ids);
207
208 $messageCollection = new MessageCollection($ids);
209
210 if ($messageCollection->isEmpty())
211 {
212 $this->addError(new MessageError(MessageError::NOT_FOUND));
213
214 return null;
215 }
216
217 return $messageCollection;
218 }
219
220 protected function convertCharToBool(string $char, bool $default = false): bool
221 {
222 if ($char === 'Y')
223 {
224 return true;
225 }
226 if ($char === 'N')
227 {
228 return false;
229 }
230
231 return $default;
232 }
233
234 protected function getRawValue(string $key)
235 {
236 return $this->prepareRawValue($this->request->getPostList(), $key)
237 ?? $this->prepareRawValue($this->request->getQueryList(), $key)
238 ?? null
239 ;
240 }
241
242 private function prepareRawValue(ParameterDictionary $list, string $key)
243 {
244 $rawData = $list->toArrayRaw();
245 if (isset($rawData[$key]))
246 {
247 return $rawData[$key];
248 }
249
250 $data = $list->toArray();
251
252 return $data[$key] ?? null;
253 }
254
255 protected function prepareFields(array $fields, array $whiteList): array
256 {
257 $converter = new Converter(Converter::TO_SNAKE | Converter::TO_UPPER | Converter::KEYS);
258 $fields = $converter->process($fields);
259
260 return $this->checkWhiteList($fields, $whiteList);
261 }
262}
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
static getChatId($dialogId, $userId=null)
Определения dialog.php:93
static get(Message $message, bool $createIfNotExists=true)
Определения CommentChat.php:33
getChatByPostId(int $postId, bool $createIfNotExists)
Определения BaseController.php:175
toRestFormat(RestConvertible ... $entities)
Определения BaseController.php:108
convertCharToBool(string $char, bool $default=false)
Определения BaseController.php:220
load(\Bitrix\Im\V2\Chat $chat, int $messageLimit, int $pinLimit, bool $ignoreMark=false, ?Message $targetMessage=null)
Определения BaseController.php:113
prepareFields(array $fields, array $whiteList)
Определения BaseController.php:255
toRestFormatWithPaginationData(array $entities, int $needCount, int $realCount)
Определения BaseController.php:132
static recursiveWhiteList($fields, $whiteList, bool $sanitizeOnly=false)
Определения BaseController.php:140
checkWhiteList(array $fields, array $whiteList)
Определения BaseController.php:160
static getInstance()
Определения application.php:98
$data['IS_AVAILABLE']
Определения .description.php:13
</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
Определения ActionUuid.php:3
$message
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$messages
Определения template.php:8
$fields
Определения yandex_run.php:501