Subscribers.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Subscription\Controller;
  20. use App\Core\DB;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Log;
  24. use App\Core\Router;
  25. use App\Entity\Actor;
  26. use App\Util\Common;
  27. use App\Util\Exception\ClientException;
  28. use App\Util\Exception\RedirectException;
  29. use App\Util\Exception\ServerException;
  30. use Component\Collection\Util\Controller\CircleController;
  31. use Component\Subscription\Subscription as SubscriptionComponent;
  32. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  33. use Symfony\Component\HttpFoundation\Request;
  34. /**
  35. * Collection of an actor's subscribers
  36. */
  37. class Subscribers extends CircleController
  38. {
  39. /**
  40. * @throws ServerException
  41. *
  42. * @return ControllerResultType
  43. */
  44. public function subscribersByActor(Request $request, Actor $actor): array
  45. {
  46. return [
  47. '_template' => 'collection/actors.html.twig',
  48. 'title' => _m('Subscribers'),
  49. 'empty_message' => _m('No subscribers.'),
  50. 'sort_form_fields' => [],
  51. 'page' => $this->int('page') ?? 1,
  52. 'actors' => $actor->getSubscribers(),
  53. ];
  54. }
  55. /**
  56. * @throws ClientException
  57. * @throws ServerException
  58. *
  59. * @return ControllerResultType
  60. */
  61. public function subscribersByActorId(Request $request, int $id): array
  62. {
  63. $actor = Actor::getById($id);
  64. if (\is_null($actor)) {
  65. throw new ClientException(_m('No such actor.'), 404);
  66. }
  67. return $this->subscribersByActor($request, $actor);
  68. }
  69. /**
  70. * @throws \App\Util\Exception\DuplicateFoundException
  71. * @throws \App\Util\Exception\NoLoggedInUser
  72. * @throws \App\Util\Exception\NotFoundException
  73. * @throws \App\Util\Exception\ServerException
  74. * @throws ClientException
  75. * @throws RedirectException
  76. *
  77. * @return ControllerResultType
  78. */
  79. public function subscribersAdd(Request $request, int $object_id): array
  80. {
  81. $subject = Common::ensureLoggedIn();
  82. $object = Actor::getById($object_id);
  83. $form = Form::create(
  84. [
  85. ['subscriber_add', SubmitType::class, ['label' => _m('Subscribe!')]],
  86. ],
  87. );
  88. $form->handleRequest($request);
  89. if ($form->isSubmitted() && $form->isValid()) {
  90. if (!\is_null(SubscriptionComponent::subscribe($subject, $object))) {
  91. DB::flush();
  92. SubscriptionComponent::refreshSubscriptionCount($subject, $object);
  93. }
  94. // Redirect user to where they came from
  95. // Prevent open redirect
  96. if (!\is_null($from = $this->string('from'))) {
  97. if (Router::isAbsolute($from)) {
  98. Log::warning("Actor {$object_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
  99. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  100. }
  101. // TODO anchor on element id
  102. throw new RedirectException(url: $from);
  103. }
  104. // If we don't have a URL to return to, go to the instance root
  105. throw new RedirectException('root');
  106. }
  107. return [
  108. '_template' => 'subscription/add_subscriber.html.twig',
  109. 'form' => $form->createView(),
  110. 'object' => $object,
  111. ];
  112. }
  113. /**
  114. * @throws \App\Util\Exception\DuplicateFoundException
  115. * @throws \App\Util\Exception\NoLoggedInUser
  116. * @throws \App\Util\Exception\NotFoundException
  117. * @throws \App\Util\Exception\ServerException
  118. * @throws ClientException
  119. * @throws RedirectException
  120. *
  121. * @return ControllerResultType
  122. */
  123. public function subscribersRemove(Request $request, int $object_id): array
  124. {
  125. $subject = Common::ensureLoggedIn();
  126. $object = Actor::getById($object_id);
  127. $form = Form::create(
  128. [
  129. ['subscriber_remove', SubmitType::class, ['label' => _m('Unsubscribe')]],
  130. ],
  131. );
  132. $form->handleRequest($request);
  133. if ($form->isSubmitted() && $form->isValid()) {
  134. if (!\is_null(SubscriptionComponent::unsubscribe($subject, $object))) {
  135. DB::flush();
  136. SubscriptionComponent::refreshSubscriptionCount($subject, $object);
  137. }
  138. // Redirect user to where they came from
  139. // Prevent open redirect
  140. if (!\is_null($from = $this->string('from'))) {
  141. if (Router::isAbsolute($from)) {
  142. Log::warning("Actor {$object_id} attempted to subscribe an actor and then get redirected to another host, or the URL was invalid ({$from})");
  143. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  144. }
  145. // TODO anchor on element id
  146. throw new RedirectException(url: $from);
  147. }
  148. // If we don't have a URL to return to, go to the instance root
  149. throw new RedirectException('root');
  150. }
  151. return [
  152. '_template' => 'subscription/remove_subscriber.html.twig',
  153. 'form' => $form->createView(),
  154. 'object' => $object,
  155. ];
  156. }
  157. }