| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // {{{ License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Component\Group\Controller; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 15:02:21 +00:00
										 |  |  | use App\Core\ActorLocalRoles; | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | use App\Core\Cache; | 
					
						
							|  |  |  | use App\Core\DB\DB; | 
					
						
							|  |  |  | use App\Core\Form; | 
					
						
							|  |  |  | use function App\Core\I18n\_m; | 
					
						
							|  |  |  | use App\Core\Log; | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  | use App\Entity\Actor; | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | use App\Entity as E; | 
					
						
							|  |  |  | use App\Util\Common; | 
					
						
							| 
									
										
										
										
											2021-12-23 17:13:08 +00:00
										 |  |  | use App\Util\Exception\ClientException; | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  | use App\Util\Exception\DuplicateFoundException; | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  | use App\Util\Exception\NicknameEmptyException; | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  | use App\Util\Exception\NicknameException; | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  | use App\Util\Exception\NicknameInvalidException; | 
					
						
							|  |  |  | use App\Util\Exception\NicknameNotAllowedException; | 
					
						
							|  |  |  | use App\Util\Exception\NicknameTakenException; | 
					
						
							|  |  |  | use App\Util\Exception\NicknameTooLongException; | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  | use App\Util\Exception\NotFoundException; | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | use App\Util\Exception\RedirectException; | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  | use App\Util\Exception\ServerException; | 
					
						
							| 
									
										
										
										
											2021-12-23 17:13:08 +00:00
										 |  |  | use App\Util\Form\ActorForms; | 
					
						
							| 
									
										
										
										
											2022-01-02 20:04:52 +00:00
										 |  |  | use Component\Collection\Util\Controller\FeedController; | 
					
						
							| 
									
										
										
										
											2021-12-27 17:27:07 +00:00
										 |  |  | use Component\Group\Entity\GroupMember; | 
					
						
							|  |  |  | use Component\Group\Entity\LocalGroup; | 
					
						
							| 
									
										
										
										
											2022-01-06 11:26:48 +00:00
										 |  |  | use Component\Subscription\Entity\ActorSubscription; | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  | use Symfony\Component\Form\Extension\Core\Type\TextType; | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  | use Symfony\Component\Form\FormInterface; | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | use Symfony\Component\HttpFoundation\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 20:04:52 +00:00
										 |  |  | class Group extends FeedController | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |      * @throws ServerException | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |     public function groupView(Request $request, Actor $group): array | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-27 18:21:19 +00:00
										 |  |  |         $actor          = Common::actor(); | 
					
						
							|  |  |  |         $subscribe_form = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |         if (!\is_null($actor) | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |             && \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']); | 
					
						
							| 
									
										
										
										
											2021-12-27 18:21:19 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |         $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()]); | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2021-12-27 18:21:19 +00:00
										 |  |  |             '_template'      => 'group/view.html.twig', | 
					
						
							|  |  |  |             'actor'          => $group, | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |             'nickname'       => $group->getNickname(), | 
					
						
							| 
									
										
										
										
											2021-12-27 18:21:19 +00:00
										 |  |  |             'notes'          => $notes, | 
					
						
							|  |  |  |             'subscribe_form' => $subscribe_form?->createView(), | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-23 17:13:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @throws ClientException | 
					
						
							|  |  |  |      * @throws ServerException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |     public function groupViewId(Request $request, int $id): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |         $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); | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * View a group feed by its nickname | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $nickname The group's nickname to be shown | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |      * @throws ClientException | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |      * @throws ServerException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function groupViewNickname(Request $request, string $nickname): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $group = LocalGroup::getActorByNickname($nickname); | 
					
						
							|  |  |  |         if (\is_null($group)) { | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |             throw new ClientException(_m('No such group.'), 404); | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |         return $this->groupView($request, $group); | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Page that allows an actor to create a new group | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws RedirectException | 
					
						
							|  |  |  |      * @throws ServerException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |     public function groupCreate(Request $request): array | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (\is_null($actor = Common::actor())) { | 
					
						
							|  |  |  |             throw new RedirectException('security_login'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |         $create_form = self::getGroupCreateForm($request, $actor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             '_template'   => 'group/create.html.twig', | 
					
						
							|  |  |  |             'create_form' => $create_form->createView(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Settings page for the group with the provided nickname, checks if the current actor can administrate given group | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws ClientException | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |      * @throws DuplicateFoundException | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |      * @throws NicknameEmptyException | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |      * @throws NicknameException | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |      * @throws NicknameInvalidException | 
					
						
							|  |  |  |      * @throws NicknameNotAllowedException | 
					
						
							|  |  |  |      * @throws NicknameTakenException | 
					
						
							|  |  |  |      * @throws NicknameTooLongException | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |      * @throws NotFoundException | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |      * @throws ServerException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |     public function groupSettings(Request $request, int $id): array | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |         $local_group = DB::findOneBy(LocalGroup::class, ['actor_id' => $id]); | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |         $group_actor = $local_group->getActor(); | 
					
						
							|  |  |  |         $actor       = Common::actor(); | 
					
						
							|  |  |  |         if (!\is_null($group_actor) && $actor->canAdmin($group_actor)) { | 
					
						
							|  |  |  |             return [ | 
					
						
							|  |  |  |                 '_template'          => 'group/settings.html.twig', | 
					
						
							|  |  |  |                 'group'              => $group_actor, | 
					
						
							|  |  |  |                 'personal_info_form' => ActorForms::personalInfo($request, $actor, $local_group)->createView(), | 
					
						
							|  |  |  |                 'open_details_query' => $this->string('open'), | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |             throw new ClientException(_m('You do not have permission to edit settings for the group "{group}"', ['{group}' => $id]), code: 404); | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new Group FormInterface getter | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws RedirectException | 
					
						
							|  |  |  |      * @throws ServerException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function getGroupCreateForm(Request $request, E\Actor $actor): FormInterface | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |         $create_form = Form::create([ | 
					
						
							|  |  |  |             ['group_nickname', TextType::class, ['label' => _m('Group nickname')]], | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |             ['group_type', ChoiceType::class, ['label' => _m('Type:'), 'multiple' => false, 'expanded' => false, 'choices' => [ | 
					
						
							|  |  |  |                 _m('Group')        => 'group', | 
					
						
							|  |  |  |                 _m('Organisation') => 'organisation', | 
					
						
							|  |  |  |             ]]], | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |             ['group_create', SubmitType::class, ['label' => _m('Create this group!')]], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $create_form->handleRequest($request); | 
					
						
							|  |  |  |         if ($create_form->isSubmitted() && $create_form->isValid()) { | 
					
						
							|  |  |  |             $data     = $create_form->getData(); | 
					
						
							|  |  |  |             $nickname = $data['group_nickname']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Log::info( | 
					
						
							|  |  |  |                 _m( | 
					
						
							|  |  |  |                     'Actor id:{actor_id} nick:{actor_nick} created the group {nickname}', | 
					
						
							|  |  |  |                     ['{actor_id}' => $actor->getId(), 'actor_nick' => $actor->getNickname(), 'nickname' => $nickname], | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             DB::persist($group = E\Actor::create([ | 
					
						
							|  |  |  |                 'nickname' => $nickname, | 
					
						
							|  |  |  |                 'type'     => E\Actor::GROUP, | 
					
						
							|  |  |  |                 'is_local' => true, | 
					
						
							| 
									
										
										
										
											2022-01-22 15:02:21 +00:00
										 |  |  |                 'roles'    => ActorLocalRoles::VISITOR, // Can send direct messages to other actors
 | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |             ])); | 
					
						
							|  |  |  |             DB::persist(LocalGroup::create([ | 
					
						
							| 
									
										
										
										
											2022-02-10 16:02:51 +00:00
										 |  |  |                 'actor_id' => $group->getId(), | 
					
						
							| 
									
										
										
										
											2022-02-10 04:31:06 +00:00
										 |  |  |                 'type'     => $data['group_type'], | 
					
						
							| 
									
										
										
										
											2022-01-17 21:33:38 +00:00
										 |  |  |                 'nickname' => $nickname, | 
					
						
							|  |  |  |             ])); | 
					
						
							|  |  |  |             DB::persist(ActorSubscription::create([ | 
					
						
							|  |  |  |                 'subscriber_id' => $group->getId(), | 
					
						
							|  |  |  |                 'subscribed_id' => $group->getId(), | 
					
						
							|  |  |  |             ])); | 
					
						
							|  |  |  |             DB::persist(GroupMember::create([ | 
					
						
							|  |  |  |                 'group_id' => $group->getId(), | 
					
						
							|  |  |  |                 'actor_id' => $actor->getId(), | 
					
						
							|  |  |  |                 'is_admin' => true, | 
					
						
							|  |  |  |             ])); | 
					
						
							|  |  |  |             DB::flush(); | 
					
						
							|  |  |  |             Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribers']); | 
					
						
							|  |  |  |             Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             throw new RedirectException(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-23 18:56:57 +00:00
										 |  |  |         return $create_form; | 
					
						
							| 
									
										
										
										
											2021-12-23 17:13:08 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-21 17:20:22 +00:00
										 |  |  | } |