feat: load forum title/subtitle from public endpoint

This commit is contained in:
2025-11-18 10:24:03 +02:00
parent 2d10b461c0
commit 9719f518e2
4 changed files with 170 additions and 9 deletions

View File

@@ -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);

View File

@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Chen Asraf <contact@casraf.dev>
// 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<Http::STATUS_OK, array{title: string, subtitle: string}, array{}>
*
* 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);
}
}
}

View File

@@ -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",

View File

@@ -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)