mirror of
https://github.com/nextcloud/server.git
synced 2026-10-07 19:51:40 +02:00
PHP 8.3 allows class constants to declare a type. Adopt it where doing so cannot break third-party apps. Typing an inheritable constant is a hard BC break: a subclass that redeclares it untyped fails to load with "Type of C::FOO must be compatible with P::FOO of type string". Changes are therefore limited to constants that cannot be redeclared by a subclass: - private const (not inherited) - final public/protected const - constants declared in a final class or an enum Interface constants, trait constants, and public/protected constants in non-final (including abstract) classes are left untyped, as is all of lib/public (OCP) and lib/unstable (NCU). One review-requested exception: the public OBJECT_PREFIX/OBJECT_SUFFIX constants of the app-internal CalDAV import helpers (TextImporter, XmlImporter) are typed as well; these classes are not public API and have no subclasses. Only string, int and array are used. float is avoided because it would silently coerce an int literal and change === comparisons. No constant name or value is modified: every changed line adds only the type token, so runtime behaviour is unchanged. Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
91 lines
2.3 KiB
PHP
91 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OC\User;
|
|
|
|
use OCP\EventDispatcher\Event;
|
|
use OCP\EventDispatcher\IEventListener;
|
|
use OCP\ICache;
|
|
use OCP\ICacheFactory;
|
|
use OCP\IUser;
|
|
use OCP\IUserManager;
|
|
use OCP\User\Events\UserChangedEvent;
|
|
use OCP\User\Events\UserDeletedEvent;
|
|
|
|
/**
|
|
* Class that cache the relation UserId -> Display name
|
|
*
|
|
* This saves fetching the user from a user backend and later on fetching
|
|
* their preferences. It's generally not an issue if this data is slightly
|
|
* outdated.
|
|
* @template-implements IEventListener<UserChangedEvent|UserDeletedEvent>
|
|
*/
|
|
class DisplayNameCache implements IEventListener {
|
|
private const int CACHE_TTL = 24 * 60 * 60; // 1 day
|
|
|
|
private array $cache = [];
|
|
private ICache $memCache;
|
|
|
|
public function __construct(
|
|
ICacheFactory $cacheFactory,
|
|
private IUserManager $userManager,
|
|
) {
|
|
$this->memCache = $cacheFactory->createDistributed('displayNameMappingCache');
|
|
}
|
|
|
|
/**
|
|
* @return ?non-empty-string
|
|
*/
|
|
public function getDisplayName(string $userId): ?string {
|
|
if (isset($this->cache[$userId])) {
|
|
return $this->cache[$userId];
|
|
}
|
|
|
|
if (strlen($userId) > IUser::MAX_USERID_LENGTH) {
|
|
return null;
|
|
}
|
|
|
|
$displayName = $this->memCache->get($userId);
|
|
if ($displayName) {
|
|
$this->cache[$userId] = $displayName;
|
|
return $displayName;
|
|
}
|
|
|
|
$user = $this->userManager->get($userId);
|
|
if ($user) {
|
|
$displayName = $user->getDisplayName();
|
|
} else {
|
|
$displayName = null;
|
|
}
|
|
$this->cache[$userId] = $displayName;
|
|
$this->memCache->set($userId, $displayName, self::CACHE_TTL);
|
|
|
|
return $displayName;
|
|
}
|
|
|
|
public function clear(): void {
|
|
$this->cache = [];
|
|
$this->memCache->clear();
|
|
}
|
|
|
|
#[\Override]
|
|
public function handle(Event $event): void {
|
|
if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') {
|
|
$userId = $event->getUser()->getUID();
|
|
$newDisplayName = $event->getValue();
|
|
$this->cache[$userId] = $newDisplayName;
|
|
$this->memCache->set($userId, $newDisplayName, self::CACHE_TTL);
|
|
}
|
|
if ($event instanceof UserDeletedEvent) {
|
|
$userId = $event->getUser()->getUID();
|
|
unset($this->cache[$userId]);
|
|
$this->memCache->remove($userId);
|
|
}
|
|
}
|
|
}
|