Reply.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Event;
  20. use App\Core\Form;
  21. use App\Core\Modules\NoteHandlerPlugin;
  22. use App\Entity\Note;
  23. use App\Util\Common;
  24. use App\Util\Exception\RedirectException;
  25. use Component\Posting\Posting;
  26. use Plugin\Reply\Controller\Reply as ReplyController;
  27. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  28. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use function App\Core\I18n\_m;
  31. class Reply extends NoteHandlerPlugin
  32. {
  33. public function onAddRoute($r)
  34. {
  35. $r->connect('note_reply', '/note/reply/{reply_to<\\d*>}', ReplyController::class);
  36. return Event::next;
  37. }
  38. /**
  39. * HTML rendering event that adds the reply form as a note action,
  40. * if a user is logged in
  41. *
  42. * @throws RedirectException
  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,
  54. [
  55. 'label' => ' ',
  56. 'attr' => [
  57. 'class' => 'note-actions-unset button-container reply-button-container',
  58. 'title' => 'Reply to this note!',
  59. ],
  60. ],
  61. ],
  62. ]);
  63. // Handle form
  64. $ret = self::noteActionHandle($request, $form, $note, 'reply', function ($note, $data, $user) use ($request) {
  65. if ($data['content'] !== null) {
  66. // JS submitted
  67. // TODO Implement in JS
  68. $actor_id = $user->getId();
  69. Posting::storeNote(
  70. $actor_id,
  71. $data['content'],
  72. $data['attachments'],
  73. $is_local = true,
  74. $data['reply_to'],
  75. $repeat_of = null
  76. );
  77. } else {
  78. // JS disabled, redirect
  79. throw new RedirectException('note_reply', ['reply_to' => $note->getId(), 'return_to' => $request->getRequestUri()]);
  80. return Event::stop;
  81. }
  82. });
  83. if ($ret !== null) {
  84. return $ret;
  85. }
  86. $actions[] = $form->createView();
  87. return Event::next;
  88. }
  89. }