Skip to content

Commit 9a7a0c2

Browse files
author
Carl Schwan
committed
perf(comments): Add a way to get comments for multiple objects at the same time
Signed-off-by: Carl Schwan <carl.schwan@nextclound.com>
1 parent 2979a2b commit 9a7a0c2

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lib/private/Comments/Manager.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,35 @@ public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime
650650
return (int)$data[0];
651651
}
652652

653+
/** @inheritDoc */
654+
public function getNumberOfCommentsForObjects(string $objectType, array $objectIds, ?\DateTime $notOlderThan = null, string $verb = '') {
655+
$qb = $this->dbConn->getQueryBuilder();
656+
$query = $qb->select($qb->func()->count('id'), 'object_id')
657+
->from('comments')
658+
->where($qb->expr()->eq('object_type', $qb->createNamedParameter($objectType, IQueryBuilder::PARAM_STR)))
659+
->andWhere($qb->expr()->in('object_id', $qb->createNamedParameter($objectIds, IQueryBuilder::PARAM_INT_ARRAY)));
660+
661+
if (!is_null($notOlderThan)) {
662+
$query
663+
->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
664+
->setParameter('notOlderThan', $notOlderThan, 'datetime');
665+
}
666+
667+
if ($verb !== '') {
668+
$query->andWhere($qb->expr()->eq('verb', $qb->createNamedParameter($verb)));
669+
}
670+
671+
$query->groupBy('object_id');
672+
$comments = array_fill_keys($objectIds, 0);
673+
$resultStatement = $query->execute();
674+
while ($data = $resultStatement->fetch()) {
675+
$comments[$data[1]] = (int)$data[0];
676+
}
677+
$resultStatement->closeCursor();
678+
679+
return $comments;
680+
}
681+
653682
/**
654683
* @param string $objectType the object type, e.g. 'files'
655684
* @param string[] $objectIds the id of the object

lib/public/Comments/ICommentsManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,21 @@ public function searchForObjects(string $search, string $objectType, array $obje
183183
* @param string $verb Limit the verb of the comment - Added in 14.0.0
184184
* @return Int
185185
* @since 9.0.0
186+
* @depreacted Use getNumberOfCommentsForObjects instead which operate on multiple object at the same time
186187
*/
187188
public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime $notOlderThan = null, $verb = '');
188189

190+
/**
191+
* @param $objectType string the object type, e.g. 'files'
192+
* @param $objectIds string[] the ids of the object
193+
* @param \DateTime|null $notOlderThan optional, timestamp of the oldest comments
194+
* that may be returned
195+
* @param string $verb Limit the verb of the comment - Added in 14.0.0
196+
* @return array<string, int>
197+
* @since 32.0.0
198+
*/
199+
public function getNumberOfCommentsForObjects(string $objectType, array $objectIds, ?\DateTime $notOlderThan = null, string $verb = '');
200+
189201
/**
190202
* @param string $objectType the object type, e.g. 'files'
191203
* @param string[] $objectIds the id of the object

tests/lib/Comments/FakeManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function getCommentsWithVerbForObjectSinceComment(
5959
public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime $notOlderThan = null, $verb = '') {
6060
}
6161

62+
public function getNumberOfCommentsForObjects(string $objectType, array $objectIds, ?\DateTime $notOlderThan = null, string $verb = '') :array {
63+
return array_fill_keys($objectIds, 0);
64+
}
65+
6266
public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array {
6367
return [];
6468
}

0 commit comments

Comments
 (0)