apply date search filter in SQL queries instead of computing it in php
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
This commit is contained in:
@@ -94,15 +94,15 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
|||||||
return $board;
|
return $board;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findAllForUser(string $userId, int $since = -1, $includeArchived = true): array {
|
public function findAllForUser(string $userId, int $since = -1, $includeArchived = true, ?int $before = null): array {
|
||||||
$useCache = ($since === -1 && $includeArchived === true);
|
$useCache = ($since === -1 && $includeArchived === true && $before === null);
|
||||||
if (!isset($this->userBoardCache[$userId]) || !$useCache) {
|
if (!isset($this->userBoardCache[$userId]) || !$useCache) {
|
||||||
$groups = $this->groupManager->getUserGroupIds(
|
$groups = $this->groupManager->getUserGroupIds(
|
||||||
$this->userManager->get($userId)
|
$this->userManager->get($userId)
|
||||||
);
|
);
|
||||||
$userBoards = $this->findAllByUser($userId, null, null, $since, $includeArchived);
|
$userBoards = $this->findAllByUser($userId, null, null, $since, $includeArchived, $before);
|
||||||
$groupBoards = $this->findAllByGroups($userId, $groups, null, null, $since, $includeArchived);
|
$groupBoards = $this->findAllByGroups($userId, $groups, null, null, $since, $includeArchived, $before);
|
||||||
$circleBoards = $this->findAllByCircles($userId, null, null, $since, $includeArchived);
|
$circleBoards = $this->findAllByCircles($userId, null, null, $since, $includeArchived, $before);
|
||||||
$allBoards = array_unique(array_merge($userBoards, $groupBoards, $circleBoards));
|
$allBoards = array_unique(array_merge($userBoards, $groupBoards, $circleBoards));
|
||||||
if ($useCache) {
|
if ($useCache) {
|
||||||
$this->userBoardCache[$userId] = $allBoards;
|
$this->userBoardCache[$userId] = $allBoards;
|
||||||
@@ -120,19 +120,29 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
|||||||
* @param null $offset
|
* @param null $offset
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function findAllByUser($userId, $limit = null, $offset = null, $since = -1, $includeArchived = true) {
|
public function findAllByUser($userId, $limit = null, $offset = null, $since = -1, $includeArchived = true, ?int $before = null) {
|
||||||
// FIXME: One moving to QBMapper we should allow filtering the boards probably by method chaining for additional where clauses
|
// FIXME: One moving to QBMapper we should allow filtering the boards probably by method chaining for additional where clauses
|
||||||
$sql = 'SELECT id, title, owner, color, archived, deleted_at, 0 as shared, last_modified FROM `*PREFIX*deck_boards` WHERE owner = ? AND last_modified > ?';
|
$sql = 'SELECT id, title, owner, color, archived, deleted_at, 0 as shared, last_modified FROM `*PREFIX*deck_boards` WHERE owner = ? AND last_modified > ?';
|
||||||
|
$params = [$userId, $since];
|
||||||
if (!$includeArchived) {
|
if (!$includeArchived) {
|
||||||
$sql .= ' AND NOT archived AND deleted_at = 0';
|
$sql .= ' AND NOT archived AND deleted_at = 0';
|
||||||
}
|
}
|
||||||
|
if ($before !== null) {
|
||||||
|
$sql .= ' AND last_modified < ?';
|
||||||
|
$params[] = $before;
|
||||||
|
}
|
||||||
$sql .= ' UNION ' .
|
$sql .= ' UNION ' .
|
||||||
'SELECT boards.id, title, owner, color, archived, deleted_at, 1 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
|
'SELECT boards.id, title, owner, color, archived, deleted_at, 1 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
|
||||||
'JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=? AND boards.owner != ? AND last_modified > ?';
|
'JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=? AND boards.owner != ? AND last_modified > ?';
|
||||||
|
array_push($params, $userId, Acl::PERMISSION_TYPE_USER, $userId, $since);
|
||||||
if (!$includeArchived) {
|
if (!$includeArchived) {
|
||||||
$sql .= ' AND NOT archived AND deleted_at = 0';
|
$sql .= ' AND NOT archived AND deleted_at = 0';
|
||||||
}
|
}
|
||||||
$entries = $this->findEntities($sql, [$userId, $since, $userId, Acl::PERMISSION_TYPE_USER, $userId, $since], $limit, $offset);
|
if ($before !== null) {
|
||||||
|
$sql .= ' AND last_modified < ?';
|
||||||
|
$params[] = $before;
|
||||||
|
}
|
||||||
|
$entries = $this->findEntities($sql, $params, $limit, $offset);
|
||||||
/* @var Board $entry */
|
/* @var Board $entry */
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$acl = $this->aclMapper->findAll($entry->id);
|
$acl = $this->aclMapper->findAll($entry->id);
|
||||||
@@ -155,12 +165,13 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
|||||||
* @param null $offset
|
* @param null $offset
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function findAllByGroups($userId, $groups, $limit = null, $offset = null, $since = -1,$includeArchived = true) {
|
public function findAllByGroups($userId, $groups, $limit = null, $offset = null, $since = -1, $includeArchived = true, ?int $before = null) {
|
||||||
if (count($groups) <= 0) {
|
if (count($groups) <= 0) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
|
$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
|
||||||
'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
|
'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
|
||||||
|
$params = [$userId, Acl::PERMISSION_TYPE_GROUP];
|
||||||
for ($i = 0, $iMax = count($groups); $i < $iMax; $i++) {
|
for ($i = 0, $iMax = count($groups); $i < $iMax; $i++) {
|
||||||
$sql .= 'acl.participant = ? ';
|
$sql .= 'acl.participant = ? ';
|
||||||
if (count($groups) > 1 && $i < count($groups) - 1) {
|
if (count($groups) > 1 && $i < count($groups) - 1) {
|
||||||
@@ -168,10 +179,15 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$sql .= ')';
|
$sql .= ')';
|
||||||
|
array_push($params, ...$groups);
|
||||||
if (!$includeArchived) {
|
if (!$includeArchived) {
|
||||||
$sql .= ' AND NOT archived AND deleted_at = 0';
|
$sql .= ' AND NOT archived AND deleted_at = 0';
|
||||||
}
|
}
|
||||||
$entries = $this->findEntities($sql, array_merge([$userId, Acl::PERMISSION_TYPE_GROUP], $groups), $limit, $offset);
|
if ($before !== null) {
|
||||||
|
$sql .= ' AND last_modified < ?';
|
||||||
|
$params[] = $before;
|
||||||
|
}
|
||||||
|
$entries = $this->findEntities($sql, $params, $limit, $offset);
|
||||||
/* @var Board $entry */
|
/* @var Board $entry */
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$acl = $this->aclMapper->findAll($entry->id);
|
$acl = $this->aclMapper->findAll($entry->id);
|
||||||
@@ -180,7 +196,7 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
|||||||
return $entries;
|
return $entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findAllByCircles($userId, $limit = null, $offset = null, $since = -1,$includeArchived = true) {
|
public function findAllByCircles($userId, $limit = null, $offset = null, $since = -1, $includeArchived = true, ?int $before = null) {
|
||||||
if (!$this->circlesEnabled) {
|
if (!$this->circlesEnabled) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -193,6 +209,7 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
|||||||
|
|
||||||
$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
|
$sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' .
|
||||||
'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
|
'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND (';
|
||||||
|
$params = [$userId, Acl::PERMISSION_TYPE_CIRCLE];
|
||||||
for ($i = 0, $iMax = count($circles); $i < $iMax; $i++) {
|
for ($i = 0, $iMax = count($circles); $i < $iMax; $i++) {
|
||||||
$sql .= 'acl.participant = ? ';
|
$sql .= 'acl.participant = ? ';
|
||||||
if (count($circles) > 1 && $i < count($circles) - 1) {
|
if (count($circles) > 1 && $i < count($circles) - 1) {
|
||||||
@@ -200,10 +217,15 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$sql .= ')';
|
$sql .= ')';
|
||||||
|
array_push($params, ...$circles);
|
||||||
if (!$includeArchived) {
|
if (!$includeArchived) {
|
||||||
$sql .= ' AND NOT archived AND deleted_at = 0';
|
$sql .= ' AND NOT archived AND deleted_at = 0';
|
||||||
}
|
}
|
||||||
$entries = $this->findEntities($sql, array_merge([$userId, Acl::PERMISSION_TYPE_CIRCLE], $circles), $limit, $offset);
|
if ($before !== null) {
|
||||||
|
$sql .= ' AND last_modified < ?';
|
||||||
|
$params[] = $before;
|
||||||
|
}
|
||||||
|
$entries = $this->findEntities($sql, $params, $limit, $offset);
|
||||||
/* @var Board $entry */
|
/* @var Board $entry */
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$acl = $this->aclMapper->findAll($entry->id);
|
$acl = $this->aclMapper->findAll($entry->id);
|
||||||
|
|||||||
@@ -118,8 +118,8 @@ class BoardService {
|
|||||||
/**
|
/**
|
||||||
* Get all boards that are shared with a user, their groups or circles
|
* Get all boards that are shared with a user, their groups or circles
|
||||||
*/
|
*/
|
||||||
public function getUserBoards(int $since = -1, bool $includeArchived = true): array {
|
public function getUserBoards(int $since = -1, bool $includeArchived = true, ?int $before = null): array {
|
||||||
return $this->boardMapper->findAllForUser($this->userId, $since, $includeArchived);
|
return $this->boardMapper->findAllForUser($this->userId, $since, $includeArchived, $before);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -90,14 +90,11 @@ class SearchService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function searchBoards(string $term, ?int $limit, ?int $cursor): array {
|
public function searchBoards(string $term, ?int $limit, ?int $cursor): array {
|
||||||
$boards = $this->boardService->getUserBoards();
|
$boards = $this->boardService->getUserBoards(-1, true, $cursor);
|
||||||
// get boards that have a lastmodified date which is lower than the cursor
|
// get boards that have a lastmodified date which is lower than the cursor
|
||||||
// and which match the search term
|
// and which match the search term
|
||||||
$filteredBoards = array_filter($boards, static function (Board $board) use ($term, $cursor) {
|
$filteredBoards = array_filter($boards, static function (Board $board) use ($term, $cursor) {
|
||||||
return (
|
return mb_stripos(mb_strtolower($board->getTitle()), mb_strtolower($term)) > -1;
|
||||||
($cursor === null || $board->getLastModified() < $cursor)
|
|
||||||
&& mb_stripos(mb_strtolower($board->getTitle()), mb_strtolower($term)) > -1
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
// sort the boards, recently modified first
|
// sort the boards, recently modified first
|
||||||
usort($filteredBoards, function ($boardA, $boardB) {
|
usort($filteredBoards, function ($boardA, $boardB) {
|
||||||
|
|||||||
Reference in New Issue
Block a user