[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 6ed89c77f4
commit 4507b12976
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
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\Event;
use App\Core\Module;
use App\Util\Common;
use App\Util\Nickname;
class Media extends Module
@ -39,7 +40,9 @@ class Media extends Module
public function onEndTwigPopulateVars(array &$vars)
{
if (Common::user() != null) {
$vars['user_avatar'] = self::getAvatarUrl();
}
return Event::next;
}

View File

@ -52,7 +52,6 @@ abstract class Utils
'file_hash' => $hash,
'actor_id' => $actor_id,
'mimetype' => $sfile->getMimeType(),
'size' => $sfile->getSize(),
'title' => $title ?: _m('Untitled attachment'),
'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) {
$user = Common::user();
if ($user != null) {
$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) {