Repeat.php 5.3 KB

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