27 private const CONFIRM_TYPE = 1;
28 private const SIMPLE_TYPE = 3;
29 private const ALL_TYPES = 4;
42 private string $groupTag;
48 $this->convertText =
$options[
'CONVERT_TEXT'] ??
null;
49 $this->searchText =
$options[
'SEARCH_TEXT'] ??
null;
50 $this->searchType =
$options[
'SEARCH_TYPE'] ??
null;
51 $this->searchDate =
$options[
'SEARCH_DATE'] ??
null;
52 $this->pageLimit =
$options[
'LIMIT'] ??
null;
53 $this->lastType =
$options[
'LAST_TYPE'] ??
null;
54 $this->lastId =
$options[
'LAST_ID'] ??
null;
55 $this->groupTag = (string)(
$options[
'GROUP_TAG'] ??
'');
56 $this->firstPage = !$this->lastId && !$this->lastType;
59 $chatData = $this->getChatData();
60 if ($chatData !==
null)
62 $this->chatId = (int)$chatData[
'CHAT_ID'];
63 $this->totalCount = (int)$chatData[
'IM_MODEL_RELATION_CHAT_MESSAGE_COUNT'];
67 private function getChatData(): ?
array
75 $chatData = \Bitrix\Im\Model\RelationTable::getList([
76 'select' => [
'CHAT_ID',
'CHAT.MESSAGE_COUNT'],
79 '=MESSAGE_TYPE' =>
'S'
109 if (is_array($chatId))
111 foreach($chatId as $id)
117 $chatList[$id] = $id;
120 $chatList = array_values($chatList);
121 $isMulti =
count($chatList) > 1;
125 $id = intval($chatId);
177 public function get()
179 if (!$this->chatId || !$this->totalCount)
182 'notifications' => [],
187 $confirmCollection = $this->fetchConfirms();
190 $offset =
count($confirmCollection);
191 $simpleCollection = $this->fetchSimple($offset);
192 $notifications = array_merge($confirmCollection, $simpleCollection);
211 'TOTAL_UNREAD_COUNT' => (int)$unreadCount,
212 'CHAT_ID' => $this->chatId,
213 'NOTIFICATIONS' => $notifications,
214 'USERS' => $this->users,
217 foreach (
$result[
'NOTIFICATIONS'] as
$key => $value)
219 if ($value[
'DATE'] instanceof
DateTime)
221 $result[
'NOTIFICATIONS'][
$key][
'DATE'] = date(
'c', $value[
'DATE']->getTimestamp());
226 $result[
'NOTIFICATIONS'] = array_values(
$result[
'NOTIFICATIONS']);
233 private function requestData(
int $requestType,
int $limit):
array
236 $ormParams = $this->prepareGettingIdParams($requestType, $limit);
237 $ids = \Bitrix\Im\Model\MessageTable::getList($ormParams)->fetchAll();
238 if (
count($ids) === 0)
243 $ids = array_map(
static function($item) {
244 return (
int)$item[
'ID'];
247 $ormParams = $this->prepareFilteringByIdParams($ids);
248 $ormResult = \Bitrix\Im\Model\MessageTable::getList($ormParams);
250 foreach ($ormResult as $notifyItem)
252 if ($notifyItem[
'NOTIFY_EVENT'] === self::EVENT_PRIVATE_SYSTEM)
254 $notifyItem[
'AUTHOR_ID'] = 0;
257 $collection[$notifyItem[
'ID']] = [
258 'ID' => (int)$notifyItem[
'ID'],
259 'CHAT_ID' => $this->chatId,
260 'AUTHOR_ID' => (
int)$notifyItem[
'AUTHOR_ID'],
261 'DATE' => $notifyItem[
'DATE_CREATE'],
262 'NOTIFY_TYPE' => (int)$notifyItem[
'NOTIFY_TYPE'],
263 'NOTIFY_MODULE' => $notifyItem[
'NOTIFY_MODULE'],
264 'NOTIFY_EVENT' => $notifyItem[
'NOTIFY_EVENT'],
265 'NOTIFY_TAG' => $notifyItem[
'NOTIFY_TAG'],
266 'NOTIFY_SUB_TAG' => $notifyItem[
'NOTIFY_SUB_TAG'],
267 'NOTIFY_TITLE' => $notifyItem[
'NOTIFY_TITLE'],
269 'SETTING_NAME' => $notifyItem[
'NOTIFY_MODULE'].
'|'.$notifyItem[
'NOTIFY_EVENT'],
271 $collection[$notifyItem[
'ID']][
'TEXT'] = \Bitrix\Im\Text::parse(
273 [
'LINK_TARGET_SELF' =>
'Y']
275 if ($notifyItem[
'AUTHOR_ID'] && !isset($this->users[$notifyItem[
'AUTHOR_ID']]))
281 $user[
'last_activity_date'] =
282 $notifyItem[
'USER_LAST_ACTIVITY_DATE']
283 ? date(
'c', $notifyItem[
'USER_LAST_ACTIVITY_DATE']->
getTimestamp())
286 $user[
'desktop_last_date'] =
false;
287 $user[
'mobile_last_date'] =
false;
288 $user[
'idle'] =
false;
290 $this->users[$notifyItem[
'AUTHOR_ID']] =
$user;
294 if ($notifyItem[
'NOTIFY_BUTTONS'])
296 $buttons = unserialize($notifyItem[
'NOTIFY_BUTTONS'], [
'allowed_classes' =>
false]);
298 $keyboard = new \Bitrix\Im\Bot\Keyboard(111);
299 $command =
'notifyConfirm';
300 foreach ($buttons as $button)
302 $keyboard->addButton(
304 'TEXT' => $button[
'TITLE'],
305 'COMMAND' => $command,
306 'COMMAND_PARAMS' => $notifyItem[
'ID'].
'|'.$button[
'VALUE'],
307 'TEXT_COLOR' =>
'#fff',
308 'BG_COLOR' => $button[
'TYPE'] ===
'accept' ?
'#8BC84B' :
'#ef4b57',
313 $collection[$notifyItem[
'ID']][
'NOTIFY_BUTTONS'] = $keyboard->getJson();
317 if (
count($collection) > 0)
319 $params = \CIMMessageParam::Get(array_keys($collection));
320 foreach (
$params as $notificationId => $param)
322 $collection[$notificationId][
'PARAMS'] = empty($param) ? null : $param;
325 $collection = $this->fillReadStatuses($collection);
331 private function fetchConfirms():
array
333 $confirmCollection = [];
335 $nextPageIsConfirm = $this->lastType === self::CONFIRM_TYPE;
336 if ($this->firstPage || $nextPageIsConfirm)
338 $confirmCollection = $this->requestData(self::CONFIRM_TYPE, $this->pageLimit);
341 return $confirmCollection;
344 private function fetchSimple(
int $offset):
array
346 $simpleCollection = [];
347 $nextPageIsSimple = $this->lastType === self::SIMPLE_TYPE;
348 $needMoreOnFirstPage = $this->firstPage && $offset < $this->pageLimit;
349 $notEnoughFromPreviousStep = $this->lastType === self::CONFIRM_TYPE && $offset < $this->pageLimit;
351 if ($needMoreOnFirstPage || $notEnoughFromPreviousStep || $nextPageIsSimple)
353 $simpleCollection = $this->requestData(self::SIMPLE_TYPE, $this->pageLimit - $offset);
356 return $simpleCollection;
366 if (!$this->searchText && !$this->searchType && !$this->searchDate)
371 if ($this->lastId > 0)
373 $this->lastType = self::ALL_TYPES;
374 $this->firstPage =
false;
378 $collection = $this->requestData(self::ALL_TYPES, $this->pageLimit);
381 'CHAT_ID' => $this->chatId,
382 'NOTIFICATIONS' => $collection,
383 'USERS' => $this->users,
388 $result[
'TOTAL_RESULTS'] = $this->requestSearchTotalCount();
391 foreach (
$result[
'NOTIFICATIONS'] as
$key => $value)
393 if ($value[
'DATE'] instanceof
DateTime)
395 $result[
'NOTIFICATIONS'][
$key][
'DATE'] = date(
'c', $value[
'DATE']->getTimestamp());
400 $result[
'NOTIFICATIONS'] = array_values(
$result[
'NOTIFICATIONS']);
419 $result = \Bitrix\Im\Model\MessageTable::getList([
420 'select' => [
'ID',
'CHAT_ID'],
423 '<DATE_CREATE' => ConvertTimeStamp((time() - 86400 * $dayCount),
'FULL')
431 while ($row =
$result->fetch())
440 $message = (
new Message())->setMessageId((
int)$row[
'ID'])->setChatId((
int)$row[
'CHAT_ID']);
443 if ($batch->count() !== 0)
449 foreach ($batches as $batch)
451 $messageIds = $batch->getIds();
452 if (empty($messageIds))
460 '=MESSAGE_ID' => $messageIds
462 $counterService->deleteByMessagesForAll($batch);
465 return __METHOD__.
'();';
468 private function requestSearchTotalCount(): int
470 return \Bitrix\Im\Model\MessageTable::getCount($this->getFilterConditions());
482 private function prepareGettingIdParams(
int $requestType,
int $limit):
array
486 'filter' => $this->getFilterConditions($requestType,
true),
487 'order' => [
'DATE_CREATE' =>
'DESC',
'ID' =>
'DESC'],
492 private function getFilterConditions(
int $requestType = self::ALL_TYPES,
bool $withIdCondition =
false):
array
495 '=CHAT_ID' => $this->chatId,
498 if ($requestType === self::CONFIRM_TYPE)
502 elseif ($requestType === self::SIMPLE_TYPE)
506 elseif ($requestType === self::ALL_TYPES)
508 if ($this->searchText)
510 $filter[
'*%MESSAGE'] = $this->searchText;
512 if ($this->searchType)
514 $options = explode(
'|', $this->searchType);
521 if ($this->searchDate)
523 $dateStart =
new DateTime(
525 \DateTimeInterface::RFC3339,
526 new \DateTimeZone(
'UTC')
531 \DateTimeInterface::RFC3339,
532 new \DateTimeZone(
'UTC')
535 $filter[
'><DATE_CREATE'] = [$dateStart, $dateEnd];
540 $filter = array_merge(
$filter, $this->getGroupConditions()->toFilterFormat());
544 if ($withIdCondition && !$this->firstPage)
547 $requestType === self::CONFIRM_TYPE
548 || ($requestType === self::SIMPLE_TYPE && $this->lastType === self::SIMPLE_TYPE)
549 || ($requestType === self::ALL_TYPES && $this->lastType === self::ALL_TYPES)
566 private function prepareFilteringByIdParams(
array $ids):
array
582 'USER_LAST_ACTIVITY_DATE' =>
'AUTHOR.LAST_ACTIVITY_DATE',
587 'filter' => [
'=ID' => $ids],
588 'order' => [
'DATE_CREATE' =>
'DESC',
'ID' =>
'DESC'],
601 'filter' => [
'=CHAT_ID' => $this->chatId],
602 'order' => [
'DATE_CREATE' =>
'DESC',
'ID' =>
'DESC'],
606 $getListResult = \Bitrix\Im\Model\MessageTable::getList($ormParams)->fetch();
612 if (
count($getListResult) === 1)
614 return (
int)$getListResult[
'ID'];
620 private function fillReadStatuses(
array $notifications):
array
622 $messageIds = array_keys($notifications);
626 foreach ($notifications as $id => $notification)
628 $notifications[$id][
'NOTIFY_READ'] = $readStatuses[$id] ?
'Y' :
'N';
631 return $notifications;
634 private function getGroupConditions(): Conditions
636 if (!isset($this->groupConditions))
638 $this->groupConditions = (
new ConditionFactory())->makeByTag($this->groupTag, $this->userId);
641 return $this->groupConditions;
647 $notifyTag = (string)
$arParams[
'NOTIFY_TAG'];
650 if (!$notifyTag || $chatId <= 0)
655 $query = MessageTable::query()
656 ->setSelect([
'ID',
'AUTHOR_ID',
'CHAT_ID'])
657 ->where(
'NOTIFY_TAG', $notifyTag)
662 $query->where(
'CHAT_ID', $chatId);
671 while ($row =
$query->fetch())
676 if ((
int)$row[
'CHAT_ID'] !== $chatId)
683 'AUTHOR_ID' => (int)$row[
'AUTHOR_ID'],
684 'CHAT_ID' => (
int)$row[
'CHAT_ID'],
690 $maxAuthorId = (int)$row[
'AUTHOR_ID'];
702 $newParams[$param] = (int)$param;
706 $newParams = array_diff($newParams,
$params[$maxId] ?? []);
708 unset($messageIds[$maxId]);
709 CIMNotify::deleteList($messageIds, [
'NOTIFY_TAG' => $notifyTag]);
711 if ($currentMessageId >= $maxId)
713 unset($newParams[
$arParams[
'AUTHOR_ID']]);
717 unset($newParams[$maxAuthorId ?? 0]);
721 return $currentMessageId < $maxId ?
$result :
$result->setResult(array_values($newParams));
724 private static function getUsersParam(
array $messageIds):
array
726 if (empty($messageIds))
731 $query = \Bitrix\Im\Model\MessageParamTable::query()
732 ->setSelect([
'MESSAGE_ID',
'PARAM_VALUE'])
733 ->whereIn(
'MESSAGE_ID', $messageIds)
734 ->where(
'PARAM_NAME', Params::USERS)
739 while ($row =
$query->fetch())
741 $params[$row[
'MESSAGE_ID']][(int)$row[
'PARAM_VALUE']] = (
int)$row[
'PARAM_VALUE'];
static convertHtmlToBbCode($html)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)