Group.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Group\Controller;
  20. use App\Core\ActorLocalRoles;
  21. use App\Core\Cache;
  22. use App\Core\DB\DB;
  23. use App\Core\Form;
  24. use function App\Core\I18n\_m;
  25. use App\Core\Log;
  26. use App\Entity as E;
  27. use App\Util\Common;
  28. use App\Util\Exception\ClientException;
  29. use App\Util\Exception\NicknameEmptyException;
  30. use App\Util\Exception\NicknameInvalidException;
  31. use App\Util\Exception\NicknameNotAllowedException;
  32. use App\Util\Exception\NicknameTakenException;
  33. use App\Util\Exception\NicknameTooLongException;
  34. use App\Util\Exception\NoLoggedInUser;
  35. use App\Util\Exception\RedirectException;
  36. use App\Util\Exception\ServerException;
  37. use App\Util\Form\ActorForms;
  38. use App\Util\Nickname;
  39. use Component\Collection\Util\Controller\FeedController;
  40. use Component\Group\Entity\GroupMember;
  41. use Component\Group\Entity\LocalGroup;
  42. use Component\Subscription\Entity\ActorSubscription;
  43. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  44. use Symfony\Component\Form\Extension\Core\Type\TextType;
  45. use Symfony\Component\HttpFoundation\Request;
  46. class Group extends FeedController
  47. {
  48. /**
  49. * View a group feed by its nickname
  50. *
  51. * @param string $nickname The group's nickname to be shown
  52. *
  53. * @throws NicknameEmptyException
  54. * @throws NicknameNotAllowedException
  55. * @throws NicknameTakenException
  56. * @throws NicknameTooLongException
  57. * @throws ServerException
  58. *
  59. * @return array
  60. */
  61. public function groupViewNickname(Request $request, string $nickname)
  62. {
  63. Nickname::validate($nickname, which: Nickname::CHECK_LOCAL_GROUP); // throws
  64. $group = LocalGroup::getActorByNickname($nickname);
  65. $actor = Common::actor();
  66. $subscribe_form = null;
  67. if (!\is_null($group)
  68. && !\is_null($actor)
  69. && \is_null(Cache::get(
  70. ActorSubscription::cacheKeys($actor, $group)['subscribed'],
  71. fn () => DB::findOneBy('actor_subscription', [
  72. 'subscriber_id' => $actor->getId(),
  73. 'subscribed_id' => $group->getId(),
  74. ], return_null: true),
  75. ))
  76. ) {
  77. $subscribe_form = Form::create([['subscribe', SubmitType::class, ['label' => _m('Subscribe to this group')]]]);
  78. $subscribe_form->handleRequest($request);
  79. if ($subscribe_form->isSubmitted() && $subscribe_form->isValid()) {
  80. DB::persist(ActorSubscription::create([
  81. 'subscriber_id' => $actor->getId(),
  82. 'subscribed_id' => $group->getId(),
  83. ]));
  84. DB::flush();
  85. Cache::delete(E\Actor::cacheKeys($group->getId())['subscribers']);
  86. Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']);
  87. Cache::delete(ActorSubscription::cacheKeys($actor, $group)['subscribed']);
  88. }
  89. }
  90. $notes = !\is_null($group) ? DB::dql(
  91. <<<'EOF'
  92. select n from note n
  93. join activity a with n.id = a.object_id
  94. join group_inbox gi with a.id = gi.activity_id
  95. where a.object_type = 'note' and gi.group_id = :group_id
  96. order by a.created desc, a.id desc
  97. EOF,
  98. ['group_id' => $group->getId()],
  99. ) : [];
  100. return [
  101. '_template' => 'group/view.html.twig',
  102. 'actor' => $group,
  103. 'nickname' => $group?->getNickname() ?? $nickname,
  104. 'notes' => $notes,
  105. 'subscribe_form' => $subscribe_form?->createView(),
  106. ];
  107. }
  108. /**
  109. * Page that allows an actor to create a new group
  110. *
  111. * @throws RedirectException
  112. * @throws ServerException
  113. *
  114. * @return array
  115. */
  116. public function groupCreate(Request $request)
  117. {
  118. if (\is_null($actor = Common::actor())) {
  119. throw new RedirectException('security_login');
  120. }
  121. $create_form = Form::create([
  122. ['group_nickname', TextType::class, ['label' => _m('Group nickname')]],
  123. ['group_create', SubmitType::class, ['label' => _m('Create this group!')]],
  124. ]);
  125. $create_form->handleRequest($request);
  126. if ($create_form->isSubmitted() && $create_form->isValid()) {
  127. $data = $create_form->getData();
  128. $nickname = $data['group_nickname'];
  129. Log::info(
  130. _m(
  131. 'Actor id:{actor_id} nick:{actor_nick} created the group {nickname}',
  132. ['{actor_id}' => $actor->getId(), 'actor_nick' => $actor->getNickname(), 'nickname' => $nickname],
  133. ),
  134. );
  135. DB::persist($group = E\Actor::create([
  136. 'nickname' => $nickname,
  137. 'type' => E\Actor::GROUP,
  138. 'is_local' => true,
  139. 'roles' => ActorLocalRoles::VISITOR, // Can send direct messages to other actors
  140. ]));
  141. DB::persist(LocalGroup::create([
  142. 'group_id' => $group->getId(),
  143. 'nickname' => $nickname,
  144. ]));
  145. DB::persist(ActorSubscription::create([
  146. 'subscriber_id' => $group->getId(),
  147. 'subscribed_id' => $group->getId(),
  148. ]));
  149. DB::persist(GroupMember::create([
  150. 'group_id' => $group->getId(),
  151. 'actor_id' => $actor->getId(),
  152. 'is_admin' => true,
  153. ]));
  154. DB::flush();
  155. Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribers']);
  156. Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']);
  157. throw new RedirectException();
  158. }
  159. return [
  160. '_template' => 'group/create.html.twig',
  161. 'create_form' => $create_form->createView(),
  162. ];
  163. }
  164. /**
  165. * Settings page for the group with the provided nickname, checks if the current actor can administrate given group
  166. *
  167. * @throws ClientException
  168. * @throws NicknameEmptyException
  169. * @throws NicknameInvalidException
  170. * @throws NicknameNotAllowedException
  171. * @throws NicknameTakenException
  172. * @throws NicknameTooLongException
  173. * @throws NoLoggedInUser
  174. * @throws ServerException
  175. *
  176. * @return array
  177. */
  178. public function groupSettings(Request $request, string $nickname)
  179. {
  180. $local_group = LocalGroup::getByNickname($nickname);
  181. $group_actor = $local_group->getActor();
  182. $actor = Common::actor();
  183. if (!\is_null($group_actor) && $actor->canAdmin($group_actor)) {
  184. return [
  185. '_template' => 'group/settings.html.twig',
  186. 'group' => $group_actor,
  187. 'personal_info_form' => ActorForms::personalInfo($request, $actor, $local_group)->createView(),
  188. 'open_details_query' => $this->string('open'),
  189. ];
  190. } else {
  191. throw new ClientException(_m('You do not have permission to edit settings for the group "{group}"', ['{group}' => $nickname]), code: 404);
  192. }
  193. }
  194. }