Skip to content

Commit 48fa3fb

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. Signed-off-by: Carl Schwan <carl.schwan@nextclound.com>
1 parent a6f32be commit 48fa3fb

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

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

Lines changed: 24 additions & 12 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->getNumberOfCommentsForObject('files', (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

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

135-
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
147+
return $this->commentsManager->getNumberOfCommentsForObject('files', $objectId, $lastRead);
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)) {

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)