Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/Controller/AiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IRequest;

class AiController extends ApiController {
Expand All @@ -33,6 +34,8 @@ public function tagFile(int $fileId): DataResponse {
return new DataResponse([]);
} catch (NotFoundException) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (NotPermittedException) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
}
}
33 changes: 24 additions & 9 deletions lib/Service/AiTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace OCA\Text\Service;

use OCP\Constants;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\SystemTag\ISystemTagObjectMapper;
use Psr\Log\LoggerInterface;

Expand All @@ -29,22 +31,35 @@ public function __construct(
* @return void
*
* @throws NotFoundException
* @throws NotPermittedException
*
* @since 34.0.0 (ISystemTagObjectMapper::assignGeneratedByAITag)
*/
public function tagFileAsAiGenerated(int $fileId): void {
if ($this->userId === null || $this->rootFolder->getUserFolder($this->userId)->getFirstNodeById($fileId) === null) {
if ($this->userId === null) {
throw new NotFoundException('File not found');
}

try {
$this->systemTagObjectMapper->assignGeneratedByAITag((string)$fileId, 'files');
} catch (\Exception $e) {
$this->logger->warning('Failed to tag file {fileId} as AI-generated: {error}', [
'fileId' => $fileId,
'error' => $e->getMessage(),
'exception' => $e,
]);
$nodes = $this->rootFolder->getUserFolder($this->userId)->getById($fileId);
if ($nodes === []) {
throw new NotFoundException('File not found');
}

foreach ($nodes as $node) {
if (($node->getPermissions() & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the helper from FileService here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the 'isReadOnly' checks the update permission and also blocks the file when it is locked, tag only needs the update permission

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, you're right.

try {
$this->systemTagObjectMapper->assignGeneratedByAITag((string)$fileId, 'files');
} catch (\Exception $e) {
$this->logger->warning('Failed to tag file {fileId} as AI-generated: {error}', [
'fileId' => $fileId,
'error' => $e->getMessage(),
'exception' => $e,
]);
}
return;
}
}

throw new NotPermittedException('No permission to assign tag to ' . $fileId);
}
}
13 changes: 13 additions & 0 deletions tests/unit/Controller/AiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
Expand Down Expand Up @@ -64,4 +65,16 @@ public function testTagFileReturnsNotFoundWhenInaccessible(): void {
$this->assertSame(Http::STATUS_NOT_FOUND, $response->getStatus());
$this->assertSame([], $response->getData());
}

public function testTagFileReturnsForbiddenWhenNotPermitted(): void {
$this->aiTagService->expects($this->once())
->method('tagFileAsAiGenerated')
->with(42)
->willThrowException(new NotPermittedException('No permission to assign tag to 42'));

$response = $this->controller->tagFile(42);

$this->assertSame(Http::STATUS_FORBIDDEN, $response->getStatus());
$this->assertSame([], $response->getData());
}
}
32 changes: 27 additions & 5 deletions tests/unit/Service/AiTagServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

namespace OCA\Text\Service;

use OCP\Constants;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\IUserFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\SystemTag\ISystemTagObjectMapper;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -61,9 +63,9 @@ public function testTagFileAsAiGeneratedThrowsWhenUserMissing(): void {

public function testTagFileAsAiGeneratedThrowsWhenFileNotAccessible(): void {
$this->userFolder->expects($this->once())
->method('getFirstNodeById')
->method('getById')
->with(42)
->willReturn(null);
->willReturn([]);

$this->systemTagObjectMapper->expects($this->never())
->method('assignGeneratedByAITag');
Expand All @@ -72,12 +74,32 @@ public function testTagFileAsAiGeneratedThrowsWhenFileNotAccessible(): void {
$this->service->tagFileAsAiGenerated(42);
}

public function testTagFileAsAiGeneratedTagsWhenAccessible(): void {
public function testTagFileAsAiGeneratedThrowsWhenFileIsReadOnly(): void {
$file = $this->createMock(File::class);
$file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);

$this->userFolder->expects($this->once())
->method('getById')
->with(42)
->willReturn([$file]);

$this->systemTagObjectMapper->expects($this->never())
->method('assignGeneratedByAITag');

$this->expectException(NotPermittedException::class);
$this->service->tagFileAsAiGenerated(42);
}

public function testTagFileAsAiGeneratedTagsWhenUpdateIsAllowed(): void {
$readOnly = $this->createMock(File::class);
$readOnly->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
$writable = $this->createMock(File::class);
$writable->method('getPermissions')->willReturn(Constants::PERMISSION_UPDATE);

$this->userFolder->expects($this->once())
->method('getFirstNodeById')
->method('getById')
->with(42)
->willReturn($file);
->willReturn([$readOnly, $writable]);

$this->systemTagObjectMapper->expects($this->once())
->method('assignGeneratedByAITag')
Expand Down
Loading