aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJulius Knorr <jus@bitgrid.net>2025-04-09 12:33:38 +0200
committerGitHub <noreply@github.com>2025-04-09 12:33:38 +0200
commit455fb740d085c5649b0892b1185989451f7a14fa (patch)
tree5b2d723a63815a4f1ffe2f9f7edf7994355291a7 /lib
parent3808f86c88053b7cd226a6af953cae239142f235 (diff)
parent6a3c53def3155dcff4aa6bd532e34beab8918035 (diff)
downloadnextcloud-server-master.tar.gz
nextcloud-server-master.zip
Merge pull request #52066 from nextcloud/perf/noid/dont-load-addressbook-on-resolving-cloudidHEADmaster
fix(federation): Don't load the addressbook when resolving a cloud ID
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Federation/CloudId.php42
-rw-r--r--lib/private/Federation/CloudIdManager.php16
2 files changed, 30 insertions, 28 deletions
diff --git a/lib/private/Federation/CloudId.php b/lib/private/Federation/CloudId.php
index c20dbfc6418..b807c29d812 100644
--- a/lib/private/Federation/CloudId.php
+++ b/lib/private/Federation/CloudId.php
@@ -9,29 +9,15 @@ declare(strict_types=1);
namespace OC\Federation;
use OCP\Federation\ICloudId;
+use OCP\Federation\ICloudIdManager;
class CloudId implements ICloudId {
- /** @var string */
- private $id;
- /** @var string */
- private $user;
- /** @var string */
- private $remote;
- /** @var string|null */
- private $displayName;
-
- /**
- * CloudId constructor.
- *
- * @param string $id
- * @param string $user
- * @param string $remote
- */
- public function __construct(string $id, string $user, string $remote, ?string $displayName = null) {
- $this->id = $id;
- $this->user = $user;
- $this->remote = $remote;
- $this->displayName = $displayName;
+ public function __construct(
+ protected string $id,
+ protected string $user,
+ protected string $remote,
+ protected ?string $displayName = null,
+ ) {
}
/**
@@ -44,12 +30,18 @@ class CloudId implements ICloudId {
}
public function getDisplayId(): string {
+ if ($this->displayName === null) {
+ /** @var CloudIdManager $cloudIdManager */
+ $cloudIdManager = \OCP\Server::get(ICloudIdManager::class);
+ $this->displayName = $cloudIdManager->getDisplayNameFromContact($this->getId());
+ }
+
+ $atHost = str_replace(['http://', 'https://'], '', $this->getRemote());
+
if ($this->displayName) {
- $atPos = strrpos($this->getId(), '@');
- $atHost = substr($this->getId(), $atPos);
- return $this->displayName . $atHost;
+ return $this->displayName . '@' . $atHost;
}
- return str_replace('https://', '', str_replace('http://', '', $this->getId()));
+ return $this->getUser() . '@' . $atHost;
}
/**
diff --git a/lib/private/Federation/CloudIdManager.php b/lib/private/Federation/CloudIdManager.php
index 1c808c03eda..fa8ac0fa0c2 100644
--- a/lib/private/Federation/CloudIdManager.php
+++ b/lib/private/Federation/CloudIdManager.php
@@ -28,6 +28,7 @@ class CloudIdManager implements ICloudIdManager {
/** @var IUserManager */
private $userManager;
private ICache $memCache;
+ private ICache $displayNameCache;
/** @var array[] */
private array $cache = [];
@@ -42,6 +43,7 @@ class CloudIdManager implements ICloudIdManager {
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->memCache = $cacheFactory->createDistributed('cloud_id_');
+ $this->displayNameCache = $cacheFactory->createDistributed('cloudid_name_');
$eventDispatcher->addListener(UserChangedEvent::class, [$this, 'handleUserEvent']);
$eventDispatcher->addListener(CardUpdatedEvent::class, [$this, 'handleCardEvent']);
}
@@ -108,13 +110,18 @@ class CloudIdManager implements ICloudIdManager {
if (!empty($user) && !empty($remote)) {
$remote = $this->ensureDefaultProtocol($remote);
- return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id));
+ return new CloudId($id, $user, $remote, null);
}
}
throw new \InvalidArgumentException('Invalid cloud id');
}
- protected function getDisplayNameFromContact(string $cloudId): ?string {
+ public function getDisplayNameFromContact(string $cloudId): ?string {
+ $cachedName = $this->displayNameCache->get($cloudId);
+ if ($cachedName !== null) {
+ return $cachedName;
+ }
+
$addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD'], [
'limit' => 1,
'enumeration' => false,
@@ -128,14 +135,17 @@ class CloudIdManager implements ICloudIdManager {
// Warning, if user decides to make their full name local only,
// no FN is found on federated servers
if (isset($entry['FN'])) {
+ $this->displayNameCache->set($cloudId, $entry['FN'], 15 * 60);
return $entry['FN'];
} else {
+ $this->displayNameCache->set($cloudId, $cloudID, 15 * 60);
return $cloudID;
}
}
}
}
}
+ $this->displayNameCache->set($cloudId, $cloudId, 15 * 60);
return null;
}
@@ -168,7 +178,7 @@ class CloudIdManager implements ICloudIdManager {
$localUser = $this->userManager->get($user);
$displayName = $localUser ? $localUser->getDisplayName() : '';
} else {
- $displayName = $this->getDisplayNameFromContact($user . '@' . $host);
+ $displayName = null;
}
// For the visible cloudID we only strip away https