[Avatar] We definitely don't need an event to retrieve avatar urls

This commit is contained in:
Diogo Peralta Cordeiro 2021-09-18 04:54:35 +01:00
parent 2bd19fa087
commit fda998e335
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
7 changed files with 25 additions and 39 deletions

View File

@ -43,6 +43,11 @@ class Avatar extends Component
return Event::next; return Event::next;
} }
/**
* @param mixed $tabs
*
* @throws \App\Util\Exception\ClientException
*/
public function onPopulateProfileSettingsTabs(Request $request, &$tabs): bool public function onPopulateProfileSettingsTabs(Request $request, &$tabs): bool
{ {
// TODO avatar template shouldn't be on settings folder // TODO avatar template shouldn't be on settings folder
@ -55,20 +60,6 @@ class Avatar extends Component
return Event::next; return Event::next;
} }
public function onStartTwigPopulateVars(array &$vars): bool
{
if (Common::user() !== null) {
$vars['user_avatar'] = self::getAvatarUrl();
}
return Event::next;
}
public function onGetAvatarUrl(int $actor_id, ?string &$url): bool
{
$url = self::getAvatarUrl($actor_id);
return Event::next;
}
public function onAvatarUpdate(int $actor_id): bool public function onAvatarUpdate(int $actor_id): bool
{ {
Cache::delete('avatar-' . $actor_id); Cache::delete('avatar-' . $actor_id);
@ -98,11 +89,10 @@ class Avatar extends Component
/** /**
* Get the cached avatar associated with the given Actor id, or the current user if not given * Get the cached avatar associated with the given Actor id, or the current user if not given
*/ */
public static function getAvatarUrl(?int $actor_id = null, string $size = 'full'): string public static function getAvatarUrl(int $actor_id, string $size = 'full'): string
{ {
$actor_id = $actor_id ?: Common::userId(); return Cache::get("avatar-url-{$actor_id}", function () use ($actor_id, $size) {
return Cache::get("avatar-url-{$actor_id}", function () use ($actor_id) { return Router::url('avatar', ['actor_id' => $actor_id, 'size' => $size]);
return Router::url('avatar', ['actor_id' => $actor_id, 'size' => 'full']);
}); });
} }

View File

@ -38,6 +38,7 @@ class Left extends Component
$vars['user_tags'] = $actor->getSelfTags(); $vars['user_tags'] = $actor->getSelfTags();
$vars['user_followers'] = $actor->getFollowersCount(); $vars['user_followers'] = $actor->getFollowersCount();
$vars['user_followed'] = $actor->getFollowedCount(); $vars['user_followed'] = $actor->getFollowedCount();
$vars['user_avatar'] = $actor->getAvatarUrl();
} }
} catch (Exception $e) { } catch (Exception $e) {
Log::error('Got an exception while populating variables for the left panel: ' . $e); Log::error('Got an exception while populating variables for the left panel: ' . $e);

View File

@ -1,11 +1,12 @@
{% set actor_nickname = actor.nickname() %} {% set actor_nickname = actor.getNickname() %}
{% set actor_avatar = actor.getAvatarUrl() %}
{% set actor_tags = actor.getSelfTags() %} {% set actor_tags = actor.getSelfTags() %}
{% set actor_bio = actor.getBio() %} {% set actor_bio = actor.getBio() %}
{% block body %} {% block body %}
<section class='section-widget section-widget-padded' title="{{ 'Your profile information.' | trans }}"> <section class='section-widget section-widget-padded' title="{{ 'Your profile information.' | trans }}">
<a id="user" href="{{ path('actor_view_nickname', {'nickname' : actor_nickname}) }}"> <a id="user" href="{{ path('actor_view_nickname', {'nickname' : actor_nickname}) }}">
<img src='{{ user_avatar }}' class="icon icon-avatar" alt="{{ 'Your account\'s avatar.' | trans }}"> <img src='{{ actor_avatar }}' class="icon icon-avatar" alt="{{ 'Your account\'s avatar.' | trans }}">
<div class="user-info"> <div class="user-info">
<strong id="user-nickname" title="{{ 'Your account\' nickname.' | trans }}">{{ actor_nickname }}</strong> <strong id="user-nickname" title="{{ 'Your account\' nickname.' | trans }}">{{ actor_nickname }}</strong>

View File

@ -24,11 +24,11 @@ namespace App\Entity;
use App\Core\Cache; use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\Event;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Core\UserRoles; use App\Core\UserRoles;
use App\Util\Exception\NicknameException; use App\Util\Exception\NicknameException;
use App\Util\Nickname; use App\Util\Nickname;
use Component\Avatar\Avatar;
use DateTimeInterface; use DateTimeInterface;
use Functional as F; use Functional as F;
@ -215,11 +215,9 @@ class Actor extends Entity
return DB::findOneBy('local_user', ['id' => $this->getId()]); return DB::findOneBy('local_user', ['id' => $this->getId()]);
} }
public function getAvatarUrl() public function getAvatarUrl(string $size = 'full')
{ {
$url = null; return Avatar::getAvatarUrl($this->getId(), $size);
Event::handle('GetAvatarUrl', [$this->getId(), &$url]);
return $url;
} }
public static function getFromId(int $id): ?self public static function getFromId(int $id): ?self

View File

@ -24,8 +24,8 @@ namespace App\Entity;
use App\Core\Cache; use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\Event;
use App\Core\VisibilityScope; use App\Core\VisibilityScope;
use Component\Avatar\Avatar;
use DateTimeInterface; use DateTimeInterface;
/** /**
@ -220,11 +220,9 @@ class Note extends Entity
return Actor::getNicknameFromId($this->actor_id); return Actor::getNicknameFromId($this->actor_id);
} }
public function getAvatarUrl() public function getActorAvatarUrl(string $size = 'full'): string
{ {
$url = null; return Avatar::getAvatarUrl($this->getActorId(), $size);
Event::handle('GetAvatarUrl', [$this->getActorId(), &$url]);
return $url;
} }
public static function getAllNotes(int $noteScope): array public static function getAllNotes(int $noteScope): array

View File

@ -1,19 +1,17 @@
{% extends 'stdgrid.html.twig' %} {% extends 'stdgrid.html.twig' %}
{% set id = actor.id %} {% set id = actor.getId() %}
{% set nick = actor.nickname %} {% set nickname = actor.getNickname() %}
{# TODO: how to get avatar in here? Tags and rest of profile info? #} {% set avatar = actor.getAvatarUrl() %}
{% set avatar = actor.nickname %}
{% block title %}{{ nick }}'s profile{% endblock %} {% block title %}{{ nickname }}'s profile{% endblock %}
{% block body %} {% block body %}
<section class="section-widget section-widget-padded"> <section class="section-widget section-widget-padded">
<div class="section-title"> <div class="section-title">
<img class="icon icon-avatar" src="{{ avatar }}" alt="{{ nick }}'s avatar">{{ nick }} <img class="icon icon-avatar" src="{{ avatar }}" alt="{{ nickname }}'s avatar">{{ nickname }}
</div> </div>
{#
<div class="actor-nickname-and-tags"> <div class="actor-nickname-and-tags">
@ -37,7 +35,7 @@
{% else %} {% else %}
<p>{{ '(No bio)' | trans }}</p> <p>{{ '(No bio)' | trans }}</p>
{% endif %} {% endif %}
</div>#} </div>
</section> </section>
<main class="timeline" tabindex="0" role="feed"> <main class="timeline" tabindex="0" role="feed">

View File

@ -2,7 +2,7 @@
{% set nickname = note.getActorNickname() %} {% set nickname = note.getActorNickname() %}
<aside class="note-sidebar"> <aside class="note-sidebar">
<img class="u-logo avatar" src="{{ note.getAvatarUrl() }}" alt="{{ nickname }}'s avatar" width="32px" height="32px"> <img class="u-logo avatar" src="{{ note.getActorAvatarUrl() }}" alt="{{ nickname }}'s avatar" width="32px" height="32px">
</aside> </aside>
<div class="note-wrapper"> <div class="note-wrapper">