|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | +namespace OCA\Whiteboard\Listener; |
| 9 | + |
| 10 | +use OCP\EventDispatcher\Event; |
| 11 | +use OCP\EventDispatcher\IEventListener; |
| 12 | +use OCP\Files\File; |
| 13 | +use OCP\Files\Template\FileCreatedFromTemplateEvent; |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | + |
| 16 | +/** @template-implements IEventListener<FileCreatedFromTemplateEvent|Event> */ |
| 17 | +/** |
| 18 | + * @psalm-suppress MissingTemplateParam |
| 19 | + */ |
| 20 | +final class FileCreatedFromTemplateListener implements IEventListener { |
| 21 | + private const LIB_EXTENSION = '.excalidrawlib'; |
| 22 | + private const WHITEBOARD_EXTENSION = '.whiteboard'; |
| 23 | + private const VOLATILE_ELEMENT_KEYS = [ |
| 24 | + 'id' => true, |
| 25 | + 'seed' => true, |
| 26 | + 'version' => true, |
| 27 | + 'versionNonce' => true, |
| 28 | + 'updated' => true, |
| 29 | + 'index' => true, |
| 30 | + 'groupIds' => true, |
| 31 | + 'frameId' => true, |
| 32 | + 'boundElements' => true, |
| 33 | + 'containerId' => true, |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * @psalm-suppress PossiblyUnusedMethod |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + private LoggerInterface $logger, |
| 41 | + ) { |
| 42 | + } |
| 43 | + |
| 44 | + #[\Override] |
| 45 | + public function handle(Event $event): void { |
| 46 | + if (!($event instanceof FileCreatedFromTemplateEvent)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $template = $event->getTemplate(); |
| 51 | + $target = $event->getTarget(); |
| 52 | + if (!($template instanceof File)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (!$this->isLibraryTemplate($template) || !$this->isWhiteboardTarget($target)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + $libraryItems = $this->parseLibraryItems($template); |
| 61 | + if ($libraryItems === []) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + $target->putContent(json_encode([ |
| 67 | + 'elements' => [], |
| 68 | + 'files' => [], |
| 69 | + 'libraryItems' => $libraryItems, |
| 70 | + 'scrollToContent' => true, |
| 71 | + ], JSON_THROW_ON_ERROR)); |
| 72 | + } catch (\Throwable $e) { |
| 73 | + $this->logger->warning('Failed to normalize whiteboard created from library template', [ |
| 74 | + 'app' => 'whiteboard', |
| 75 | + 'template' => $template->getPath(), |
| 76 | + 'target' => $target->getPath(), |
| 77 | + 'exception' => $e, |
| 78 | + ]); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private function isLibraryTemplate(File $file): bool { |
| 83 | + return str_ends_with(strtolower($file->getName()), self::LIB_EXTENSION); |
| 84 | + } |
| 85 | + |
| 86 | + private function isWhiteboardTarget(File $file): bool { |
| 87 | + return str_ends_with(strtolower($file->getName()), self::WHITEBOARD_EXTENSION); |
| 88 | + } |
| 89 | + |
| 90 | + private function parseLibraryItems(File $file): array { |
| 91 | + try { |
| 92 | + $data = json_decode($file->getContent(), true, 512, JSON_THROW_ON_ERROR); |
| 93 | + } catch (\Throwable) { |
| 94 | + return []; |
| 95 | + } |
| 96 | + |
| 97 | + if (!is_array($data)) { |
| 98 | + return []; |
| 99 | + } |
| 100 | + |
| 101 | + if (isset($data['libraryItems']) && is_array($data['libraryItems'])) { |
| 102 | + return $this->normalizeLibraryItems($data['libraryItems']); |
| 103 | + } |
| 104 | + |
| 105 | + if (isset($data['library']) && is_array($data['library'])) { |
| 106 | + $items = []; |
| 107 | + foreach ($data['library'] as $elements) { |
| 108 | + if (!is_array($elements) || count($elements) === 0) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + $items[] = [ |
| 112 | + 'id' => $this->createLibraryItemId($elements), |
| 113 | + 'elements' => array_values($elements), |
| 114 | + 'status' => 'published', |
| 115 | + ]; |
| 116 | + } |
| 117 | + return $this->normalizeLibraryItems($items); |
| 118 | + } |
| 119 | + |
| 120 | + return []; |
| 121 | + } |
| 122 | + |
| 123 | + private function normalizeLibraryItems(array $items): array { |
| 124 | + $normalized = []; |
| 125 | + $seen = []; |
| 126 | + |
| 127 | + foreach ($items as $item) { |
| 128 | + if (!is_array($item) || !isset($item['elements']) || !is_array($item['elements']) || count($item['elements']) === 0) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + unset($item['templateName'], $item['scope'], $item['filename'], $item['basename']); |
| 133 | + $item['elements'] = array_values($item['elements']); |
| 134 | + $key = $this->createLibraryItemId($item['elements']); |
| 135 | + if (isset($seen[$key])) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + $seen[$key] = true; |
| 139 | + |
| 140 | + $item['id'] = isset($item['id']) && is_string($item['id']) && $item['id'] !== '' |
| 141 | + ? $item['id'] |
| 142 | + : $key; |
| 143 | + $item['created'] = isset($item['created']) && is_numeric($item['created']) |
| 144 | + ? (int)$item['created'] |
| 145 | + : $this->nowMs(); |
| 146 | + $item['status'] = isset($item['status']) && is_string($item['status']) |
| 147 | + ? $item['status'] |
| 148 | + : 'unpublished'; |
| 149 | + |
| 150 | + $normalized[] = $item; |
| 151 | + } |
| 152 | + |
| 153 | + return $normalized; |
| 154 | + } |
| 155 | + |
| 156 | + private function createLibraryItemId(array $elements): string { |
| 157 | + $canonicalElements = $this->canonicalizeLibraryValue($elements); |
| 158 | + $encoded = json_encode($canonicalElements); |
| 159 | + return substr(hash('sha256', $encoded !== false ? $encoded : serialize($canonicalElements)), 0, 20); |
| 160 | + } |
| 161 | + |
| 162 | + private function canonicalizeLibraryValue(mixed $value): mixed { |
| 163 | + if (!is_array($value)) { |
| 164 | + return $value; |
| 165 | + } |
| 166 | + |
| 167 | + if ($this->isListArray($value)) { |
| 168 | + return array_map(fn ($item) => $this->canonicalizeLibraryValue($item), $value); |
| 169 | + } |
| 170 | + |
| 171 | + ksort($value); |
| 172 | + $normalized = []; |
| 173 | + foreach ($value as $key => $item) { |
| 174 | + if (is_string($key) && isset(self::VOLATILE_ELEMENT_KEYS[$key])) { |
| 175 | + continue; |
| 176 | + } |
| 177 | + $normalized[$key] = $this->canonicalizeLibraryValue($item); |
| 178 | + } |
| 179 | + return $normalized; |
| 180 | + } |
| 181 | + |
| 182 | + private function nowMs(): int { |
| 183 | + return (int)floor((float)microtime(true) * 1000.0); |
| 184 | + } |
| 185 | + |
| 186 | + private function isListArray(array $value): bool { |
| 187 | + return $value === [] || array_keys($value) === range(0, count($value) - 1); |
| 188 | + } |
| 189 | +} |
0 commit comments