Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Messenger.php
1<?php
2
4
15use Bitrix\Im\V2\Common\ContextCustomer;
21use Bitrix\Tasks\Internals\TaskObject;
22
24{
25 use ContextCustomer;
26
27 private const INTRANET_MENU_ID = 'menu_im_messenger';
28
33 public static function getInstance(): self
34 {
35 return Locator::getMessenger();
36 }
37
38 public function checkAccessibility(): \Bitrix\Im\V2\Result
39 {
40 $result = new \Bitrix\Im\V2\Result();
41
42 if (!$this->isPullEnabled())
43 {
45 }
46
47 if (!$this->isEnabled())
48 {
50 }
51
52 return $result;
53 }
54
55 //region Chats
56
62 public function getPrivateChat(int $fromUserId, int $toUserId): Chat
63 {
64 $chatFactory = ChatFactory::getInstance();
65 $chat = $chatFactory
66 ->setContext($this->context)
67 ->getPrivateChat($fromUserId, $toUserId)
68 ;
69
70 if (!$chat)
71 {
72 return new NullChat();
73 }
74
75 return $chat;
76 }
77
83 public function getEntityChat(string $entityType, string $entityId): Chat
84 {
85 $chatFactory = ChatFactory::getInstance();
86 $chat = $chatFactory
87 ->setContext($this->context)
88 ->getEntityChat($entityType, $entityId)
89 ;
90
91 if (!$chat)
92 {
93 return (new NullChat())
94 ->setPreparedParams([
95 'TYPE' => Chat::IM_TYPE_CHAT,
96 'ENTITY_TYPE' => $entityType,
97 'ENTITY_ID' => $entityId,
98 ]);
99 }
100
101 if (!$chat->hasAccess())
102 {
103 return new NullChat();
104 }
105
106 return $chat;
107 }
108
109 public function getGeneralChat(): Chat
110 {
111 return Chat\GeneralChat::get();
112 }
113
118 public function getChat(int $chatId): Chat
119 {
120 return Chat\ChatFactory::getInstance()->getChatById($chatId);
121 }
122
123 //endregion
124
125 //region Messages
126
130 public function createMessage($source = null): Message
131 {
132 if (is_string($source))
133 {
134 $source = ['MESSAGE' => $source];
135 }
136
137 return new Message($source);
138 }
139
147 public function deleteMessage(Message $message, int $mode = 0): Result
148 {
149 $result = new Result();
150
151 $deleteService = new Message\Delete\DeleteService($message);
152 $deleteService->setMode($mode);
153 $deleteService->delete();
154
155 return $result;
156 }
157
165 public function disappearMessage(Message $message, int $hours): Result
166 {
167 $deleteService = new DeleteService($message);
168 if ($deleteService->canDelete() < DeleteService::DELETE_HARD)
169 {
170 return (new Result())->addError(new MessageError(MessageError::MESSAGE_ACCESS_ERROR));
171 }
172
173 return Message\Delete\DisappearService::disappearMessage($message, $hours);
174 }
175
183 public function updateMessage(Message $message, ?string $messageText): Result
184 {
185 $updateService = new Message\Update\UpdateService($message);
186 return $updateService->update($messageText);
187 }
188
189 //endregion
190
191 //region Task processing
192
193 public function registerTask(int $chatId, int $messageId, TaskObject $task): void
194 {
195 try
196 {
197 $taskService = new TaskService();
198
199 if (!Chat::getInstance($chatId)->hasAccess())
200 {
201 return;
202 }
203
204 $taskService->registerTask($chatId, $messageId, TaskItem::initByTaskObject($task));
205 }
206 catch (\Bitrix\Main\SystemException $exception)
207 {
208 //todo: log
209 }
210 }
211
218 public function unregisterTask(array $taskData, bool $saveDelete): void
219 {
220 try
221 {
222 $taskService = new TaskService();
223
224 $taskService->unregisterTaskByEntity(TaskItem::initByRow($taskData), $saveDelete);
225 }
226 catch (\Bitrix\Main\SystemException $exception)
227 {
228 //todo: log
229 }
230 }
231
237 public function deleteTask(int $taskId): void
238 {
239 try
240 {
241 $taskService = new TaskService();
242
243 $taskService->deleteLinkByTaskId($taskId);
244 }
245 catch (\Bitrix\Main\SystemException $exception)
246 {
247 //todo: log
248 }
249 }
250
251 public function updateTask(TaskObject $task): void
252 {
253 try
254 {
255 $taskService = new TaskService();
256
257 $taskService->updateTask(TaskItem::initByTaskObject($task));
258 }
259 catch (\Bitrix\Main\SystemException $exception)
260 {
261 //todo: log
262 }
263 }
264
265 //endregion
266
267 //region Calendar processing
268
269 public static function updateCalendar(int $eventId, array $entryFields): void
270 {
271 if ($entryFields['ID'] !== $entryFields['PARENT_ID'])
272 {
273 return;
274 }
275
276 $calendarService = new CalendarService();
277 $calendar = CalendarItem::getByCalendarId($eventId);
278 if ($calendar === null)
279 {
280 return;
281 }
282 $calendarService->updateCalendar($calendar);
283 }
284
285 public static function unregisterCalendar(int $eventId, array $entry): void
286 {
287 if ($entry['ID'] !== $entry['PARENT_ID'])
288 {
289 return;
290 }
291
292 $calendarService = new CalendarService();
293 $calendar = CalendarItem::getByCalendarId($eventId, false);
294 if ($calendar === null)
295 {
296 return;
297 }
298 $calendarService->unregisterCalendar($calendar);
299 }
300
301 //endregion
302
303 private function isPullEnabled(): bool
304 {
305 return \CModule::IncludeModule("pull") && \CPullOptions::GetQueueServerStatus();
306 }
307
308 private function isEnabled(): bool
309 {
310 if (
311 Loader::includeModule('intranet')
312 && method_exists(\Bitrix\Intranet\Settings\Tools\ToolsManager::class, 'checkAvailabilityByMenuId')
313 )
314 {
315 return \Bitrix\Intranet\Settings\Tools\ToolsManager::getInstance()
316 ->checkAvailabilityByMenuId(static::INTRANET_MENU_ID)
317 ;
318 }
319
320 return true;
321 }
322}
const IM_TYPE_CHAT
Definition Chat.php:56
static initByRow(array $row)
Definition TaskItem.php:76
static initByTaskObject(TaskObject $taskObject)
Definition TaskItem.php:101
static unregisterCalendar(int $eventId, array $entry)
deleteMessage(Message $message, int $mode=0)
unregisterTask(array $taskData, bool $saveDelete)
static updateCalendar(int $eventId, array $entryFields)
updateTask(TaskObject $task)
getPrivateChat(int $fromUserId, int $toUserId)
Definition Messenger.php:62
updateMessage(Message $message, ?string $messageText)
registerTask(int $chatId, int $messageId, TaskObject $task)
getEntityChat(string $entityType, string $entityId)
Definition Messenger.php:83
disappearMessage(Message $message, int $hours)