Skip to content

Commit 2211e6d

Browse files
committed
fix(files_sharing): dark avatar support
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 7b935b8 commit 2211e6d

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

apps/files_sharing/src/actions/sharingStatusAction.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ import { getCurrentUser } from '@nextcloud/auth'
3434

3535
import './sharingStatusAction.scss'
3636

37+
const isDarkMode = window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches === true
38+
|| document.querySelector('[data-themes*=dark]') !== null
39+
3740
const generateAvatarSvg = (userId: string, isGuest = false) => {
38-
const avatarUrl = generateUrl(isGuest ? '/avatar/guest/{userId}/32' : '/avatar/{userId}/32?guestFallback=true', { userId })
41+
const url = isDarkMode ? '/avatar/{userId}/32/dark' : '/avatar/{userId}/32'
42+
const avatarUrl = generateUrl(isGuest ? url : url + '?guestFallback=true', { userId })
3943
return `<svg width="32" height="32" viewBox="0 0 32 32"
4044
xmlns="http://www.w3.org/2000/svg" class="sharing-status__avatar">
4145
<image href="${avatarUrl}" height="32" width="32" />

core/Controller/AvatarController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
use OCP\AppFramework\Http\DataDisplayResponse;
1515
use OCP\AppFramework\Http\FileDisplayResponse;
1616
use OCP\AppFramework\Http\JSONResponse;
17-
use OCP\AppFramework\Http\RedirectResponse;
17+
use OCP\AppFramework\Http\Response;
1818
use OCP\Files\File;
1919
use OCP\Files\IRootFolder;
2020
use OCP\IAvatarManager;
2121
use OCP\ICache;
2222
use OCP\IL10N;
2323
use OCP\IRequest;
24-
use OCP\IURLGenerator;
2524
use OCP\IUserManager;
2625
use Psr\Log\LoggerInterface;
2726

@@ -42,7 +41,6 @@ public function __construct(
4241
protected LoggerInterface $logger,
4342
protected ?string $userId,
4443
protected TimeFactory $timeFactory,
45-
protected IURLGenerator $urlGenerator,
4644
protected GuestAvatarController $guestAvatarController,
4745
) {
4846
parent::__construct($appName, $request);
@@ -58,7 +56,8 @@ public function __construct(
5856
*
5957
* @param string $userId ID of the user
6058
* @param int $size Size of the avatar
61-
* @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
59+
* @param bool $guestFallback Fallback to guest avatar if not found
60+
* @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{}>
6261
*
6362
* 200: Avatar returned
6463
* 404: Avatar not found
@@ -108,7 +107,8 @@ public function getAvatarDark(string $userId, int $size, bool $guestFallback = f
108107
*
109108
* @param string $userId ID of the user
110109
* @param int $size Size of the avatar
111-
* @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
110+
* @param bool $guestFallback Fallback to guest avatar if not found
111+
* @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{}>
112112
*
113113
* 200: Avatar returned
114114
* 404: Avatar not found

tests/Core/Controller/AvatarControllerTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function is_uploaded_file($filename) {
1919

2020
use OC\AppFramework\Utility\TimeFactory;
2121
use OC\Core\Controller\AvatarController;
22+
use OC\Core\Controller\GuestAvatarController;
2223
use OCP\AppFramework\Http;
2324
use OCP\Files\File;
2425
use OCP\Files\IRootFolder;
@@ -42,13 +43,15 @@ function is_uploaded_file($filename) {
4243
class AvatarControllerTest extends \Test\TestCase {
4344
/** @var AvatarController */
4445
private $avatarController;
46+
/** @var GuestAvatarController */
47+
private $guestAvatarController;
48+
4549
/** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */
4650
private $avatarMock;
4751
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
4852
private $userMock;
4953
/** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */
5054
private $avatarFile;
51-
5255
/** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
5356
private $avatarManager;
5457
/** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
@@ -83,6 +86,13 @@ protected function setUp(): void {
8386
$this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock();
8487
$this->userMock = $this->getMockBuilder(IUser::class)->getMock();
8588

89+
$this->guestAvatarController = new GuestAvatarController(
90+
'core',
91+
$this->request,
92+
$this->avatarManager,
93+
$this->logger
94+
);
95+
8696
$this->avatarController = new AvatarController(
8797
'core',
8898
$this->request,
@@ -93,7 +103,8 @@ protected function setUp(): void {
93103
$this->rootFolder,
94104
$this->logger,
95105
'userid',
96-
$this->timeFactory
106+
$this->timeFactory,
107+
$this->guestAvatarController,
97108
);
98109

99110
// Configure userMock

0 commit comments

Comments
 (0)