From 9719f518e2b1a9dced781431a6b0d4123aef952c Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Tue, 18 Nov 2025 10:24:03 +0200 Subject: [PATCH] feat: load forum title/subtitle from public endpoint --- lib/Controller/AdminController.php | 15 ++-- lib/Controller/SettingsController.php | 57 ++++++++++++++ openapi.json | 102 ++++++++++++++++++++++++++ src/views/CategoriesView.vue | 5 +- 4 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 lib/Controller/SettingsController.php diff --git a/lib/Controller/AdminController.php b/lib/Controller/AdminController.php index a753fa6..c6ca198 100644 --- a/lib/Controller/AdminController.php +++ b/lib/Controller/AdminController.php @@ -7,7 +7,6 @@ declare(strict_types=1); namespace OCA\Forum\Controller; -use OCA\Forum\AppInfo\Application; use OCA\Forum\Attribute\RequirePermission; use OCA\Forum\Db\CategoryMapper; use OCA\Forum\Db\PostMapper; @@ -21,6 +20,7 @@ use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; use OCP\IConfig; +use OCP\IL10N; use OCP\IRequest; use OCP\IUserManager; use OCP\IUserSession; @@ -41,6 +41,7 @@ class AdminController extends OCSController { private IUserSession $userSession, private IConfig $config, private LoggerInterface $logger, + private IL10N $l10n, ) { parent::__construct($appName, $request); } @@ -171,8 +172,8 @@ class AdminController extends OCSController { public function getSettings(): DataResponse { try { $settings = [ - 'title' => $this->config->getAppValue(Application::APP_ID, 'title', 'Forum'), - 'subtitle' => $this->config->getAppValue(Application::APP_ID, 'subtitle', 'Welcome to the forum'), + 'title' => $this->config->getSystemValueString('title', $this->l10n->t('Forum')), + 'subtitle' => $this->config->getSystemValueString('subtitle', $this->l10n->t('Welcome to the forum!')), ]; return new DataResponse($settings); @@ -197,17 +198,17 @@ class AdminController extends OCSController { public function updateSettings(?string $title = null, ?string $subtitle = null): DataResponse { try { if ($title !== null) { - $this->config->setAppValue(Application::APP_ID, 'title', $title); + $this->config->setSystemValue('title', $title); } if ($subtitle !== null) { - $this->config->setAppValue(Application::APP_ID, 'subtitle', $subtitle); + $this->config->setSystemValue('subtitle', $subtitle); } // Return updated settings $settings = [ - 'title' => $this->config->getAppValue(Application::APP_ID, 'title', 'Forum'), - 'subtitle' => $this->config->getAppValue(Application::APP_ID, 'subtitle', 'Welcome to the forum'), + 'title' => $this->config->getSystemValueString('title', $this->l10n->t('Forum')), + 'subtitle' => $this->config->getSystemValueString('subtitle', $this->l10n->t('Welcome to the forum!')), ]; return new DataResponse($settings); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php new file mode 100644 index 0000000..0ecbe94 --- /dev/null +++ b/lib/Controller/SettingsController.php @@ -0,0 +1,57 @@ + +// SPDX-License-Identifier: AGPL-3.0-or-later + +namespace OCA\Forum\Controller; + +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\ApiRoute; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCSController; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IRequest; +use Psr\Log\LoggerInterface; + +class SettingsController extends OCSController { + + public function __construct( + string $appName, + IRequest $request, + private IConfig $config, + private LoggerInterface $logger, + private IL10N $l10n, + ) { + parent::__construct($appName, $request); + } + + /** + * Get public forum settings (title and subtitle) + * + * This endpoint is publicly accessible to all users. + * For admin-only settings, use AdminController::getSettings() + * + * @return DataResponse + * + * 200: Settings retrieved successfully + */ + #[NoAdminRequired] + #[ApiRoute(verb: 'GET', url: '/api/settings')] + public function getPublicSettings(): DataResponse { + try { + $settings = [ + 'title' => $this->config->getSystemValueString('title', $this->l10n->t('Forum')), + 'subtitle' => $this->config->getSystemValueString('subtitle', $this->l10n->t('Welcome to the forum!')), + ]; + + return new DataResponse($settings); + } catch (\Exception $e) { + $this->logger->error('Error fetching public settings: ' . $e->getMessage()); + return new DataResponse(['error' => 'Failed to fetch settings'], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/openapi.json b/openapi.json index 0da064b..3703b0e 100644 --- a/openapi.json +++ b/openapi.json @@ -6630,6 +6630,108 @@ } } }, + "/ocs/v2.php/apps/forum/api/settings": { + "get": { + "operationId": "settings-get-public-settings", + "summary": "Get public forum settings (title and subtitle)", + "description": "This endpoint is publicly accessible to all users. For admin-only settings, use AdminController::getSettings()", + "tags": [ + "settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Settings retrieved successfully", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": { + "type": "object", + "required": [ + "title", + "subtitle" + ], + "properties": { + "title": { + "type": "string" + }, + "subtitle": { + "type": "string" + } + } + } + } + } + } + } + } + } + }, + "401": { + "description": "Current user is not logged in", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, "/ocs/v2.php/apps/forum/api/threads": { "get": { "operationId": "thread-index", diff --git a/src/views/CategoriesView.vue b/src/views/CategoriesView.vue index 88e22b7..12608d4 100644 --- a/src/views/CategoriesView.vue +++ b/src/views/CategoriesView.vue @@ -117,9 +117,10 @@ export default defineComponent({ methods: { async fetchForumSettings() { try { - const response = await ocs.get<{ title: string; subtitle: string }>('/admin/settings') + this.settingsLoading = true + const response = await ocs.get<{ title: string; subtitle: string }>('/settings') this.forumTitle = response.data.title || t('forum', 'Forum') - this.forumSubtitle = response.data.subtitle || t('forum', 'Welcome to the forum') + this.forumSubtitle = response.data.subtitle || t('forum', 'Welcome to the forum!') } catch (e) { // Silently fail and use defaults if settings can't be loaded console.debug('Could not load forum settings, using defaults', e)