Posting.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Component\Posting;
  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\Core\Security;
  25. use App\Entity\FileToNote;
  26. use App\Entity\Note;
  27. use App\Util\Common;
  28. use App\Util\Exceptiion\InvalidFormException;
  29. use App\Util\Exception\RedirectException;
  30. use Component\Media\Media;
  31. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  32. use Symfony\Component\Form\Extension\Core\Type\FileType;
  33. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  34. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  35. class Posting extends Module
  36. {
  37. /**
  38. * HTML render event handler responsible for adding and handling
  39. * the result of adding the note submission form, only if a user is logged in
  40. */
  41. public function onStartTwigPopulateVars(array &$vars): bool
  42. {
  43. if (($user = Common::user()) == null) {
  44. return Event::next;
  45. }
  46. $actor_id = $user->getId();
  47. $to_tags = [];
  48. foreach (DB::dql('select c.tag from App\Entity\GSActorCircle c where c.tagger = :tagger', ['tagger' => $actor_id]) as $t) {
  49. $t = $t['tag'];
  50. $to_tags[$t] = $t;
  51. }
  52. $placeholder_string = ['How are you feeling?', 'Have something to share?', 'How was your day?'];
  53. $rand_key = array_rand($placeholder_string);
  54. $request = $vars['request'];
  55. $form = Form::create([
  56. ['content', TextareaType::class, ['label' => ' ', 'data' => '', 'attr' => ['placeholder' => _m($placeholder_string[$rand_key])]]],
  57. ['attachments', FileType::class, ['label' => ' ', 'data' => null, 'multiple' => true, 'required' => false]],
  58. ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'expanded' => true, 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]],
  59. ['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => true, 'expanded' => true, 'choices' => $to_tags]],
  60. ['post', SubmitType::class, ['label' => _m('Post')]],
  61. ]);
  62. $form->handleRequest($request);
  63. if ($form->isSubmitted()) {
  64. $data = $form->getData();
  65. if ($form->isValid()) {
  66. self::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true);
  67. throw new RedirectException();
  68. } else {
  69. throw new InvalidFormException();
  70. }
  71. }
  72. $vars['post_form'] = $form->createView();
  73. return Event::next;
  74. }
  75. /**
  76. * Store the given note with $content and $attachments, created by
  77. * $actor_id, possibly as a reply to note $reply_to and with flag
  78. * $is_local. Sanitizes $content and $attachments
  79. */
  80. public static function storeNote(int $actor_id, string $content, array $attachments, bool $is_local, ?int $reply_to = null, ?int $repeat_of = null)
  81. {
  82. $note = Note::create([
  83. 'gsactor_id' => $actor_id,
  84. 'content' => Security::sanitize($content),
  85. 'is_local' => $is_local,
  86. 'reply_to' => $reply_to,
  87. 'repeat_of' => $repeat_of,
  88. ]);
  89. $files = [];
  90. foreach ($attachments as $f) {
  91. $nf = Media::validateAndStoreFile($f, Common::config('attachments', 'dir'),
  92. Security::sanitize($title = $f->getClientOriginalName()),
  93. $is_local = true, $actor_id);
  94. $files[] = $nf;
  95. DB::persist($nf);
  96. }
  97. DB::persist($note);
  98. // Need file and note ids for the next step
  99. DB::flush();
  100. if ($attachments != []) {
  101. foreach ($files as $f) {
  102. DB::persist(FileToNote::create(['file_id' => $f->getId(), 'note_id' => $note->getId()]));
  103. }
  104. DB::flush();
  105. }
  106. }
  107. }