|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Mail\Controller; |
| 11 | + |
| 12 | +use OCA\Mail\AppInfo\Application; |
| 13 | +use OCA\Mail\Db\SnippetShare; |
| 14 | +use OCA\Mail\Http\JsonResponse; |
| 15 | +use OCA\Mail\Http\TrapError; |
| 16 | +use OCA\Mail\Service\SnippetService; |
| 17 | +use OCP\AppFramework\Controller; |
| 18 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 19 | +use OCP\AppFramework\Http; |
| 20 | +use OCP\AppFramework\Http\Attribute\NoAdminRequired; |
| 21 | +use OCP\IRequest; |
| 22 | + |
| 23 | +class SnippetController extends Controller { |
| 24 | + private ?string $uid; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + IRequest $request, |
| 28 | + ?string $userId, |
| 29 | + private SnippetService $snippetService, |
| 30 | + ) { |
| 31 | + parent::__construct(Application::APP_ID, $request); |
| 32 | + $this->uid = $userId; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @NoAdminRequired |
| 37 | + * |
| 38 | + * @return JsonResponse |
| 39 | + */ |
| 40 | + #[TrapError] |
| 41 | + public function getOwnSnippets(): JsonResponse { |
| 42 | + if ($this->uid === null) { |
| 43 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 44 | + } |
| 45 | + $snippets = $this->snippetService->findAll($this->uid); |
| 46 | + |
| 47 | + return JsonResponse::success($snippets); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @NoAdminRequired |
| 52 | + * |
| 53 | + * @return JsonResponse |
| 54 | + */ |
| 55 | + #[TrapError] |
| 56 | + public function getSharedSnippets(): JsonResponse { |
| 57 | + if ($this->uid === null) { |
| 58 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 59 | + } |
| 60 | + try { |
| 61 | + $snippets = $this->snippetService->findAllSharedWithMe($this->uid); |
| 62 | + } catch (DoesNotExistException $e) { |
| 63 | + return JsonResponse::error('Sharee not found', Http::STATUS_UNAUTHORIZED); |
| 64 | + } |
| 65 | + |
| 66 | + return JsonResponse::success($snippets); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @NoAdminRequired |
| 71 | + * @param string $title |
| 72 | + * @param string $content |
| 73 | + * |
| 74 | + * @return JsonResponse |
| 75 | + */ |
| 76 | + #[TrapError] |
| 77 | + public function create(string $title, string $content): JsonResponse { |
| 78 | + if ($this->uid === null) { |
| 79 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 80 | + } |
| 81 | + $snippet = $this->snippetService->create($this->uid, $title, $content); |
| 82 | + |
| 83 | + return JsonResponse::success($snippet, Http::STATUS_CREATED); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @NoAdminRequired |
| 88 | + * @param int $id |
| 89 | + * @param string $title |
| 90 | + * @param string $content |
| 91 | + * |
| 92 | + * @return JsonResponse |
| 93 | + */ |
| 94 | + #[TrapError] |
| 95 | + public function update(int $id, string $title, string $content): JsonResponse { |
| 96 | + |
| 97 | + if ($this->uid === null) { |
| 98 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 99 | + } |
| 100 | + |
| 101 | + $snippet = $this->snippetService->find($id, $this->uid); |
| 102 | + |
| 103 | + if ($snippet === null) { |
| 104 | + return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); |
| 105 | + } |
| 106 | + |
| 107 | + $this->snippetService->update($id, $this->uid, $title, $content); |
| 108 | + |
| 109 | + return JsonResponse::success($snippet, Http::STATUS_OK); |
| 110 | + } |
| 111 | + |
| 112 | + public function delete(int $id): JsonResponse { |
| 113 | + if ($this->uid === null) { |
| 114 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 115 | + } |
| 116 | + try { |
| 117 | + $this->snippetService->delete($id, $this->uid); |
| 118 | + return JsonResponse::success(); |
| 119 | + } catch (DoesNotExistException $e) { |
| 120 | + return JsonResponse::fail('Snippet not found', Http::STATUS_NOT_FOUND); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @NoAdminRequired |
| 126 | + * @param int $snippetId |
| 127 | + * @param string $shareWith |
| 128 | + * @param string $type |
| 129 | + * |
| 130 | + * @return JsonResponse |
| 131 | + */ |
| 132 | + #[TrapError] |
| 133 | + public function share(int $snippetId, string $shareWith, string $type): JsonResponse { |
| 134 | + if ($this->uid === null) { |
| 135 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 136 | + } |
| 137 | + |
| 138 | + $snippet = $this->snippetService->find($snippetId, $this->uid); |
| 139 | + |
| 140 | + if ($snippet === null) { |
| 141 | + return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); |
| 142 | + } |
| 143 | + |
| 144 | + switch ($type) { |
| 145 | + case SnippetShare::TYPE_USER: |
| 146 | + $this->snippetService->share($snippetId, $shareWith); |
| 147 | + return JsonResponse::success(); |
| 148 | + case SnippetShare::TYPE_GROUP: |
| 149 | + $this->snippetService->shareWithGroup($snippetId, $shareWith); |
| 150 | + return JsonResponse::success(); |
| 151 | + default: |
| 152 | + return JsonResponse::fail('Invalid share type', Http::STATUS_BAD_REQUEST); |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + public function getShares(int $id): JsonResponse { |
| 158 | + if ($this->uid === null) { |
| 159 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 160 | + } |
| 161 | + |
| 162 | + $snippet = $this->snippetService->find($id, $this->uid); |
| 163 | + |
| 164 | + if ($snippet === null) { |
| 165 | + return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); |
| 166 | + } |
| 167 | + |
| 168 | + $shares = $this->snippetService->getShares($id); |
| 169 | + |
| 170 | + return JsonResponse::success($shares); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @NoAdminRequired |
| 175 | + * @param int $snippetId |
| 176 | + * @param string $shareWith |
| 177 | + * |
| 178 | + * @return JsonResponse |
| 179 | + */ |
| 180 | + #[TrapError] |
| 181 | + public function deleteShare(int $snippetId, string $shareWith): JsonResponse { |
| 182 | + if ($this->uid === null) { |
| 183 | + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); |
| 184 | + } |
| 185 | + |
| 186 | + $snippet = $this->snippetService->find($snippetId, $this->uid); |
| 187 | + |
| 188 | + if ($snippet === null) { |
| 189 | + return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); |
| 190 | + } |
| 191 | + |
| 192 | + $this->snippetService->unshare($snippetId, $shareWith); |
| 193 | + |
| 194 | + return JsonResponse::success(); |
| 195 | + } |
| 196 | + |
| 197 | +} |
0 commit comments