Skip to content

Commit

Permalink
Add pagination support
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen authored and danxuliu committed Jul 25, 2018
1 parent 1e16f7e commit ac2314e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
47 changes: 30 additions & 17 deletions apps/comments/lib/Search/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,40 @@ public function search($query): array {
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;
Expand Down
10 changes: 8 additions & 2 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,11 @@ protected function getLastKnownComment(string $objectType,
* @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('*')
Expand All @@ -511,7 +513,8 @@ public function search(string $search, string $objectType, string $objectId, str
'%' . $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)));
Expand All @@ -522,6 +525,9 @@ public function search(string $search, string $objectType, string $objectId, str
if ($verb !== '') {
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
}
if ($offset !== 0) {
$query->setFirstResult($offset);
}

$comments = [];
$result = $query->execute();
Expand Down
4 changes: 3 additions & 1 deletion lib/public/Comments/ICommentsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ public function getForObjectSince(
* @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'
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Comments/FakeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getForObjectSince(

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 [];
}

Expand Down

0 comments on commit ac2314e

Please sign in to comment.