Posting.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Component\Posting\Controller;
  4. use App\Core;
  5. use App\Core\Controller;
  6. use App\Core\Event;
  7. use function App\Core\I18n\_m;
  8. use App\Core\VisibilityScope;
  9. use App\Util\Common;
  10. use App\Util\Exception\ClientException;
  11. use Component\Posting\Form;
  12. use Symfony\Component\HttpFoundation\File\Exception\FormSizeFileException;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class Posting extends Controller
  16. {
  17. public function onPost(Request $request): RedirectResponse
  18. {
  19. $actor = Common::actor();
  20. $form = Form\Posting::create($request);
  21. $form->handleRequest($request);
  22. if ($form->isSubmitted()) {
  23. try {
  24. if ($form->isValid()) {
  25. $data = $form->getData();
  26. Event::handle('PostingModifyData', [$request, $actor, &$data, $form]);
  27. if (empty($data['content']) && empty($data['attachments'])) {
  28. // TODO Display error: At least one of `content` and `attachments` must be provided
  29. throw new ClientException(_m('You must enter content or provide at least one attachment to post a note.'));
  30. }
  31. if (\is_null(VisibilityScope::tryFrom($data['visibility']))) {
  32. throw new ClientException(_m('You have selected an impossible visibility.'));
  33. }
  34. $extra_args = [];
  35. Event::handle('AddExtraArgsToNoteContent', [$request, $actor, $data, &$extra_args, $form]);
  36. if (\array_key_exists('in', $data) && $data['in'] !== 'public') {
  37. $target = $data['in'];
  38. }
  39. \Component\Posting\Posting::storeLocalNote(
  40. actor: $actor,
  41. content: $data['content'],
  42. content_type: $data['content_type'],
  43. locale: $data['language'],
  44. scope: VisibilityScope::from($data['visibility']),
  45. attentions: isset($target) ? [$target] : [],
  46. reply_to: \array_key_exists('reply_to_id', $data) ? $data['reply_to_id'] : null,
  47. attachments: $data['attachments'],
  48. process_note_content_extra_args: $extra_args,
  49. );
  50. return Core\Form::forceRedirect($form, $request);
  51. }
  52. } catch (FormSizeFileException $e) {
  53. throw new ClientException(_m('Invalid file size given.'), previous: $e);
  54. }
  55. }
  56. throw new ClientException(_m('Invalid form submission.'));
  57. }
  58. }