Skip to content

Commit 7c10fdb

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 7c10fdb

4 files changed

Lines changed: 35 additions & 18 deletions

File tree

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

Lines changed: 30 additions & 13 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,27 @@ 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+
$user = $this->userSession->getUser();
72+
if ($user) {
73+
$unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $user);
74+
foreach ($unread as $id => $count) {
75+
$this->cachedUnreadCount[(int)$id] = $count;
76+
}
77+
} else {
78+
foreach ($ids as $id) {
79+
$this->cachedUnreadCount[(int)$id] = null;
80+
}
81+
}
82+
}
6683

67-
foreach ($unread as $id => $count) {
68-
$this->cachedUnreadCount[(int)$id] = $count;
84+
if (!is_null($propFind->getStatus(self::PROPERTY_NAME_COUNT))) {
85+
$commentCounts = $this->commentsManager->getNumberOfCommentsForObjects('files', $ids);
86+
foreach ($commentCounts as $id => $count) {
87+
$this->cachedCount[(int)$id] = $count;
88+
}
6989
}
90+
7091
}
7192

7293
/**
@@ -86,15 +107,12 @@ public function handleGetProperties(
86107
}
87108

88109
// 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);
110+
if ($node instanceof Directory && $propFind->getDepth() !== 0) {
111+
$this->cacheDirectory($node, $propFind);
94112
}
95113

96114
$propFind->handle(self::PROPERTY_NAME_COUNT, function () use ($node): int {
97-
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
115+
return $this->cachedCount[$node->getId()] ?? $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
98116
});
99117

100118
$propFind->handle(self::PROPERTY_NAME_HREF, function () use ($node): ?string {
@@ -130,8 +148,7 @@ public function getUnreadCount(Node $node): ?int {
130148
return null;
131149
}
132150

133-
$lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
134-
135-
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
151+
$objectId = (string)$node->getId();
152+
return $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', [$objectId], $user)[$objectId];
136153
}
137154
}

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('getNumberOfUnreadCommentsForObjects')
108+
->willReturn(['4567' => 42]);
109109

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

lib/private/Comments/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime
632632
/** @inheritDoc */
633633
public function getNumberOfCommentsForObjects(string $objectType, array $objectIds, ?\DateTime $notOlderThan = null, string $verb = ''): array {
634634
$qb = $this->dbConn->getQueryBuilder();
635-
$query = $qb->select($qb->func()->count('id'), 'object_id')
635+
$query = $qb->select($qb->func()->count('id', 'num_comments'), 'object_id')
636636
->from('comments')
637637
->where($qb->expr()->eq('object_type', $qb->createNamedParameter($objectType, IQueryBuilder::PARAM_STR)))
638638
->andWhere($qb->expr()->in('object_id', $qb->createNamedParameter($objectIds, IQueryBuilder::PARAM_STR_ARRAY)));
@@ -651,7 +651,7 @@ public function getNumberOfCommentsForObjects(string $objectType, array $objectI
651651
$comments = array_fill_keys($objectIds, 0);
652652
$resultStatement = $query->execute();
653653
while ($data = $resultStatement->fetch()) {
654-
$comments[$data[1]] = (int)$data[0];
654+
$comments[$data['object_id']] = (int)$data['num_comments'];
655655
}
656656
$resultStatement->closeCursor();
657657

lib/public/Comments/ICommentsManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public function getNumberOfCommentsForObject($objectType, $objectId, ?\DateTime
191191
* @param $objectIds string[] the ids of the object
192192
* @param \DateTime|null $notOlderThan optional, timestamp of the oldest comments
193193
* that may be returned
194-
* @param string $verb Limit the verb of the comment - Added in 14.0.0
194+
* @param string $verb Limit the verb of the comment
195195
* @return array<string, int>
196196
* @since 32.0.0
197197
*/

0 commit comments

Comments
 (0)