[COMPONENT][Attachment] Wrap delete operations in transactions and correct sequence of deletation

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-28 06:15:39 +00:00
parent bf4a0008ef
commit 46d121ef7b
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 6 additions and 3 deletions

View File

@ -22,6 +22,7 @@ declare(strict_types = 1);
namespace Component\Attachment; namespace Component\Attachment;
use App\Core\Cache; use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Modules\Component; use App\Core\Modules\Component;
use App\Core\Router\RouteLoader; use App\Core\Router\RouteLoader;
@ -56,13 +57,14 @@ class Attachment extends Component
return Event::stop; return Event::stop;
} }
public function onNoteDeleteRelated(Note &$note): bool public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
{ {
Cache::delete("note-attachments-{$note->getId()}"); Cache::delete("note-attachments-{$note->getId()}");
E\AttachmentToNote::removeWhereNoteId($note->getId());
foreach ($note->getAttachments() as $attachment) { foreach ($note->getAttachments() as $attachment) {
$attachment->kill(); $attachment->kill();
} }
DB::wrapInTransaction(fn () => E\AttachmentToNote::removeWhereNoteId($note->getId()));
Cache::delete("note-attachments-{$note->getId()}");
return Event::next; return Event::next;
} }

View File

@ -202,7 +202,7 @@ class Attachment extends Entity
public function kill(): bool public function kill(): bool
{ {
if ($this->livesDecrementAndGet() <= 0) { if ($this->livesDecrementAndGet() <= 0) {
return $this->delete(); return DB::wrapInTransaction(fn () => $this->delete());
} }
return true; return true;
} }
@ -237,6 +237,7 @@ class Attachment extends Entity
/** /**
* Attachment delete always removes dependencies, cleanups and flushes * 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 * @see kill() It's more likely that you want to use that rather than call delete directly
*/ */