From 1682b04e74df3b099f6d7576aa8870908ac2bf02 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Tue, 21 Dec 2021 17:20:22 +0000 Subject: [PATCH] [COMPONENT][Group][CONTROLLER][Actor] Move group related stuff to it's own component --- components/Group/Controller/Group.php | 126 ++++++++++++++++++ components/Group/Group.php | 47 +++++++ .../Group/templates/group/view.html.twig | 2 +- src/Controller/Actor.php | 96 +------------ src/Routes/Actor.php | 2 - templates/cards/profile/view.html.twig | 2 +- 6 files changed, 176 insertions(+), 99 deletions(-) create mode 100644 components/Group/Controller/Group.php create mode 100644 components/Group/Group.php rename templates/actor/group_view.html.twig => components/Group/templates/group/view.html.twig (95%) diff --git a/components/Group/Controller/Group.php b/components/Group/Controller/Group.php new file mode 100644 index 0000000000..62508e2b30 --- /dev/null +++ b/components/Group/Controller/Group.php @@ -0,0 +1,126 @@ +. + +// }}} + +namespace Component\Group\Controller; + +use App\Core\Cache; +use App\Core\Controller\ActorController; +use App\Core\DB\DB; +use App\Core\Form; +use function App\Core\I18n\_m; +use App\Core\Log; +use App\Entity\Actor; +use App\Entity as E; +use App\Util\Common; +use App\Util\Exception\RedirectException; +use App\Util\Nickname; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\HttpFoundation\Request; + +class Group extends ActorController +{ + public function groupViewId(Request $request, int $id) + { + return $this->handleActorById( + $id, + fn ($actor) => [ + '_template' => 'group/view.html.twig', + 'actor' => $actor, + ], + ); + } + + /** + * View a group feed and give the option of creating it if it doesn't exist + */ + public function groupViewNickname(Request $request, string $nickname) + { + Nickname::validate($nickname, which: Nickname::CHECK_LOCAL_GROUP); // throws + $group = Actor::getByNickname($nickname, type: Actor::GROUP); + if (\is_null($group)) { + $actor = Common::actor(); + if (!\is_null($actor)) { + $form = Form::create([ + ['create', SubmitType::class, ['label' => _m('Create this group')]], + ]); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + Log::info( + _m( + 'Actor id:{actor_id} nick:{actor_nick} created the group {nickname}', + ['{actor_id}' => $actor->getId(), 'actor_nick' => $actor->getNickname(), 'nickname' => $nickname], + ), + ); + + $group = Actor::create([ + 'nickname' => $nickname, + 'type' => Actor::GROUP, + 'is_local' => true, + ]); + DB::persist($group); + DB::persist(E\Subscription::create([ + 'subscriber' => $group->getId(), + 'subscribed' => $group->getId(), + ])); + DB::persist(E\Subscription::create([ + 'subscriber' => $actor->getId(), + 'subscribed' => $group->getId(), + ])); + DB::persist(E\GroupMember::create([ + 'group_id' => $group->getId(), + 'actor_id' => $actor->getId(), + 'is_admin' => true, + ])); + DB::flush(); + Cache::delete(Actor::cacheKeys($actor->getId())['subscriber']); + Cache::delete(Actor::cacheKeys($actor->getId())['subscribed']); + throw new RedirectException; + } + + return [ + '_template' => 'group/view.html.twig', + 'nickname' => $nickname, + 'create_form' => $form->createView(), + ]; + } + } + + $notes = !\is_null($group) ? DB::dql( + <<<'EOF' + select n from note n + join activity a with n.id = a.object_id + join group_inbox gi with a.id = gi.activity_id + where a.object_type = 'note' and gi.group_id = :group_id + EOF, + ['group_id' => $group->getId()], + ) : []; + + return [ + '_template' => 'group/view.html.twig', + 'actor' => $group, + 'nickname' => $group?->getNickname() ?? $nickname, + 'notes' => $notes, + ]; + } +} diff --git a/components/Group/Group.php b/components/Group/Group.php new file mode 100644 index 0000000000..3733b3d748 --- /dev/null +++ b/components/Group/Group.php @@ -0,0 +1,47 @@ +. +// }}} + +namespace Component\Group; + +use App\Core\Event; +use App\Core\Modules\Component; +use App\Core\Router\RouteLoader; +use App\Util\Nickname; +use Component\Group\Controller as C; + +class Group extends Component +{ + public function onAddRoute(RouteLoader $r): bool + { + $r->connect(id: 'group_actor_view_id', uri_path: '/group/{id<\d+>}', target: [C\Group::class, 'groupViewId']); + $r->connect(id: 'group_actor_view_nickname', uri_path: '/!{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\Group::class, 'groupViewNickname'], options: ['is_system_path' => false]); + return Event::next; + } + + public function onAppendCardProfile(array $vars, array &$res): bool + { + $actor = $vars['actor']; + if ($actor->isGroup()) { + dd($actor); + } + return Event::next; + } +} diff --git a/templates/actor/group_view.html.twig b/components/Group/templates/group/view.html.twig similarity index 95% rename from templates/actor/group_view.html.twig rename to components/Group/templates/group/view.html.twig index 8a862ccd05..a2adeae603 100644 --- a/templates/actor/group_view.html.twig +++ b/components/Group/templates/group/view.html.twig @@ -13,7 +13,7 @@ {% if actor is defined and actor is not null %} {% block profile_view %} - {% include 'cards/profile/view.html.twig' %} + {% include 'cards/profile/view.html.twig' with { 'actor': actor } only %} {% endblock profile_view %}
diff --git a/src/Controller/Actor.php b/src/Controller/Actor.php index 424e622847..0a57560157 100644 --- a/src/Controller/Actor.php +++ b/src/Controller/Actor.php @@ -23,17 +23,8 @@ declare(strict_types = 1); namespace App\Controller; -use App\Core\Cache; use App\Core\Controller\ActorController; -use App\Core\DB\DB; -use App\Core\Form; -use function App\Core\I18n\_m; -use App\Core\Log; use App\Entity as E; -use App\Util\Common; -use App\Util\Exception\RedirectException; -use App\Util\Nickname; -use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; class Actor extends ActorController @@ -58,93 +49,8 @@ class Actor extends ActorController '_template' => 'actor/view.html.twig', 'actor' => $actor, 'nickname' => $actor->getNickname(), - 'notes' => \App\Entity\Note::getAllNotesByActor($actor), + 'notes' => E\Note::getAllNotesByActor($actor), ], ); } - - public function groupViewId(Request $request, int $id) - { - return $this->handleActorById( - $id, - fn ($actor) => [ - '_template' => 'actor/group_view.html.twig', - 'actor' => $actor, - ], - ); - } - - /** - * View a group feed and give the option of creating it if it doesn't exist - */ - public function groupViewNickname(Request $request, string $nickname) - { - Nickname::validate($nickname, which: Nickname::CHECK_LOCAL_GROUP); // throws - $group = E\Actor::getByNickname($nickname, type: E\Actor::GROUP); - if (\is_null($group)) { - $actor = Common::actor(); - if (!\is_null($actor)) { - $form = Form::create([ - ['create', SubmitType::class, ['label' => _m('Create this group')]], - ]); - - $form->handleRequest($request); - if ($form->isSubmitted() && $form->isValid()) { - Log::info( - _m( - 'Actor id:{actor_id} nick:{actor_nick} created the group {nickname}', - ['{actor_id}' => $actor->getId(), 'actor_nick' => $actor->getNickname(), 'nickname' => $nickname], - ), - ); - - $group = E\Actor::create([ - 'nickname' => $nickname, - 'type' => E\Actor::GROUP, - 'is_local' => true, - ]); - DB::persist($group); - DB::persist(E\Subscription::create([ - 'subscriber' => $group->getId(), - 'subscribed' => $group->getId(), - ])); - DB::persist(E\Subscription::create([ - 'subscriber' => $actor->getId(), - 'subscribed' => $group->getId(), - ])); - DB::persist(E\GroupMember::create([ - 'group_id' => $group->getId(), - 'actor_id' => $actor->getId(), - 'is_admin' => true, - ])); - DB::flush(); - Cache::delete(E\Actor::cacheKeys($actor->getId())['subscriber']); - Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']); - throw new RedirectException; - } - - return [ - '_template' => 'actor/group_view.html.twig', - 'nickname' => $nickname, - 'create_form' => $form->createView(), - ]; - } - } - - $notes = !\is_null($group) ? DB::dql( - <<<'EOF' - select n from note n - join activity a with n.id = a.object_id - join group_inbox gi with a.id = gi.activity_id - where a.object_type = 'note' and gi.group_id = :group_id - EOF, - ['group_id' => $group->getId()], - ) : []; - - return [ - '_template' => 'actor/group_view.html.twig', - 'actor' => $group, - 'nickname' => $group?->getNickname() ?? $nickname, - 'notes' => $notes, - ]; - } } diff --git a/src/Routes/Actor.php b/src/Routes/Actor.php index 4d47e24412..9474e6e357 100644 --- a/src/Routes/Actor.php +++ b/src/Routes/Actor.php @@ -47,7 +47,5 @@ abstract class Actor { $r->connect(id: 'actor_view_id', uri_path: '/actor/{id<\d+>}', target: [C\Actor::class, 'actorViewId']); $r->connect(id: 'actor_view_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\Actor::class, 'actorViewNickname'], options: ['is_system_path' => false]); - $r->connect(id: 'group_actor_view_id', uri_path: '/group/{id<\d+>}', target: [C\Actor::class, 'groupViewId']); - $r->connect(id: 'group_actor_view_nickname', uri_path: '/!{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\Actor::class, 'groupViewNickname'], options: ['is_system_path' => false]); } } diff --git a/templates/cards/profile/view.html.twig b/templates/cards/profile/view.html.twig index afd94ac092..aa6586ec64 100644 --- a/templates/cards/profile/view.html.twig +++ b/templates/cards/profile/view.html.twig @@ -37,7 +37,7 @@ {% endif %} - {% for block in handle_event('AppendCardProfile', {'actor': actor}) %} + {% for block in handle_event('AppendCardProfile', { 'actor': actor }) %} {{ block | raw }} {% endfor %}