61 public static function find(array $filter, array $order, ?
int $limit =
null, ?
Context $context =
null, array $select = []): self
65 $query = MessageTable::query();
66 $query->setSelect([
'ID']);
70 $query->setLimit($limit);
73 $messageOrder = [
'DATE_CREATE' => $order[
'ID'] ??
'DESC',
'ID' => $order[
'ID'] ??
'DESC'];
74 $query->setOrder($messageOrder);
75 static::processFilters($query, $filter, $messageOrder);
76 $messageIds = $query->fetchCollection()->getIdList();
78 if (empty($messageIds))
88 return new static(MessageTable::query()->whereIn(
'ID', $messageIds)->setOrder($messageOrder)->setSelect($select)->fetchCollection());
171 if (!$this->isFileFilled)
176 foreach ($fileIdsByMessages as $fileIdsByMessage)
178 foreach ($fileIdsByMessage as $fileId)
180 $fileIds[] = $fileId;
186 foreach ($this as $message)
189 foreach ($fileIdsByMessages[$message->getId()] ?? [] as $fileId)
191 $file = $files->getById($fileId);
194 $messagesFiles[] = $file->setChatId($message->getChatId());
197 $message->fillFiles($messagesFiles);
200 $this->isFileFilled =
true;
267 if ($this->isUrlsFilled)
273 $urlIdByMessageIds = [];
274 foreach ($this as $message)
276 $urlId = $message->getParams()->get(Params::URL_ID)->getValue()[0] ??
null;
279 $urlIdByMessageIds[$message->getId()] = $urlId;
283 foreach ($this as $message)
285 if (isset($urlIdByMessageIds[$message->getId()]))
287 $urlId = $urlIdByMessageIds[$message->getId()];
288 $message->setUrl($urlCollection->getById($urlId));
292 $this->isUrlsFilled =
true;
321 if ($this->isViewedFilled)
326 $notOwnMessages = [];
328 foreach ($this as $message)
330 if ($message->getAuthorId() === $this->getContext()->getUserId())
332 $message->setViewed(
true);
337 $notOwnMessages[] = $message->getMessageId();
340 $viewStatuses = (
new ReadService())->getViewStatusesByMessageIds($notOwnMessages);
342 foreach ($notOwnMessages as $notOwnMessageId)
344 $this[$notOwnMessageId]->setViewed($viewStatuses[$notOwnMessageId]);
347 $this->isViewedFilled =
true;
354 if ($this->isViewedByOthersFilled)
361 foreach ($this as $message)
363 $status = $statuses[$message->getId()] ?? \IM_MESSAGE_STATUS_RECEIVED;
364 $message->setViewedByOthers($status === \IM_MESSAGE_STATUS_DELIVERED);
367 $this->isViewedByOthersFilled =
true;
444 foreach ($this as $message)
446 $usersFromMessage = $message->getUserIds();
448 if ($message->getParams()->isSet(Params::FORWARD_USER_ID))
450 $forwardUserId = $message->getParams()->get(Params::FORWARD_USER_ID)->getValue();
451 $usersFromMessage[] = $forwardUserId;
454 if ($message->getParams()->isSet(Params::CHAT_USER))
456 foreach ($message->getParams()->get(Params::CHAT_USER)->getValue() as $chatUser)
458 $usersFromMessage[] = $chatUser;
462 foreach ($usersFromMessage as $userFromMessage)
464 $users[] = $userFromMessage;
538 if (isset($filter[
'CHAT_ID']))
540 $query->where(
'CHAT_ID', $filter[
'CHAT_ID']);
543 if (isset($filter[
'SEARCH_MESSAGE']) && mb_strlen($filter[
'SEARCH_MESSAGE']) > 2)
545 $connection = \Bitrix\Main\Application::getConnection();
546 if ($connection instanceof \
Bitrix\Main\DB\PgsqlConnection)
548 $filter[
'SEARCH_MESSAGE'] = $connection->getSqlHelper()->forSql($filter[
'SEARCH_MESSAGE']);
551 'CASE_INSENSITIVE_MESSAGE',
552 "(CASE WHEN %s ILIKE '%%{$filter['SEARCH_MESSAGE']}%%' THEN 1 ELSE 0 END)",
556 $query->where(
'CASE_INSENSITIVE_MESSAGE',
'=',
'1');
560 $query->whereLike(
'MESSAGE',
"%{$filter['SEARCH_MESSAGE']}%");
564 if (isset($filter[
'START_ID']) && (
int)$filter[
'START_ID'] > 0)
566 $query->where(
'ID',
'>=', $filter[
'START_ID']);
569 if (isset($filter[
'LAST_ID']))
571 $operator = $order[
'ID'] ===
'DESC' ?
'<' :
'>';
572 $query->where(
'ID', $operator, $filter[
'LAST_ID']);
575 if (isset($filter[
'DATE_FROM']))
577 $query->where(
'DATE_CREATE',
'>=', $filter[
'DATE_FROM']);
580 if (isset($filter[
'DATE_TO']))
582 $query->where(
'DATE_CREATE',
'<=', $filter[
'DATE_TO']);
585 if (isset($filter[
'DATE']))
587 $query->where(
'DATE_CREATE',
'>=', $filter[
'DATE']);
589 $to = clone $filter[
'DATE'];
592 $query->where(
'DATE_CREATE',
'<=', $to);