mirror of
https://github.com/nextcloud/server.git
synced 2026-06-12 02:00:51 -04:00
feat(UserManager): Add getters for avatar URLs
Signed-off-by: provokateurin <kate@provokateurin.de>
This commit is contained in:
parent
bf2043ea6d
commit
3ce12c83e1
3 changed files with 49 additions and 0 deletions
|
|
@ -19,6 +19,7 @@ use OCP\IConfig;
|
|||
use OCP\IDBConnection;
|
||||
use OCP\IGroup;
|
||||
use OCP\IRequest;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserBackend;
|
||||
use OCP\IUserManager;
|
||||
|
|
@ -37,6 +38,7 @@ use OCP\User\Events\UserCreatedEvent;
|
|||
use OCP\UserInterface;
|
||||
use OCP\Util;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class Manager
|
||||
|
|
@ -70,6 +72,9 @@ class Manager extends PublicEmitter implements IUserManager {
|
|||
|
||||
private DisplayNameCache $displayNameCache;
|
||||
|
||||
// IURLGenerator can't be injected through DI
|
||||
private ?IURLGenerator $urlGenerator;
|
||||
|
||||
// This constructor can't autoload any class requiring a DB connection.
|
||||
public function __construct(
|
||||
private IConfig $config,
|
||||
|
|
@ -862,4 +867,25 @@ class Manager extends PublicEmitter implements IUserManager {
|
|||
public function getExistingUser(string $userId, ?string $displayName = null): IUser {
|
||||
return new LazyUser($userId, $this, $displayName);
|
||||
}
|
||||
|
||||
|
||||
#[\Override]
|
||||
public function getAvatarUrlLight(string $userId, int $size): string {
|
||||
$url = ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => $size]);
|
||||
if ($url === '') {
|
||||
throw new RuntimeException('The URL is empty.');
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function getAvatarUrlDark(string $userId, int $size): string {
|
||||
$url = ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatarDark', ['userId' => $userId, 'size' => $size]);
|
||||
if ($url === '') {
|
||||
throw new RuntimeException('The URL is empty.');
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -266,4 +266,19 @@ interface IUserManager {
|
|||
* @since 33.0.0
|
||||
*/
|
||||
public function getExistingUser(string $userId, ?string $displayName = null): IUser;
|
||||
|
||||
|
||||
/**
|
||||
* @param 64|512 $size
|
||||
* @return non-empty-string
|
||||
* @since 34.0.0
|
||||
*/
|
||||
public function getAvatarUrlLight(string $userId, int $size): string;
|
||||
|
||||
/**
|
||||
* @param 64|512 $size
|
||||
* @return non-empty-string
|
||||
* @since 34.0.0
|
||||
*/
|
||||
public function getAvatarUrlDark(string $userId, int $size): string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -733,4 +733,12 @@ class ManagerTest extends TestCase {
|
|||
$this->assertEquals('nobody', $user->getUID());
|
||||
$this->assertEquals('None', $user->getDisplayName());
|
||||
}
|
||||
|
||||
public function testGetAvatarUrlLight(): void {
|
||||
$this->assertEquals('http://localhost/index.php/avatar/userid/64', $this->manager->getAvatarUrlLight('userid', 64));
|
||||
}
|
||||
|
||||
public function testGetAvatarUrlDark(): void {
|
||||
$this->assertEquals('http://localhost/index.php/avatar/userid/64/dark', $this->manager->getAvatarUrlDark('userid', 64));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue