Skip to content
Open
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
20 changes: 19 additions & 1 deletion lib/Cron/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Text\Cron;

use OCA\Text\Exception\DocumentHasUnsavedChangesException;
use OCA\Text\Service\AttachmentService;
use OCA\Text\Service\DocumentService;
use OCA\Text\Service\SessionService;
Expand All @@ -17,6 +18,8 @@
use Psr\Log\LoggerInterface;

class Cleanup extends TimedJob {
private const string ABANDONED_UNSAVED_CHANGES_AGE = '-30 days';

public function __construct(
ITimeFactory $time,
private readonly SessionService $sessionService,
Expand All @@ -33,8 +36,23 @@ public function __construct(
*/
protected function run($argument): void {
$this->logger->debug('Run cleanup job for text documents');
$cutoff = $this->time->getDateTime(self::ABANDONED_UNSAVED_CHANGES_AGE)->getTimestamp();
foreach ($this->documentService->getAllWithNoActiveSession() as $document) {
$this->attachmentService->cleanupAttachments($document->getId());
$documentId = $document->getId();
try {
$this->documentService->resetDocument($documentId);
} catch (DocumentHasUnsavedChangesException) {
$lastStepTime = $this->documentService->getLatestStepTimestamp($documentId);
if ($lastStepTime === null || $lastStepTime >= $cutoff) {
continue;
}
$this->documentService->resetDocument($documentId, true);
$this->logger->warning('Force reset document with abandoned unsaved changes', [
'documentId' => $documentId,
'lastStepTime' => $lastStepTime,
]);
}
$this->attachmentService->cleanupAttachments($documentId);
}

$this->logger->debug('Run cleanup job for text sessions');
Expand Down
18 changes: 18 additions & 0 deletions lib/Db/StepMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ public function getLatestVersion(int $documentId): ?int {
return $data['id'];
}

public function getLatestTimestamp(int $documentId): ?int {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('timestamp')
->from($this->getTableName())
->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId)))
->setMaxResults(1)
->orderBy('id', 'DESC')
->executeQuery();

$data = $result->fetchAssociative();
$result->closeCursor();
if ($data === false) {
return null;
}

return (int)$data['timestamp'];
}

public function getBeforeVersion(int $documentId, int $version, int $offset): int {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('id')
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ public function autosave(Document $document, File $file, int $version, string $a
return $document;
}

public function getLatestStepTimestamp(int $documentId): ?int {
return $this->stepMapper->getLatestTimestamp($documentId);
}

/**
* @throws DocumentHasUnsavedChangesException
* @throws Exception
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Service/AiTagServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\IUserFolder;
use OCP\Files\NotFoundException;
use OCP\SystemTag\ISystemTagObjectMapper;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -30,7 +31,7 @@ protected function setUp(): void {
$this->systemTagObjectMapper = $this->createMock(ISystemTagObjectMapper::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->userFolder = $this->createMock(Folder::class);
$this->userFolder = $this->createMock(interface_exists(IUserFolder::class) ? IUserFolder::class : Folder::class);

$this->rootFolder->method('getUserFolder')
->with('testUser')
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/Service/FileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\IUserFolder;
use OCP\Files\NotPermittedException;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
Expand Down Expand Up @@ -36,7 +37,7 @@ public function setUp(): void {
}

public function testGetFileById() {
$userFolder = $this->createMock(Folder::class);
$userFolder = $this->createMock(interface_exists(IUserFolder::class) ? IUserFolder::class : Folder::class);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

$file = $this->createMock(\OCP\Files\File::class);
Expand All @@ -47,7 +48,7 @@ public function testGetFileById() {
}

public function testGetFileByIdSortUpdatableFirst() {
$userFolder = $this->createMock(Folder::class);
$userFolder = $this->createMock(interface_exists(IUserFolder::class) ? IUserFolder::class : Folder::class);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

$file1 = $this->createMock(\OCP\Files\File::class);
Expand All @@ -60,7 +61,7 @@ public function testGetFileByIdSortUpdatableFirst() {
}

public function testGetFileByIdNoRead() {
$userFolder = $this->createMock(Folder::class);
$userFolder = $this->createMock(interface_exists(IUserFolder::class) ? IUserFolder::class : Folder::class);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

$file = $this->createMock(\OCP\Files\File::class);
Expand Down Expand Up @@ -131,7 +132,7 @@ public function testValidTokenWithOtherPasswords(): void {
private function invokeGetDocumentIdFromShare(int $fileId, IShare $share): int {
$this->shareManager->method('getShareByToken')->willReturn($share);

$folder = $this->createMock(Folder::class);
$folder = $this->createMock(interface_exists(IUserFolder::class) ? IUserFolder::class : Folder::class);
$folder->method('getFirstNodeById')->willReturn($this->createMock(File::class));
$this->rootFolder->method('getUserFolder')->with('owner')->willReturn($folder);

Expand Down
Loading