diff options
author | skjnldsv <skjnldsv@protonmail.com> | 2024-06-06 19:36:19 +0200 |
---|---|---|
committer | skjnldsv <skjnldsv@protonmail.com> | 2024-06-12 13:30:16 +0200 |
commit | f1f2f5ca985bd038d7490ae7be6e5839e3cee790 (patch) | |
tree | d3cc1102edf593d8514a69b30a9a4fc8dd965550 | |
parent | 14c1b53b56c2fa3650edcd1888de6b5d2e05fe00 (diff) | |
download | nextcloud-server-f1f2f5ca985bd038d7490ae7be6e5839e3cee790.tar.gz nextcloud-server-f1f2f5ca985bd038d7490ae7be6e5839e3cee790.zip |
fix(files_sharing): dark avatar support
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
-rw-r--r-- | apps/files_sharing/src/actions/sharingStatusAction.ts | 6 | ||||
-rw-r--r-- | core/Controller/AvatarController.php | 14 | ||||
-rw-r--r-- | tests/Core/Controller/AvatarControllerTest.php | 15 |
3 files changed, 25 insertions, 10 deletions
diff --git a/apps/files_sharing/src/actions/sharingStatusAction.ts b/apps/files_sharing/src/actions/sharingStatusAction.ts index 828408f1846..4f9648fa27f 100644 --- a/apps/files_sharing/src/actions/sharingStatusAction.ts +++ b/apps/files_sharing/src/actions/sharingStatusAction.ts @@ -34,8 +34,12 @@ import { getCurrentUser } from '@nextcloud/auth' import './sharingStatusAction.scss' +const isDarkMode = window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches === true + || document.querySelector('[data-themes*=dark]') !== null + const generateAvatarSvg = (userId: string, isGuest = false) => { - const avatarUrl = generateUrl(isGuest ? '/avatar/guest/{userId}/32' : '/avatar/{userId}/32?guestFallback=true', { userId }) + const url = isDarkMode ? '/avatar/{userId}/32/dark' : '/avatar/{userId}/32' + const avatarUrl = generateUrl(isGuest ? url : url + '?guestFallback=true', { userId }) return `<svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg" class="sharing-status__avatar"> <image href="${avatarUrl}" height="32" width="32" /> diff --git a/core/Controller/AvatarController.php b/core/Controller/AvatarController.php index e2aba26b8b7..a13c722a445 100644 --- a/core/Controller/AvatarController.php +++ b/core/Controller/AvatarController.php @@ -37,14 +37,13 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataDisplayResponse; use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\JSONResponse; -use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\Response; use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\IAvatarManager; use OCP\ICache; use OCP\IL10N; use OCP\IRequest; -use OCP\IURLGenerator; use OCP\IUserManager; use Psr\Log\LoggerInterface; @@ -65,7 +64,6 @@ class AvatarController extends Controller { protected LoggerInterface $logger, protected ?string $userId, protected TimeFactory $timeFactory, - protected IURLGenerator $urlGenerator, protected GuestAvatarController $guestAvatarController, ) { parent::__construct($appName, $request); @@ -81,12 +79,13 @@ class AvatarController extends Controller { * * @param string $userId ID of the user * @param int $size Size of the avatar - * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}> + * @param bool $guestFallback Fallback to guest avatar if not found + * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar?: int}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}> * * 200: Avatar returned * 404: Avatar not found */ - public function getAvatarDark(string $userId, int $size) { + public function getAvatarDark(string $userId, int $size, bool $guestFallback = false) { if ($size <= 64) { if ($size !== 64) { $this->logger->debug('Avatar requested in deprecated size ' . $size); @@ -130,12 +129,13 @@ class AvatarController extends Controller { * * @param string $userId ID of the user * @param int $size Size of the avatar - * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}> + * @param bool $guestFallback Fallback to guest avatar if not found + * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar?: int}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}> * * 200: Avatar returned * 404: Avatar not found */ - public function getAvatar(string $userId, int $size) { + public function getAvatar(string $userId, int $size, bool $guestFallback = false) { if ($size <= 64) { if ($size !== 64) { $this->logger->debug('Avatar requested in deprecated size ' . $size); diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 256f5665795..19221caf972 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -33,6 +33,7 @@ namespace Tests\Core\Controller; use OC\AppFramework\Utility\TimeFactory; use OC\Core\Controller\AvatarController; +use OC\Core\Controller\GuestAvatarController; use OCP\AppFramework\Http; use OCP\Files\File; use OCP\Files\IRootFolder; @@ -56,13 +57,15 @@ use Psr\Log\LoggerInterface; class AvatarControllerTest extends \Test\TestCase { /** @var AvatarController */ private $avatarController; + /** @var GuestAvatarController */ + private $guestAvatarController; + /** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */ private $avatarMock; /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */ private $userMock; /** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */ private $avatarFile; - /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */ private $avatarManager; /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */ @@ -97,6 +100,13 @@ class AvatarControllerTest extends \Test\TestCase { $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock(); $this->userMock = $this->getMockBuilder(IUser::class)->getMock(); + $this->guestAvatarController = new GuestAvatarController( + 'core', + $this->request, + $this->avatarManager, + $this->logger + ); + $this->avatarController = new AvatarController( 'core', $this->request, @@ -107,7 +117,8 @@ class AvatarControllerTest extends \Test\TestCase { $this->rootFolder, $this->logger, 'userid', - $this->timeFactory + $this->timeFactory, + $this->guestAvatarController, ); // Configure userMock |