Reply.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Modules\Plugin;
  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 Plugin
  35. {
  36. public function onAddRoute($r)
  37. {
  38. $r->connect('note_reply', '/note/reply/{reply_to<\\d*>}', [self::class, 'replyController']);
  39. return Event::next;
  40. }
  41. /**
  42. * HTML rendering event that adds the reply form as a note action,
  43. * if a user is logged in
  44. */
  45. public function onAddNoteActions(Request $request, Note $note, array &$actions)
  46. {
  47. if (($user = Common::user()) == null) {
  48. return Event::next;
  49. }
  50. $form = Form::create([
  51. ['content', HiddenType::class, ['label' => ' ', 'required' => false]],
  52. ['attachments', HiddenType::class, ['label' => ' ', 'required' => false]],
  53. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  54. ['reply', SubmitType::class, ['label' => ' ']],
  55. ]);
  56. // Handle form
  57. $ret = self::noteActionHandle($request, $form, $note, 'reply', function ($note, $data) {
  58. if ($data['content'] !== null) {
  59. // JS submitted
  60. // TODO Implement in JS
  61. $actor_id = $user->getId();
  62. Posting::storeNote(
  63. $actor_id,
  64. $data['content'],
  65. $data['attachments'],
  66. $is_local = true,
  67. $data['reply_to'],
  68. $repeat_of = null
  69. );
  70. } else {
  71. // JS disabled, redirect
  72. throw new RedirectException('note_reply', ['reply_to' => $note->getId()]);
  73. }
  74. });
  75. if ($ret != null) {
  76. return $ret;
  77. }
  78. $actions[] = $form->createView();
  79. return Event::next;
  80. }
  81. /**
  82. * Controller for the note reply non-JS page
  83. */
  84. public function replyController(Request $request, string $reply_to)
  85. {
  86. $user = Common::ensureLoggedIn();
  87. $actor_id = $user->getId();
  88. $note = DB::find('note', ['id' => (int) $reply_to]);
  89. if ($note == null || !$note->isVisibleTo($user)) {
  90. throw new NoSuchNoteException();
  91. }
  92. $form = Form::create([
  93. ['content', TextareaType::class, ['label' => ' ']],
  94. ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]],
  95. ['replyform', SubmitType::class, ['label' => _m('Submit')]],
  96. ]);
  97. $form->handleRequest($request);
  98. if ($form->isSubmitted()) {
  99. $data = $form->getData();
  100. if ($form->isValid()) {
  101. Posting::storeNote(
  102. $actor_id,
  103. $data['content'],
  104. $data['attachments'],
  105. $is_local = true,
  106. $reply_to,
  107. $repeat_of = null
  108. );
  109. } else {
  110. throw new InvalidFormException();
  111. }
  112. }
  113. return [
  114. '_template' => 'note/reply.html.twig',
  115. 'note' => $note,
  116. 'reply' => $form->createView(),
  117. ];
  118. }
  119. }