[UI][MEDIA] Add actor avatar in feed timeline
This commit is contained in:
parent
de22f18abf
commit
e3c5d7e5dc
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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'));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -59,10 +59,11 @@
|
||||
<div class="notes">
|
||||
{% if notes is defined %}
|
||||
{% for note in notes %}
|
||||
<div>
|
||||
<b>{{ note.getActorNickname() }}</b>
|
||||
<div>{{ note.getContent() }}</div>
|
||||
</div>
|
||||
<div>
|
||||
<img src="{{ note.getAvatarUrl() }}" alt="{{ note.getActorNickname() }}'s avatar" width="64px">
|
||||
<b>{{ note.getActorNickname() }}</b>
|
||||
<div>{{ note.getContent() }}</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{{ 'No notes here.' | trans }}
|
||||
|
Loading…
Reference in New Issue
Block a user