Files
John Molakvoæ fd43a6243d feat(viewer): keep the user's viewer settings
apps/viewer becomes a minimal, always enabled app again, the server side
of the viewer. It keeps the user's viewer settings the way the Files app
keeps its own: a config lexicon declaring them, a UserConfig service
holding what the lexicon cannot say, an OCS endpoint to change one, and the settings handed to the viewer as initial
state. They are the slideshow delay, and the volume of videos and audio
and whether they start muted. Core keeps loading the viewer itself.

It is no longer an abandoned app to clean up after, and continues the
old app's versions at 9.0.0-dev.0, after 8.0.0 in 35.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com>
2026-10-14 11:22:36 +02:00

98 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Viewer\Service;
use OCA\Viewer\AppInfo\Application;
use OCA\Viewer\ConfigLexicon;
use OCP\Config\IUserConfig;
use OCP\Config\Lexicon\Entry;
use OCP\Config\ValueType;
use OCP\IUserSession;
/**
* The settings a user can change in the viewer, as its config lexicon
* declares them, the way the Files app keeps its own.
*/
class UserConfig {
/** The numbers a setting accepts, which the lexicon cannot say */
private const RANGES = [
ConfigLexicon::SLIDESHOW_DELAY => [1, 60],
ConfigLexicon::VOLUME => [0, 100],
];
public function __construct(
private IUserConfig $userConfig,
private IUserSession $userSession,
private ConfigLexicon $lexicon,
) {
}
/**
* Set a user config
*
* @throws \InvalidArgumentException for an unknown key, or a value it does not accept
* @throws \RuntimeException when no user is logged in
*/
public function setConfig(string $key, int|bool $value): void {
$entry = $this->getEntry($key);
if ($entry->getValueType() === ValueType::BOOL) {
if (!is_bool($value)) {
throw new \InvalidArgumentException('Invalid config value');
}
$this->userConfig->setValueBool($this->getUserId(), Application::APP_ID, $key, $value);
return;
}
[$min, $max] = self::RANGES[$key];
if (!is_int($value) || $value < $min || $value > $max) {
throw new \InvalidArgumentException('Invalid config value');
}
$this->userConfig->setValueInt($this->getUserId(), Application::APP_ID, $key, $value);
}
/**
* Get the current user configs, the lexicon's defaults for what they never set
*
* @return array<string, int|bool>
* @throws \RuntimeException when no user is logged in
*/
public function getConfigs(): array {
$userId = $this->getUserId();
$configs = [];
foreach ($this->lexicon->getUserConfigs() as $entry) {
$key = $entry->getKey();
$configs[$key] = $entry->getValueType() === ValueType::BOOL
? $this->userConfig->getValueBool($userId, Application::APP_ID, $key)
: $this->userConfig->getValueInt($userId, Application::APP_ID, $key);
}
return $configs;
}
/**
* @throws \InvalidArgumentException for an unknown key
*/
private function getEntry(string $key): Entry {
foreach ($this->lexicon->getUserConfigs() as $entry) {
if ($entry->getKey() === $key) {
return $entry;
}
}
throw new \InvalidArgumentException('Unknown config key');
}
private function getUserId(): string {
$user = $this->userSession->getUser();
if ($user === null) {
throw new \RuntimeException('No user logged in');
}
return $user->getUID();
}
}