Use user displayname cache in activities

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2022-08-18 09:57:34 +02:00
Родитель 62c85c3c7e
Коммит 659c2ba7ff
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: C400AAF20C1BB6FC
2 изменённых файлов: 12 добавлений и 64 удалений

Просмотреть файл

@ -43,9 +43,6 @@ abstract class Base implements IProvider {
protected IUserManager $userManager;
protected Manager $manager;
/** @var string[] */
protected array $displayNames = [];
public function __construct(IFactory $languageFactory,
IURLGenerator $url,
Config $config,
@ -134,22 +131,10 @@ abstract class Base implements IProvider {
}
protected function getUser(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;
}
}

Просмотреть файл

@ -254,44 +254,7 @@ class BaseTest extends TestCase {
], self::invokePrivate($provider, 'getRoom', [$room, 'user']));
}
public function dataGetUser() {
return [
['test', [], false, 'Test'],
['foo', ['admin' => 'Admin'], false, 'Bar'],
['admin', ['admin' => 'Administrator'], true, 'Administrator'],
];
}
/**
* @dataProvider dataGetUser
*
* @param string $uid
* @param array $cache
* @param bool $cacheHit
* @param string $name
*/
public function testGetUser($uid, $cache, $cacheHit, $name) {
$provider = $this->getProvider(['getDisplayName']);
self::invokePrivate($provider, 'displayNames', [$cache]);
if (!$cacheHit) {
$provider->expects($this->once())
->method('getDisplayName')
->with($uid)
->willReturn($name);
} else {
$provider->expects($this->never())
->method('getDisplayName');
}
$result = self::invokePrivate($provider, 'getUser', [$uid]);
$this->assertSame('user', $result['type']);
$this->assertSame($uid, $result['id']);
$this->assertSame($name, $result['name']);
}
public function dataGetDisplayName() {
public function dataGetUser(): array {
return [
['test', true, 'Test'],
['foo', false, 'foo'],
@ -299,31 +262,31 @@ class BaseTest extends TestCase {
}
/**
* @dataProvider dataGetDisplayName
* @dataProvider dataGetUser
*
* @param string $uid
* @param bool $validUser
* @param string $name
*/
public function testGetDisplayName($uid, $validUser, $name) {
public function testGetUser(string $uid, bool $validUser, string $name): void {
$provider = $this->getProvider();
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->assertSame($name, self::invokePrivate($provider, 'getDisplayName', [$uid]));
$this->assertSame([
'type' => 'user',
'id' => $uid,
'name' => $name,
], self::invokePrivate($provider, 'getUser', [$uid]));
}
}