[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;
use App\Core\DB\DB;
use App\Core\Event;
use App\Core\Log;
use App\Core\Module;
use App\Entity\File;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Nickname;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
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']);
}
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;
use App\Core\DB\DB;
use App\Util\Common;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -62,14 +60,8 @@ class Controller extends AbstractController implements EventSubscriberInterface
$controller = $event->getController();
$request = $event->getRequest();
if (($user = Common::user()) !== null && ($avatar = DB::find('avatar', ['gsactor_id' => $user->getActor()->getId()])) != null) {
$avatar_filename = $avatar->getUrl();
} else {
$avatar_filename = '/public/assets/default_avatar.svg';
}
$this->vars = ['controler' => $controller, 'request' => $request, 'user_avatar' => $avatar_filename];
Event::handle('StartTwigPopulateVars', [&$this->vars]);
$this->vars = ['controler' => $controller, 'request' => $request];
Event::handle('start_twig_populate_vars', [&$this->vars]);
return $event;
}
@ -83,7 +75,7 @@ class Controller extends AbstractController implements EventSubscriberInterface
}
$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'];
unset($this->vars['_template'], $this->vars['request']);