From 33e768c298d4bc6d1e08010a2bb09eb8fb1f382d Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Tue, 15 Feb 2022 17:13:16 +0000 Subject: [PATCH] [COMPONENT][Group][Controller] Separate feed from other group features --- components/Group/Controller/Group.php | 91 +-------------- components/Group/Controller/GroupFeed.php | 128 ++++++++++++++++++++++ components/Group/Group.php | 14 ++- 3 files changed, 139 insertions(+), 94 deletions(-) create mode 100644 components/Group/Controller/GroupFeed.php diff --git a/components/Group/Controller/Group.php b/components/Group/Controller/Group.php index 134a8f7a6e..c687b4eeaa 100644 --- a/components/Group/Controller/Group.php +++ b/components/Group/Controller/Group.php @@ -25,13 +25,12 @@ namespace Component\Group\Controller; use App\Core\ActorLocalRoles; use App\Core\Cache; +use App\Core\Controller; use App\Core\DB\DB; use App\Core\Form; use App\Util\Nickname; use function App\Core\I18n\_m; use App\Core\Log; -use App\Core\Router\Router; -use App\Entity\Actor; use App\Entity as E; use App\Util\Common; use App\Util\Exception\ClientException; @@ -46,7 +45,6 @@ use App\Util\Exception\NotFoundException; use App\Util\Exception\RedirectException; use App\Util\Exception\ServerException; use App\Util\Form\ActorForms; -use Component\Collection\Util\Controller\FeedController; use Component\Group\Entity\GroupMember; use Component\Group\Entity\LocalGroup; use Component\Subscription\Entity\ActorSubscription; @@ -56,93 +54,8 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; -class Group extends FeedController +class Group extends Controller { - /** - * @throws ServerException - */ - public function groupView(Request $request, Actor $group): array - { - $actor = Common::actor(); - $subscribe_form = null; - - if (!\is_null($actor) - && \is_null(Cache::get( - ActorSubscription::cacheKeys($actor, $group)['subscribed'], - fn () => DB::findOneBy('actor_subscription', [ - 'subscriber_id' => $actor->getId(), - 'subscribed_id' => $group->getId(), - ], return_null: true), - )) - ) { - $subscribe_form = Form::create([['subscribe', SubmitType::class, ['label' => _m('Subscribe to this group')]]]); - $subscribe_form->handleRequest($request); - if ($subscribe_form->isSubmitted() && $subscribe_form->isValid()) { - DB::persist(ActorSubscription::create([ - 'subscriber_id' => $actor->getId(), - 'subscribed_id' => $group->getId(), - ])); - DB::flush(); - Cache::delete(E\Actor::cacheKeys($group->getId())['subscribers']); - Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']); - Cache::delete(ActorSubscription::cacheKeys($actor, $group)['subscribed']); - } - } - - $notes = DB::dql(<<<'EOF' - SELECT n FROM \App\Entity\Note AS n - WHERE n.id IN ( - SELECT act.object_id FROM \App\Entity\Activity AS act - WHERE act.object_type = 'note' AND act.id IN - (SELECT att.activity_id FROM \Component\Notification\Entity\Notification AS att WHERE att.target_id = :id) - ) - EOF, ['id' => $group->getId()]); - - return [ - '_template' => 'group/view.html.twig', - 'actor' => $group, - 'nickname' => $group->getNickname(), - 'notes' => $notes, - 'subscribe_form' => $subscribe_form?->createView(), - ]; - } - - /** - * @throws ClientException - * @throws ServerException - */ - public function groupViewId(Request $request, int $id): array - { - $group = Actor::getById($id); - if (\is_null($group) || !$group->isGroup()) { - throw new ClientException(_m('No such group.'), 404); - } - if ($group->getIsLocal()) { - return [ - '_redirect' => Router::url('group_actor_view_nickname', ['nickname' => $group->getNickname()]), - 'actor' => $group, - ]; - } - return $this->groupView($request, $group); - } - - /** - * View a group feed by its nickname - * - * @param string $nickname The group's nickname to be shown - * - * @throws ClientException - * @throws ServerException - */ - public function groupViewNickname(Request $request, string $nickname): array - { - $group = LocalGroup::getActorByNickname($nickname); - if (\is_null($group)) { - throw new ClientException(_m('No such group.'), 404); - } - return $this->groupView($request, $group); - } - /** * Page that allows an actor to create a new group * diff --git a/components/Group/Controller/GroupFeed.php b/components/Group/Controller/GroupFeed.php new file mode 100644 index 0000000000..4fa9cf58e0 --- /dev/null +++ b/components/Group/Controller/GroupFeed.php @@ -0,0 +1,128 @@ +. + +// }}} + +namespace Component\Group\Controller; + +use App\Core\Cache; +use App\Core\DB\DB; +use App\Core\Form; +use App\Core\Router\Router; +use App\Entity as E; +use App\Entity\Actor; +use App\Util\Common; +use App\Util\Exception\ClientException; +use App\Util\Exception\ServerException; +use Component\Collection\Util\Controller\FeedController; +use Component\Group\Entity\LocalGroup; +use Component\Subscription\Entity\ActorSubscription; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\HttpFoundation\Request; +use function App\Core\I18n\_m; + +class GroupFeed extends FeedController +{ + /** + * @throws ServerException + */ + public function groupView(Request $request, Actor $group): array + { + $actor = Common::actor(); + $subscribe_form = null; + + if (!\is_null($actor) + && \is_null(Cache::get( + ActorSubscription::cacheKeys($actor, $group)['subscribed'], + fn () => DB::findOneBy('actor_subscription', [ + 'subscriber_id' => $actor->getId(), + 'subscribed_id' => $group->getId(), + ], return_null: true), + )) + ) { + $subscribe_form = Form::create([['subscribe', SubmitType::class, ['label' => _m('Subscribe to this group')]]]); + $subscribe_form->handleRequest($request); + if ($subscribe_form->isSubmitted() && $subscribe_form->isValid()) { + DB::persist(ActorSubscription::create([ + 'subscriber_id' => $actor->getId(), + 'subscribed_id' => $group->getId(), + ])); + DB::flush(); + Cache::delete(E\Actor::cacheKeys($group->getId())['subscribers']); + Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']); + Cache::delete(ActorSubscription::cacheKeys($actor, $group)['subscribed']); + } + } + + $notes = DB::dql(<<<'EOF' + SELECT n FROM \App\Entity\Note AS n + WHERE n.id IN ( + SELECT act.object_id FROM \App\Entity\Activity AS act + WHERE act.object_type = 'note' AND act.id IN + (SELECT att.activity_id FROM \Component\Notification\Entity\Notification AS att WHERE att.target_id = :id) + ) + EOF, ['id' => $group->getId()]); + + return [ + '_template' => 'group/view.html.twig', + 'actor' => $group, + 'nickname' => $group->getNickname(), + 'notes' => $notes, + 'subscribe_form' => $subscribe_form?->createView(), + ]; + } + + /** + * @throws ClientException + * @throws ServerException + */ + public function groupViewId(Request $request, int $id): array + { + $group = Actor::getById($id); + if (\is_null($group) || !$group->isGroup()) { + throw new ClientException(_m('No such group.'), 404); + } + if ($group->getIsLocal()) { + return [ + '_redirect' => Router::url('group_actor_view_nickname', ['nickname' => $group->getNickname()]), + 'actor' => $group, + ]; + } + return $this->groupView($request, $group); + } + + /** + * View a group feed by its nickname + * + * @param string $nickname The group's nickname to be shown + * + * @throws ClientException + * @throws ServerException + */ + public function groupViewNickname(Request $request, string $nickname): array + { + $group = LocalGroup::getActorByNickname($nickname); + if (\is_null($group)) { + throw new ClientException(_m('No such group.'), 404); + } + return $this->groupView($request, $group); + } +} diff --git a/components/Group/Group.php b/components/Group/Group.php index a1d43caa7f..f5fd52433e 100644 --- a/components/Group/Group.php +++ b/components/Group/Group.php @@ -41,8 +41,8 @@ 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']); + $r->connect(id: 'group_actor_view_id', uri_path: '/group/{id<\d+>}', target: [C\GroupFeed::class, 'groupViewId']); + $r->connect(id: 'group_actor_view_nickname', uri_path: '/!{nickname<' . Nickname::DISPLAY_FMT . '>}', target: [C\GroupFeed::class, 'groupViewNickname']); $r->connect(id: 'group_create', uri_path: '/group/new', target: [C\Group::class, 'groupCreate']); $r->connect(id: 'group_settings', uri_path: '/group/{id<\d+>}/settings', target: [C\Group::class, 'groupSettings']); return Event::next; @@ -76,9 +76,13 @@ class Group extends Component { $actor = Common::actor(); $group = $vars['actor']; - if (!\is_null($actor) && $group->isGroup() && $actor->canModerate($group)) { - $url = Router::url('group_settings', ['id' => $group->getId()]); - $res[] = HTML::html(['a' => ['attrs' => ['href' => $url, 'title' => _m('Edit group settings'), 'class' => 'profile-extra-actions'], _m('Group settings')]]); + if (!\is_null($actor) && $group->isGroup()) { + if ($actor->canModerate($group)) { + $url = Router::url('group_settings', ['id' => $group->getId()]); + $res[] = HTML::html(['a' => ['attrs' => ['href' => $url, 'title' => _m('Edit group settings'), 'class' => 'profile-extra-actions'], _m('Group settings')]]); + } + $res[] = HTML::html(['a' => ['attrs' => ['href' => Router::url('blog_post', ['in' => $group->getId()]), 'title' => _m('Make a new blog post'), 'class' => 'profile-extra-actions'], _m('Post in blog')]]); + } return Event::next; }