From 4820a863a908f043d7d9828a50e2d59525d8d1ea Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Thu, 23 Sep 2021 14:03:50 +0100 Subject: [PATCH] [Avatar] Fix cache usage and other minor bugs --- components/Avatar/Avatar.php | 8 ++++---- components/Avatar/Entity/Avatar.php | 16 ++++++++-------- src/Entity/AttachmentThumbnail.php | 25 ++++++------------------- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/components/Avatar/Avatar.php b/components/Avatar/Avatar.php index 00e2d8b1e9..dae757a16c 100644 --- a/components/Avatar/Avatar.php +++ b/components/Avatar/Avatar.php @@ -63,9 +63,9 @@ class Avatar extends Component public function onAvatarUpdate(int $actor_id): bool { + Cache::delete("avatar-{$actor_id}"); foreach (['full', 'big', 'medium', 'small'] as $size) { foreach ([Router::ABSOLUTE_PATH, Router::ABSOLUTE_URL] as $type) { - Cache::delete("avatar-{$actor_id}-{$size}-{$type}"); Cache::delete("avatar-url-{$actor_id}-{$size}-{$type}"); Cache::delete("avatar-file-info-{$actor_id}-{$size}-{$type}"); } @@ -109,11 +109,11 @@ class Avatar extends Component * Returns the avatar file's hash, mimetype, title and path. * Ensures exactly one cached value exists */ - public static function getAvatarFileInfo(int $actor_id, int $size = 0): array + public static function getAvatarFileInfo(int $actor_id, string $size = 'full'): array { - $res = Cache::get("avatar-file-info-{$actor_id}", + $res = Cache::get("avatar-file-info-{$actor_id}-{$size}", function () use ($actor_id) { - return DB::dql('select f.id, f.filename, a.filename title, f.mimetype ' . + return DB::dql('select f.id, f.filename, a.title, f.mimetype ' . 'from App\Entity\Attachment f ' . 'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' . 'where a.actor_id = :actor_id', diff --git a/components/Avatar/Entity/Avatar.php b/components/Avatar/Entity/Avatar.php index 2bae0566eb..2aa15ef586 100644 --- a/components/Avatar/Entity/Avatar.php +++ b/components/Avatar/Entity/Avatar.php @@ -49,7 +49,7 @@ class Avatar extends Entity // @codeCoverageIgnoreStart private int $actor_id; private int $attachment_id; - private ?string $filename; + private ?string $title; private DateTimeInterface $created; private DateTimeInterface $modified; @@ -78,17 +78,17 @@ class Avatar extends Entity /** * @return null|string */ - public function getFilename(): ?string + public function getTitle(): ?string { - return $this->filename; + return $this->title; } /** - * @param null|string $filename + * @param null|string $title */ - public function setFilename(?string $filename): void + public function setTitle(?string $title): void { - $this->filename = $filename; + $this->title = $title; } public function setCreated(DateTimeInterface $created): self @@ -128,7 +128,7 @@ class Avatar extends Entity public function getAttachment(): Attachment { - $this->attachment = $this->attachment ?: DB::findOneBy('attachment', ['id' => $this->attachment_id]); + $this->attachment = $this->attachment ?? DB::findOneBy('attachment', ['id' => $this->attachment_id]); return $this->attachment; } @@ -163,7 +163,7 @@ class Avatar extends Entity 'fields' => [ 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to actor table'], 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to attachment table'], - 'filename' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'], + 'title' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'], 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'], ], diff --git a/src/Entity/AttachmentThumbnail.php b/src/Entity/AttachmentThumbnail.php index b50f6a5b1e..6ff83d09c3 100644 --- a/src/Entity/AttachmentThumbnail.php +++ b/src/Entity/AttachmentThumbnail.php @@ -89,17 +89,6 @@ class AttachmentThumbnail extends Entity return $this->mimetype; } - public function setWidth(int $width): self - { - $this->width = $width; - return $this; - } - - public function getWidth(): int - { - return $this->width; - } - public function getSize(): int { return $this->size; @@ -154,8 +143,7 @@ class AttachmentThumbnail extends Entity /** * @param Attachment $attachment - * @param int $width - * @param int $height + * @param ?string $size * @param bool $crop * * @throws ClientException @@ -174,7 +162,7 @@ class AttachmentThumbnail extends Entity }; try { return Cache::get('thumb-' . $attachment->getId() . "-{$size}", - function () use ($crop, $attachment, $size_int) { + function () use ($attachment, $size_int) { return DB::findOneBy('attachment_thumbnail', ['attachment_id' => $attachment->getId(), 'size' => $size_int]); }); } catch (NotFoundException) { @@ -250,11 +238,10 @@ class AttachmentThumbnail extends Entity * Values will scale _up_ to fit max values if cropping is enabled! * With cropping disabled, the max value of each axis will be respected. * - * @param int $existing_width Original width - * @param int $existing_height Original height - * @param int $allowed_aspect_ratios - * @param int $requested_size - * @param bool $crop + * @param int $existing_width Original width + * @param int $existing_height Original height + * @param string $requested_size + * @param bool $crop * * @return array [predicted width, predicted height] */