fix(avatar): use default avatar when no avatar url has been set

- get avatar and cover urls from entity: if no image exists, retrieve the default ones.
- always
set icon and image in actor object: set the default ones if podcast hasn't set them.

fixes #111
This commit is contained in:
Yassine Doghri 2021-04-14 13:37:11 +00:00
parent e2b85a1d8f
commit 9d23c7e7e1
No known key found for this signature in database
GPG Key ID: 3E7F89498B960C9F
9 changed files with 71 additions and 25 deletions

View File

@ -11,4 +11,15 @@ class ActivityPub extends ActivityPubBase
*/
public $actorObject = 'App\Libraries\PodcastActor';
public $noteObject = 'App\Libraries\NoteObject';
/**
* --------------------------------------------------------------------
* Default avatar and cover images
* --------------------------------------------------------------------
*/
public $defaultAvatarImagePath = 'assets/images/castopod-avatar-default.jpg';
public $defaultAvatarImageMimetype = 'image/jpeg';
public $defaultCoverImagePath = 'assets/images/castopod-cover-default.jpg';
public $defaultCoverImageMimetype = 'image/jpeg';
}

View File

@ -19,4 +19,15 @@ class ActivityPub extends BaseConfig
*/
public $actorObject = 'ActivityPub\Objects\ActorObject';
public $noteObject = 'ActivityPub\Objects\NoteObject';
/**
* --------------------------------------------------------------------
* Default avatar and cover images
* --------------------------------------------------------------------
*/
public $defaultAvatarImagePath = 'assets/images/avatar-default.jpg';
public $defaultAvatarImageMimetype = 'image/jpeg';
public $defaultCoverImagePath = 'assets/images/cover-default.jpg';
public $defaultCoverImageMimetype = 'image/jpeg';
}

View File

@ -54,12 +54,14 @@ class AddActors extends Migration
'avatar_image_url' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => true,
],
// constraint is 13 because the longest safe mimetype for images is image/svg+xml,
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#image_types
'avatar_image_mimetype' => [
'type' => 'VARCHAR',
'constraint' => 13,
'null' => true,
],
'cover_image_url' => [
'type' => 'VARCHAR',

View File

@ -36,8 +36,8 @@ class Actor extends Entity
'summary' => '?string',
'private_key' => '?string',
'public_key' => '?string',
'avatar_image_url' => 'string',
'avatar_image_mimetype' => 'string',
'avatar_image_url' => '?string',
'avatar_image_mimetype' => '?string',
'cover_image_url' => '?string',
'cover_image_mimetype' => '?string',
'inbox_url' => 'string',
@ -81,4 +81,40 @@ class Actor extends Entity
return $this->followers;
}
public function getAvatarImageUrl()
{
if (empty($this->attributes['avatar_image_url'])) {
return base_url(config('ActivityPub')->defaultAvatarImagePath);
}
return $this->attributes['avatar_image_url'];
}
public function getAvatarImageMimetype()
{
if (empty($this->attributes['avatar_image_mimetype'])) {
return config('ActivityPub')->defaultAvatarImageMimetype;
}
return $this->attributes['avatar_image_mimetype'];
}
public function getCoverImageUrl()
{
if (empty($this->attributes['cover_image_url'])) {
return base_url(config('ActivityPub')->defaultCoverImagePath);
}
return $this->attributes['cover_image_url'];
}
public function getCoverImageMimetype()
{
if (empty($this->attributes['cover_image_mimetype'])) {
return config('ActivityPub')->defaultCoverImageMimetype;
}
return $this->attributes['cover_image_mimetype'];
}
}

View File

@ -91,13 +91,12 @@ class ActorObject extends ObjectType
$this->outbox = $actor->outbox_url;
$this->followers = $actor->followers_url;
if ($actor->cover_image_url) {
$this->image = [
'type' => 'Image',
'mediaType' => $actor->cover_image_mimetype,
'url' => $actor->cover_image_url,
];
}
$this->image = [
'type' => 'Image',
'mediaType' => $actor->cover_image_mimetype,
'url' => $actor->cover_image_url,
];
$this->icon = [
'type' => 'Image',
'mediaType' => $actor->avatar_image_mimetype,

View File

@ -297,12 +297,6 @@ class PodcastModel extends Model
'public_key' => $publickey,
'display_name' => $data['data']['title'],
'summary' => $data['data']['description_html'],
'avatar_image_url' => '',
'avatar_image_mimetype' => '',
'cover_image_url' => base_url(
'assets/images/castopod-cover-default.jpg',
),
'cover_image_mimetype' => 'image/jpeg',
'inbox_url' => url_to('inbox', $username),
'outbox_url' => url_to('outbox', $username),
'followers_url' => url_to('followers', $username),
@ -342,6 +336,7 @@ class PodcastModel extends Model
$actor->display_name = $podcast->title;
$actor->summary = $podcast->description_html;
$actor->avatar_image_url = $podcast->image->thumbnail_url;
$actor->avatar_image_mimetype = $podcast->image_mimetype;
if ($actor->hasChanged()) {
$actorModel->update($actor->id, $actor);

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

View File

@ -1,10 +1,6 @@
<header id="main-header" class="fixed top-0 left-0 flex-col flex-shrink-0 h-screen transform -translate-x-full sm:left-auto sm:-translate-x-0 sm:sticky w-80 sm:w-64 lg:w-80 xl:w-112 sm:flex">
<?php if ($podcast->actor->cover_image_url): ?>
<img src="<?= $podcast->actor
->cover_image_url ?>" alt="" class="object-cover w-full h-48 bg-pine-900"/>
<?php else: ?>
<div class="w-full h-48 bg-pine-900"></div>
<?php endif; ?>
<img src="<?= $podcast->actor
->cover_image_url ?>" alt="" class="object-cover w-full h-48 bg-pine-900"/>
<div class="flex items-center justify-between px-4 py-2 mb-4 lg:px-6 -mt-14 lg:-mt-16 xl:-mt-20">
<img src="<?= $podcast->image
->thumbnail_url ?>" alt="<?= $podcast->title ?>" class="h-24 rounded-full shadow-xl xl:h-36 lg:h-28 ring-4 ring-pine-50" />

View File

@ -31,11 +31,7 @@
'ActivityPub.follow.subtitle',
) ?></h1>
<div class="flex flex-col w-full max-w-xs -mt-24 overflow-hidden bg-white shadow rounded-xl">
<?php if ($actor->cover_image_url): ?>
<img src="<?= $actor->cover_image_url ?>" alt="" class="object-cover w-full h-32 bg-pine-800" />
<?php else: ?>
<div class="w-full h-32 bg-pine-800"></div>
<?php endif; ?>
<div class="flex px-4 py-2">
<img src="<?= $actor->avatar_image_url ?>" alt="<?= $actor->display_name ?>"
class="w-16 h-16 mr-4 -mt-8 rounded-full shadow-xl ring-2 ring-white" />