Posting.php 4.2 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 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. public function onStartTwigPopulateVars(array &$vars)
  38. {
  39. if (($user = Common::user()) == null) {
  40. return;
  41. }
  42. $actor_id = $user->getId();
  43. $to_tags = [];
  44. foreach (DB::dql('select c.tag from App\Entity\GSActorCircle c where c.tagger = :tagger', ['tagger' => $actor_id]) as $t) {
  45. $t = $t['tag'];
  46. $to_tags[$t] = $t;
  47. }
  48. $placeholder_string = ['How are you feeling?', 'Have something to share?', 'How was your day?'];
  49. $rand_key = array_rand($placeholder_string);
  50. $request = $vars['request'];
  51. $form = Form::create([
  52. ['content', TextareaType::class, ['label' => ' ', 'data' => '', 'attr' => ['placeholder' => _m($placeholder_string[$rand_key])]]],
  53. ['attachments', FileType::class, ['label' => ' ', 'data' => null, 'multiple' => true, 'required' => false]],
  54. ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'expanded' => true, 'choices' => [_m('Public') => 'public', _m('Instance') => 'instance', _m('Private') => 'private']]],
  55. ['to', ChoiceType::class, ['label' => _m('To:'), 'multiple' => true, 'expanded' => true, 'choices' => $to_tags]],
  56. ['post', SubmitType::class, ['label' => _m('Post')]],
  57. ]);
  58. $form->handleRequest($request);
  59. if ($form->isSubmitted()) {
  60. $data = $form->getData();
  61. if ($form->isValid()) {
  62. C\Post::storeNote($actor_id, $data['content'], $data['attachments'], $is_local = true);
  63. throw new RedirectException();
  64. } else {
  65. throw new InvalidFormException();
  66. }
  67. }
  68. $vars['post_form'] = $form->createView();
  69. return Event::next;
  70. }
  71. public static function storeNote(int $actor_id, string $content, array $attachments, bool $is_local, ?int $reply_to = null, ?int $repeat_of = null)
  72. {
  73. $note = Note::create(['gsactor_id' => $actor_id, 'content' => $content, 'is_local' => $is_local, 'reply_to' => $reply_to, 'repeat_of' => $repeat_of]);
  74. $files = [];
  75. foreach ($attachments as $f) {
  76. $nf = Media::validateAndStoreFile($f, Common::config('attachments', 'dir'),
  77. Security::sanitize($title = $f->getClientOriginalName()),
  78. $is_local = true, $actor_id);
  79. $files[] = $nf;
  80. DB::persist($nf);
  81. }
  82. DB::persist($note);
  83. // Need file and note ids for the next step
  84. DB::flush();
  85. if ($attachments != []) {
  86. foreach ($files as $f) {
  87. DB::persist(FileToNote::create(['file_id' => $f->getId(), 'note_id' => $note->getId()]));
  88. }
  89. DB::flush();
  90. }
  91. }
  92. }