Reply.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public function onAddNoteActions(Request $request, Note $note, array &$actions)
  41. {
  42. $form = Form::create([
  43. ['content', HiddenType::class, ['label' => ' ', 'required' => false]],
  44. ['attachments', HiddenType::class, ['label' => ' ', 'required' => false]],
  45. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  46. ['reply', SubmitType::class, ['label' => ' ']],
  47. ]);
  48. $ret = self::noteActionHandle($request, $form, $note, 'reply', function ($note, $data) {
  49. if ($data['content'] !== null) {
  50. // JS submitted
  51. // TODO DO THE THING
  52. } else {
  53. // JS disabled, redirect
  54. throw new RedirectException('note_reply', ['reply_to' => $note->getId()]);
  55. }
  56. });
  57. if ($ret != null) {
  58. return $ret;
  59. }
  60. $actions[] = $form->createView();
  61. return Event::next;
  62. }
  63. public function replyController(Request $request, string $reply_to)
  64. {
  65. $user = Common::ensureLoggedIn();
  66. $actor_id = $user->getId();
  67. $note = DB::find('note', ['id' => (int) $reply_to]);
  68. if ($note == null || !$note->isVisibleTo($user)) {
  69. throw new NoSuchNoteException();
  70. }
  71. $form = Form::create([
  72. ['content', TextareaType::class, ['label' => ' ']],
  73. ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]],
  74. ['reply', SubmitType::class, ['label' => _m('Submit')]],
  75. ]);
  76. $form->handleRequest($request);
  77. if ($form->isSubmitted()) {
  78. $data = $form->getData();
  79. if ($form->isValid()) {
  80. Posting::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true, $data['reply_to'], null);
  81. } else {
  82. throw new InvalidFormException();
  83. }
  84. }
  85. return [
  86. '_template' => 'note/reply.html.twig',
  87. 'note' => $note,
  88. 'reply' => $form->createView(),
  89. ];
  90. }
  91. }