diff options
author | Joas Schilling <coding@schilljs.com> | 2018-04-23 14:58:43 +0200 |
---|---|---|
committer | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-07-25 18:53:45 +0200 |
commit | ac2314e3d0e195b2af06473a6acfa5f76534cd63 (patch) | |
tree | d028b627370a2d98f2978f805057740014ebd911 | |
parent | 1e16f7ecd9f6aa28af73545b4a53feeae2f8056d (diff) | |
download | nextcloud-server-ac2314e3d0e195b2af06473a6acfa5f76534cd63.tar.gz nextcloud-server-ac2314e3d0e195b2af06473a6acfa5f76534cd63.zip |
Add pagination support
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | apps/comments/lib/Search/Provider.php | 47 | ||||
-rw-r--r-- | lib/private/Comments/Manager.php | 10 | ||||
-rw-r--r-- | lib/public/Comments/ICommentsManager.php | 4 | ||||
-rw-r--r-- | tests/lib/Comments/FakeManager.php | 2 |
4 files changed, 42 insertions, 21 deletions
diff --git a/apps/comments/lib/Search/Provider.php b/apps/comments/lib/Search/Provider.php index 78442e3ce0b..ac5afef6669 100644 --- a/apps/comments/lib/Search/Provider.php +++ b/apps/comments/lib/Search/Provider.php @@ -50,27 +50,40 @@ class Provider extends \OCP\Search\Provider { return []; } - /** @var IComment[] $comments */ - $comments = $cm->search($query, 'files', '', 'comment'); - $result = []; - foreach ($comments as $comment) { - if ($comment->getActorType() !== 'users') { - continue; + $numComments = 50; + $offset = 0; + + while (\count($result) < $numComments) { + /** @var IComment[] $comments */ + $comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments); + + foreach ($comments as $comment) { + if ($comment->getActorType() !== 'users') { + continue; + } + + $displayName = $cm->resolveDisplayName('user', $comment->getActorId()); + + try { + $file = $this->getFileForComment($uf, $comment); + $result[] = new Result($query, + $comment, + $displayName, + $file->getPath() + ); + } catch (NotFoundException $e) { + continue; + } } - $displayName = $cm->resolveDisplayName('user', $comment->getActorId()); - - try { - $file = $this->getFileForComment($uf, $comment); - $result[] = new Result($query, - $comment, - $displayName, - $file->getPath() - ); - } catch (NotFoundException $e) { - continue; + if (\count($comments) < $numComments) { + // Didn't find more comments when we tried to get, so there are no more comments. + return $result; } + + $offset += $numComments; + $numComments = 50 - \count($result); } return $result; diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index a06a2a0ec59..8f76d49b192 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -500,9 +500,11 @@ class Manager implements ICommentsManager { * @param string $objectType Limit the search by object type * @param string $objectId Limit the search by object id * @param string $verb Limit the verb of the comment + * @param int $offset + * @param int $limit * @return IComment[] */ - public function search(string $search, string $objectType, string $objectId, string $verb): array { + public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { $query = $this->dbConn->getQueryBuilder(); $query->select('*') @@ -511,7 +513,8 @@ class Manager implements ICommentsManager { '%' . $this->dbConn->escapeLikeParameter($search). '%' ))) ->orderBy('creation_timestamp', 'DESC') - ->addOrderBy('id', 'DESC'); + ->addOrderBy('id', 'DESC') + ->setMaxResults($limit); if ($objectType !== '') { $query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); @@ -522,6 +525,9 @@ class Manager implements ICommentsManager { if ($verb !== '') { $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); } + if ($offset !== 0) { + $query->setFirstResult($offset); + } $comments = []; $result = $query->execute(); diff --git a/lib/public/Comments/ICommentsManager.php b/lib/public/Comments/ICommentsManager.php index 2ea5ef29291..ca98214cd72 100644 --- a/lib/public/Comments/ICommentsManager.php +++ b/lib/public/Comments/ICommentsManager.php @@ -145,10 +145,12 @@ interface ICommentsManager { * @param string $objectType Limit the search by object type * @param string $objectId Limit the search by object id * @param string $verb Limit the verb of the comment + * @param int $offset + * @param int $limit * @return IComment[] * @since 14.0.0 */ - public function search(string $search, string $objectType, string $objectId, string $verb): array; + public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array; /** * @param $objectType string the object type, e.g. 'files' diff --git a/tests/lib/Comments/FakeManager.php b/tests/lib/Comments/FakeManager.php index 62a44a53f11..e758a951e8b 100644 --- a/tests/lib/Comments/FakeManager.php +++ b/tests/lib/Comments/FakeManager.php @@ -32,7 +32,7 @@ class FakeManager implements ICommentsManager { public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) {} - public function search(string $search, string $objectType, string $objectId, string $verb): array { + public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { return []; } |