From e3c5d7e5dc03fbad70842d808e778995ab7153e9 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 19 Aug 2020 15:31:52 +0000 Subject: [PATCH] [UI][MEDIA] Add actor avatar in feed timeline --- components/Media/Media.php | 31 +++++++++++++------------ components/Media/Utils.php | 37 +++++++++++++++++++++++++----- src/Entity/Note.php | 10 +++++++- templates/network/public.html.twig | 9 ++++---- 4 files changed, 61 insertions(+), 26 deletions(-) diff --git a/components/Media/Media.php b/components/Media/Media.php index 13e6bcbd26..21910a1f36 100644 --- a/components/Media/Media.php +++ b/components/Media/Media.php @@ -19,19 +19,16 @@ namespace Component\Media; +use App\Core\Cache; use App\Core\Event; use App\Core\Module; -use App\Util\Common; use App\Util\Nickname; -use Exception; -use Symfony\Component\Asset\Package; -use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy; class Media extends Module { public static function __callStatic(string $name, array $args) { - return Utils::$name(...$args); + return Utils::{$name}(...$args); } public function onAddRoute($r) @@ -42,16 +39,20 @@ class Media extends Module public function onEndTwigPopulateVars(array &$vars) { - try { - $user = Common::user(); - if ($user != null) { - $vars['user_avatar'] = self::getAvatar($user->getNickname())->getUrl(); - return Event::next; - } - } catch (Exception $e) { - } - $package = new Package(new EmptyVersionStrategy()); - $vars['user_avatar'] = $package->getUrl(Common::config('avatar', 'default')); + $vars['user_avatar'] = self::getAvatarUrl(); return Event::next; } + + public function onGetAvatarUrl(string $nickname, ?string &$url) + { + $url = self::getAvatarUrl($nickname); + return Event::next; + } + + public function onDeleteCachedAvatar(string $nickname) + { + Cache::delete('avatar-' . $nickname); + Cache::delete('avatar-url-' . $nickname); + Cache::delete('avatar-file-info-' . $nickname); + } } diff --git a/components/Media/Utils.php b/components/Media/Utils.php index 906f8d4447..0297f47a1f 100644 --- a/components/Media/Utils.php +++ b/components/Media/Utils.php @@ -28,7 +28,10 @@ use App\Core\Log; use App\Entity\Avatar; use App\Entity\File; use App\Util\Common; +use Component\Media\Exception\NoAvatarException; use Exception; +use Symfony\Component\Asset\Package; +use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Symfony\Component\HttpFoundation\HeaderUtils; @@ -86,11 +89,15 @@ abstract class Utils public static function error($res, string $nickname) { - if (count($res) > 1) { + switch (count($res)) { + case 0: + throw new NoAvatarException(); + case 1: + return $res[0]; + default: Log::error('Avatar query returned more than one result for nickname ' . $nickname); throw new Exception(_m('Internal server error')); } - return $res[0]; } public static function getAvatar(string $nickname) @@ -99,8 +106,8 @@ abstract class Utils Cache::get('avatar-' . $nickname, function () use ($nickname) { return DB::dql('select a from App\\Entity\\Avatar a ' . - 'join App\Entity\GSActor p with a.gsactor_id = p.id ' . - 'where p.nickname = :nickname', + 'join App\Entity\GSActor g with a.gsactor_id = g.id ' . + 'where g.nickname = :nickname', ['nickname' => $nickname]); }), $nickname @@ -116,8 +123,8 @@ abstract class Utils return DB::dql('select f.file_hash, f.mimetype, f.title ' . 'from App\\Entity\\File f ' . 'join App\\Entity\\Avatar a with f.id = a.file_id ' . - 'join App\\Entity\\GSActor p with p.id = a.gsactor_id ' . - 'where p.nickname = :nickname', + 'join App\\Entity\\GSActor g with g.id = a.gsactor_id ' . + 'where g.nickname = :nickname', ['nickname' => $nickname]); }), $nickname @@ -129,4 +136,22 @@ abstract class Utils return ['file_path' => $filepath, 'mimetype' => 'image/svg+xml', 'title' => null]; } } + + public static function getAvatarUrl(string $nickname = null) + { + if ($nickname == null) { + $user = Common::user(); + if ($user != null) { + $nickname = $user->getNickname(); + } + } + return Cache::get('avatar-url-' . $nickname, function () use ($nickname) { + try { + return self::getAvatar($nickname)->getUrl(); + } catch (NoAvatarException $e) { + } + $package = new Package(new EmptyVersionStrategy()); + return $package->getUrl(Common::config('avatar', 'default')); + }); + } } diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 2fb7dc936a..b2733f797f 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -23,6 +23,7 @@ namespace App\Entity; use App\Core\DB\DB; use App\Core\Entity; +use App\Core\Event; use DateTimeInterface; /** @@ -176,7 +177,14 @@ class Note extends Entity public function getActorNickname() { - return DB::find('gsactor', $this->gsactor_id)->getNickname(); + return GSActor::getNicknameFromId($this->gsactor_id); + } + + public function getAvatarUrl() + { + $url = null; + Event::handle('get_avatar_url', [$this->getActorNickname(), &$url]); + return $url; } public static function schemaDef(): array diff --git a/templates/network/public.html.twig b/templates/network/public.html.twig index b514e5db94..6f6c0c48b6 100644 --- a/templates/network/public.html.twig +++ b/templates/network/public.html.twig @@ -59,10 +59,11 @@
{% if notes is defined %} {% for note in notes %} -
- {{ note.getActorNickname() }} -
{{ note.getContent() }}
-
+
+ {{ note.getActorNickname() }}'s avatar + {{ note.getActorNickname() }} +
{{ note.getContent() }}
+
{% endfor %} {% else %} {{ 'No notes here.' | trans }}