|
| 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\Whiteboard\DirectEditing; |
| 11 | + |
| 12 | +use OCA\Whiteboard\AppInfo\Application; |
| 13 | +use OCA\Whiteboard\Exception\UnauthorizedException; |
| 14 | +use OCA\Whiteboard\Service\Authentication\AuthenticateUserServiceFactory; |
| 15 | +use OCA\Whiteboard\Service\ConfigService; |
| 16 | +use OCA\Whiteboard\Service\JWTService; |
| 17 | +use OCP\AppFramework\Http\NotFoundResponse; |
| 18 | +use OCP\AppFramework\Http\Response; |
| 19 | +use OCP\AppFramework\Http\TemplateResponse; |
| 20 | +use OCP\AppFramework\Services\IInitialState; |
| 21 | +use OCP\DirectEditing\IEditor; |
| 22 | +use OCP\DirectEditing\IToken; |
| 23 | +use OCP\Files\InvalidPathException; |
| 24 | +use OCP\Files\NotFoundException; |
| 25 | +use OCP\IL10N; |
| 26 | +use OCP\Util; |
| 27 | + |
| 28 | +class WhiteboardDirectEditor implements IEditor { |
| 29 | + |
| 30 | + /** @psalm-suppress PossiblyUnusedMethod */ |
| 31 | + public function __construct( |
| 32 | + private IL10N $l10n, |
| 33 | + private IInitialState $initialState, |
| 34 | + private ConfigService $configService, |
| 35 | + private JWTService $jwtService, |
| 36 | + private AuthenticateUserServiceFactory $authenticateUserServiceFactory, |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + #[\Override] |
| 41 | + public function getId(): string { |
| 42 | + return Application::APP_ID; |
| 43 | + } |
| 44 | + |
| 45 | + #[\Override] |
| 46 | + public function getName(): string { |
| 47 | + return $this->l10n->t('Whiteboard'); |
| 48 | + } |
| 49 | + |
| 50 | + #[\Override] |
| 51 | + public function getMimetypes(): array { |
| 52 | + return [ |
| 53 | + 'application/vnd.excalidraw+json', |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + #[\Override] |
| 58 | + public function getMimetypesOptional(): array { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + #[\Override] |
| 63 | + public function getCreators(): array { |
| 64 | + return [ |
| 65 | + new WhiteboardCreator($this->l10n), |
| 66 | + ]; |
| 67 | + } |
| 68 | + |
| 69 | + #[\Override] |
| 70 | + public function isSecure(): bool { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + #[\Override] |
| 75 | + public function open(IToken $token): Response { |
| 76 | + $token->useTokenScope(); |
| 77 | + |
| 78 | + try { |
| 79 | + $file = $token->getFile(); |
| 80 | + |
| 81 | + Util::addScript('whiteboard', 'whiteboard-main'); |
| 82 | + Util::addStyle('whiteboard', 'whiteboard-main'); |
| 83 | + |
| 84 | + $user = $this->authenticateUserServiceFactory->create(null)->authenticate(); |
| 85 | + $jwt = $this->jwtService->generateJWT($user, $file, false); |
| 86 | + |
| 87 | + $this->initialState->provideInitialState('file_id', $file->getId()); |
| 88 | + $this->initialState->provideInitialState('directEditing', true); |
| 89 | + $this->initialState->provideInitialState('jwt', $jwt); |
| 90 | + $this->initialState->provideInitialState('collabBackendUrl', $this->configService->getCollabBackendUrl()); |
| 91 | + |
| 92 | + return new TemplateResponse(Application::APP_ID, 'directEditing', [], 'base'); |
| 93 | + } catch (InvalidPathException|NotFoundException|UnauthorizedException) { |
| 94 | + return new NotFoundResponse(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments