1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Messenger.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Service;
4
5use Bitrix\Im\V2\Application;
6use Bitrix\Im\V2\Chat;
7use Bitrix\Im\V2\Chat\GroupChat;
8use Bitrix\Im\V2\Chat\PrivateChat;
9use Bitrix\Im\V2\Chat\EntityChat;
10use Bitrix\Im\V2\Chat\NullChat;
11use Bitrix\Im\V2\Link\Calendar\CalendarItem;
12use Bitrix\Im\V2\Link\Calendar\CalendarService;
13use Bitrix\Im\V2\Link\Task\TaskService;
14use Bitrix\Im\V2\Entity\Task\TaskItem;
15use Bitrix\Im\V2\Chat\ChatFactory;
16use Bitrix\Im\V2\Common\ContextCustomer;
17use Bitrix\Im\V2\Message;
18use Bitrix\Im\V2\Message\Delete\DeleteService;
19use Bitrix\Im\V2\Message\Delete\DeletionMode;
20use Bitrix\Im\V2\Message\MessageError;
21use Bitrix\Im\V2\MessageCollection;
22use Bitrix\Im\V2\Permission\Action;
23use Bitrix\Main\Loader;
24use Bitrix\Main\Result;
25use Bitrix\Tasks\Internals\TaskObject;
26
28{
29 use ContextCustomer;
30
31 private const INTRANET_MENU_ID = 'menu_im_messenger';
32
33 private Application $application;
34
39 public static function getInstance(): self
40 {
41 return Locator::getMessenger();
42 }
43
44 public function checkAccessibility(): \Bitrix\Im\V2\Result
45 {
46 $result = new \Bitrix\Im\V2\Result();
47
48 if (!$this->isPullEnabled())
49 {
51 }
52
53 if (!$this->isEnabled())
54 {
56 }
57
58 return $result;
59 }
60
61 public function getApplication(): Application
62 {
63 $this->application ??= new Application();
64
65 return $this->application;
66 }
67
68 //region Chats
69
75 public function getPrivateChat(int $fromUserId, int $toUserId): Chat
76 {
77 $chatFactory = ChatFactory::getInstance();
78 $chat = $chatFactory
79 ->setContext($this->context)
80 ->getPrivateChat($fromUserId, $toUserId)
81 ;
82
83 if (!$chat)
84 {
85 return new NullChat();
86 }
87
88 return $chat;
89 }
90
91 public function getEntityChat(string $entityType, string $entityId): Chat
92 {
93 return ChatFactory::getInstance()->getEntityChat($entityType, $entityId);
94 }
95
96 public function getGeneralChat(): Chat
97 {
98 return Chat\GeneralChat::get();
99 }
100
105 public function getChat(int $chatId): Chat
106 {
107 return Chat\ChatFactory::getInstance()->getChatById($chatId);
108 }
109
115 public function getChats(array $chatIds, bool $checkAccess = false): array
116 {
117 $chats = [];
118 foreach ($chatIds as $chatId)
119 {
120 $chats[$chatId] = Chat::getInstance($chatId);
121 }
122
123 if (empty($chats) || !$checkAccess)
124 {
125 return $chats;
126 }
127
128 $currentUserId = $this->getContext()->getUserId();
129 if (!$currentUserId)
130 {
131 return [];
132 }
133
134 return array_filter($chats, static fn (Chat $chat) => $chat->checkAccess($currentUserId)->isSuccess());
135 }
136
137 //endregion
138
139 //region Messages
140
144 public function createMessage($source = null): Message
145 {
146 if (is_string($source))
147 {
148 $source = ['MESSAGE' => $source];
149 }
150
151 return new Message($source);
152 }
153
155 {
156 return \Bitrix\Im\V2\Chat\ChatFactory::getInstance()->addChat($fields);
157 }
158
166 public function deleteMessage(Message $message, int $mode = 0): Result
167 {
168 $result = new Result();
169
170 $deleteService = DeleteService::getInstanceByMessage($message);
171 $deleteService->setMode(DeletionMode::tryFrom($mode));
172 $deleteService->delete();
173
174 return $result;
175 }
176
185 {
186 $deleteService = DeleteService::getInstanceByMessage($message);
187 if ($deleteService->canDelete($message->getId()) !== DeletionMode::Complete)
188 {
189 return (new Result())->addError(new MessageError(MessageError::ACCESS_DENIED));
190 }
191
193 }
194
202 public function updateMessage(Message $message, ?string $messageText): Result
203 {
204 $updateService = new Message\Update\UpdateService($message);
205 return $updateService->update($messageText);
206 }
207
208 //endregion
209
210 //region Task processing
211
212 public function registerTask(int $chatId, int $messageId, TaskObject $task): void
213 {
214 try
215 {
216 $taskService = new TaskService();
217 $chat = Chat::getInstance($chatId);
218
219 if (!$chat->checkAccess()->isSuccess() || !$chat->canDo(Action::CreateTask))
220 {
221 return;
222 }
223
224 $taskService->registerTask($chatId, $messageId, TaskItem::initByTaskObject($task));
225 }
226 catch (\Bitrix\Main\SystemException $exception)
227 {
228 //todo: log
229 }
230 }
231
238 public function unregisterTask(array $taskData, bool $saveDelete): void
239 {
240 try
241 {
242 $taskService = new TaskService();
243
244 $taskService->unregisterTaskByEntity(TaskItem::initByRow($taskData), $saveDelete);
245 }
246 catch (\Bitrix\Main\SystemException $exception)
247 {
248 //todo: log
249 }
250 }
251
257 public function deleteTask(int $taskId): void
258 {
259 try
260 {
261 $taskService = new TaskService();
262
263 $taskService->deleteLinkByTaskId($taskId);
264 }
265 catch (\Bitrix\Main\SystemException $exception)
266 {
267 //todo: log
268 }
269 }
270
271 public function updateTask(TaskObject $task): void
272 {
273 try
274 {
275 $taskService = new TaskService();
276
277 $taskService->updateTask(TaskItem::initByTaskObject($task));
278 }
279 catch (\Bitrix\Main\SystemException $exception)
280 {
281 //todo: log
282 }
283 }
284
285 //endregion
286
287 //region Calendar processing
288
289 public static function updateCalendar(int $eventId, array $entryFields): void
290 {
291 if ($entryFields['ID'] !== $entryFields['PARENT_ID'])
292 {
293 return;
294 }
295
296 $calendarService = new CalendarService();
297 $calendar = CalendarItem::getByCalendarId($eventId);
298 if ($calendar === null)
299 {
300 return;
301 }
302 $calendarService->updateCalendar($calendar);
303 }
304
305 public static function unregisterCalendar(int $eventId, array $entry): void
306 {
307 if ($entry['ID'] !== $entry['PARENT_ID'])
308 {
309 return;
310 }
311
312 $calendarService = new CalendarService();
313 $calendar = CalendarItem::getByCalendarId($eventId, false);
314 if ($calendar === null)
315 {
316 return;
317 }
318 $calendarService->unregisterCalendar($calendar);
319 }
320
321 //endregion
322
323 private function isPullEnabled(): bool
324 {
325 return \CModule::IncludeModule("pull") && \CPullOptions::GetQueueServerStatus();
326 }
327
328 private function isEnabled(): bool
329 {
330 if (
331 Loader::includeModule('intranet')
332 && method_exists(\Bitrix\Intranet\Settings\Tools\ToolsManager::class, 'checkAvailabilityByMenuId')
333 )
334 {
335 return \Bitrix\Intranet\Settings\Tools\ToolsManager::getInstance()
336 ->checkAvailabilityByMenuId(static::INTRANET_MENU_ID)
337 ;
338 }
339
340 return true;
341 }
342}
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
static getInstance()
Определения ChatFactory.php:37
static get()
Определения GeneralChat.php:108
static initByRow(array $row)
Определения TaskItem.php:76
static initByTaskObject(TaskObject $taskObject)
Определения TaskItem.php:101
static disappearMessage(Message $message, int $delay)
Определения DisappearService.php:64
static getMessenger()
Определения Locator.php:15
static unregisterCalendar(int $eventId, array $entry)
Определения Messenger.php:305
deleteMessage(Message $message, int $mode=0)
Определения Messenger.php:166
deleteTask(int $taskId)
Определения Messenger.php:257
unregisterTask(array $taskData, bool $saveDelete)
Определения Messenger.php:238
static updateCalendar(int $eventId, array $entryFields)
Определения Messenger.php:289
updateTask(TaskObject $task)
Определения Messenger.php:271
getPrivateChat(int $fromUserId, int $toUserId)
Определения Messenger.php:75
createChat(array $fields)
Определения Messenger.php:154
getChats(array $chatIds, bool $checkAccess=false)
Определения Messenger.php:115
updateMessage(Message $message, ?string $messageText)
Определения Messenger.php:202
registerTask(int $chatId, int $messageId, TaskObject $task)
Определения Messenger.php:212
getChat(int $chatId)
Определения Messenger.php:105
getEntityChat(string $entityType, string $entityId)
Определения Messenger.php:91
static getInstance()
Определения Messenger.php:39
disappearMessage(Message $message, int $hours)
Определения Messenger.php:184
createMessage($source=null)
Определения Messenger.php:144
static getInstance()
Определения application.php:98
Определения result.php:20
static includeModule($moduleName)
Определения loader.php:67
$hours
Определения cron_html_pages.php:15
</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
$application
Определения bitrix.php:23
Определения Uuid.php:3
Определения ActionUuid.php:3
$entityId
Определения payment.php:4
$message
Определения payment.php:8
$fields
Определения yandex_run.php:501