Repeat.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Plugin\RepeatNote\Controller;
  20. use App\Core\Controller;
  21. use App\Core\DB;
  22. use App\Core\Event;
  23. use App\Core\Form;
  24. use App\Core\Log;
  25. use App\Core\Router;
  26. use App\Entity\Activity;
  27. use App\Entity\Actor;
  28. use App\Entity\Note;
  29. use App\Util\Common;
  30. use App\Util\Exception\ClientException;
  31. use App\Util\Exception\NoLoggedInUser;
  32. use App\Util\Exception\RedirectException;
  33. use App\Util\Exception\ServerException;
  34. use Component\Notification\Entity\Attention;
  35. use function App\Core\I18n\_m;
  36. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  37. use Symfony\Component\HttpFoundation\Request;
  38. class Repeat extends Controller
  39. {
  40. /**
  41. * Controller for the note repeat non-JS page
  42. *
  43. * @param int $note_id Note being repeated
  44. *
  45. * @throws \App\Util\Exception\DuplicateFoundException
  46. * @throws ClientException
  47. * @throws NoLoggedInUser
  48. * @throws RedirectException
  49. * @throws ServerException
  50. */
  51. public function repeatAddNote(Request $request, int $note_id): bool|array
  52. {
  53. $user = Common::ensureLoggedIn();
  54. $actor_id = $user->getId();
  55. $note = Note::getByPK(['id' => $note_id]);
  56. $form_add_to_repeat = Form::create([
  57. ['add_repeat', SubmitType::class,
  58. [
  59. 'label' => _m('Repeat note!'),
  60. 'attr' => [
  61. 'title' => _m('Repeat this note!'),
  62. ],
  63. ],
  64. ],
  65. ]);
  66. $form_add_to_repeat->handleRequest($request);
  67. if ($form_add_to_repeat->isSubmitted()) {
  68. $activity = \Plugin\RepeatNote\RepeatNote::repeatNote(note: $note, actor_id: $actor_id);
  69. $actor = Actor::getById($actor_id);
  70. foreach ($actor->getSubscribers() as $subscriber) {
  71. $target_id = $subscriber->getId();
  72. DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $target_id]));
  73. $effective_attentions[$target_id] = $subscriber;
  74. }
  75. DB::flush();
  76. Event::handle('NewNotification', [$actor, $activity, $activity->getAttentionTargets(), _m('{actor_id} repeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $activity->getObjectId()])]);
  77. // Redirect user to where they came from
  78. // Prevent open redirect
  79. if (!\is_null($from = $this->string('from'))) {
  80. if (Router::isAbsolute($from)) {
  81. Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
  82. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  83. }
  84. // TODO anchor on element id
  85. throw new RedirectException(url: $from);
  86. }
  87. // If we don't have a URL to return to, go to the instance root
  88. throw new RedirectException('root');
  89. }
  90. return [
  91. '_template' => 'repeat/add_to_repeats.html.twig',
  92. 'note' => $note,
  93. 'add_repeat' => $form_add_to_repeat->createView(),
  94. ];
  95. }
  96. /**
  97. * Controller for the note unrepeat non-JS page
  98. *
  99. * @param int $note_id Note being unrepeated
  100. *
  101. * @throws \App\Util\Exception\DuplicateFoundException
  102. * @throws \App\Util\Exception\NotFoundException
  103. * @throws ClientException
  104. * @throws NoLoggedInUser
  105. * @throws RedirectException
  106. * @throws ServerException
  107. */
  108. public function repeatRemoveNote(Request $request, int $note_id): array
  109. {
  110. $user = Common::ensureLoggedIn();
  111. $actor_id = $user->getId();
  112. $form_remove_repeat = Form::create([
  113. ['remove_repeat', SubmitType::class,
  114. [
  115. 'label' => _m('Remove repeat'),
  116. 'attr' => [
  117. 'title' => _m('Remove note from repeats.'),
  118. ],
  119. ],
  120. ],
  121. ]);
  122. $form_remove_repeat->handleRequest($request);
  123. if ($form_remove_repeat->isSubmitted()) {
  124. if (!\is_null($activity = \Plugin\RepeatNote\RepeatNote::unrepeatNote(note_id: $note_id, actor_id: $actor_id))) {
  125. $actor = Actor::getById($actor_id);
  126. foreach ($actor->getSubscribers() as $subscriber) {
  127. $target_id = $subscriber->getId();
  128. DB::persist(Attention::create(['object_type' => Activity::schemaName(), 'object_id' => $activity->getId(), 'target_id' => $target_id]));
  129. $effective_attentions[$target_id] = $subscriber;
  130. }
  131. DB::flush();
  132. Event::handle('NewNotification', [$actor, $activity, $activity->getAttentionTargets(), _m('{actor_id} unrepeated note {note_id}.', ['{actor_id}' => $actor->getId(), '{note_id}' => $note_id])]);
  133. } else {
  134. throw new ClientException(_m('Note wasn\'t repeated!'));
  135. }
  136. // Redirect user to where they came from
  137. // Prevent open redirect
  138. if (!\is_null($from = $this->string('from'))) {
  139. if (Router::isAbsolute($from)) {
  140. Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
  141. throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
  142. }
  143. // TODO anchor on element id
  144. throw new RedirectException(url: $from);
  145. }
  146. // If we don't have a URL to return to, go to the instance root
  147. throw new RedirectException('root');
  148. }
  149. return [
  150. '_template' => 'repeat/remove_from_repeats.html.twig',
  151. 'note' => Note::getById($note_id),
  152. 'remove_repeat' => $form_remove_repeat->createView(),
  153. ];
  154. }
  155. }