|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Deck\Provider; |
| 10 | + |
| 11 | +use Co\Http\Server; |
| 12 | +use OC\Security\SecureRandom; |
| 13 | +use OCA\Deck\Db\Acl; |
| 14 | +use OCA\Deck\Db\CardMapper; |
| 15 | +use OCA\Deck\Service\CardService; |
| 16 | +use OCA\Deck\Service\PermissionService; |
| 17 | +use OCA\Text\Context\DocumentData; |
| 18 | +use OCA\Text\Context\IContext; |
| 19 | +use OCA\Text\Context\SessionInfo; |
| 20 | +use OCA\Text\Db\Document; |
| 21 | +use OCA\Text\Exception\DocumentSaveConflictException; |
| 22 | +use OCP\Files\File; |
| 23 | + |
| 24 | +class TextContextProvider implements IContext { |
| 25 | + public function __construct( |
| 26 | + private readonly SecureRandom $secureRandom, |
| 27 | + private readonly CardService $cardService, |
| 28 | + private readonly CardMapper $cardMapper, |
| 29 | + private readonly PermissionService $permissionService, |
| 30 | + private readonly string $userId, |
| 31 | + private readonly int $cardId, |
| 32 | + ) { |
| 33 | + } |
| 34 | + public function getId(): int { |
| 35 | + return $this->cardId; |
| 36 | + } |
| 37 | + |
| 38 | + public function getType(): string { |
| 39 | + return 'deck_card'; |
| 40 | + } |
| 41 | + |
| 42 | + public function toString(): string { |
| 43 | + return $this->getType() . ' (' . $this->getId() . ')'; |
| 44 | + } |
| 45 | + |
| 46 | + public function buildDocument(): Document{ |
| 47 | + $document = new Document(); |
| 48 | + $document->setContextType($this->getType()); |
| 49 | + $document->setContextId($this->getId()); |
| 50 | + $document->setLastSavedVersion(0); |
| 51 | + $document->setLastSavedVersionTime(1); |
| 52 | + $document->setLastSavedVersionEtag($this->secureRandom->generate(6)); |
| 53 | + |
| 54 | + $document->setChecksum($this->computeChecksum()); |
| 55 | + $document->setBaseVersionEtag(uniqid()); |
| 56 | + return $document; |
| 57 | + } |
| 58 | + |
| 59 | + public function prepareSession(DocumentData $documentData): SessionInfo { |
| 60 | + if ($documentData->documentState === null) { |
| 61 | + $content = null; |
| 62 | + } else { |
| 63 | + $content = $this->cardService->find($this->cardId)->getDescription(); |
| 64 | + } |
| 65 | + $readonly = !$this->permissionService->checkPermission($this->cardMapper, $this->cardId, Acl::PERMISSION_EDIT, $this->userId); |
| 66 | + return new SessionInfo( |
| 67 | + content: $content, |
| 68 | + readOnly: $readonly, |
| 69 | + lock: null, |
| 70 | + hasOwner: true, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function isReadOnly(): bool { |
| 75 | + return !$this->permissionService->checkPermission($this->cardMapper, $this->cardId, Acl::PERMISSION_EDIT, $this->userId); |
| 76 | + } |
| 77 | + |
| 78 | + public function updateDocument(Document $document): ?Document { |
| 79 | + if ($this->computeChecksum() !== $document->getChecksum()) { |
| 80 | + $card = $this->cardService->find($this->cardId); |
| 81 | + throw new DocumentSaveConflictException($card->getDescription()); |
| 82 | + } |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + public function getFile(): ?File{ |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + public function loadContent(): ?string { |
| 91 | + $card = $this->cardService->find($this->cardId); |
| 92 | + if ($card === null) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + return $card->getDescription(); |
| 96 | + } |
| 97 | + |
| 98 | + public function saveWithLock(string $content, callable $doWhileLocked): void { |
| 99 | + $card = $this->cardService->find($this->cardId); |
| 100 | + if ($card === null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + $card->setDescription($content); |
| 104 | + $this->cardService->update($card->getId(), $card->getTitle(), $card->getStackId(), $card->getType(), $card->getOwner(), $card->getDescription(), $card->getOrder()); |
| 105 | + } |
| 106 | + |
| 107 | + public function cleanup(): void { |
| 108 | + } |
| 109 | + |
| 110 | + private function computeChecksum(): string { |
| 111 | + $card = $this->cardService->find($this->cardId); |
| 112 | + if ($card === null) { |
| 113 | + return ''; |
| 114 | + } |
| 115 | + return hash('crc32', $card->getDescription()); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments