From 046731a05a1e6b73f330d7ad13a6f192cd17db16 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Sun, 2 Jan 2022 03:06:07 +0000 Subject: [PATCH] [COMPONENT][Avatar] Save title if possible, delete correctly, no early flushes [PLUGIN][ActivityPub] Minor bug fixes in Actor translation --- components/Avatar/Controller/Avatar.php | 25 ++++++------- components/Avatar/Entity/Avatar.php | 6 ++-- plugins/ActivityPub/Util/Model/Actor.php | 46 ++++++++++++++++++------ 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/components/Avatar/Controller/Avatar.php b/components/Avatar/Controller/Avatar.php index d51d2b5449..26f3053b28 100644 --- a/components/Avatar/Controller/Avatar.php +++ b/components/Avatar/Controller/Avatar.php @@ -33,7 +33,6 @@ use function App\Core\I18n\_m; use App\Core\Log; use App\Util\Common; use App\Util\Exception\ClientException; -use App\Util\Exception\NotFoundException; use App\Util\TemporaryFile; use Component\Avatar\Entity\Avatar as AvatarEntity; use Exception; @@ -80,15 +79,14 @@ class Avatar extends Controller $user = Common::user(); $actor_id = $user->getId(); if ($data['remove'] == true) { - try { - $avatar = DB::findOneBy('avatar', ['actor_id' => $actor_id]); + if (\is_null($avatar = DB::findOneBy(AvatarEntity::class, ['actor_id' => $actor_id], return_null: true))) { + $form->addError(new FormError(_m('No avatar set, so cannot delete.'))); + } else { $avatar->delete(); - Event::handle('AvatarUpdate', [$user->getId()]); - } catch (NotFoundException) { - $form->addError(new FormError(_m('No avatar set, so cannot delete'))); } } else { $attachment = null; + $title = $data['avatar']?->getClientOriginalName() ?? null; if (isset($data['hidden'])) { // Cropped client side $matches = []; @@ -108,15 +106,18 @@ class Avatar extends Controller $file = $data['avatar']; $attachment = GSFile::storeFileAsAttachment($file); } else { - throw new ClientException('Invalid form'); + throw new ClientException(_m('Invalid form.')); } // Delete current avatar if there's one - $avatar = DB::find('avatar', ['actor_id' => $actor_id]); - $avatar?->delete(); + if (!\is_null($avatar = DB::findOneBy(AvatarEntity::class, ['actor_id' => $actor_id], return_null: true))) { + $avatar->delete(); + } DB::persist($attachment); - // Can only get new id after inserting - DB::flush(); - DB::persist(AvatarEntity::create(['actor_id' => $actor_id, 'attachment_id' => $attachment->getId()])); + DB::persist(AvatarEntity::create([ + 'actor_id' => $actor_id, + 'attachment_id' => $attachment->getId(), + 'title' => $title, + ])); DB::flush(); Event::handle('AvatarUpdate', [$user->getId()]); } diff --git a/components/Avatar/Entity/Avatar.php b/components/Avatar/Entity/Avatar.php index 88f8b0dd05..bbc51b01a6 100644 --- a/components/Avatar/Entity/Avatar.php +++ b/components/Avatar/Entity/Avatar.php @@ -26,6 +26,7 @@ namespace Component\Avatar\Entity; use App\Core\Cache; use App\Core\DB\DB; use App\Core\Entity; +use App\Core\Event; use App\Core\Router\Router; use App\Util\Common; use Component\Attachment\Entity\Attachment; @@ -148,10 +149,11 @@ class Avatar extends Entity */ public function delete(): bool { - DB::remove($this); + $actor_id = $this->getActorId(); $attachment = $this->getAttachment(); + DB::wrapInTransaction(fn () => DB::removeBy(static::class, ['actor_id' => $actor_id])); $attachment->kill(); - DB::flush(); + Event::handle('AvatarUpdate', [$actor_id]); return true; } diff --git a/plugins/ActivityPub/Util/Model/Actor.php b/plugins/ActivityPub/Util/Model/Actor.php index c83f5c29fe..c3ab3546a4 100644 --- a/plugins/ActivityPub/Util/Model/Actor.php +++ b/plugins/ActivityPub/Util/Model/Actor.php @@ -135,11 +135,16 @@ class Actor extends Model $temp_file->write($media); $attachment = GSFile::storeFileAsAttachment($temp_file); // Delete current avatar if there's one - $avatar = DB::find('avatar', ['actor_id' => $actor->getId()]); - $avatar?->delete(); - DB::wrapInTransaction(function () use ($attachment, $actor) { + if (!\is_null($avatar = DB::findOneBy(\Component\Avatar\Entity\Avatar::class, ['actor_id' => $actor->getId()], return_null: true))) { + $avatar->delete(); + } + DB::wrapInTransaction(function () use ($attachment, $actor, $person) { DB::persist($attachment); - DB::persist(\Component\Avatar\Entity\Avatar::create(['actor_id' => $actor->getId(), 'attachment_id' => $attachment->getId()])); + DB::persist(\Component\Avatar\Entity\Avatar::create([ + 'actor_id' => $actor->getId(), + 'attachment_id' => $attachment->getId(), + 'title' => $person->get('icon')->get('name') ?? null, + ])); }); Event::handle('AvatarUpdate', [$actor->getId()]); } @@ -176,11 +181,11 @@ class Actor extends Model } $rsa = ActivitypubRsa::getByActor($object); $public_key = $rsa->getPublicKey(); - $uri = null; + $uri = $object->getUri(Router::ABSOLUTE_URL); $attr = [ '@context' => 'https://www.w3.org/ns/activitystreams', 'type' => 'Person', - 'id' => $object->getUri(Router::ABSOLUTE_URL), + 'id' => $uri, 'inbox' => Router::url('activitypub_actor_inbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL), 'outbox' => Router::url('activitypub_actor_outbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL), 'following' => Router::url('actor_subscriptions_id', ['id' => $object->getId()], Router::ABSOLUTE_URL), @@ -198,17 +203,38 @@ class Actor extends Model 'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), 'summary' => $object->getBio(), //'tag' => $object->getSelfTags(), - 'updated' => $object->getModified()->format(DateTimeInterface::RFC3339), - 'url' => $object->getUrl(Router::ABSOLUTE_URL), + 'updated' => $object->getModified()->format(DateTimeInterface::RFC3339), + 'url' => $object->getUrl(Router::ABSOLUTE_URL), + 'endpoints' => [ + 'sharedInbox' => Router::url('activitypub_inbox', type: Router::ABSOLUTE_URL), + ], ]; // Avatar try { $avatar = Avatar::getAvatar($object->getId()); - $attr['icon'] = $attr['image'] = [ + $attr['icon'] = [ 'type' => 'Image', + 'summary' => 'Small Avatar', + 'name' => \is_null($avatar->getTitle()) ? null : 'small-' . $avatar->getTitle(), 'mediaType' => $avatar->getAttachment()->getMimetype(), - 'url' => $avatar->getUrl(type: Router::ABSOLUTE_URL), + 'url' => $avatar->getUrl(size: 'small', type: Router::ABSOLUTE_URL), + ]; + $attr['image'] = [ + [ + 'type' => 'Image', + 'summary' => 'Medium Avatar', + 'name' => \is_null($avatar->getTitle()) ? null : 'medium-' . $avatar->getTitle(), + 'mediaType' => $avatar->getAttachment()->getMimetype(), + 'url' => $avatar->getUrl(size: 'medium', type: Router::ABSOLUTE_URL), + ], + [ + 'type' => 'Image', + 'summary' => 'Full Avatar', + 'name' => $avatar->getTitle(), + 'mediaType' => $avatar->getAttachment()->getMimetype(), + 'url' => $avatar->getUrl(size: 'full', type: Router::ABSOLUTE_URL), + ], ]; } catch (Exception) { // No icon for this actor