[MEDIA] Only try to get an avatar if a user is logged in

This commit is contained in:
Hugo Sales 2020-08-20 00:40:52 +00:00 committed by Hugo Sales
parent 1b0cab6dc8
commit 9204213dbc
2 changed files with 7 additions and 3 deletions

View File

@ -22,6 +22,7 @@ namespace Component\Media;
use App\Core\Cache; use App\Core\Cache;
use App\Core\Event; use App\Core\Event;
use App\Core\Module; use App\Core\Module;
use App\Util\Common;
use App\Util\Nickname; use App\Util\Nickname;
class Media extends Module class Media extends Module
@ -39,7 +40,9 @@ class Media extends Module
public function onEndTwigPopulateVars(array &$vars) public function onEndTwigPopulateVars(array &$vars)
{ {
if (Common::user() != null) {
$vars['user_avatar'] = self::getAvatarUrl(); $vars['user_avatar'] = self::getAvatarUrl();
}
return Event::next; return Event::next;
} }

View File

@ -52,7 +52,6 @@ abstract class Utils
'file_hash' => $hash, 'file_hash' => $hash,
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'mimetype' => $sfile->getMimeType(), 'mimetype' => $sfile->getMimeType(),
'size' => $sfile->getSize(),
'title' => $title ?: _m('Untitled attachment'), 'title' => $title ?: _m('Untitled attachment'),
'is_local' => $is_local, 'is_local' => $is_local,
]); ]);
@ -137,12 +136,14 @@ abstract class Utils
} }
} }
public static function getAvatarUrl(string $nickname = null) public static function getAvatarUrl(?string $nickname = null)
{ {
if ($nickname == null) { if ($nickname == null) {
$user = Common::user(); $user = Common::user();
if ($user != null) { if ($user != null) {
$nickname = $user->getNickname(); $nickname = $user->getNickname();
} else {
throw new Exception('No user is logged in and no avatar provided to `getAvatarUrl`');
} }
} }
return Cache::get('avatar-url-' . $nickname, function () use ($nickname) { return Cache::get('avatar-url-' . $nickname, function () use ($nickname) {