Skip to content

Commit 17135e2

Browse files
author
Carl Schwan
committed
perf(comments): Also cache the comments count
Since we now have an easy way to fetch the comments count and port away from the deprecated method. Signed-off-by: Carl Schwan <carl.schwan@nextclound.com>
1 parent 9a7a0c2 commit 17135e2

4 files changed

Lines changed: 31 additions & 42 deletions

File tree

apps/dav/lib/Connector/Sabre/CommentPropertiesPlugin.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CommentPropertiesPlugin extends ServerPlugin {
2121

2222
protected ?Server $server = null;
2323
private array $cachedUnreadCount = [];
24+
private array $cachedCount = [];
2425

2526
public function __construct(
2627
private ICommentsManager $commentsManager,
@@ -44,7 +45,11 @@ public function initialize(\Sabre\DAV\Server $server) {
4445
$this->server->on('propFind', [$this, 'handleGetProperties']);
4546
}
4647

47-
private function cacheDirectory(Directory $directory): void {
48+
private function cacheDirectory(Directory $directory, PropFind $propFind): void {
49+
if (is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD)) && is_null($propFind->getStatus(self::PROPERTY_NAME_COUNT))) {
50+
return;
51+
}
52+
4853
$children = $directory->getChildren();
4954

5055
$ids = [];
@@ -62,11 +67,20 @@ private function cacheDirectory(Directory $directory): void {
6267
}
6368

6469
$ids[] = (string)$directory->getId();
65-
$unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $this->userSession->getUser());
70+
if (!is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))) {
71+
$unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $this->userSession->getUser());
72+
foreach ($unread as $id => $count) {
73+
$this->cachedUnreadCount[(int)$id] = $count;
74+
}
75+
}
6676

67-
foreach ($unread as $id => $count) {
68-
$this->cachedUnreadCount[(int)$id] = $count;
77+
if (!is_null($propFind->getStatus(self::PROPERTY_NAME_COUNT))) {
78+
$commentCounts = $this->commentsManager->getNumberOfCommentsForObjects('files', $ids);
79+
foreach ($commentCounts as $id => $count) {
80+
$this->cachedCount[(int)$id] = $count;
81+
}
6982
}
83+
7084
}
7185

7286
/**
@@ -86,15 +100,12 @@ public function handleGetProperties(
86100
}
87101

88102
// need prefetch ?
89-
if ($node instanceof Directory
90-
&& $propFind->getDepth() !== 0
91-
&& !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
92-
) {
93-
$this->cacheDirectory($node);
103+
if ($node instanceof Directory && $propFind->getDepth() !== 0) {
104+
$this->cacheDirectory($node, $propFind);
94105
}
95106

96107
$propFind->handle(self::PROPERTY_NAME_COUNT, function () use ($node): int {
97-
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
108+
return $this->cachedCount[$node->getId()] ?? $this->commentsManager->getNumberOfCommentsForObjects('files', [(string)$node->getId()])[(string)$node->getId()];
98109
});
99110

100111
$propFind->handle(self::PROPERTY_NAME_HREF, function () use ($node): ?string {
@@ -130,8 +141,9 @@ public function getUnreadCount(Node $node): ?int {
130141
return null;
131142
}
132143

144+
$objectId = (string)$node->getId();
133145
$lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
134146

135-
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
147+
return $this->commentsManager->getNumberOfCommentsForObjects('files', [$objectId], $lastRead)[$objectId];
136148
}
137149
}

apps/dav/tests/unit/Connector/Sabre/CommentsPropertiesPluginTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public function testGetUnreadCount(?string $user): void {
104104
->willReturn($user);
105105

106106
$this->commentsManager->expects($this->any())
107-
->method('getNumberOfCommentsForObject')
108-
->willReturn(42);
107+
->method('getNumberOfCommentsForObjects')
108+
->willReturn(['4567' => 42]);
109109

110110
$unread = $this->plugin->getUnreadCount($node);
111111
if (is_null($user)) {

lib/private/Comments/Manager.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -625,33 +625,12 @@ 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 = '') {
629-
$qb = $this->dbConn->getQueryBuilder();
630-
$query = $qb->select($qb->func()->count('id'))
631-
->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);
636-
637-
if (!is_null($notOlderThan)) {
638-
$query
639-
->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
640-
->setParameter('notOlderThan', $notOlderThan, 'datetime');
641-
}
642-
643-
if ($verb !== '') {
644-
$query->andWhere($qb->expr()->eq('verb', $qb->createNamedParameter($verb)));
645-
}
646-
647-
$resultStatement = $query->execute();
648-
$data = $resultStatement->fetch(\PDO::FETCH_NUM);
649-
$resultStatement->closeCursor();
650-
return (int)$data[0];
628+
public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime $notOlderThan = null, $verb = ''): int {
629+
return $this->getNumberOfCommentsForObjects($objectType, [$objectId], $notOlderThan, $verb)[$objectId];
651630
}
652631

653632
/** @inheritDoc */
654-
public function getNumberOfCommentsForObjects(string $objectType, array $objectIds, ?\DateTime $notOlderThan = null, string $verb = '') {
633+
public function getNumberOfCommentsForObjects(string $objectType, array $objectIds, ?\DateTime $notOlderThan = null, string $verb = ''): array {
655634
$qb = $this->dbConn->getQueryBuilder();
656635
$query = $qb->select($qb->func()->count('id'), 'object_id')
657636
->from('comments')

tests/lib/Comments/ManagerTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,9 @@ public function testGetNumberOfCommentsForObject(): void {
326326

327327
$manager = $this->getManager();
328328

329-
$amount = $manager->getNumberOfCommentsForObject('untype', '00');
330-
$this->assertSame(0, $amount);
331-
332-
$amount = $manager->getNumberOfCommentsForObject('files', 'file64');
333-
$this->assertSame(4, $amount);
329+
$amount = $manager->getNumberOfCommentsForObjects('untype', ['00', 'file64']);
330+
$this->assertSame(0, $amount['00']);
331+
$this->assertSame(4, $amount['64']);
334332
}
335333

336334
public function testGetNumberOfUnreadCommentsForFolder(): void {

0 commit comments

Comments
 (0)