Reply.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\Reply;
  19. use App\Core\DB\DB;
  20. use App\Core\Event;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Module;
  24. use App\Entity\Note;
  25. use App\Util\Common;
  26. use App\Util\Exceptiion\InvalidFormException;
  27. use App\Util\Exception\RedirectException;
  28. use Component\Posting\Posting;
  29. use Symfony\Component\Form\Extension\Core\Type\FileType;
  30. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  31. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  32. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  33. use Symfony\Component\HttpFoundation\Request;
  34. class Reply extends Module
  35. {
  36. public function onAddRoute($r)
  37. {
  38. $r->connect('note_reply', '/note/reply/{reply_to<\\d*>}', [self::class, 'replyController']);
  39. }
  40. /**
  41. * HTML rendering event that adds the reply form as a note action,
  42. * if a user is logged in
  43. */
  44. public function onAddNoteActions(Request $request, Note $note, array &$actions)
  45. {
  46. if (($user = Common::user()) == null) {
  47. return Event::next;
  48. }
  49. $form = Form::create([
  50. ['content', HiddenType::class, ['label' => ' ', 'required' => false]],
  51. ['attachments', HiddenType::class, ['label' => ' ', 'required' => false]],
  52. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  53. ['reply', SubmitType::class, ['label' => ' ']],
  54. ]);
  55. // Handle form
  56. $ret = self::noteActionHandle($request, $form, $note, 'reply', function ($note, $data) {
  57. if ($data['content'] !== null) {
  58. // JS submitted
  59. // TODO Implement in JS
  60. $actor_id = $user->getId();
  61. Posting::storeNote(
  62. $actor_id,
  63. $data['content'],
  64. $data['attachments'],
  65. $is_local = true,
  66. $data['reply_to'],
  67. $repeat_of = null
  68. );
  69. } else {
  70. // JS disabled, redirect
  71. throw new RedirectException('note_reply', ['reply_to' => $note->getId()]);
  72. }
  73. });
  74. if ($ret != null) {
  75. return $ret;
  76. }
  77. $actions[] = $form->createView();
  78. return Event::next;
  79. }
  80. /**
  81. * Controller for the note reply non-JS page
  82. */
  83. public function replyController(Request $request, string $reply_to)
  84. {
  85. $user = Common::ensureLoggedIn();
  86. $actor_id = $user->getId();
  87. $note = DB::find('note', ['id' => (int) $reply_to]);
  88. if ($note == null || !$note->isVisibleTo($user)) {
  89. throw new NoSuchNoteException();
  90. }
  91. $form = Form::create([
  92. ['content', TextareaType::class, ['label' => ' ']],
  93. ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]],
  94. ['reply', SubmitType::class, ['label' => _m('Submit')]],
  95. ]);
  96. $form->handleRequest($request);
  97. if ($form->isSubmitted()) {
  98. $data = $form->getData();
  99. if ($form->isValid()) {
  100. Posting::storeNote(
  101. $actor_id,
  102. $data['content'],
  103. $data['attachments'],
  104. $is_local = true,
  105. $data['reply_to'],
  106. $repeat_of = null
  107. );
  108. } else {
  109. throw new InvalidFormException();
  110. }
  111. }
  112. return [
  113. '_template' => 'note/reply.html.twig',
  114. 'note' => $note,
  115. 'reply' => $form->createView(),
  116. ];
  117. }
  118. }