[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,17 +34,18 @@ use Symfony\Component\HttpFoundation\Request;
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('settings_avatar', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
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
$tabs[] = ['title' => 'Avatar',
$tabs[] = [
'title' => 'Avatar',
'desc' => 'Change your avatar.',
'controller' => C\Avatar::settings_avatar($request),
];
@ -52,7 +53,7 @@ class Avatar extends Component
return Event::next;
}
public function onStartTwigPopulateVars(array &$vars)
public function onStartTwigPopulateVars(array &$vars): bool
{
if (Common::user() != null) {
$vars['user_avatar'] = self::getAvatarUrl();
@ -60,7 +61,7 @@ class Avatar extends Component
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);
return Event::next;
@ -78,14 +79,14 @@ class Avatar extends Component
/**
* 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();
return GSFile::error(NoAvatarException::class,
$gsactor_id,
Cache::get("avatar-{$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',
['gsactor_id' => $gsactor_id]);
}));
@ -122,11 +123,11 @@ class Avatar extends Component
function () use ($gsactor_id) {
return DB::dql('select f.file_hash, f.mimetype, f.title ' .
'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',
['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;
} catch (Exception $e) {
$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 as M;
use function App\Core\I18n\_m;
use App\Entity\Avatar as AvatarEntity;
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;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
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\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Avatar extends Controller
{
/**
* @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) {
case 'full':
@ -59,7 +62,7 @@ class Avatar extends Controller
/**
* Local user avatar panel
*/
public static function settings_avatar(Request $request)
public static function settings_avatar(Request $request): array
{
$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]],
@ -97,7 +100,7 @@ class Avatar extends Controller
}
}
} 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'];
} else {
throw new ClientException('Invalid form');
@ -112,9 +115,7 @@ class Avatar extends Controller
// Must get old id before inserting another one
$old_attachment = null;
$avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]);
if ($avatar != null) {
$old_attachment = $avatar->delete();
}
$old_attachment = $avatar?->delete();
DB::persist($attachment);
// Can only get new id after inserting
DB::flush();

View File

@ -19,11 +19,13 @@
// }}}
namespace App\Entity;
namespace Component\Avatar\Entity;
use App\Core\DB\DB;
use App\Core\Entity;
use App\Core\Log;
use App\Core\Router\Router;
use App\Entity\Attachment;
use App\Util\Common;
use DateTimeInterface;
@ -47,8 +49,8 @@ class Avatar extends Entity
// @codeCoverageIgnoreStart
private int $gsactor_id;
private int $attachment_id;
private \DateTimeInterface $created;
private \DateTimeInterface $modified;
private DateTimeInterface $created;
private DateTimeInterface $modified;
public function setGSActorId(int $gsactor_id): self
{
@ -134,7 +136,7 @@ class Avatar extends Entity
$filepath = $this->getPath();
if (file_exists($filepath)) {
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);

View File

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