diff --git a/components/Attachment/Attachment.php b/components/Attachment/Attachment.php index 1f39e397f2..b2dce2c73b 100644 --- a/components/Attachment/Attachment.php +++ b/components/Attachment/Attachment.php @@ -22,6 +22,7 @@ declare(strict_types = 1); namespace Component\Attachment; use App\Core\Cache; +use App\Core\DB\DB; use App\Core\Event; use App\Core\Modules\Component; use App\Core\Router\RouteLoader; @@ -56,13 +57,14 @@ class Attachment extends Component return Event::stop; } - public function onNoteDeleteRelated(Note &$note): bool + public function onNoteDeleteRelated(Note &$note, Actor $actor): bool { Cache::delete("note-attachments-{$note->getId()}"); - E\AttachmentToNote::removeWhereNoteId($note->getId()); foreach ($note->getAttachments() as $attachment) { $attachment->kill(); } + DB::wrapInTransaction(fn () => E\AttachmentToNote::removeWhereNoteId($note->getId())); + Cache::delete("note-attachments-{$note->getId()}"); return Event::next; } diff --git a/components/Attachment/Entity/Attachment.php b/components/Attachment/Entity/Attachment.php index dcf4c687c7..99a1a9c5fc 100644 --- a/components/Attachment/Entity/Attachment.php +++ b/components/Attachment/Entity/Attachment.php @@ -202,7 +202,7 @@ class Attachment extends Entity public function kill(): bool { if ($this->livesDecrementAndGet() <= 0) { - return $this->delete(); + return DB::wrapInTransaction(fn () => $this->delete()); } return true; } @@ -237,6 +237,7 @@ class Attachment extends Entity /** * Attachment delete always removes dependencies, cleanups and flushes + * WARNING: Wrap this function in a transaction! * * @see kill() It's more likely that you want to use that rather than call delete directly */