Subscribers.php 5.9 KB

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