29 public static function find(
34 array $select = self::COMMON_FIELDS
37 $query = RelationTable::query()->setSelect($select);
41 $query->setLimit($limit);
44 static::processFilters($query, $filter, $order);
46 return new static($query->fetchCollection());
49 public static function getStartId(
int $userId,
int $chatId): int
51 if (isset(self::$startIdStaticCache[$chatId][$userId]))
53 return self::$startIdStaticCache[$chatId][$userId];
56 $relation = static::find([
'CHAT_ID' => $chatId,
'USER_ID' => $userId], [], 1)->getByUserId($userId, $chatId);
58 if ($relation ===
null)
63 return $relation->getStartId() ?? 0;
98 if (isset($filter[
'CHAT_ID']))
100 $query->where(
'CHAT_ID', (
int)$filter[
'CHAT_ID']);
103 if (isset($filter[
'MANAGER']))
105 $query->where(
'MANAGER', (
string)$filter[
'MANAGER']);
108 if (isset($filter[
'USER_ID']))
110 if (is_array($filter[
'USER_ID']) && !empty($filter[
'USER_ID']))
112 $query->whereIn(
'USER_ID', $filter[
'USER_ID']);
116 $query->where(
'USER_ID', (
int)$filter[
'USER_ID']);
120 if (isset($filter[
'!USER_ID']))
122 if (is_array($filter[
'!USER_ID']) && !empty($filter[
'!USER_ID']))
124 $query->whereNotIn(
'USER_ID', $filter[
'!USER_ID']);
128 $query->whereNot(
'USER_ID', (
int)$filter[
'!USER_ID']);
132 if (isset($filter[
'MESSAGE_TYPE']))
134 $query->where(
'MESSAGE_TYPE', (
string)$filter[
'MESSAGE_TYPE']);
137 foreach ([
'ID',
'USER_ID',
'LAST_SEND_MESSAGE_ID'] as $allowedFieldToOrder)
139 if (isset($order[$allowedFieldToOrder]))
141 $orderField = $allowedFieldToOrder;
142 $relationOrder[$allowedFieldToOrder] = $order[$allowedFieldToOrder];
147 if (isset($orderField))
152 if (isset($filter[
'LAST_ID']))
155 if (isset($orderField) && $relationOrder[$orderField] ===
'ASC')
159 $query->where($orderField, $operator, (
int)$filter[
'LAST_ID']);
162 if (isset($filter[
'ACTIVE']))
164 $query->where(
'USER.ACTIVE', $filter[
'ACTIVE']);
167 if (isset($filter[
'ONLY_INTERNAL_TYPE']) && $filter[
'ONLY_INTERNAL_TYPE'])
172 ->whereNotIn(
'USER.EXTERNAL_AUTH_ID', UserTable::getExternalUserTypes())
173 ->whereNull(
'USER.EXTERNAL_AUTH_ID')