[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\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()]);
}

View File

@ -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;
}

View File

@ -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