[Avatar] Fix cache usage and other minor bugs

This commit is contained in:
Diogo Peralta Cordeiro 2021-09-23 14:03:50 +01:00 committed by Hugo Sales
parent 64c881173b
commit 4820a863a9
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 18 additions and 31 deletions

View File

@ -63,9 +63,9 @@ class Avatar extends Component
public function onAvatarUpdate(int $actor_id): bool public function onAvatarUpdate(int $actor_id): bool
{ {
Cache::delete("avatar-{$actor_id}");
foreach (['full', 'big', 'medium', 'small'] as $size) { foreach (['full', 'big', 'medium', 'small'] as $size) {
foreach ([Router::ABSOLUTE_PATH, Router::ABSOLUTE_URL] as $type) { 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-url-{$actor_id}-{$size}-{$type}");
Cache::delete("avatar-file-info-{$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. * Returns the avatar file's hash, mimetype, title and path.
* Ensures exactly one cached value exists * 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) { 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 ' . 'from App\Entity\Attachment f ' .
'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' . 'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' .
'where a.actor_id = :actor_id', 'where a.actor_id = :actor_id',

View File

@ -49,7 +49,7 @@ class Avatar extends Entity
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
private int $actor_id; private int $actor_id;
private int $attachment_id; private int $attachment_id;
private ?string $filename; private ?string $title;
private DateTimeInterface $created; private DateTimeInterface $created;
private DateTimeInterface $modified; private DateTimeInterface $modified;
@ -78,17 +78,17 @@ class Avatar extends Entity
/** /**
* @return null|string * @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 public function setCreated(DateTimeInterface $created): self
@ -128,7 +128,7 @@ class Avatar extends Entity
public function getAttachment(): Attachment 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; return $this->attachment;
} }
@ -163,7 +163,7 @@ class Avatar extends Entity
'fields' => [ 'fields' => [
'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to actor table'], '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'], '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'], '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'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
], ],

View File

@ -89,17 +89,6 @@ class AttachmentThumbnail extends Entity
return $this->mimetype; 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 public function getSize(): int
{ {
return $this->size; return $this->size;
@ -154,8 +143,7 @@ class AttachmentThumbnail extends Entity
/** /**
* @param Attachment $attachment * @param Attachment $attachment
* @param int $width * @param ?string $size
* @param int $height
* @param bool $crop * @param bool $crop
* *
* @throws ClientException * @throws ClientException
@ -174,7 +162,7 @@ class AttachmentThumbnail extends Entity
}; };
try { try {
return Cache::get('thumb-' . $attachment->getId() . "-{$size}", 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]); return DB::findOneBy('attachment_thumbnail', ['attachment_id' => $attachment->getId(), 'size' => $size_int]);
}); });
} catch (NotFoundException) { } catch (NotFoundException) {
@ -252,8 +240,7 @@ class AttachmentThumbnail extends Entity
* *
* @param int $existing_width Original width * @param int $existing_width Original width
* @param int $existing_height Original height * @param int $existing_height Original height
* @param int $allowed_aspect_ratios * @param string $requested_size
* @param int $requested_size
* @param bool $crop * @param bool $crop
* *
* @return array [predicted width, predicted height] * @return array [predicted width, predicted height]