From 67d62cf37b00ea4ecc89f0b27f9aa00c8ece95ea Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Sat, 18 Sep 2021 07:27:17 +0100 Subject: [PATCH] [TWIG] Remove kludge event TwigPopulateVars --- components/Left/Left.php | 22 ----------- components/Posting/Posting.php | 4 +- components/Right/Right.php | 6 --- plugins/Cover/Cover.php | 26 ++++++------- plugins/Directory/Directory.php | 12 +++--- plugins/Favourite/Favourite.php | 10 ++--- plugins/Poll/Poll.php | 16 ++++---- plugins/ProfileColor/ProfileColor.php | 32 +++++++-------- src/Core/Controller.php | 11 ++++-- src/Entity/Actor.php | 4 +- src/Util/Common.php | 2 +- templates/network/public.html.twig | 2 +- templates/note/view.html.twig | 28 +++++++------- templates/sidepanel/left/left.html.twig | 37 +++++++++--------- templates/sidepanel/right/right.html.twig | 47 ++++++++++------------- 15 files changed, 112 insertions(+), 147 deletions(-) diff --git a/components/Left/Left.php b/components/Left/Left.php index 9e0e4bfc14..25f6f9464b 100644 --- a/components/Left/Left.php +++ b/components/Left/Left.php @@ -19,30 +19,8 @@ namespace Component\Left; -use App\Core\Event; -use App\Core\Log; use App\Core\Modules\Component; -use App\Util\Common; -use Exception; class Left extends Component { - public function onEndTwigPopulateVars(array &$vars) - { - try { - $user = Common::user(); - if ($user != null) { - $actor = $user->getActor(); - $vars['user_id'] = $user->getId(); - $vars['user_nickname'] = $user->getNickname(); - $vars['user_tags'] = $actor->getSelfTags(); - $vars['user_followers'] = $actor->getFollowersCount(); - $vars['user_followed'] = $actor->getFollowedCount(); - $vars['user_avatar'] = $actor->getAvatarUrl(); - } - } catch (Exception $e) { - Log::error('Got an exception while populating variables for the left panel: ' . $e); - } - return Event::next; - } } diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index ddb5f054ed..cd96df7f4d 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -53,7 +53,7 @@ class Posting extends Component * @throws RedirectException * @throws ServerException */ - public function onStartTwigPopulateVars(array &$vars): bool + public function onAppendRightPostingBlock(array $vars, array &$res): bool { if (($user = Common::user()) === null) { return Event::next; @@ -106,7 +106,7 @@ class Posting extends Component } } - $vars['post_form'] = $form->createView(); + $res = $form->createView(); return Event::next; } diff --git a/components/Right/Right.php b/components/Right/Right.php index c4d2c6fc0b..bc60b76806 100644 --- a/components/Right/Right.php +++ b/components/Right/Right.php @@ -19,14 +19,8 @@ namespace Component\Right; -use App\Core\Event; use App\Core\Modules\Component; -use App\Util\Common; class Right extends Component { - public function onStartTwigPopulateVars(array &$vars) - { - return Event::next; - } } diff --git a/plugins/Cover/Cover.php b/plugins/Cover/Cover.php index e195794128..cd0d5adc87 100644 --- a/plugins/Cover/Cover.php +++ b/plugins/Cover/Cover.php @@ -71,19 +71,19 @@ class Cover extends Plugin * @param array $vars * * @return bool hook value; true means continue processing, false means stop. - */ - public function onStartTwigPopulateVars(array &$vars): bool - { - /*if (Common::user() != null) { - $cover = DB::find('cover', ['actor_id' => Common::user()->getId()]); - if ($cover != null) { - $vars['profile_extras'][] = ['name' => 'cover', 'vars' => ['img' => '/cover']]; - } else { - $vars['profile_extras'][] = ['name' => 'cover', 'vars' => []]; - } - }*/ - return Event::next; - } + * + * public function onStartTwigPopulateVars(array &$vars): bool + * { + * if (Common::user() != null) { + * $cover = DB::find('cover', ['actor_id' => Common::user()->getId()]); + * if ($cover != null) { + * $vars['profile_extras'][] = ['name' => 'cover', 'vars' => ['img' => '/cover']]; + * } else { + * $vars['profile_extras'][] = ['name' => 'cover', 'vars' => []]; + * } + * } + * return Event::next; + * }*/ /** * Output our dedicated stylesheet diff --git a/plugins/Directory/Directory.php b/plugins/Directory/Directory.php index e21f344c97..f3c79020d4 100644 --- a/plugins/Directory/Directory.php +++ b/plugins/Directory/Directory.php @@ -24,6 +24,7 @@ namespace Plugin\Directory; use App\Core\Event; use App\Core\Modules\Plugin; use App\Core\Router\RouteLoader; +use App\Core\Router\Router; class Directory extends Plugin { @@ -39,17 +40,16 @@ class Directory extends Plugin } /** - * Populate twig vars + * Add Links to menu * - * @param array $vars + * @param array $res out menu items * * @return bool hook value; true means continue processing, false means stop. */ - public function onStartTwigPopulateVars(array &$vars): bool + public function onAddMainNavigationItem(array $vars, array &$res): bool { - $vars['main_nav_tabs'][] = ['title' => 'Actors', 'route' => 'directory_actors']; - $vars['main_nav_tabs'][] = ['title' => 'Groups', 'route' => 'directory_groups']; - + $res[] = ['title' => 'Actors', 'path' => Router::url('directory_actors', [])]; + $res[] = ['title' => 'Groups', 'path' => Router::url('directory_groups', [])]; return Event::next; } } diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index 570ab46b60..4811d49890 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -27,12 +27,12 @@ use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Modules\NoteHandlerPlugin; use App\Core\Router\RouteLoader; +use App\Core\Router\Router; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\InvalidFormException; use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\RedirectException; -use App\Util\Formatting; use App\Util\Nickname; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; @@ -114,12 +114,10 @@ class Favourite extends NoteHandlerPlugin return Event::next; } - public function onInsertLeftPanelLink(string $user_nickname, &$res): bool + public function onAddProfileNavigationItem(array $vars, array &$res): bool { - $res[] = Formatting::twigRenderString(<<Favourites -Reverse Favs -END, ['user_nickname' => $user_nickname]); + $res[] = ['title' => 'Favourites', 'path' => Router::url('actor_favourites_nickname', ['nickname' => $vars['nickname']])]; + $res[] = ['title' => 'Reverse Favourites', 'path' => Router::url('actor_reverse_favourites_nickname', ['nickname' => $vars['nickname']])]; return Event::next; } diff --git a/plugins/Poll/Poll.php b/plugins/Poll/Poll.php index 65154ae450..2a17861fb3 100644 --- a/plugins/Poll/Poll.php +++ b/plugins/Poll/Poll.php @@ -70,14 +70,14 @@ class Poll extends NoteHandlerPlugin * @param array $vars * * @return bool hook value; true means continue processing, false means stop. - */ - public function onStartTwigPopulateVars(array &$vars): bool - { - $vars['tabs'][] = ['title' => 'Poll', - 'href' => 'newpoll', - ]; - return Event::next; - } + * + * public function onStartTwigPopulateVars(array &$vars): bool + * { + * $vars['tabs'][] = ['title' => 'Poll', + * 'href' => 'newpoll', + * ]; + * return Event::next; + * }*/ /** * Output our dedicated stylesheet diff --git a/plugins/ProfileColor/ProfileColor.php b/plugins/ProfileColor/ProfileColor.php index 3095885168..efb7478bb1 100644 --- a/plugins/ProfileColor/ProfileColor.php +++ b/plugins/ProfileColor/ProfileColor.php @@ -71,22 +71,22 @@ class ProfileColor extends Plugin * @param array $vars * * @return bool hook value; true means continue processing, false means stop. - */ - public function onStartTwigPopulateVars(array &$vars): bool - { - /*$vars['profile_tabs'][] = [ - 'title' => 'Color', - 'desc' => 'Change your profile color.', - 'path' => 'profilecolor/profilecolor.html.twig', - ];*/ - if (Common::user() != null) { - $color = DB::find('profile_color', ['actor_id' => Common::user()->getId()]); - if ($color != null) { - $vars['profile_extras'][] = ['name' => 'profilecolor', 'vars' => ['color' => $color->getColor()]]; - } - } - return Event::next; - } + * + * public function onStartTwigPopulateVars(array &$vars): bool + * { + * /*$vars['profile_tabs'][] = [ + * 'title' => 'Color', + * 'desc' => 'Change your profile color.', + * 'path' => 'profilecolor/profilecolor.html.twig', + * ]; + * if (Common::user() != null) { + * $color = DB::find('profile_color', ['actor_id' => Common::user()->getId()]); + * if ($color != null) { + * $vars['profile_extras'][] = ['name' => 'profilecolor', 'vars' => ['color' => $color->getColor()]]; + * } + * } + * return Event::next; + * }*/ /** * Output our dedicated stylesheet diff --git a/src/Core/Controller.php b/src/Core/Controller.php index 45a294d122..baa4a4ea2a 100644 --- a/src/Core/Controller.php +++ b/src/Core/Controller.php @@ -89,8 +89,12 @@ class Controller extends AbstractController implements EventSubscriberInterface $request = $event->getRequest(); $this->request = $request; - $this->vars = ['controller' => $controller, 'request' => $request, 'have_user' => Common::user() !== null]; - Event::handle('StartTwigPopulateVars', [&$this->vars]); + + $this->vars = ['controller' => $controller, 'request' => $request]; + $user = Common::user(); + if ($user !== null) { + $this->vars['current_actor'] = $user->getActor(); + } $event->stopPropagation(); return $event; @@ -111,10 +115,9 @@ class Controller extends AbstractController implements EventSubscriberInterface } $this->vars = array_merge_recursive($this->vars, $response); - Event::handle('EndTwigPopulateVars', [&$this->vars]); $template = $this->vars['_template']; - unset($this->vars['_template'], $this->vars['request'], $response['_template']); + unset($this->vars['_template'], $response['_template']); // Respond in the most preferred acceptable content type $accept = $request->getAcceptableContentTypes() ?: ['text/html']; diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index eb1c253ed2..cd7676cdd6 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -265,7 +265,7 @@ class Actor extends Entity Cache::delete('selftags-' . $this->id); } - public function getFollowersCount() + public function getSubscribersCount() { return Cache::get('followers-' . $this->id, function () { @@ -274,7 +274,7 @@ class Actor extends Entity }); } - public function getFollowedCount() + public function getSubscriptionsCount() { return Cache::get('followed-' . $this->id, function () { diff --git a/src/Util/Common.php b/src/Util/Common.php index 494e6d7a59..f783f3da1c 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -113,7 +113,7 @@ abstract class Common public static function actor(): ?Actor { - return self::user()->getActor(); + return self::user()?->getActor(); } public static function userNickname(): ?string diff --git a/templates/network/public.html.twig b/templates/network/public.html.twig index 18b05cbc64..3cadc8c550 100644 --- a/templates/network/public.html.twig +++ b/templates/network/public.html.twig @@ -8,7 +8,7 @@
{% if notes is defined and notes is not empty %} {% for conversation in notes %} - {% include '/note/view.html.twig' with {'note': conversation['note'], 'have_user': have_user, 'replies': conversation['replies']} only %} + {% include '/note/view.html.twig' with {'note': conversation['note'], 'replies': conversation['replies']} only %}
{% endfor %} {% else %} diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index 655d6be85f..89726169f1 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -16,21 +16,19 @@ {% if app.user %}
- {% if have_user %} - {% for current_action in get_note_actions(note) %} - {{ form_start(current_action) }} - {% if current_action.submit_favourite is defined %} - {{ form_widget(current_action.submit_favourite) }} - {% endif %} - {% if current_action.submit_repeat is defined %} - {{ form_widget(current_action.submit_repeat) }} - {% endif %} - {% if current_action.reply is defined %} - {{ form_widget(current_action.reply) }} - {% endif %} - {{ form_end(current_action) }} - {% endfor %} - {% endif %} + {% for current_action in get_note_actions(note) %} + {{ form_start(current_action) }} + {% if current_action.submit_favourite is defined %} + {{ form_widget(current_action.submit_favourite) }} + {% endif %} + {% if current_action.submit_repeat is defined %} + {{ form_widget(current_action.submit_repeat) }} + {% endif %} + {% if current_action.reply is defined %} + {{ form_widget(current_action.reply) }} + {% endif %} + {{ form_end(current_action) }} + {% endfor %}
{% endif %} diff --git a/templates/sidepanel/left/left.html.twig b/templates/sidepanel/left/left.html.twig index 0999d02842..b77a46e698 100644 --- a/templates/sidepanel/left/left.html.twig +++ b/templates/sidepanel/left/left.html.twig @@ -6,12 +6,13 @@
+ {% set current_path = app.request.get('_route') %} {% if right_panel_vars is defined %}