[DB][Attachments] Use count function rathar than fetch and count, rename to refCount, rather than countDepencies

This commit is contained in:
Hugo Sales 2021-08-10 08:04:03 +00:00
parent 8880405dee
commit 39006fb6b5
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 8 additions and 15 deletions

View File

@ -75,10 +75,9 @@ class Avatar extends Component
return Event::next;
}
public function onAttachmentCountDependencies(int $attachment_id, int &$dependencies): bool
public function onAttachmentRefCount(int $attachment_id, int &$dependencies): bool
{
$avatars = DB::findBy('avatar', ['attachment_id' => $attachment_id]);
$dependencies += count($avatars);
$dependencies += DB::count('avatar', ['attachment_id' => $attachment_id]);
return Event::next;
}

View File

@ -133,7 +133,7 @@ class Avatar extends Entity
if ($cascade) {
$attachment = $this->getAttachment();
// We can't use $attachment->isSafeDelete() because underlying findBy doesn't respect remove persistence
if ($attachment->countDependencies() - 1 === 0) {
if ($attachment->refCount() - 1 === 0) {
$attachment->delete(cascade: true, flush: false);
}
}

View File

@ -246,28 +246,22 @@ class Attachment extends Entity
const URLHASH_ALGO = 'sha256';
const FILEHASH_ALGO = 'sha256';
/**
*
* Warning: Underlying DB::findBy doesn't respect remove persistence
* @return int
*/
public function countDependencies(): int
public function refCount(): int
{
$attachment_id = $this->getId();
$notes = DB::findBy('attachment_to_note', ['attachment_id' => $attachment_id]);
$dependencies = count($notes);
Event::handle('AttachmentCountDependencies', [$attachment_id, &$dependencies]);
$dependencies = DB::count('attachment_to_note', ['attachment_id' => $attachment_id]);
Event::handle('AttachmentRefCount', [$attachment_id, &$dependencies]);
return $dependencies;
}
/**
* @depends Attachment->refCount()
*
* @depends Attachment->countDependencies()
* @return bool
*/
public function isSafeDelete(): bool
{
return $this->countDependencies() === 0;
return $this->refCount() === 0;
}
/**