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
37 changes: 20 additions & 17 deletions lib/BackgroundJob/RetentionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
parent::__construct($timeFactory);
// Run once a day
$this->setInterval(24 * 60 * 60);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}

public function run($argument): void {
Expand Down Expand Up @@ -119,7 +120,7 @@ public function run($argument): void {
$deleted = $this->expireNode($node, $deleteBefore, $timeAfter);

if ($notifyDayBefore && !$deleted) {
$this->notifyNode($node, $notifyBefore);
$this->notifyNode($node, $notifyBefore, $timeAfter);
}
}

Expand Down Expand Up @@ -190,18 +191,28 @@ protected function getDeletableNodeFromMountPoint(ICachedMountFileInfo $mountPoi
throw new NotPermittedException();
}

private function expireNode(Node $node, \DateTime $deleteBefore, int $timeAfter): bool {
$mtime = new \DateTime();
protected function getDateFromNode(Node $node, int $timeAfter): \DateTime {
$time = new \DateTime();

// Fallback is the mtime
$mtime->setTimestamp($node->getMTime());
$time->setTimestamp($node->getMTime());

// Use the upload time if we have it
if ($timeAfter === Constants::MODE_CTIME && $node->getUploadTime() !== 0) {
$mtime->setTimestamp($node->getUploadTime());
$time->setTimestamp($node->getUploadTime());
} elseif ($timeAfter === Constants::MODE_MTIME && $node->getMTime() < $node->getUploadTime()) {
// Use the upload time if it's newer than the modification time
$time->setTimestamp($node->getUploadTime());
$this->logger->debug('Upload time of file ' . $node->getId() . ' is newer than modification time, continuing with that');
}

if ($mtime < $deleteBefore) {
return $time;
}

private function expireNode(Node $node, \DateTime $deleteBefore, int $timeAfter): bool {
$time = $this->getDateFromNode($node, $timeAfter);

if ($time < $deleteBefore) {
$this->logger->debug('Expiring file ' . $node->getId());
try {
$node->delete();
Expand All @@ -218,18 +229,10 @@ private function expireNode(Node $node, \DateTime $deleteBefore, int $timeAfter)
return false;
}

private function notifyNode(Node $node, \DateTime $notifyBefore): void {
$mtime = new \DateTime();

// Fallback is the mtime
$mtime->setTimestamp($node->getMTime());

// Use the upload time if we have it
if ($node->getUploadTime() !== 0) {
$mtime->setTimestamp($node->getUploadTime());
}
private function notifyNode(Node $node, \DateTime $notifyBefore, int $timeAfter): void {
$time = $this->getDateFromNode($node, $timeAfter);

if ($mtime < $notifyBefore) {
if ($time < $notifyBefore) {
$this->logger->debug('Notifying about retention tomorrow for file ' . $node->getId());
try {
$notification = $this->notificationManager->createNotification();
Expand Down
Binary file modified screenshots/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 53 additions & 39 deletions tests/lib/BackgroundJob/RetentionJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,37 @@ private function addTag(int $tagId, int $timeunit, int $timeamount, int $timeaft

public static function deleteTestCases(): array {
return [
[[1, Constants::UNIT_DAY], [0, Constants::UNIT_DAY], false, 0],
[[2, Constants::UNIT_WEEK], [0, Constants::UNIT_DAY], false, 0],
[[3, Constants::UNIT_MONTH], [0, Constants::UNIT_DAY], false, 1],
[[4, Constants::UNIT_YEAR], [0, Constants::UNIT_DAY], false, 1],

[[1, Constants::UNIT_DAY], [2, Constants::UNIT_DAY], true, 0],
[[2, Constants::UNIT_WEEK], [2, Constants::UNIT_DAY], false, 0],
[[3, Constants::UNIT_MONTH], [2, Constants::UNIT_DAY], false, 1],
[[4, Constants::UNIT_YEAR], [2, Constants::UNIT_DAY], false, 1],

[[1, Constants::UNIT_DAY], [21, Constants::UNIT_DAY], true, 0],
[[2, Constants::UNIT_WEEK], [21, Constants::UNIT_DAY], true, 0],
[[3, Constants::UNIT_MONTH], [21, Constants::UNIT_DAY], false, 1],
[[4, Constants::UNIT_YEAR], [21, Constants::UNIT_DAY], false, 1],

[[1, Constants::UNIT_DAY], [180, Constants::UNIT_DAY], true, 0],
[[2, Constants::UNIT_WEEK], [180, Constants::UNIT_DAY], true, 0],
[[3, Constants::UNIT_MONTH], [180, Constants::UNIT_DAY], true, 1],
[[4, Constants::UNIT_YEAR], [180, Constants::UNIT_DAY], false, 1],

[[1, Constants::UNIT_DAY], [10000, Constants::UNIT_DAY], true, 0],
[[2, Constants::UNIT_WEEK], [10000, Constants::UNIT_DAY], true, 0],
[[3, Constants::UNIT_MONTH], [10000, Constants::UNIT_DAY], true, 1],
[[4, Constants::UNIT_YEAR], [10000, Constants::UNIT_DAY], true, 1],
[[1, Constants::UNIT_DAY], [0, Constants::UNIT_DAY], false, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], [0, Constants::UNIT_DAY], false, Constants::MODE_CTIME],
[[3, Constants::UNIT_MONTH], [0, Constants::UNIT_DAY], false, Constants::MODE_MTIME],
[[4, Constants::UNIT_YEAR], [0, Constants::UNIT_DAY], false, Constants::MODE_MTIME],

[[1, Constants::UNIT_DAY], [2, Constants::UNIT_DAY], true, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], [2, Constants::UNIT_DAY], false, Constants::MODE_CTIME],
[[3, Constants::UNIT_MONTH], [2, Constants::UNIT_DAY], false, Constants::MODE_MTIME],
[[4, Constants::UNIT_YEAR], [2, Constants::UNIT_DAY], false, Constants::MODE_MTIME],

[[1, Constants::UNIT_DAY], [21, Constants::UNIT_DAY], true, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], [21, Constants::UNIT_DAY], true, Constants::MODE_CTIME],
[[3, Constants::UNIT_MONTH], [21, Constants::UNIT_DAY], false, Constants::MODE_MTIME],
[[4, Constants::UNIT_YEAR], [21, Constants::UNIT_DAY], false, Constants::MODE_MTIME],

[[1, Constants::UNIT_DAY], [180, Constants::UNIT_DAY], true, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], [180, Constants::UNIT_DAY], true, Constants::MODE_CTIME],
[[3, Constants::UNIT_MONTH], [180, Constants::UNIT_DAY], true, Constants::MODE_MTIME],
[[4, Constants::UNIT_YEAR], [180, Constants::UNIT_DAY], false, Constants::MODE_MTIME],

[[1, Constants::UNIT_DAY], [10000, Constants::UNIT_DAY], true, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], [10000, Constants::UNIT_DAY], true, Constants::MODE_CTIME],
[[3, Constants::UNIT_MONTH], [10000, Constants::UNIT_DAY], true, Constants::MODE_MTIME],
[[4, Constants::UNIT_YEAR], [10000, Constants::UNIT_DAY], true, Constants::MODE_MTIME],

[[2, Constants::UNIT_WEEK], ['mtime' => 10, 'ctime' => 10], false, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], ['mtime' => 10, 'ctime' => 16], true, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], ['mtime' => 16, 'ctime' => 16], true, Constants::MODE_CTIME],
[[2, Constants::UNIT_WEEK], ['mtime' => 10, 'ctime' => 10], false, Constants::MODE_MTIME],
[[2, Constants::UNIT_WEEK], ['mtime' => 10, 'ctime' => 16], false, Constants::MODE_MTIME],
[[2, Constants::UNIT_WEEK], ['mtime' => 16, 'ctime' => 16], true, Constants::MODE_MTIME],
];
}

Expand All @@ -136,17 +143,14 @@ public function testDeleteFile(array $retentionTime, array $fileTime, bool $dele
->willReturn([$mountPoint]);

$user = $this->createMock(IUser::class);
$mountPoint->expects($this->once())
->method('getUser')
$mountPoint->method('getUser')
->willReturn($user);

$user->expects($this->once())
->method('getUID')
$user->method('getUID')
->willReturn('admin');

$userFolder = $this->createMock(Folder::class);
$this->rootFolder->expects($this->once())
->method('getUserFolder')
$this->rootFolder->method('getUserFolder')
->with('admin')
->willReturn($userFolder);

Expand All @@ -156,19 +160,29 @@ public function testDeleteFile(array $retentionTime, array $fileTime, bool $dele
->with(1337)
->willReturn([$node]);

$delta = new \DateInterval('P' . $fileTime[0] . 'D');
$now = new \DateTime();
$now->setTimestamp($this->timestampbase);
$mtime = $now->sub($delta);
if (isset($fileTime['mtime'])) {
$delta = new \DateInterval('P' . $fileTime['mtime'] . 'D');
$now = new \DateTime();
$now->setTimestamp($this->timestampbase);
$mtime = $now->sub($delta);

$node->expects($this->once())
->method('getMTime')
->willReturn($mtime->getTimestamp());
$delta = new \DateInterval('P' . $fileTime['ctime'] . 'D');
$now = new \DateTime();
$now->setTimestamp($this->timestampbase);
$ctime = $now->sub($delta);
} else {
$delta = new \DateInterval('P' . $fileTime[0] . 'D');
$now = new \DateTime();
$now->setTimestamp($this->timestampbase);
$mtime = $ctime = $now->sub($delta);
}

$node->expects($after === 0 ? $this->exactly(2) : $this->never())
->method('getUploadTime')
$node->method('getMTime')
->willReturn($mtime->getTimestamp());

$node->method('getUploadTime')
->willReturn($ctime->getTimestamp());

$node->method('isDeletable')
->willReturn(true);

Expand Down
Loading