18 private const CONFIRM_TYPE = 1;
19 private const SIMPLE_TYPE = 3;
20 private const ALL_TYPES = 4;
36 $this->convertText = $options[
'CONVERT_TEXT'] ??
null;
37 $this->searchText = $options[
'SEARCH_TEXT'] ??
null;
38 $this->searchType = $options[
'SEARCH_TYPE'] ??
null;
39 $this->searchDate = $options[
'SEARCH_DATE'] ??
null;
40 $this->pageLimit = $options[
'LIMIT'] ??
null;
41 $this->lastType = $options[
'LAST_TYPE'] ??
null;
42 $this->lastId = $options[
'LAST_ID'] ??
null;
43 $this->firstPage = !$this->lastId && !$this->lastType;
45 $chatData = $this->getChatData();
46 if ($chatData !==
null)
48 $this->chatId = (int)$chatData[
'CHAT_ID'];
49 $this->totalCount = (int)$chatData[
'IM_MODEL_RELATION_CHAT_MESSAGE_COUNT'];
53 private function getChatData(): ?array
55 $userId = \Bitrix\Im\Common::getUserId();
61 $chatData = \Bitrix\Im\Model\RelationTable::getList([
62 'select' => [
'CHAT_ID',
'CHAT.MESSAGE_COUNT'],
64 '=USER_ID' => $userId,
65 '=MESSAGE_TYPE' =>
'S'
95 if (is_array($chatId))
97 foreach($chatId as $id)
103 $chatList[$id] = $id;
106 $chatList = array_values($chatList);
107 $isMulti = count($chatList) > 1;
111 $id = intval($chatId);
157 $result[$chatList[0]] = $counter;
163 public function get()
165 if (!$this->chatId || !$this->totalCount)
168 'notifications' => [],
173 $confirmCollection = $this->fetchConfirms();
176 $offset = count($confirmCollection);
177 $simpleCollection = $this->fetchSimple($offset);
178 $notifications = array_merge($confirmCollection, $simpleCollection);
196 'TOTAL_COUNT' => $this->totalCount,
197 'TOTAL_UNREAD_COUNT' => (int)$unreadCount,
198 'CHAT_ID' => $this->chatId,
199 'NOTIFICATIONS' => $notifications,
200 'USERS' => $this->users,
203 foreach ($result[
'NOTIFICATIONS'] as $key => $value)
205 if ($value[
'DATE'] instanceof
DateTime)
207 $result[
'NOTIFICATIONS'][$key][
'DATE'] = date(
'c', $value[
'DATE']->getTimestamp());
210 $result[
'NOTIFICATIONS'][$key] = array_change_key_case($result[
'NOTIFICATIONS'][$key], CASE_LOWER);
212 $result[
'NOTIFICATIONS'] = array_values($result[
'NOTIFICATIONS']);
213 $result[
'USERS'] = array_values($result[
'USERS']);
214 $result = array_change_key_case($result, CASE_LOWER);
219 private function requestData(
int $requestType,
int $limit): array
222 $ormParams = $this->prepareGettingIdParams($requestType, $limit);
223 $ids = \Bitrix\Im\Model\MessageTable::getList($ormParams)->fetchAll();
224 if (count($ids) === 0)
229 $ids = array_map(
static function($item) {
230 return (
int)$item[
'ID'];
233 $ormParams = $this->prepareFilteringByIdParams($ids);
234 $ormResult = \Bitrix\Im\Model\MessageTable::getList($ormParams);
236 foreach ($ormResult as $notifyItem)
238 if ($notifyItem[
'NOTIFY_EVENT'] === self::EVENT_PRIVATE_SYSTEM)
240 $notifyItem[
'AUTHOR_ID'] = 0;
243 $collection[$notifyItem[
'ID']] = [
244 'ID' => (int)$notifyItem[
'ID'],
245 'CHAT_ID' => $this->chatId,
246 'AUTHOR_ID' => (
int)$notifyItem[
'AUTHOR_ID'],
247 'DATE' => $notifyItem[
'DATE_CREATE'],
248 'NOTIFY_TYPE' => (int)$notifyItem[
'NOTIFY_TYPE'],
249 'NOTIFY_MODULE' => $notifyItem[
'NOTIFY_MODULE'],
250 'NOTIFY_EVENT' => $notifyItem[
'NOTIFY_EVENT'],
251 'NOTIFY_TAG' => $notifyItem[
'NOTIFY_TAG'],
252 'NOTIFY_SUB_TAG' => $notifyItem[
'NOTIFY_SUB_TAG'],
253 'NOTIFY_TITLE' => $notifyItem[
'NOTIFY_TITLE'],
255 'SETTING_NAME' => $notifyItem[
'NOTIFY_MODULE'].
'|'.$notifyItem[
'NOTIFY_EVENT'],
257 $collection[$notifyItem[
'ID']][
'TEXT'] = \Bitrix\Im\Text::parse(
259 [
'LINK_TARGET_SELF' =>
'Y']
261 if ($notifyItem[
'AUTHOR_ID'] && !isset($this->users[$notifyItem[
'AUTHOR_ID']]))
267 $user[
'last_activity_date'] =
268 $notifyItem[
'USER_LAST_ACTIVITY_DATE']
269 ? date(
'c', $notifyItem[
'USER_LAST_ACTIVITY_DATE']->
getTimestamp())
272 $user[
'desktop_last_date'] =
false;
273 $user[
'mobile_last_date'] =
false;
274 $user[
'idle'] =
false;
276 $this->users[$notifyItem[
'AUTHOR_ID']] = $user;
280 if ($notifyItem[
'NOTIFY_BUTTONS'])
282 $buttons = unserialize($notifyItem[
'NOTIFY_BUTTONS'], [
'allowed_classes' =>
false]);
284 $keyboard = new \Bitrix\Im\Bot\Keyboard(111);
285 $command =
'notifyConfirm';
286 foreach ($buttons as $button)
288 $keyboard->addButton(
290 'TEXT' => $button[
'TITLE'],
291 'COMMAND' => $command,
292 'COMMAND_PARAMS' => $notifyItem[
'ID'].
'|'.$button[
'VALUE'],
293 'TEXT_COLOR' =>
'#fff',
294 'BG_COLOR' => $button[
'TYPE'] ===
'accept' ?
'#8BC84B' :
'#ef4b57',
299 $collection[$notifyItem[
'ID']][
'NOTIFY_BUTTONS'] = $keyboard->getJson();
303 if (count($collection) > 0)
305 $params = \CIMMessageParam::Get(array_keys($collection));
306 foreach ($params as $notificationId => $param)
308 $collection[$notificationId][
'PARAMS'] = empty($param) ? null : $param;
311 $collection = $this->fillReadStatuses($collection);
317 private function fetchConfirms(): array
319 $confirmCollection = [];
321 $nextPageIsConfirm = $this->lastType === self::CONFIRM_TYPE;
322 if ($this->firstPage || $nextPageIsConfirm)
324 $confirmCollection = $this->requestData(self::CONFIRM_TYPE, $this->pageLimit);
327 return $confirmCollection;
330 private function fetchSimple(
int $offset): array
332 $simpleCollection = [];
333 $nextPageIsSimple = $this->lastType === self::SIMPLE_TYPE;
334 $needMoreOnFirstPage = $this->firstPage && $offset < $this->pageLimit;
335 $notEnoughFromPreviousStep = $this->lastType === self::CONFIRM_TYPE && $offset < $this->pageLimit;
337 if ($needMoreOnFirstPage || $notEnoughFromPreviousStep || $nextPageIsSimple)
339 $simpleCollection = $this->requestData(self::SIMPLE_TYPE, $this->pageLimit - $offset);
342 return $simpleCollection;
352 if (!$this->searchText && !$this->searchType && !$this->searchDate)
357 if ($this->lastId > 0)
359 $this->lastType = self::ALL_TYPES;
360 $this->firstPage =
false;
364 $collection = $this->requestData(self::ALL_TYPES, $this->pageLimit);
367 'CHAT_ID' => $this->chatId,
368 'NOTIFICATIONS' => $collection,
369 'USERS' => $this->users,
374 $result[
'TOTAL_RESULTS'] = $this->requestSearchTotalCount();
377 foreach ($result[
'NOTIFICATIONS'] as $key => $value)
379 if ($value[
'DATE'] instanceof
DateTime)
381 $result[
'NOTIFICATIONS'][$key][
'DATE'] = date(
'c', $value[
'DATE']->getTimestamp());
384 $result[
'NOTIFICATIONS'][$key] = array_change_key_case($result[
'NOTIFICATIONS'][$key], CASE_LOWER);
386 $result[
'NOTIFICATIONS'] = array_values($result[
'NOTIFICATIONS']);
387 $result[
'USERS'] = array_values($result[
'USERS']);
388 $result = array_change_key_case($result, CASE_LOWER);
405 $result = \Bitrix\Im\Model\MessageTable::getList([
408 '=NOTIFY_TYPE' => [IM_NOTIFY_CONFIRM, IM_NOTIFY_FROM, IM_NOTIFY_SYSTEM],
409 '<DATE_CREATE' => ConvertTimeStamp((time() - 86400 * $dayCount),
'FULL')
417 while ($row = $result->fetch())
426 $batch[] = (int)$row[
'ID'];
434 foreach ($batches as $batch)
436 \Bitrix\Im\Model\MessageTable::deleteBatch([
439 \Bitrix\Im\Model\MessageParamTable::deleteBatch([
440 '=MESSAGE_ID' => $batch
442 $counterService->deleteByMessageIdsForAll($batch);
445 return __METHOD__.
'();';
448 private function requestSearchTotalCount(): int
451 '=CHAT_ID' => $this->chatId,
454 if ($this->searchText)
456 $filter[
'*%MESSAGE'] = $this->searchText;
458 if ($this->searchType)
460 $options = explode(
'|', $this->searchType);
461 $filter[
'=NOTIFY_MODULE'] = $options[0];
462 if (isset($options[1]))
464 $filter[
'=NOTIFY_EVENT'] = $options[1];
467 if ($this->searchDate)
469 $dateStart =
new DateTime(
471 \DateTimeInterface::RFC3339,
472 new \DateTimeZone(
'UTC')
477 \DateTimeInterface::RFC3339,
478 new \DateTimeZone(
'UTC')
482 $filter[
'><DATE_CREATE'] = [$dateStart, $dateEnd];
485 return \Bitrix\Im\Model\MessageTable::getCount($filter);
497 private function prepareGettingIdParams(
int $requestType,
int $limit): array
501 'filter' => [
'=CHAT_ID' => $this->chatId],
502 'order' => [
'DATE_CREATE' =>
'DESC',
'ID' =>
'DESC'],
506 if ($requestType === self::CONFIRM_TYPE)
508 $ormParams[
'filter'][
'=NOTIFY_TYPE'] = IM_NOTIFY_CONFIRM;
510 elseif ($requestType === self::SIMPLE_TYPE)
512 $ormParams[
'filter'][
'!=NOTIFY_TYPE'] = IM_NOTIFY_CONFIRM;
514 elseif ($requestType === self::ALL_TYPES)
516 if ($this->searchText)
518 $ormParams[
'filter'][
'*%MESSAGE'] = $this->searchText;
520 if ($this->searchType)
522 $options = explode(
'|', $this->searchType);
523 $ormParams[
'filter'][
'=NOTIFY_MODULE'] = $options[0];
524 if (isset($options[1]))
526 $ormParams[
'filter'][
'=NOTIFY_EVENT'] = $options[1];
529 if ($this->searchDate)
531 $dateStart =
new DateTime(
533 \DateTimeInterface::RFC3339,
534 new \DateTimeZone(
'UTC')
539 \DateTimeInterface::RFC3339,
540 new \DateTimeZone(
'UTC')
543 $ormParams[
'filter'][
'><DATE_CREATE'] = [$dateStart, $dateEnd];
547 if (!$this->firstPage)
550 $requestType === self::CONFIRM_TYPE
551 || ($requestType === self::SIMPLE_TYPE && $this->lastType === self::SIMPLE_TYPE)
552 || ($requestType === self::ALL_TYPES && $this->lastType === self::ALL_TYPES)
555 $ormParams[
'filter'][
'<ID'] = $this->lastId;
569 private function prepareFilteringByIdParams(array $ids): array
585 'USER_LAST_ACTIVITY_DATE' =>
'AUTHOR.LAST_ACTIVITY_DATE',
590 'filter' => [
'=ID' => $ids],
591 'order' => [
'DATE_CREATE' =>
'DESC',
'ID' =>
'DESC'],
604 'filter' => [
'=CHAT_ID' => $this->chatId],
605 'order' => [
'DATE_CREATE' =>
'DESC',
'ID' =>
'DESC'],
609 $getListResult = \Bitrix\Im\Model\MessageTable::getList($ormParams)->fetch();
615 if (count($getListResult) === 1)
617 return (
int)$getListResult[
'ID'];
623 private function fillReadStatuses(array $notifications): array
625 $messageIds = array_keys($notifications);
629 foreach ($notifications as $id => $notification)
631 $notifications[$id][
'NOTIFY_READ'] = $readStatuses[$id] ?
'Y' :
'N';
634 return $notifications;
static convertHtmlToBbCode($html)