diff options
author | Joas Schilling <coding@schilljs.com> | 2022-08-18 23:52:34 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-08-19 10:48:32 +0200 |
commit | 7e1177819023a185480b5251e3a22dc429948518 (patch) | |
tree | 66288b17692cfcdde3e3273051e00224b4167b1f /apps | |
parent | cab0f1327e28104ef61e8767f963d20cf38544af (diff) | |
download | nextcloud-server-7e1177819023a185480b5251e3a22dc429948518.tar.gz nextcloud-server-7e1177819023a185480b5251e3a22dc429948518.zip |
Use user name cache in activity providers
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/comments/lib/Activity/Provider.php | 16 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/Activity/Provider/Base.php | 24 | ||||
-rw-r--r-- | apps/dav/lib/CardDAV/Activity/Provider/Base.php | 22 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/Activity/Provider/BaseTest.php | 37 | ||||
-rw-r--r-- | apps/files/lib/Activity/Provider.php | 8 | ||||
-rw-r--r-- | apps/files/tests/Activity/ProviderTest.php | 11 | ||||
-rw-r--r-- | apps/files_sharing/lib/Activity/Providers/Base.php | 8 | ||||
-rw-r--r-- | apps/settings/lib/Activity/GroupProvider.php | 25 | ||||
-rw-r--r-- | apps/settings/lib/Activity/Provider.php | 18 | ||||
-rw-r--r-- | apps/sharebymail/lib/Activity.php | 22 | ||||
-rw-r--r-- | apps/systemtags/lib/Activity/Provider.php | 24 |
11 files changed, 19 insertions, 196 deletions
diff --git a/apps/comments/lib/Activity/Provider.php b/apps/comments/lib/Activity/Provider.php index 715be29e2ed..c6e55326580 100644 --- a/apps/comments/lib/Activity/Provider.php +++ b/apps/comments/lib/Activity/Provider.php @@ -43,8 +43,6 @@ class Provider implements IProvider { protected ICommentsManager $commentsManager; protected IUserManager $userManager; protected IManager $activityManager; - /** @var string[] */ - protected array $displayNames = []; public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) { $this->languageFactory = $languageFactory; @@ -213,22 +211,10 @@ class Provider implements IProvider { } protected function generateUserParameter(string $uid): array { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); - } - return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } - - protected function getDisplayName(string $uid): string { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } - return $uid; - } } diff --git a/apps/dav/lib/CalDAV/Activity/Provider/Base.php b/apps/dav/lib/CalDAV/Activity/Provider/Base.php index 7f70980a72b..48ed7b8b107 100644 --- a/apps/dav/lib/CalDAV/Activity/Provider/Base.php +++ b/apps/dav/lib/CalDAV/Activity/Provider/Base.php @@ -112,35 +112,15 @@ abstract class Base implements IProvider { ]; } - /** - * @param string $uid - * @return array - */ - protected function generateUserParameter($uid) { - if (!isset($this->userDisplayNames[$uid])) { - $this->userDisplayNames[$uid] = $this->getUserDisplayName($uid); - } - + protected function generateUserParameter(string $uid): array { return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->userDisplayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } /** - * @param string $uid - * @return string - */ - protected function getUserDisplayName($uid) { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } - return $uid; - } - - /** * @param string $gid * @return array */ diff --git a/apps/dav/lib/CardDAV/Activity/Provider/Base.php b/apps/dav/lib/CardDAV/Activity/Provider/Base.php index 2f6de31de15..3e7a966c08a 100644 --- a/apps/dav/lib/CardDAV/Activity/Provider/Base.php +++ b/apps/dav/lib/CardDAV/Activity/Provider/Base.php @@ -98,35 +98,15 @@ abstract class Base implements IProvider { ]; } - /** - * @param string $uid - * @return array - */ protected function generateUserParameter(string $uid): array { - if (!isset($this->userDisplayNames[$uid])) { - $this->userDisplayNames[$uid] = $this->getUserDisplayName($uid); - } - return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->userDisplayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } /** - * @param string $uid - * @return string - */ - protected function getUserDisplayName(string $uid): string { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } - return $uid; - } - - /** * @param string $gid * @return array */ diff --git a/apps/dav/tests/unit/CalDAV/Activity/Provider/BaseTest.php b/apps/dav/tests/unit/CalDAV/Activity/Provider/BaseTest.php index 21cafee4f88..9e1e02189f5 100644 --- a/apps/dav/tests/unit/CalDAV/Activity/Provider/BaseTest.php +++ b/apps/dav/tests/unit/CalDAV/Activity/Provider/BaseTest.php @@ -160,41 +160,4 @@ class BaseTest extends TestCase { 'name' => $gid, ], $this->invokePrivate($this->provider, 'generateGroupParameter', [$gid])); } - - public function dataGenerateUserParameter() { - $u1 = $this->createMock(IUser::class); - $u1->expects($this->any()) - ->method('getDisplayName') - ->willReturn('User 1'); - return [ - ['u1', 'User 1', $u1], - ['u2', 'u2', null], - ]; - } - - /** - * @dataProvider dataGenerateUserParameter - * @param string $uid - * @param string $displayName - * @param IUser|null $user - */ - public function testGenerateUserParameter(string $uid, string $displayName, ?IUser $user) { - $this->userManager->expects($this->once()) - ->method('get') - ->with($uid) - ->willReturn($user); - - $this->assertEquals([ - 'type' => 'user', - 'id' => $uid, - 'name' => $displayName, - ], $this->invokePrivate($this->provider, 'generateUserParameter', [$uid])); - - // Test caching (only 1 user manager invocation allowed) - $this->assertEquals([ - 'type' => 'user', - 'id' => $uid, - 'name' => $displayName, - ], $this->invokePrivate($this->provider, 'generateUserParameter', [$uid])); - } } diff --git a/apps/files/lib/Activity/Provider.php b/apps/files/lib/Activity/Provider.php index a7db85a0de1..c21e8f9ad16 100644 --- a/apps/files/lib/Activity/Provider.php +++ b/apps/files/lib/Activity/Provider.php @@ -527,12 +527,12 @@ class Provider implements IProvider { */ protected function getUser($uid) { // First try local user - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { + $displayName = $this->userManager->getDisplayName($uid); + if ($displayName !== null) { return [ 'type' => 'user', - 'id' => $user->getUID(), - 'name' => $user->getDisplayName(), + 'id' => $uid, + 'name' => $displayName, ]; } diff --git a/apps/files/tests/Activity/ProviderTest.php b/apps/files/tests/Activity/ProviderTest.php index 7c5bdf27ba1..3566d83f415 100644 --- a/apps/files/tests/Activity/ProviderTest.php +++ b/apps/files/tests/Activity/ProviderTest.php @@ -177,17 +177,10 @@ class ProviderTest extends TestCase { $provider = $this->getProvider(); if ($userDisplayName !== null) { - $user = $this->createMock(IUser::class); - $user->expects($this->once()) - ->method('getUID') - ->willReturn($uid); - $user->expects($this->once()) - ->method('getDisplayName') - ->willReturn($userDisplayName); $this->userManager->expects($this->once()) - ->method('get') + ->method('getDisplayName') ->with($uid) - ->willReturn($user); + ->willReturn($userDisplayName); } if ($cloudIdData !== null) { $this->cloudIdManager->expects($this->once()) diff --git a/apps/files_sharing/lib/Activity/Providers/Base.php b/apps/files_sharing/lib/Activity/Providers/Base.php index cbd16c134cc..ca5b9b55b62 100644 --- a/apps/files_sharing/lib/Activity/Providers/Base.php +++ b/apps/files_sharing/lib/Activity/Providers/Base.php @@ -177,12 +177,12 @@ abstract class Base implements IProvider { */ protected function getUser($uid) { // First try local user - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { + $displayName = $this->userManager->getDisplayName($uid); + if ($displayName !== null) { return [ 'type' => 'user', - 'id' => $user->getUID(), - 'name' => $user->getDisplayName(), + 'id' => $uid, + 'name' => $displayName, ]; } diff --git a/apps/settings/lib/Activity/GroupProvider.php b/apps/settings/lib/Activity/GroupProvider.php index a1709de5c3f..466bb9abeee 100644 --- a/apps/settings/lib/Activity/GroupProvider.php +++ b/apps/settings/lib/Activity/GroupProvider.php @@ -51,8 +51,6 @@ class GroupProvider implements IProvider { /** @var string[] */ protected $groupDisplayNames = []; - /** @var string[] */ - protected $userDisplayNames = []; public function __construct(L10nFactory $l10n, @@ -169,32 +167,11 @@ class GroupProvider implements IProvider { return $gid; } - /** - * @param string $uid - * @return array - */ protected function generateUserParameter(string $uid): array { - if (!isset($this->displayNames[$uid])) { - $this->userDisplayNames[$uid] = $this->getDisplayName($uid); - } - return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->userDisplayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } - - /** - * @param string $uid - * @return string - */ - protected function getDisplayName(string $uid): string { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } else { - return $uid; - } - } } diff --git a/apps/settings/lib/Activity/Provider.php b/apps/settings/lib/Activity/Provider.php index a6314fdfb11..7d8a7f0da9a 100644 --- a/apps/settings/lib/Activity/Provider.php +++ b/apps/settings/lib/Activity/Provider.php @@ -66,9 +66,6 @@ class Provider implements IProvider { /** @var IManager */ private $activityManager; - /** @var string[] cached displayNames - key is the UID and value the displayname */ - protected $displayNames = []; - public function __construct(IFactory $languageFactory, IURLGenerator $url, IUserManager $userManager, @@ -206,23 +203,10 @@ class Provider implements IProvider { } protected function generateUserParameter(string $uid): array { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); - } - return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } - - protected function getDisplayName(string $uid): string { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } - - return $uid; - } } diff --git a/apps/sharebymail/lib/Activity.php b/apps/sharebymail/lib/Activity.php index 3992f0f942b..3693455dda4 100644 --- a/apps/sharebymail/lib/Activity.php +++ b/apps/sharebymail/lib/Activity.php @@ -55,9 +55,6 @@ class Activity implements IProvider { protected $contactsManager; /** @var array */ - protected $displayNames = []; - - /** @var array */ protected $contactNames = []; public const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self'; @@ -346,14 +343,10 @@ class Activity implements IProvider { * @return array */ protected function generateUserParameter($uid) { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); - } - return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } @@ -381,17 +374,4 @@ class Activity implements IProvider { return $email; } - - /** - * @param string $uid - * @return string - */ - protected function getDisplayName($uid) { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } else { - return $uid; - } - } } diff --git a/apps/systemtags/lib/Activity/Provider.php b/apps/systemtags/lib/Activity/Provider.php index fe1df169d40..16a5687fc81 100644 --- a/apps/systemtags/lib/Activity/Provider.php +++ b/apps/systemtags/lib/Activity/Provider.php @@ -55,9 +55,6 @@ class Provider implements IProvider { /** @var IUserManager */ protected $userManager; - /** @var string[] */ - protected $displayNames = []; - /** * @param IFactory $languageFactory * @param IURLGenerator $url @@ -334,15 +331,11 @@ class Provider implements IProvider { ]; } - protected function getUserParameter($uid) { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); - } - + protected function getUserParameter(string $uid): array { return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } @@ -355,17 +348,4 @@ class Provider implements IProvider { return $this->l->t('%s (invisible)', $parameter['name']); } } - - /** - * @param string $uid - * @return string - */ - protected function getDisplayName($uid) { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } else { - return $uid; - } - } } |