|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Talk\Controller; |
| 11 | + |
| 12 | +use OCA\Talk\Model\ConversationSection; |
| 13 | +use OCA\Talk\ResponseDefinitions; |
| 14 | +use OCA\Talk\Service\ConversationSectionService; |
| 15 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 16 | +use OCP\AppFramework\Http; |
| 17 | +use OCP\AppFramework\Http\Attribute\ApiRoute; |
| 18 | +use OCP\AppFramework\Http\Attribute\NoAdminRequired; |
| 19 | +use OCP\AppFramework\Http\DataResponse; |
| 20 | +use OCP\AppFramework\OCSController; |
| 21 | +use OCP\IRequest; |
| 22 | + |
| 23 | +/** |
| 24 | + * @psalm-import-type TalkConversationSection from ResponseDefinitions |
| 25 | + */ |
| 26 | +class ConversationSectionController extends OCSController { |
| 27 | + public function __construct( |
| 28 | + string $appName, |
| 29 | + IRequest $request, |
| 30 | + protected ConversationSectionService $sectionService, |
| 31 | + protected ?string $userId, |
| 32 | + ) { |
| 33 | + parent::__construct($appName, $request); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get all conversation sections for the current user |
| 38 | + * |
| 39 | + * Required capability: `conversation-sections` |
| 40 | + * |
| 41 | + * @return DataResponse<Http::STATUS_OK, list<TalkConversationSection>, array{}> |
| 42 | + * |
| 43 | + * 200: Sections returned |
| 44 | + */ |
| 45 | + #[NoAdminRequired] |
| 46 | + #[ApiRoute(verb: 'GET', url: '/api/{apiVersion}/sections', requirements: [ |
| 47 | + 'apiVersion' => '(v4)', |
| 48 | + ])] |
| 49 | + public function getSections(): DataResponse { |
| 50 | + $sections = $this->sectionService->getSections($this->userId); |
| 51 | + return new DataResponse(array_values(array_map([$this, 'formatSection'], $sections))); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a new conversation section |
| 56 | + * |
| 57 | + * Required capability: `conversation-sections` |
| 58 | + * |
| 59 | + * @param string $name Name of the section |
| 60 | + * @return DataResponse<Http::STATUS_CREATED, TalkConversationSection, array{}> |
| 61 | + * |
| 62 | + * 201: Section created |
| 63 | + */ |
| 64 | + #[NoAdminRequired] |
| 65 | + #[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/sections', requirements: [ |
| 66 | + 'apiVersion' => '(v4)', |
| 67 | + ])] |
| 68 | + public function createSection(string $name): DataResponse { |
| 69 | + $section = $this->sectionService->createSection($this->userId, $name); |
| 70 | + return new DataResponse($this->formatSection($section), Http::STATUS_CREATED); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Update a conversation section |
| 75 | + * |
| 76 | + * Required capability: `conversation-sections` |
| 77 | + * |
| 78 | + * @param int $sectionId ID of the section |
| 79 | + * @param string $name New name for the section |
| 80 | + * @return DataResponse<Http::STATUS_OK, TalkConversationSection, array{}>|DataResponse<Http::STATUS_NOT_FOUND, null, array{}> |
| 81 | + * |
| 82 | + * 200: Section updated |
| 83 | + * 404: Section not found |
| 84 | + */ |
| 85 | + #[NoAdminRequired] |
| 86 | + #[ApiRoute(verb: 'PUT', url: '/api/{apiVersion}/sections/{sectionId}', requirements: [ |
| 87 | + 'apiVersion' => '(v4)', |
| 88 | + 'sectionId' => '\d+', |
| 89 | + ])] |
| 90 | + public function updateSection(int $sectionId, string $name): DataResponse { |
| 91 | + try { |
| 92 | + $section = $this->sectionService->updateSection($sectionId, $this->userId, $name); |
| 93 | + return new DataResponse($this->formatSection($section)); |
| 94 | + } catch (DoesNotExistException) { |
| 95 | + return new DataResponse(null, Http::STATUS_NOT_FOUND); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Delete a conversation section |
| 101 | + * |
| 102 | + * Required capability: `conversation-sections` |
| 103 | + * |
| 104 | + * @param int $sectionId ID of the section |
| 105 | + * @return DataResponse<Http::STATUS_OK, null, array{}>|DataResponse<Http::STATUS_NOT_FOUND, null, array{}> |
| 106 | + * |
| 107 | + * 200: Section deleted |
| 108 | + * 404: Section not found |
| 109 | + */ |
| 110 | + #[NoAdminRequired] |
| 111 | + #[ApiRoute(verb: 'DELETE', url: '/api/{apiVersion}/sections/{sectionId}', requirements: [ |
| 112 | + 'apiVersion' => '(v4)', |
| 113 | + 'sectionId' => '\d+', |
| 114 | + ])] |
| 115 | + public function deleteSection(int $sectionId): DataResponse { |
| 116 | + try { |
| 117 | + $this->sectionService->deleteSection($sectionId, $this->userId); |
| 118 | + return new DataResponse(null); |
| 119 | + } catch (DoesNotExistException) { |
| 120 | + return new DataResponse(null, Http::STATUS_NOT_FOUND); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Reorder conversation sections |
| 126 | + * |
| 127 | + * Required capability: `conversation-sections` |
| 128 | + * |
| 129 | + * @param list<int> $orderedIds Ordered list of section IDs |
| 130 | + * @return DataResponse<Http::STATUS_OK, list<TalkConversationSection>, array{}> |
| 131 | + * |
| 132 | + * 200: Sections reordered |
| 133 | + */ |
| 134 | + #[NoAdminRequired] |
| 135 | + #[ApiRoute(verb: 'PUT', url: '/api/{apiVersion}/sections/reorder', requirements: [ |
| 136 | + 'apiVersion' => '(v4)', |
| 137 | + ])] |
| 138 | + public function reorderSections(array $orderedIds): DataResponse { |
| 139 | + $this->sectionService->reorderSections($this->userId, $orderedIds); |
| 140 | + $sections = $this->sectionService->getSections($this->userId); |
| 141 | + return new DataResponse(array_values(array_map([$this, 'formatSection'], $sections))); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return TalkConversationSection |
| 146 | + */ |
| 147 | + protected function formatSection(ConversationSection $section): array { |
| 148 | + return [ |
| 149 | + 'id' => $section->getId(), |
| 150 | + 'name' => $section->getName(), |
| 151 | + 'sortOrder' => $section->getSortOrder(), |
| 152 | + 'collapsed' => $section->isCollapsed(), |
| 153 | + ]; |
| 154 | + } |
| 155 | +} |
0 commit comments