Improve searchCollections()
* Search ignores collection where we know the user can not access them * If we filtered all results, we start over with a bigger offset Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
59c92a7513
commit
a8a5472401
|
@ -100,7 +100,6 @@ class Manager implements IManager {
|
||||||
throw new CollectionException('Collection not found');
|
throw new CollectionException('Collection not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$access = $row['access'] === null ? null : (bool) $row['access'];
|
$access = $row['access'] === null ? null : (bool) $row['access'];
|
||||||
if ($user instanceof IUser) {
|
if ($user instanceof IUser) {
|
||||||
$access = [$user->getUID() => $access];
|
$access = [$user->getUID() => $access];
|
||||||
|
@ -120,23 +119,38 @@ class Manager implements IManager {
|
||||||
*/
|
*/
|
||||||
public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array {
|
public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array {
|
||||||
$query = $this->connection->getQueryBuilder();
|
$query = $this->connection->getQueryBuilder();
|
||||||
$query->select('*')
|
$userId = $user instanceof IUser ? $user->getUID() : '';
|
||||||
|
|
||||||
|
$query->select('c.*', 'a.access')
|
||||||
->from(self::TABLE_COLLECTIONS)
|
->from(self::TABLE_COLLECTIONS)
|
||||||
->where($query->expr()->iLike('name', $query->createNamedParameter($filter, IQueryBuilder::PARAM_STR)))
|
->leftJoin(
|
||||||
|
'r', self::TABLE_ACCESS_CACHE, 'a',
|
||||||
|
$query->expr()->andX(
|
||||||
|
$query->expr()->eq('c.id', 'a.resource_id'),
|
||||||
|
$query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->where($query->expr()->iLike('c.name', $query->createNamedParameter($filter, IQueryBuilder::PARAM_STR)))
|
||||||
|
->andWhere($query->expr()->neq('a.access', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
|
||||||
->setMaxResults($limit)
|
->setMaxResults($limit)
|
||||||
->setFirstResult($start);
|
->setFirstResult($start);
|
||||||
$result = $query->execute();
|
$result = $query->execute();
|
||||||
$collections = [];
|
$collections = [];
|
||||||
/** TODO: this is a huge performance bottleneck */
|
|
||||||
|
$foundResults = 0;
|
||||||
while ($row = $result->fetch()) {
|
while ($row = $result->fetch()) {
|
||||||
$collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name']);
|
$foundResults++;
|
||||||
|
$access = $row['access'] === null ? null : (bool) $row['access'];
|
||||||
|
$collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access);
|
||||||
if ($collection->canAccess($user)) {
|
if ($collection->canAccess($user)) {
|
||||||
$collections[] = $collection;
|
$collections[] = $collection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$result->closeCursor();
|
$result->closeCursor();
|
||||||
|
|
||||||
// TODO: call with increased first result if no matches found
|
if (empty($collections) && $foundResults === $limit) {
|
||||||
|
$this->searchCollections($user, $filter, $limit, $start + $limit);
|
||||||
|
}
|
||||||
|
|
||||||
return $collections;
|
return $collections;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue