Skip to content

Commit 2ae66ff

Browse files
authored
Merge pull request #59043 from nextcloud/jtr/chore-trashbin-drop-abortOperation-usage
chore(trashbin): deprecate abortOperation on BeforeNodeRestoredEvent / switch to AbortedEventException
2 parents 9b125a7 + 8bd63c5 commit 2ae66ff

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

apps/files_trashbin/lib/Events/BeforeNodeRestoredEvent.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
namespace OCA\Files_Trashbin\Events;
1010

11-
use Exception;
11+
use OCP\Exceptions\AbortedEventException;
1212
use OCP\Files\Events\Node\AbstractNodesEvent;
1313
use OCP\Files\Node;
1414

@@ -25,15 +25,10 @@ public function __construct(
2525
}
2626

2727
/**
28-
* @return never
28+
* @since 28.0.0
29+
* @deprecated 29.0.0 - use OCP\Exceptions\AbortedEventException instead
2930
*/
3031
public function abortOperation(?\Throwable $ex = null) {
31-
$this->stopPropagation();
32-
$this->run = false;
33-
if ($ex !== null) {
34-
throw $ex;
35-
} else {
36-
throw new Exception('Operation aborted');
37-
}
32+
throw new AbortedEventException($ex?->getMessage() ?? 'Operation aborted');
3833
}
3934
}

apps/files_trashbin/lib/Listeners/SyncLivePhotosListener.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
use OCA\Files_Trashbin\Trash\ITrashManager;
1515
use OCP\EventDispatcher\Event;
1616
use OCP\EventDispatcher\IEventListener;
17+
use OCP\Exceptions\AbortedEventException;
1718
use OCP\Files\Folder;
1819
use OCP\Files\Node;
19-
use OCP\Files\NotFoundException;
20-
use OCP\Files\NotPermittedException;
2120
use OCP\IUserSession;
2221

2322
/**
@@ -76,7 +75,7 @@ private function handleRestore(BeforeNodeRestoredEvent $event, Node $peerFile):
7675
unset($this->pendingRestores[$peerFile->getId()]);
7776
return;
7877
} else {
79-
$event->abortOperation(new NotPermittedException('Cannot restore the video part of a live photo'));
78+
throw new AbortedEventException('Cannot restore the video part of a live photo');
8079
}
8180
} else {
8281
$user = $this->userSession?->getUser();
@@ -94,14 +93,14 @@ private function handleRestore(BeforeNodeRestoredEvent $event, Node $peerFile):
9493
$trashItem = $this->getTrashItem($trashRoot, $peerFile->getInternalPath());
9594

9695
if ($trashItem === null) {
97-
$event->abortOperation(new NotFoundException("Couldn't find peer file in trashbin"));
96+
throw new AbortedEventException('Could not find peer file in trashbin');
9897
}
9998

10099
$this->pendingRestores[$sourceFile->getId()] = true;
101100
try {
102101
$this->trashManager->restoreItem($trashItem);
103102
} catch (\Throwable $ex) {
104-
$event->abortOperation($ex);
103+
throw new AbortedEventException($ex->getMessage());
105104
}
106105
}
107106
}

apps/files_trashbin/lib/Trashbin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCP\EventDispatcher\Event;
3030
use OCP\EventDispatcher\IEventDispatcher;
3131
use OCP\EventDispatcher\IEventListener;
32+
use OCP\Exceptions\AbortedEventException;
3233
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
3334
use OCP\Files\File;
3435
use OCP\Files\Folder;
@@ -515,7 +516,11 @@ public static function restore($file, $filename, $timestamp) {
515516
$run = true;
516517
$event = new BeforeNodeRestoredEvent($sourceNode, $targetNode, $run);
517518
$dispatcher = Server::get(IEventDispatcher::class);
518-
$dispatcher->dispatchTyped($event);
519+
try {
520+
$dispatcher->dispatchTyped($event);
521+
} catch (AbortedEventException) {
522+
$run = false;
523+
}
519524

520525
if (!$run) {
521526
return false;

0 commit comments

Comments
 (0)