Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/theming/lib/Controller/ThemingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\Theming\ITheme;
use OCA\Theming\Service\ThemesService;
use OCA\Theming\Settings\Admin;
use OCA\Theming\ConfigLexicon;
use OCA\Theming\ThemingDefaults;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
Expand Down Expand Up @@ -134,7 +135,7 @@ public function updateStylesheet(string $setting, string $value): DataResponse {
if (!in_array($value, ['yes', 'true', 'no', 'false'], true)) {
$error = $this->l10n->t('%1$s should be true or false', ['disable-user-theming']);
} else {
$this->appConfig->setAppValueBool('disable-user-theming', $value === 'yes' || $value === 'true');
$this->themingDefaults->set(ConfigLexicon::USER_THEMING_DISABLED, $value);
$saved = true;
Comment on lines -140 to 139

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this related?

}
break;
Expand Down
2 changes: 2 additions & 0 deletions apps/theming/lib/Controller/UserThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public function getBackground(): Response {
public function deleteBackground(): JSONResponse {
$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
$this->backgroundService->deleteBackgroundImage();
$currentVersion++;
$this->config->setUserValue($this->userId, Application::APP_ID, 'userCacheBuster', (string)$currentVersion);
return new JSONResponse([
'backgroundImage' => null,
'backgroundColor' => $this->themingDefaults->getColorBackground(),
Expand Down
12 changes: 12 additions & 0 deletions apps/theming/lib/Listener/BeforePreferenceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\Theming\Capabilities;
use OCA\Theming\ConfigLexicon;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\Config\BeforePreferenceDeletedEvent;
use OCP\Config\BeforePreferenceSetEvent;
use OCP\EventDispatcher\Event;
Expand All @@ -28,6 +29,7 @@ class BeforePreferenceListener implements IEventListener {

public function __construct(
private IAppManager $appManager,
private IConfig $config,
) {
}

Expand Down Expand Up @@ -71,10 +73,20 @@ private function handleThemingValues(BeforePreferenceSetEvent|BeforePreferenceDe
default:
$event->setValid(false);
}

if ($event->isValid() && $event->getConfigKey() === 'primary_color') {
$currentVersion = (int)$this->config->getUserValue($event->getUserId(), Application::APP_ID, 'userCacheBuster', '0');
$this->config->setUserValue($event->getUserId(), Application::APP_ID, 'userCacheBuster', (string)($currentVersion + 1));
}

return;
}

$event->setValid(true);
if ($event->getConfigKey() === 'primary_color') {
$currentVersion = (int)$this->config->getUserValue($event->getUserId(), Application::APP_ID, 'userCacheBuster', '0');
$this->config->setUserValue($event->getUserId(), Application::APP_ID, 'userCacheBuster', (string)($currentVersion + 1));
}
}

private function handleCoreValues(BeforePreferenceSetEvent|BeforePreferenceDeletedEvent $event): void {
Expand Down
25 changes: 21 additions & 4 deletions apps/theming/src/components/UserSectionPrimaryColor.vue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is you can close the picker and the colors stays - thats by design but in this usage its now confusing because the color is changed but not saved.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @susnux! I've updated the PR to address all your feedback:

  1. Focused scope: Reverted the unrelated ThemingController and UserThemeController changes and kept BeforePreferenceListener strictly scoped to primary_color cache invalidation.
  2. Frontend picker behavior (UserSectionPrimaryColor.vue): Restored the debounced watch(primaryColor, ...) alongside @submit="onUpdate" so closing the picker without pressing "Choose" still auto-saves after 1s, while pressing "Choose" saves immediately.
  3. Reset race condition: Guarded onReset() with an isResetting flag and debouncedUpdate.clear?.() so resetting primaryColor back to defaultPrimaryColor doesn't trigger a follow-up POST that overwrites the DELETE request.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'
import { generateOcsUrl } from '@nextcloud/router'
import debounce from 'debounce'
import { computed, ref, useTemplateRef, watch } from 'vue'
import { computed, nextTick, ref, useTemplateRef, watch } from 'vue'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcColorPicker from '@nextcloud/vue/components/NcColorPicker'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
Expand All @@ -32,9 +32,19 @@ const triggerElement = useTemplateRef('trigger')

const loading = ref(false)
const primaryColor = ref(initialPrimaryColor)
watch(primaryColor, debounce((newColor) => {

let isResetting = false

const debouncedUpdate = debounce((newColor: string) => {
onUpdate(newColor)
}, 1000))
}, 1000)

watch(primaryColor, (newColor) => {
if (isResetting) {
return
}
debouncedUpdate(newColor)
})

const isDefaultPrimaryColor = computed(() => isSameColor(primaryColor.value, defaultPrimaryColor))

Expand All @@ -57,8 +67,13 @@ function reload() {
* Reset primary color to default
*/
function onReset() {
isResetting = true
debouncedUpdate.clear?.()
primaryColor.value = defaultPrimaryColor
onUpdate(null)
nextTick(() => {
isResetting = false
})
}

/**
Expand All @@ -67,6 +82,7 @@ function onReset() {
* @param value - The new value
*/
async function onUpdate(value: string | null) {
debouncedUpdate.clear?.()
loading.value = true
const url = generateOcsUrl('apps/provisioning_api/api/v1/config/users/{appId}/{configKey}', {
appId: 'theming',
Expand Down Expand Up @@ -104,7 +120,8 @@ function numberToHex(numeric: string): string {
<div :class="$style.userPrimaryColor">
<NcColorPicker
v-model="primaryColor"
data-user-theming-primary-color>
data-user-theming-primary-color
@submit="onUpdate">
<button
ref="trigger"
:class="$style.userPrimaryColor__trigger"
Expand Down
5 changes: 5 additions & 0 deletions apps/theming/tests/Controller/ThemingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OCA\Theming\Tests\Controller;

use OC\L10N\L10N;
use OCA\Theming\ConfigLexicon;
use OCA\Theming\Controller\ThemingController;
use OCA\Theming\ImageManager;
use OCA\Theming\ITheme;
Expand Down Expand Up @@ -97,6 +98,8 @@ public static function dataUpdateStylesheetSuccess(): array {
['background_color', '#0082C9', 'Saved'],
['imprintUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
['privacyUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
['disableUserTheming', 'yes', 'Saved', ConfigLexicon::USER_THEMING_DISABLED],
['disable-user-theming', 'false', 'Saved', ConfigLexicon::USER_THEMING_DISABLED],
];
}

Expand Down Expand Up @@ -152,6 +155,8 @@ public static function dataUpdateStylesheetError(): array {
['background_color', '#0082Z9', 'The given color is invalid'],
['background_color', 'Nextcloud', 'The given color is invalid'],

['disableUserTheming', 'invalid', '%1$s should be true or false'],
['disable-user-theming', 'invalid', '%1$s should be true or false'],
['doesnotexist', 'value', 'Invalid setting key'],

...$urlTests,
Expand Down
30 changes: 30 additions & 0 deletions apps/theming/tests/Controller/UserThemeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,34 @@ public function testDisableTheme(string $themeId, ?string $exception = null): vo
$expected = new DataResponse();
$this->assertEquals($expected, $this->userThemeController->disableTheme($themeId));
}

public function testDeleteBackground(): void {
$this->config->expects($this->once())
->method('getUserValue')
->with('user', Application::APP_ID, 'userCacheBuster', '0')
->willReturn('3');

$this->backgroundService->expects($this->once())
->method('deleteBackgroundImage');

$this->config->expects($this->once())
->method('setUserValue')
->with('user', Application::APP_ID, 'userCacheBuster', '4');

$this->themingDefaults->expects($this->once())
->method('getColorBackground')
->willReturn('#ffffff');

$this->themingDefaults->expects($this->once())
->method('getColorPrimary')
->willReturn('#0082c9');

$response = $this->userThemeController->deleteBackground();
$this->assertEquals(new \OCP\AppFramework\Http\JSONResponse([
'backgroundImage' => null,
'backgroundColor' => '#ffffff',
'primaryColor' => '#0082c9',
'version' => 4,
]), $response);
}
}
133 changes: 133 additions & 0 deletions apps/theming/tests/Listener/BeforePreferenceListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Theming\Tests\Listener;

use OCA\Theming\AppInfo\Application;
use OCA\Theming\ConfigLexicon;
use OCA\Theming\Listener\BeforePreferenceListener;
use OCP\App\IAppManager;
use OCP\Config\BeforePreferenceDeletedEvent;
use OCP\Config\BeforePreferenceSetEvent;
use OCP\EventDispatcher\GenericEvent;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class BeforePreferenceListenerTest extends TestCase {
private IAppManager&MockObject $appManager;
private IConfig&MockObject $config;
private BeforePreferenceListener $listener;

protected function setUp(): void {
parent::setUp();

$this->appManager = $this->createMock(IAppManager::class);
$this->config = $this->createMock(IConfig::class);

$this->listener = new BeforePreferenceListener(
$this->appManager,
$this->config,
);
}

public function testHandleInvalidEvent(): void {
$event = new GenericEvent();
$this->listener->handle($event);
$this->assertTrue(true);
}

public function testHandleThemingValuesUnallowedKey(): void {
$event = new BeforePreferenceSetEvent('user1', Application::APP_ID, 'unallowed_key', 'value');
$this->listener->handle($event);
$this->assertFalse($event->isValid());
}

public function testHandleThemingValuesPrimaryColorValid(): void {
$this->config->expects($this->once())
->method('getUserValue')
->with('user1', Application::APP_ID, 'userCacheBuster', '0')
->willReturn('2');

$this->config->expects($this->once())
->method('setUserValue')
->with('user1', Application::APP_ID, 'userCacheBuster', '3');

$event = new BeforePreferenceSetEvent('user1', Application::APP_ID, 'primary_color', '#ff00aa');
$this->listener->handle($event);

$this->assertTrue($event->isValid());
}

public function testHandleThemingValuesPrimaryColorInvalid(): void {
$this->config->expects($this->never())
->method('setUserValue');

$event = new BeforePreferenceSetEvent('user1', Application::APP_ID, 'primary_color', 'invalid_color');
$this->listener->handle($event);

$this->assertFalse($event->isValid());
}

public function testHandleThemingValuesPrimaryColorDeleted(): void {
$this->config->expects($this->once())
->method('getUserValue')
->with('user1', Application::APP_ID, 'userCacheBuster', '0')
->willReturn('5');

$this->config->expects($this->once())
->method('setUserValue')
->with('user1', Application::APP_ID, 'userCacheBuster', '6');

$event = new BeforePreferenceDeletedEvent('user1', Application::APP_ID, 'primary_color');
$this->listener->handle($event);

$this->assertTrue($event->isValid());
}

public function testHandleThemingValuesForceEnableBlurFilter(): void {
$this->config->expects($this->never())
->method('setUserValue');

$event = new BeforePreferenceSetEvent('user1', Application::APP_ID, 'force_enable_blur_filter', 'yes');
$this->listener->handle($event);

$this->assertTrue($event->isValid());
}

public function testHandleThemingValuesShortcutsDisabled(): void {
$this->config->expects($this->never())
->method('setUserValue');

$event = new BeforePreferenceSetEvent('user1', Application::APP_ID, 'shortcuts_disabled', 'yes');
$this->listener->handle($event);

$this->assertTrue($event->isValid());
}

public function testHandleThemingValuesToastTimeout(): void {
$this->config->expects($this->never())
->method('setUserValue');

$event = new BeforePreferenceSetEvent('user1', Application::APP_ID, ConfigLexicon::TOAST_TIMEOUT, '7000');
$this->listener->handle($event);

$this->assertTrue($event->isValid());
}

public function testHandleThemingValuesOtherKeyDeleted(): void {
$this->config->expects($this->never())
->method('setUserValue');

$event = new BeforePreferenceDeletedEvent('user1', Application::APP_ID, 'shortcuts_disabled');
$this->listener->handle($event);

$this->assertTrue($event->isValid());
}
}