From 2e2ab821ece62437d21dfb380cfde740644b034d Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 16 Aug 2022 11:45:26 +0200 Subject: [PATCH 1/4] Use DisplayNameCache Reduce query by 1/3 when fetching list of rooms on the production instance Signed-off-by: Carl Schwan --- lib/Chat/MessageParser.php | 4 ++-- lib/Manager.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Chat/MessageParser.php b/lib/Chat/MessageParser.php index 5428b9f7c9e..b0b849b86a5 100644 --- a/lib/Chat/MessageParser.php +++ b/lib/Chat/MessageParser.php @@ -79,8 +79,8 @@ protected function setActor(Message $message): void { $actorId = $comment->getActorId(); $displayName = ''; if ($comment->getActorType() === Attendee::ACTOR_USERS) { - $user = $this->userManager->get($comment->getActorId()); - $displayName = $user instanceof IUser ? $user->getDisplayName() : $comment->getActorId(); + $displayNameCache = \OCP\Server::get(\OC\User\DisplayNameCache::class); + $displayName = $displayNameCache->getDisplayName($comment->getActorId()); } elseif ($comment->getActorType() === Attendee::ACTOR_BRIDGED) { $displayName = $comment->getActorId(); $actorId = MatterbridgeManager::BRIDGE_BOT_USERID; diff --git a/lib/Manager.php b/lib/Manager.php index 5acf16c2cc1..4cb44518ddd 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -988,10 +988,10 @@ public function resolveRoomDisplayName(Room $room, string $userId): string { $otherParticipant = ''; $userIsParticipant = false; + $displayNameCache = \OCP\Server::get(\OC\User\DisplayNameCache::class); foreach ($users as $participantId) { if ($participantId !== $userId) { - $user = $this->userManager->get($participantId); - $otherParticipant = $user instanceof IUser ? $user->getDisplayName() : $participantId; + $otherParticipant = $displayNameCache->getDisplayName($participantId); } else { $userIsParticipant = true; } From 197c14398107c30aac46c08cce4eae8a1e1a672d Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 16 Aug 2022 11:50:10 +0200 Subject: [PATCH 2/4] Use new OCP api Signed-off-by: Carl Schwan --- lib/Chat/MessageParser.php | 3 +-- lib/Manager.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/Chat/MessageParser.php b/lib/Chat/MessageParser.php index b0b849b86a5..ef011bffc6f 100644 --- a/lib/Chat/MessageParser.php +++ b/lib/Chat/MessageParser.php @@ -79,8 +79,7 @@ protected function setActor(Message $message): void { $actorId = $comment->getActorId(); $displayName = ''; if ($comment->getActorType() === Attendee::ACTOR_USERS) { - $displayNameCache = \OCP\Server::get(\OC\User\DisplayNameCache::class); - $displayName = $displayNameCache->getDisplayName($comment->getActorId()); + $displayName = $this->userManager->getDisplayName($comment->getActorId()); } elseif ($comment->getActorType() === Attendee::ACTOR_BRIDGED) { $displayName = $comment->getActorId(); $actorId = MatterbridgeManager::BRIDGE_BOT_USERID; diff --git a/lib/Manager.php b/lib/Manager.php index 4cb44518ddd..65a6167c685 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -988,10 +988,9 @@ public function resolveRoomDisplayName(Room $room, string $userId): string { $otherParticipant = ''; $userIsParticipant = false; - $displayNameCache = \OCP\Server::get(\OC\User\DisplayNameCache::class); foreach ($users as $participantId) { if ($participantId !== $userId) { - $otherParticipant = $displayNameCache->getDisplayName($participantId); + $otherParticipant = $this->userManager->getDisplayName($participantId); } else { $userIsParticipant = true; } From 36f511d19844c1d4f661ecdf9ac01ce7d90916b8 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 16 Aug 2022 13:16:10 +0200 Subject: [PATCH 3/4] Add display name cache to SystemMessage Signed-off-by: Carl Schwan --- composer.lock | 8 ++++---- lib/Chat/MessageParser.php | 3 +-- lib/Chat/Parser/SystemMessage.php | 7 +++---- lib/Manager.php | 2 +- tests/php/Chat/Parser/SystemMessageTest.php | 10 +++------- 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/composer.lock b/composer.lock index 524f88e1685..11614843aa4 100644 --- a/composer.lock +++ b/composer.lock @@ -242,12 +242,12 @@ "source": { "type": "git", "url": "https://github.com/ChristophWurst/nextcloud_composer.git", - "reference": "577103cb24552d134a6338014fe665cabfd9c2c8" + "reference": "433f70a9ddfece0233927997ae5ff38070fc1fcc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/577103cb24552d134a6338014fe665cabfd9c2c8", - "reference": "577103cb24552d134a6338014fe665cabfd9c2c8", + "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/433f70a9ddfece0233927997ae5ff38070fc1fcc", + "reference": "433f70a9ddfece0233927997ae5ff38070fc1fcc", "shasum": "" }, "require": { @@ -278,7 +278,7 @@ "issues": "https://github.com/ChristophWurst/nextcloud_composer/issues", "source": "https://github.com/ChristophWurst/nextcloud_composer/tree/master" }, - "time": "2022-08-09T02:21:50+00:00" + "time": "2022-08-17T02:31:52+00:00" }, { "name": "composer/package-versions-deprecated", diff --git a/lib/Chat/MessageParser.php b/lib/Chat/MessageParser.php index ef011bffc6f..e64287c4841 100644 --- a/lib/Chat/MessageParser.php +++ b/lib/Chat/MessageParser.php @@ -34,7 +34,6 @@ use OCP\Comments\IComment; use OCP\EventDispatcher\IEventDispatcher; use OCP\IL10N; -use OCP\IUser; use OCP\IUserManager; /** @@ -79,7 +78,7 @@ protected function setActor(Message $message): void { $actorId = $comment->getActorId(); $displayName = ''; if ($comment->getActorType() === Attendee::ACTOR_USERS) { - $displayName = $this->userManager->getDisplayName($comment->getActorId()); + $displayName = $this->userManager->getDisplayName($comment->getActorId()) ?? $comment->getActorId(); } elseif ($comment->getActorType() === Attendee::ACTOR_BRIDGED) { $displayName = $comment->getActorId(); $actorId = MatterbridgeManager::BRIDGE_BOT_USERID; diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index 4c1230f0cef..51666142d04 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -44,7 +44,6 @@ use OCP\IL10N; use OCP\IPreview as IPreviewManager; use OCP\IURLGenerator; -use OCP\IUser; use OCP\IUserManager; use OCP\Server; use OCP\Share\Exceptions\ShareNotFound; @@ -698,9 +697,9 @@ protected function getRemoteUser(string $federationId): array { } protected function getDisplayName(string $uid): string { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); + $userName = $this->userManager->getDisplayName($uid); + if ($userName !== null) { + return $userName; } throw new ParticipantNotFoundException(); diff --git a/lib/Manager.php b/lib/Manager.php index 65a6167c685..f72fc5c5b1b 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -990,7 +990,7 @@ public function resolveRoomDisplayName(Room $room, string $userId): string { foreach ($users as $participantId) { if ($participantId !== $userId) { - $otherParticipant = $this->userManager->getDisplayName($participantId); + $otherParticipant = $this->userManager->getDisplayName($participantId) ?? $participantId; } else { $userIsParticipant = true; } diff --git a/tests/php/Chat/Parser/SystemMessageTest.php b/tests/php/Chat/Parser/SystemMessageTest.php index ad9daae63da..1be6afc218f 100644 --- a/tests/php/Chat/Parser/SystemMessageTest.php +++ b/tests/php/Chat/Parser/SystemMessageTest.php @@ -948,17 +948,13 @@ public function testGetDisplayName(string $uid, bool $validUser, string $name) { $parser = $this->getParser(); if ($validUser) { - $user = $this->createMock(IUser::class); - $user->expects($this->once()) - ->method('getDisplayName') - ->willReturn($name); $this->userManager->expects($this->once()) - ->method('get') + ->method('getDisplayName') ->with($uid) - ->willReturn($user); + ->willReturn($name); } else { $this->userManager->expects($this->once()) - ->method('get') + ->method('getDisplayName') ->with($uid) ->willReturn(null); $this->expectException(ParticipantNotFoundException::class); From 724db041e1ea4986ebb0dc4fb07b25363b5c96ab Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 17 Aug 2022 10:31:03 +0200 Subject: [PATCH 4/4] Fix CS Signed-off-by: Joas Schilling --- tests/php/Chat/Parser/SystemMessageTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/php/Chat/Parser/SystemMessageTest.php b/tests/php/Chat/Parser/SystemMessageTest.php index 1be6afc218f..1ffa7d5d109 100644 --- a/tests/php/Chat/Parser/SystemMessageTest.php +++ b/tests/php/Chat/Parser/SystemMessageTest.php @@ -44,7 +44,6 @@ use OCP\IL10N; use OCP\IPreview as IPreviewManager; use OCP\IURLGenerator; -use OCP\IUser; use OCP\IUserManager; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IShare;