[MEDIA] Move avatar fetching and adding to ouput to media component

This commit is contained in:
Hugo Sales 2020-08-14 15:45:08 +00:00 committed by Hugo Sales
parent 8506a0248d
commit e1002eb605
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 18 additions and 11 deletions

View File

@ -19,12 +19,16 @@
namespace Component\Media; namespace Component\Media;
use App\Core\DB\DB;
use App\Core\Event;
use App\Core\Log; use App\Core\Log;
use App\Core\Module; use App\Core\Module;
use App\Entity\File; use App\Entity\File;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Nickname; use App\Util\Nickname;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileException; use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
@ -87,4 +91,15 @@ class Media extends Module
{ {
$r->connect('avatar', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'send']); $r->connect('avatar', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'send']);
} }
public function onEndTwigPopulateVars(array &$vars)
{
if (($user = Common::user()) !== null && ($avatar = DB::find('avatar', ['gsactor_id' => $user->getActor()->getId()])) != null) {
$vars['user_avatar'] = $avatar->getUrl();
} else {
$package = new Package(new EmptyVersionStrategy());
$vars['user_avatar'] = $package->getUrl('/assets/default-avatar.svg');
}
return Event::next;
}
} }

View File

@ -32,8 +32,6 @@
namespace App\Core; namespace App\Core;
use App\Core\DB\DB;
use App\Util\Common;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@ -62,14 +60,8 @@ class Controller extends AbstractController implements EventSubscriberInterface
$controller = $event->getController(); $controller = $event->getController();
$request = $event->getRequest(); $request = $event->getRequest();
if (($user = Common::user()) !== null && ($avatar = DB::find('avatar', ['gsactor_id' => $user->getActor()->getId()])) != null) { $this->vars = ['controler' => $controller, 'request' => $request];
$avatar_filename = $avatar->getUrl(); Event::handle('start_twig_populate_vars', [&$this->vars]);
} else {
$avatar_filename = '/public/assets/default_avatar.svg';
}
$this->vars = ['controler' => $controller, 'request' => $request, 'user_avatar' => $avatar_filename];
Event::handle('StartTwigPopulateVars', [&$this->vars]);
return $event; return $event;
} }
@ -83,7 +75,7 @@ class Controller extends AbstractController implements EventSubscriberInterface
} }
$this->vars = array_merge_recursive($this->vars, $response); $this->vars = array_merge_recursive($this->vars, $response);
Event::handle('EndTwigPopulateVars', [&$this->vars]); Event::handle('end_twig_populate_vars', [&$this->vars]);
$template = $this->vars['_template']; $template = $this->vars['_template'];
unset($this->vars['_template'], $this->vars['request']); unset($this->vars['_template'], $this->vars['request']);