|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Files_Sharing\Listener; |
| 11 | + |
| 12 | +use OCA\Files_Sharing\Activity\Providers\Downloads; |
| 13 | +use OCP\EventDispatcher\Event; |
| 14 | +use OCP\EventDispatcher\IEventListener; |
| 15 | +use OCP\Files\Events\BeforeZipCreatedEvent; |
| 16 | +use OCP\Files\Events\Node\BeforeNodeReadEvent; |
| 17 | +use OCP\Files\File; |
| 18 | +use OCP\Files\Folder; |
| 19 | +use OCP\Files\IRootFolder; |
| 20 | +use OCP\Files\NotFoundException; |
| 21 | +use OCP\Files\Storage\ISharedStorage; |
| 22 | +use OCP\ICache; |
| 23 | +use OCP\ICacheFactory; |
| 24 | +use OCP\IRequest; |
| 25 | +use OCP\ISession; |
| 26 | +use OCP\Share\IShare; |
| 27 | + |
| 28 | +/** |
| 29 | + * @template-implements IEventListener<BeforeNodeReadEvent|BeforeZipCreatedEvent|Event> |
| 30 | + */ |
| 31 | +class BeforeNodeReadListener implements IEventListener { |
| 32 | + private ICache $cache; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private ISession $session, |
| 36 | + private IRootFolder $rootFolder, |
| 37 | + private \OCP\Activity\IManager $activityManager, |
| 38 | + private IRequest $request, |
| 39 | + ICacheFactory $cacheFactory, |
| 40 | + ) { |
| 41 | + $this->cache = $cacheFactory->createDistributed('files_sharing_activity_events'); |
| 42 | + } |
| 43 | + |
| 44 | + public function handle(Event $event): void { |
| 45 | + if ($event instanceof BeforeZipCreatedEvent) { |
| 46 | + $this->handleBeforeZipCreatedEvent($event); |
| 47 | + } elseif ($event instanceof BeforeNodeReadEvent) { |
| 48 | + $this->handleBeforeNodeReadEvent($event); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function handleBeforeZipCreatedEvent(BeforeZipCreatedEvent $event): void { |
| 53 | + $files = $event->getFiles(); |
| 54 | + if (count($files) !== 0) { |
| 55 | + /* No need to do anything, activity will be triggered for each file in the zip by the BeforeNodeReadEvent */ |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $node = $event->getFolder(); |
| 60 | + if (!($node instanceof Folder)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + $storage = $node->getStorage(); |
| 66 | + } catch (NotFoundException) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (!$storage->instanceOfStorage(ISharedStorage::class)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + /** @var ISharedStorage $storage */ |
| 75 | + $share = $storage->getShare(); |
| 76 | + |
| 77 | + if (!in_array($share->getShareType(), [IShare::TYPE_EMAIL, IShare::TYPE_LINK])) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + /* Cache that that folder download activity was published */ |
| 82 | + $this->cache->set($this->request->getId(), $node->getPath(), 3600); |
| 83 | + |
| 84 | + $this->singleFileDownloaded($share, $node); |
| 85 | + } |
| 86 | + |
| 87 | + public function handleBeforeNodeReadEvent(BeforeNodeReadEvent $event): void { |
| 88 | + $node = $event->getNode(); |
| 89 | + if (!($node instanceof File)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + $storage = $node->getStorage(); |
| 95 | + } catch (NotFoundException) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (!$storage->instanceOfStorage(ISharedStorage::class)) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + /** @var ISharedStorage $storage */ |
| 104 | + $share = $storage->getShare(); |
| 105 | + |
| 106 | + if (!in_array($share->getShareType(), [IShare::TYPE_EMAIL, IShare::TYPE_LINK])) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + $path = $this->cache->get($this->request->getId()); |
| 111 | + if (is_string($path) && str_starts_with($node->getPath(), $path)) { |
| 112 | + /* An activity was published for a containing folder already */ |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + /* Avoid publishing several activities for one video playing */ |
| 117 | + $cacheKey = $node->getId() . $node->getPath() . $this->session->getId(); |
| 118 | + if (($this->request->getHeader('range') !== '') && ($this->cache->get($cacheKey) === 'true')) { |
| 119 | + /* This is a range request and an activity for the same file was published in the same session */ |
| 120 | + return; |
| 121 | + } |
| 122 | + $this->cache->set($cacheKey, 'true', 3600); |
| 123 | + |
| 124 | + $this->singleFileDownloaded($share, $node); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * create activity if a single file or folder was downloaded from a link share |
| 129 | + */ |
| 130 | + protected function singleFileDownloaded(IShare $share, File|Folder $node): void { |
| 131 | + $fileId = $node->getId(); |
| 132 | + |
| 133 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
| 134 | + $userNode = $userFolder->getFirstNodeById($fileId); |
| 135 | + $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
| 136 | + $userPath = $userFolder->getRelativePath($userNode?->getPath() ?? '') ?? ''; |
| 137 | + $ownerPath = $ownerFolder->getRelativePath($node->getPath()) ?? ''; |
| 138 | + |
| 139 | + $parameters = [$userPath]; |
| 140 | + |
| 141 | + if ($share->getShareType() === IShare::TYPE_EMAIL) { |
| 142 | + if ($node instanceof File) { |
| 143 | + $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
| 144 | + } else { |
| 145 | + $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
| 146 | + } |
| 147 | + $parameters[] = $share->getSharedWith(); |
| 148 | + } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
| 149 | + if ($node instanceof File) { |
| 150 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
| 151 | + } else { |
| 152 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
| 153 | + } |
| 154 | + $remoteAddress = $this->request->getRemoteAddress(); |
| 155 | + $dateTime = new \DateTime(); |
| 156 | + $dateTime = $dateTime->format('Y-m-d H'); |
| 157 | + $remoteAddressHash = md5($dateTime . '-' . $remoteAddress); |
| 158 | + $parameters[] = $remoteAddressHash; |
| 159 | + } else { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
| 164 | + |
| 165 | + if ($share->getShareOwner() !== $share->getSharedBy()) { |
| 166 | + $parameters[0] = $ownerPath; |
| 167 | + $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * publish activity |
| 173 | + */ |
| 174 | + protected function publishActivity( |
| 175 | + string $subject, |
| 176 | + array $parameters, |
| 177 | + string $affectedUser, |
| 178 | + int $fileId, |
| 179 | + string $filePath, |
| 180 | + ): void { |
| 181 | + $event = $this->activityManager->generateEvent(); |
| 182 | + $event->setApp('files_sharing') |
| 183 | + ->setType('public_links') |
| 184 | + ->setSubject($subject, $parameters) |
| 185 | + ->setAffectedUser($affectedUser) |
| 186 | + ->setObject('files', $fileId, $filePath); |
| 187 | + $this->activityManager->publish($event); |
| 188 | + } |
| 189 | +} |
0 commit comments