[Avatar] Move entity from core to component

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-04 19:12:37 +01:00 committed by Hugo Sales
parent fb6aa78ae8
commit 3334aca7b9
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 28 additions and 23 deletions

View File

@ -34,25 +34,26 @@ use Symfony\Component\HttpFoundation\Request;
class Avatar extends Component class Avatar extends Component
{ {
public function onAddRoute($r) public function onAddRoute($r): bool
{ {
$r->connect('avatar', '/{gsactor_id<\d+>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'avatar_view']); $r->connect('avatar', '/{gsactor_id<\d+>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'avatar_view']);
$r->connect('settings_avatar', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']); $r->connect('settings_avatar', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
return Event::next; return Event::next;
} }
public function onPopulateProfileSettingsTabs(Request $request, &$tabs) public function onPopulateProfileSettingsTabs(Request $request, &$tabs): bool
{ {
// TODO avatar template shouldn't be on settings folder // TODO avatar template shouldn't be on settings folder
$tabs[] = ['title' => 'Avatar', $tabs[] = [
'desc' => 'Change your avatar.', 'title' => 'Avatar',
'controller' => C\Avatar::settings_avatar($request), 'desc' => 'Change your avatar.',
'controller' => C\Avatar::settings_avatar($request),
]; ];
return Event::next; return Event::next;
} }
public function onStartTwigPopulateVars(array &$vars) public function onStartTwigPopulateVars(array &$vars): bool
{ {
if (Common::user() != null) { if (Common::user() != null) {
$vars['user_avatar'] = self::getAvatarUrl(); $vars['user_avatar'] = self::getAvatarUrl();
@ -60,7 +61,7 @@ class Avatar extends Component
return Event::next; return Event::next;
} }
public function onGetAvatarUrl(int $gsactor_id, ?string &$url) public function onGetAvatarUrl(int $gsactor_id, ?string &$url): bool
{ {
$url = self::getAvatarUrl($gsactor_id); $url = self::getAvatarUrl($gsactor_id);
return Event::next; return Event::next;
@ -78,14 +79,14 @@ class Avatar extends Component
/** /**
* Get the avatar associated with the given nickname * Get the avatar associated with the given nickname
*/ */
public static function getAvatar(?int $gsactor_id = null): \App\Entity\Avatar public static function getAvatar(?int $gsactor_id = null): Entity\Avatar
{ {
$gsactor_id = $gsactor_id ?: Common::userNickname(); $gsactor_id = $gsactor_id ?: Common::userNickname();
return GSFile::error(NoAvatarException::class, return GSFile::error(NoAvatarException::class,
$gsactor_id, $gsactor_id,
Cache::get("avatar-{$gsactor_id}", Cache::get("avatar-{$gsactor_id}",
function () use ($gsactor_id) { function () use ($gsactor_id) {
return DB::dql('select a from App\Entity\Avatar a ' . return DB::dql('select a from Component\Avatar\Entity\Avatar a ' .
'where a.gsactor_id = :gsactor_id', 'where a.gsactor_id = :gsactor_id',
['gsactor_id' => $gsactor_id]); ['gsactor_id' => $gsactor_id]);
})); }));
@ -122,11 +123,11 @@ class Avatar extends Component
function () use ($gsactor_id) { function () use ($gsactor_id) {
return DB::dql('select f.file_hash, f.mimetype, f.title ' . return DB::dql('select f.file_hash, f.mimetype, f.title ' .
'from App\Entity\Attachment f ' . 'from App\Entity\Attachment f ' .
'join App\Entity\Avatar a with f.id = a.attachment_id ' . 'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' .
'where a.gsactor_id = :gsactor_id', 'where a.gsactor_id = :gsactor_id',
['gsactor_id' => $gsactor_id]); ['gsactor_id' => $gsactor_id]);
})); }));
$res['file_path'] = \App\Entity\Avatar::getFilePathStatic($res['file_hash']); $res['file_path'] = Entity\Avatar::getFilePathStatic($res['file_hash']);
return $res; return $res;
} catch (Exception $e) { } catch (Exception $e) {
$filepath = INSTALLDIR . '/public/assets/default-avatar.svg'; $filepath = INSTALLDIR . '/public/assets/default-avatar.svg';

View File

@ -28,10 +28,12 @@ use App\Core\Form;
use App\Core\GSFile; use App\Core\GSFile;
use App\Core\GSFile as M; use App\Core\GSFile as M;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Entity\Avatar as AvatarEntity; use App\Core\Log;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\TemporaryFile; use App\Util\TemporaryFile;
use Component\Avatar\Entity\Avatar as AvatarEntity;
use Exception; use Exception;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\FileType;
@ -39,13 +41,14 @@ use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Avatar extends Controller class Avatar extends Controller
{ {
/** /**
* @throws Exception * @throws Exception
*/ */
public function avatar_view(Request $request, int $gsactor_id, string $size) public function avatar_view(Request $request, int $gsactor_id, string $size): Response
{ {
switch ($size) { switch ($size) {
case 'full': case 'full':
@ -59,7 +62,7 @@ class Avatar extends Controller
/** /**
* Local user avatar panel * Local user avatar panel
*/ */
public static function settings_avatar(Request $request) public static function settings_avatar(Request $request): array
{ {
$form = Form::create([ $form = Form::create([
['avatar', FileType::class, ['label' => _m('Avatar'), 'help' => _m('You can upload your personal avatar. The maximum file size is 2MB.'), 'multiple' => false, 'required' => false]], ['avatar', FileType::class, ['label' => _m('Avatar'), 'help' => _m('You can upload your personal avatar. The maximum file size is 2MB.'), 'multiple' => false, 'required' => false]],
@ -97,7 +100,7 @@ class Avatar extends Controller
} }
} }
} elseif (isset($data['avatar'])) { } elseif (isset($data['avatar'])) {
// Cropping failed (e.g. disabled js), have file as uploaded // Cropping failed (e.g. disabled js), use file as uploaded
$file = $data['avatar']; $file = $data['avatar'];
} else { } else {
throw new ClientException('Invalid form'); throw new ClientException('Invalid form');
@ -112,9 +115,7 @@ class Avatar extends Controller
// Must get old id before inserting another one // Must get old id before inserting another one
$old_attachment = null; $old_attachment = null;
$avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]); $avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]);
if ($avatar != null) { $old_attachment = $avatar?->delete();
$old_attachment = $avatar->delete();
}
DB::persist($attachment); DB::persist($attachment);
// Can only get new id after inserting // Can only get new id after inserting
DB::flush(); DB::flush();

View File

@ -19,11 +19,13 @@
// }}} // }}}
namespace App\Entity; namespace Component\Avatar\Entity;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\Log;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Attachment;
use App\Util\Common; use App\Util\Common;
use DateTimeInterface; use DateTimeInterface;
@ -47,8 +49,8 @@ class Avatar extends Entity
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
private int $gsactor_id; private int $gsactor_id;
private int $attachment_id; private int $attachment_id;
private \DateTimeInterface $created; private DateTimeInterface $created;
private \DateTimeInterface $modified; private DateTimeInterface $modified;
public function setGSActorId(int $gsactor_id): self public function setGSActorId(int $gsactor_id): self
{ {
@ -134,7 +136,7 @@ class Avatar extends Entity
$filepath = $this->getPath(); $filepath = $this->getPath();
if (file_exists($filepath)) { if (file_exists($filepath)) {
if (@unlink($filepath) === false) { if (@unlink($filepath) === false) {
Log::warning("Failed deleting attachment for avatar with id={$id} at {$filepath}"); Log::warning("Failed deleting attachment for avatar with id={$this->attachment_id} at {$filepath}");
} }
} }
$this->attachment->delete(cascade: true, flush: false); $this->attachment->delete(cascade: true, flush: false);
@ -152,7 +154,7 @@ class Avatar extends Entity
'fields' => [ 'fields' => [
'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to gsactor table'], 'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to gsactor 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'],
'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'],
], ],
'primary key' => ['gsactor_id'], 'primary key' => ['gsactor_id'],

View File

@ -24,6 +24,7 @@ namespace App\Entity;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\GSFile; use App\Core\GSFile;
use App\Core\Log;
use App\Util\Common; use App\Util\Common;
use DateTimeInterface; use DateTimeInterface;