Files
nextcloud-server-mirror/apps/files/lib/Controller/OpenedController.php
John Molakvoæ 7b2f1fdd52 docs(files): say the opened event is for statistics only
Clients report opens when they choose to, so the event can be missing,
repeated, or sent for a node the user never looked at. Make clear it
must not be used for permissions, access control or auditing.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com>
2026-10-29 15:40:01 +01:00

67 lines
2.1 KiB
PHP

<?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([]);
}
}