[COMPONENT][Avatar] Save title if possible, delete correctly, no early flushes

[PLUGIN][ActivityPub] Minor bug fixes in Actor translation
This commit is contained in:
Diogo Peralta Cordeiro 2022-01-02 03:06:07 +00:00
parent d27e8610d6
commit 046731a05a
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 53 additions and 24 deletions

View File

@ -33,7 +33,6 @@ use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\NotFoundException;
use App\Util\TemporaryFile; use App\Util\TemporaryFile;
use Component\Avatar\Entity\Avatar as AvatarEntity; use Component\Avatar\Entity\Avatar as AvatarEntity;
use Exception; use Exception;
@ -80,15 +79,14 @@ class Avatar extends Controller
$user = Common::user(); $user = Common::user();
$actor_id = $user->getId(); $actor_id = $user->getId();
if ($data['remove'] == true) { if ($data['remove'] == true) {
try { if (\is_null($avatar = DB::findOneBy(AvatarEntity::class, ['actor_id' => $actor_id], return_null: true))) {
$avatar = DB::findOneBy('avatar', ['actor_id' => $actor_id]); $form->addError(new FormError(_m('No avatar set, so cannot delete.')));
} else {
$avatar->delete(); $avatar->delete();
Event::handle('AvatarUpdate', [$user->getId()]);
} catch (NotFoundException) {
$form->addError(new FormError(_m('No avatar set, so cannot delete')));
} }
} else { } else {
$attachment = null; $attachment = null;
$title = $data['avatar']?->getClientOriginalName() ?? null;
if (isset($data['hidden'])) { if (isset($data['hidden'])) {
// Cropped client side // Cropped client side
$matches = []; $matches = [];
@ -108,15 +106,18 @@ class Avatar extends Controller
$file = $data['avatar']; $file = $data['avatar'];
$attachment = GSFile::storeFileAsAttachment($file); $attachment = GSFile::storeFileAsAttachment($file);
} else { } else {
throw new ClientException('Invalid form'); throw new ClientException(_m('Invalid form.'));
} }
// Delete current avatar if there's one // Delete current avatar if there's one
$avatar = DB::find('avatar', ['actor_id' => $actor_id]); if (!\is_null($avatar = DB::findOneBy(AvatarEntity::class, ['actor_id' => $actor_id], return_null: true))) {
$avatar?->delete(); $avatar->delete();
}
DB::persist($attachment); DB::persist($attachment);
// Can only get new id after inserting DB::persist(AvatarEntity::create([
DB::flush(); 'actor_id' => $actor_id,
DB::persist(AvatarEntity::create(['actor_id' => $actor_id, 'attachment_id' => $attachment->getId()])); 'attachment_id' => $attachment->getId(),
'title' => $title,
]));
DB::flush(); DB::flush();
Event::handle('AvatarUpdate', [$user->getId()]); Event::handle('AvatarUpdate', [$user->getId()]);
} }

View File

@ -26,6 +26,7 @@ namespace Component\Avatar\Entity;
use App\Core\Cache; use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\Event;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Util\Common; use App\Util\Common;
use Component\Attachment\Entity\Attachment; use Component\Attachment\Entity\Attachment;
@ -148,10 +149,11 @@ class Avatar extends Entity
*/ */
public function delete(): bool public function delete(): bool
{ {
DB::remove($this); $actor_id = $this->getActorId();
$attachment = $this->getAttachment(); $attachment = $this->getAttachment();
DB::wrapInTransaction(fn () => DB::removeBy(static::class, ['actor_id' => $actor_id]));
$attachment->kill(); $attachment->kill();
DB::flush(); Event::handle('AvatarUpdate', [$actor_id]);
return true; return true;
} }

View File

@ -135,11 +135,16 @@ class Actor extends Model
$temp_file->write($media); $temp_file->write($media);
$attachment = GSFile::storeFileAsAttachment($temp_file); $attachment = GSFile::storeFileAsAttachment($temp_file);
// Delete current avatar if there's one // Delete current avatar if there's one
$avatar = DB::find('avatar', ['actor_id' => $actor->getId()]); if (!\is_null($avatar = DB::findOneBy(\Component\Avatar\Entity\Avatar::class, ['actor_id' => $actor->getId()], return_null: true))) {
$avatar?->delete(); $avatar->delete();
DB::wrapInTransaction(function () use ($attachment, $actor) { }
DB::wrapInTransaction(function () use ($attachment, $actor, $person) {
DB::persist($attachment); 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()]); Event::handle('AvatarUpdate', [$actor->getId()]);
} }
@ -176,11 +181,11 @@ class Actor extends Model
} }
$rsa = ActivitypubRsa::getByActor($object); $rsa = ActivitypubRsa::getByActor($object);
$public_key = $rsa->getPublicKey(); $public_key = $rsa->getPublicKey();
$uri = null; $uri = $object->getUri(Router::ABSOLUTE_URL);
$attr = [ $attr = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Person', 'type' => 'Person',
'id' => $object->getUri(Router::ABSOLUTE_URL), 'id' => $uri,
'inbox' => Router::url('activitypub_actor_inbox', ['gsactor_id' => $object->getId()], Router::ABSOLUTE_URL), '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), '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), '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), 'published' => $object->getCreated()->format(DateTimeInterface::RFC3339),
'summary' => $object->getBio(), 'summary' => $object->getBio(),
//'tag' => $object->getSelfTags(), //'tag' => $object->getSelfTags(),
'updated' => $object->getModified()->format(DateTimeInterface::RFC3339), 'updated' => $object->getModified()->format(DateTimeInterface::RFC3339),
'url' => $object->getUrl(Router::ABSOLUTE_URL), 'url' => $object->getUrl(Router::ABSOLUTE_URL),
'endpoints' => [
'sharedInbox' => Router::url('activitypub_inbox', type: Router::ABSOLUTE_URL),
],
]; ];
// Avatar // Avatar
try { try {
$avatar = Avatar::getAvatar($object->getId()); $avatar = Avatar::getAvatar($object->getId());
$attr['icon'] = $attr['image'] = [ $attr['icon'] = [
'type' => 'Image', 'type' => 'Image',
'summary' => 'Small Avatar',
'name' => \is_null($avatar->getTitle()) ? null : 'small-' . $avatar->getTitle(),
'mediaType' => $avatar->getAttachment()->getMimetype(), '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) { } catch (Exception) {
// No icon for this actor // No icon for this actor