Skip to content

Commit fa5548a

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 2e81653 commit fa5548a

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

lib/private/Comments/Manager.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -625,14 +625,17 @@ public function searchForObjects(string $search, string $objectType, array $obje
625625
* @return Int
626626
* @since 9.0.0
627627
*/
628-
public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime $notOlderThan = null, $verb = '') {
628+
public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime $notOlderThan = null, $verb = ''): int {
629+
return $this->getNumberOfCommentsForObjects($objectType, [$objectId], $notOlderThan, $verb)[$objectId];
630+
}
631+
632+
/** @inheritDoc */
633+
public function getNumberOfCommentsForObjects(string $objectType, array $objectIds, ?\DateTime $notOlderThan = null, string $verb = ''): array {
629634
$qb = $this->dbConn->getQueryBuilder();
630-
$query = $qb->select($qb->func()->count('id'))
635+
$query = $qb->select($qb->func()->count('id'), 'object_id')
631636
->from('comments')
632-
->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
633-
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
634-
->setParameter('type', $objectType)
635-
->setParameter('id', $objectId);
637+
->where($qb->expr()->eq('object_type', $qb->createNamedParameter($objectType, IQueryBuilder::PARAM_STR)))
638+
->andWhere($qb->expr()->in('object_id', $qb->createNamedParameter($objectIds, IQueryBuilder::PARAM_STR_ARRAY)));
636639

637640
if (!is_null($notOlderThan)) {
638641
$query
@@ -644,10 +647,15 @@ public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime
644647
$query->andWhere($qb->expr()->eq('verb', $qb->createNamedParameter($verb)));
645648
}
646649

650+
$query->groupBy('object_id');
651+
$comments = array_fill_keys($objectIds, 0);
647652
$resultStatement = $query->execute();
648-
$data = $resultStatement->fetch(\PDO::FETCH_NUM);
653+
while ($data = $resultStatement->fetch()) {
654+
$comments[$data[1]] = (int)$data[0];
655+
}
649656
$resultStatement->closeCursor();
650-
return (int)$data[0];
657+
658+
return $comments;
651659
}
652660

653661
/**

lib/public/Comments/ICommentsManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ public function searchForObjects(string $search, string $objectType, array $obje
186186
*/
187187
public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime $notOlderThan = null, $verb = '');
188188

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