|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\Recommendations\Sabre; |
| 10 | + |
| 11 | +use OC\Files\Filesystem; |
| 12 | +use OC\Files\View; |
| 13 | +use OCA\DAV\Connector\Sabre\Directory; |
| 14 | +use OCA\DAV\Connector\Sabre\File; |
| 15 | +use OCA\Recommendations\AppInfo\Application; |
| 16 | +use OCA\Recommendations\Service\IRecommendation; |
| 17 | +use OCA\Recommendations\Service\RecommendationService; |
| 18 | +use OCP\Files\FileInfo; |
| 19 | +use OCP\IConfig; |
| 20 | +use OCP\IL10N; |
| 21 | +use OCP\IRequest; |
| 22 | +use OCP\IUser; |
| 23 | +use OCP\Share\IManager; |
| 24 | +use Sabre\DAV\Exception\Forbidden; |
| 25 | +use Sabre\DAV\Exception\NotFound; |
| 26 | +use Sabre\DAV\ICollection; |
| 27 | + |
| 28 | +class RecommendationsHome implements ICollection { |
| 29 | + |
| 30 | + /** @var ?list<IRecommendation> */ |
| 31 | + private ?array $cachedRecommendations = null; |
| 32 | + |
| 33 | + private View $fileView; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private array $principalInfo, |
| 37 | + private IUser $user, |
| 38 | + private IConfig $config, |
| 39 | + private RecommendationService $recommendationService, |
| 40 | + private IManager $shareManager, |
| 41 | + private IRequest $request, |
| 42 | + private IL10N $l10n, |
| 43 | + ) { |
| 44 | + $this->fileView = Filesystem::getView(); |
| 45 | + } |
| 46 | + |
| 47 | + public function delete() { |
| 48 | + throw new Forbidden(); |
| 49 | + } |
| 50 | + |
| 51 | + public function getName(): string { |
| 52 | + [, $name] = \Sabre\Uri\split($this->principalInfo['uri']); |
| 53 | + return $name; |
| 54 | + } |
| 55 | + |
| 56 | + public function setName($name) { |
| 57 | + throw new Forbidden(); |
| 58 | + } |
| 59 | + |
| 60 | + public function createFile($name, $data = null) { |
| 61 | + throw new Forbidden(); |
| 62 | + } |
| 63 | + |
| 64 | + public function createDirectory($name) { |
| 65 | + throw new Forbidden(); |
| 66 | + } |
| 67 | + |
| 68 | + public function getChild($name) { |
| 69 | + if (!$this->isEnabled()) { |
| 70 | + throw new NotFound('Recommendations are disabled'); |
| 71 | + } |
| 72 | + |
| 73 | + $recommendations = $this->getChildren(); |
| 74 | + foreach ($recommendations as $child) { |
| 75 | + if ($child->getName() === $name) { |
| 76 | + return $child; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + throw new NotFound("Child '$name' not found in recommendations"); |
| 81 | + } |
| 82 | + |
| 83 | + public function getChildren(): array { |
| 84 | + if (!$this->isEnabled()) { |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + if ($this->cachedRecommendations === null) { |
| 89 | + $this->cachedRecommendations = $this->recommendationService->getRecommendations($this->user); |
| 90 | + } |
| 91 | + |
| 92 | + return array_map( |
| 93 | + function (IRecommendation $recommendation) { |
| 94 | + if ($recommendation->getNode()->getType() === FileInfo::TYPE_FOLDER) { |
| 95 | + return new Directory( |
| 96 | + $this->fileView, |
| 97 | + $this->fileView->getFileInfo($recommendation->getNode()->getInternalPath()), |
| 98 | + null, |
| 99 | + $this->shareManager, |
| 100 | + ); |
| 101 | + } |
| 102 | + return new File( |
| 103 | + $this->fileView, |
| 104 | + $this->fileView->getFileInfo($recommendation->getNode()->getInternalPath()), |
| 105 | + $this->shareManager, |
| 106 | + $this->request, |
| 107 | + $this->l10n |
| 108 | + ); |
| 109 | + }, |
| 110 | + $this->cachedRecommendations |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + public function childExists($name): bool { |
| 115 | + if (!$this->isEnabled()) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + // TODO: map the recommendations to a Sabre node type |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + public function getLastModified(): int { |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + private function isEnabled(): bool { |
| 127 | + return $this->config->getUserValue($this->user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true'; |
| 128 | + } |
| 129 | +} |
0 commit comments