Skip to content

Commit

Permalink
Merge pull request #33615 from nextcloud/perf/noid/user-displayname-c…
Browse files Browse the repository at this point in the history
…ache-for-activity-providers

Use user name cache in activity providers
  • Loading branch information
nickvergessen authored Aug 19, 2022
2 parents 7d7f7ab + 7e11778 commit 71065f5
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 196 deletions.
16 changes: 1 addition & 15 deletions apps/comments/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -213,22 +211,10 @@ protected function generateFileParameter(int $id, string $path): array {
}

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;
}
}
24 changes: 2 additions & 22 deletions apps/dav/lib/CalDAV/Activity/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,14 @@ protected function generateLegacyCalendarParameter($id, $name) {
];
}

/**
* @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
Expand Down
22 changes: 1 addition & 21 deletions apps/dav/lib/CardDAV/Activity/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,14 @@ protected function generateAddressbookParameter(array $data, IL10N $l): array {
];
}

/**
* @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
Expand Down
37 changes: 0 additions & 37 deletions apps/dav/tests/unit/CalDAV/Activity/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,41 +160,4 @@ public function testGenerateGroupParameter(string $gid) {
'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]));
}
}
8 changes: 4 additions & 4 deletions apps/files/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,12 @@ protected function getParentEndToEndEncryptionContainer(Folder $userFolder, Node
*/
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,
];
}

Expand Down
11 changes: 2 additions & 9 deletions apps/files/tests/Activity/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,10 @@ public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloud
$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())
Expand Down
8 changes: 4 additions & 4 deletions apps/files_sharing/lib/Activity/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ protected function getFile($parameter, IEvent $event = null) {
*/
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,
];
}

Expand Down
25 changes: 1 addition & 24 deletions apps/settings/lib/Activity/GroupProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class GroupProvider implements IProvider {

/** @var string[] */
protected $groupDisplayNames = [];
/** @var string[] */
protected $userDisplayNames = [];


public function __construct(L10nFactory $l10n,
Expand Down Expand Up @@ -169,32 +167,11 @@ protected function getGroupDisplayName(string $gid): string {
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;
}
}
}
18 changes: 1 addition & 17 deletions apps/settings/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -206,23 +203,10 @@ protected function setSubjects(IEvent $event, string $subject, array $parameters
}

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;
}
}
22 changes: 1 addition & 21 deletions apps/sharebymail/lib/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ class Activity implements IProvider {
/** @var IContactsManager */
protected $contactsManager;

/** @var array */
protected $displayNames = [];

/** @var array */
protected $contactNames = [];

Expand Down Expand Up @@ -346,14 +343,10 @@ protected function generateEmailParameter($email) {
* @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,
];
}

Expand Down Expand Up @@ -381,17 +374,4 @@ protected function getContactName($email) {

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;
}
}
}
24 changes: 2 additions & 22 deletions apps/systemtags/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ class Provider implements IProvider {
/** @var IUserManager */
protected $userManager;

/** @var string[] */
protected $displayNames = [];

/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
Expand Down Expand Up @@ -334,15 +331,11 @@ protected function getSystemTagParameter($parameter) {
];
}

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,
];
}

Expand All @@ -355,17 +348,4 @@ protected function generatePlainSystemTag(array $parameter) {
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;
}
}
}

0 comments on commit 71065f5

Please sign in to comment.