Skip to content

Commit daef65a

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 fa5548a commit daef65a

4 files changed

Lines changed: 38 additions & 19 deletions

File tree

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CommentPropertiesPlugin extends ServerPlugin {
2222

2323
protected ?Server $server = null;
2424
private array $cachedUnreadCount = [];
25+
private array $cachedCount = [];
2526
private array $cachedDirectories = [];
2627

2728
public function __construct(
@@ -48,7 +49,11 @@ public function initialize(\Sabre\DAV\Server $server) {
4849
$this->server->on('propFind', [$this, 'handleGetProperties']);
4950
}
5051

51-
private function cacheDirectory(Directory $directory): void {
52+
private function cacheDirectory(Directory $directory, PropFind $propFind): void {
53+
if (is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD)) && is_null($propFind->getStatus(self::PROPERTY_NAME_COUNT))) {
54+
return;
55+
}
56+
5257
$children = $directory->getChildren();
5358

5459
$ids = [];
@@ -66,24 +71,39 @@ private function cacheDirectory(Directory $directory): void {
6671
}
6772

6873
$ids[] = (string)$directory->getId();
69-
$unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $this->userSession->getUser());
74+
if (!is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))) {
75+
$user = $this->userSession->getUser();
76+
if ($user) {
77+
$unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $user);
78+
foreach ($unread as $id => $count) {
79+
$this->cachedUnreadCount[(int)$id] = $count;
80+
}
81+
} else {
82+
foreach ($ids as $id) {
83+
$this->cachedUnreadCount[(int)$id] = null;
84+
}
85+
}
86+
}
7087

71-
foreach ($unread as $id => $count) {
72-
$this->cachedUnreadCount[(int)$id] = $count;
88+
if (!is_null($propFind->getStatus(self::PROPERTY_NAME_COUNT))) {
89+
$commentCounts = $this->commentsManager->getNumberOfCommentsForObjects('files', $ids);
90+
foreach ($commentCounts as $id => $count) {
91+
$this->cachedCount[(int)$id] = $count;
92+
}
7393
}
94+
7495
}
7596

76-
private function preloadCollection(PropFind $propFind, ICollection $collection):
77-
void {
97+
private function preloadCollection(PropFind $propFind, ICollection $collection): void {
7898
if (!($collection instanceof Directory)) {
7999
return;
80100
}
81101

82102
$collectionPath = $collection->getPath();
83-
if (!isset($this->cachedDirectories[$collectionPath]) && $propFind->getStatus(
84-
self::PROPERTY_NAME_UNREAD
85-
) !== null) {
86-
$this->cacheDirectory($collection);
103+
if (!isset($this->cachedDirectories[$collectionPath]) && (
104+
$propFind->getStatus( self::PROPERTY_NAME_UNREAD) !== null
105+
|| $propFind->getStatus( self::PROPERTY_NAME_COUNT) !== null)) {
106+
$this->cacheDirectory($collection, $propFind);
87107
$this->cachedDirectories[$collectionPath] = true;
88108
}
89109
}
@@ -105,7 +125,7 @@ public function handleGetProperties(
105125
}
106126

107127
$propFind->handle(self::PROPERTY_NAME_COUNT, function () use ($node): int {
108-
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
128+
return $this->cachedCount[$node->getId()] ?? $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
109129
});
110130

111131
$propFind->handle(self::PROPERTY_NAME_HREF, function () use ($node): ?string {
@@ -141,8 +161,7 @@ public function getUnreadCount(Node $node): ?int {
141161
return null;
142162
}
143163

144-
$lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
145-
146-
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
164+
$objectId = (string)$node->getId();
165+
return $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', [$objectId], $user)[$objectId];
147166
}
148167
}

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)