Repository navigation
feat(files): add an API to report that a user opened a file #65994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files\Controller; | ||
|
|
||
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Http\Attribute\ApiRoute; | ||
| use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
| use OCP\AppFramework\Http\Attribute\UserRateLimit; | ||
| use OCP\AppFramework\Http\DataResponse; | ||
| use OCP\AppFramework\OCS\OCSNotFoundException; | ||
| use OCP\AppFramework\OCSController; | ||
| use OCP\EventDispatcher\IEventDispatcher; | ||
| use OCP\Files\Events\Node\NodeOpenedEvent; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\IL10N; | ||
| use OCP\IRequest; | ||
| use OCP\IUser; | ||
|
|
||
| final class OpenedController extends OCSController { | ||
| public function __construct( | ||
| string $appName, | ||
| IRequest $request, | ||
| private readonly IRootFolder $rootFolder, | ||
| private readonly IEventDispatcher $eventDispatcher, | ||
| private readonly IL10N $l10n, | ||
| ) { | ||
| parent::__construct($appName, $request); | ||
| } | ||
|
|
||
| /** | ||
| * Report that the current user opened a file or folder | ||
| * | ||
| * Clients call this when the user actually opens a node, not when they | ||
| * read its content (sync, previews, downloads). Apps can listen to it to | ||
| * know what the user works with. It is for statistics only: clients send it | ||
| * when they choose to, so it must not be relied on for access or auditing. | ||
| * | ||
| * @param int $fileId ID of the opened file or folder | ||
| * | ||
| * @return DataResponse<Http::STATUS_OK, list<empty>, array{}> | ||
| * @throws OCSNotFoundException The file or folder was not found | ||
| * | ||
| * 200: Open reported | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[UserRateLimit(limit: 100, period: 60)] | ||
| #[ApiRoute(verb: 'POST', url: '/api/v1/opened')] | ||
| public function opened(IUser $user, int $fileId): DataResponse { | ||
| $node = $this->rootFolder->getUserFolder($user->getUID())->getFirstNodeById($fileId); | ||
|
|
||
| // Also a 404 if the node is not readable, to not leak its existence | ||
| if ($node === null || !$node->isReadable()) { | ||
| throw new OCSNotFoundException($this->l10n->t('The file cannot be found')); | ||
| } | ||
|
|
||
| $this->eventDispatcher->dispatchTyped(new NodeOpenedEvent($node, $user)); | ||
| return new DataResponse([]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files\Tests\Controller; | ||
|
|
||
| use OCA\Files\Controller\OpenedController; | ||
| use OCP\AppFramework\OCS\OCSNotFoundException; | ||
| use OCP\EventDispatcher\IEventDispatcher; | ||
| use OCP\Files\Events\Node\NodeOpenedEvent; | ||
| use OCP\Files\File; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\Files\IUserFolder; | ||
| use OCP\IL10N; | ||
| use OCP\IRequest; | ||
| use OCP\IUser; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use Test\TestCase; | ||
|
|
||
| class OpenedControllerTest extends TestCase { | ||
| private IUserFolder&MockObject $userFolder; | ||
| private IEventDispatcher&MockObject $eventDispatcher; | ||
| private IUser&MockObject $user; | ||
| private OpenedController $controller; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->user = $this->createMock(IUser::class); | ||
| $this->user->method('getUID')->willReturn('alice'); | ||
|
|
||
| $this->userFolder = $this->createMock(IUserFolder::class); | ||
| $rootFolder = $this->createMock(IRootFolder::class); | ||
| $rootFolder->method('getUserFolder')->with('alice')->willReturn($this->userFolder); | ||
|
|
||
| $this->eventDispatcher = $this->createMock(IEventDispatcher::class); | ||
|
|
||
| $this->controller = new OpenedController( | ||
| 'files', | ||
| $this->createMock(IRequest::class), | ||
| $rootFolder, | ||
| $this->eventDispatcher, | ||
| $this->createMock(IL10N::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testDispatchesEvent(): void { | ||
| $file = $this->createMock(File::class); | ||
| $file->method('isReadable')->willReturn(true); | ||
| $this->userFolder->method('getFirstNodeById')->with(42)->willReturn($file); | ||
|
|
||
| $this->eventDispatcher->expects($this->once()) | ||
| ->method('dispatchTyped') | ||
| ->with($this->callback(fn (NodeOpenedEvent $event): bool | ||
| => $event->getNode() === $file && $event->getUser() === $this->user)); | ||
|
|
||
| $this->controller->opened($this->user, 42); | ||
| } | ||
|
|
||
| public function testNotFound(): void { | ||
| $this->userFolder->method('getFirstNodeById')->with(42)->willReturn(null); | ||
| $this->eventDispatcher->expects($this->never())->method('dispatchTyped'); | ||
|
|
||
| $this->expectException(OCSNotFoundException::class); | ||
| $this->controller->opened($this->user, 42); | ||
| } | ||
|
|
||
| public function testNotReadable(): void { | ||
| $file = $this->createMock(File::class); | ||
| $file->method('isReadable')->willReturn(false); | ||
| $this->userFolder->method('getFirstNodeById')->with(42)->willReturn($file); | ||
| $this->eventDispatcher->expects($this->never())->method('dispatchTyped'); | ||
|
|
||
| $this->expectException(OCSNotFoundException::class); | ||
| $this->controller->opened($this->user, 42); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCP\Files\Events\Node; | ||
|
|
||
| use OCP\AppFramework\Attribute\Listenable; | ||
| use OCP\EventDispatcher\JsonSerializer; | ||
| use OCP\Files\Node; | ||
| use OCP\IUser; | ||
|
|
||
| /** | ||
| * A user opened a node in a client, as reported by that client. | ||
| * | ||
| * Unlike {@see BeforeNodeReadEvent}, this is not dispatched when the content | ||
| * is read (sync, previews, downloads) but only when a client says the user | ||
| * actually opened it. | ||
| * | ||
| * It is meant for statistics only, like recommending the files a user works | ||
| * with. Clients report it when they choose to, so it can be missing, repeated | ||
| * or sent for a node the user never looked at. Never use it for permissions, | ||
| * access control, auditing, or anything else that has to be reliable. | ||
| * | ||
| * @since 36.0.0 | ||
| */ | ||
| #[Listenable(since: '36.0.0')] | ||
| class NodeOpenedEvent extends AbstractNodeEvent { | ||
| /** | ||
| * @since 36.0.0 | ||
| */ | ||
| public function __construct( | ||
| Node $node, | ||
| private IUser $user, | ||
| ) { | ||
| parent::__construct($node); | ||
| } | ||
|
|
||
| /** | ||
| * The user who opened the node | ||
| * | ||
| * @since 36.0.0 | ||
| */ | ||
| public function getUser(): IUser { | ||
| return $this->user; | ||
| } | ||
|
|
||
| /** | ||
| * @since 36.0.0 | ||
| */ | ||
| #[\Override] | ||
| public function getWebhookSerializable(): array { | ||
| return [ | ||
| ...parent::getWebhookSerializable(), | ||
| 'user' => JsonSerializer::serializeUser($this->user), | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.