From 6ed89c77f4dc15a36de71daff5cd3cb7c8fae6f8 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 20 Aug 2020 00:40:06 +0000 Subject: [PATCH] [UI][NOTE] Post and see attachments --- components/Posting/Posting.php | 27 ++++++++++++++++++++++++--- src/Controller/NetworkPublic.php | 21 +++++++++++++++++---- src/Controller/UserPanel.php | 11 +++-------- templates/network/public.html.twig | 14 +++++++++++--- 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index d5a9524248..4ba35658e7 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -24,8 +24,12 @@ use App\Core\Event; use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Module; +use App\Core\Security; +use App\Entity\FileToNote; use App\Entity\Note; use App\Util\Common; +use Component\Media\Media; +use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; @@ -33,12 +37,16 @@ class Posting extends Module { public function onStartTwigPopulateVars(array &$vars) { + if (Common::user() == null) { + return; + } + $request = $vars['request']; $form = Form::create([ - ['content', TextareaType::class, ['label' => ' ']], - ['send', SubmitType::class, ['label' => _m('Send')]], + ['content', TextareaType::class, ['label' => ' ']], + ['attachments', FileType::class, ['label' => _m('Attachments'), 'multiple' => true, 'required' => false]], + ['send', SubmitType::class, ['label' => _m('Send')]], ]); - $form->handleRequest($request); if ($form->isSubmitted()) { $data = $form->getData(); @@ -46,7 +54,20 @@ class Posting extends Module $content = $data['content']; $id = Common::actor()->getId(); $note = Note::create(['gsactor_id' => $id, 'content' => $content]); + $files = []; + foreach ($data['attachments'] as $f) { + $nf = Media::validateAndStoreFile($f, Common::config('attachments', 'dir'), + Security::sanitize($title = $f->getClientOriginalName()), + $is_local = true, $actor_id = $id); + $files[] = $nf; + DB::persist($nf); + } DB::persist($note); + // Need file and note ids for the next step + DB::flush(); + foreach ($files as $f) { + DB::persist(FileToNote::create(['file_id' => $f->getId(), 'note_id' => $note->getId()])); + } DB::flush(); } else { // TODO Display error diff --git a/src/Controller/NetworkPublic.php b/src/Controller/NetworkPublic.php index dcbac49e46..8d27b3e13c 100644 --- a/src/Controller/NetworkPublic.php +++ b/src/Controller/NetworkPublic.php @@ -38,11 +38,24 @@ class NetworkPublic extends Controller { public function handle(Request $request) { - $notes = DB::findBy('note', [], ['created' => 'DESC']); - + $notes = DB::findBy('note', [], ['created' => 'DESC']); + $attachments = []; + foreach ($notes as $n) { + $a = DB::dql( + 'select f from App\Entity\File f ' . + 'join App\Entity\FileToNote ftn with ftn.file_id = f.id ' . + 'where ftn.note_id = :note_id', + ['note_id' => $n->getId()] + ); + $attachments[] = $a; + } + if ($notes === []) { + $notes = null; + } return [ - '_template' => 'network/public.html.twig', - 'notes' => $notes, + '_template' => 'network/public.html.twig', + 'notes' => $notes, + 'attachments' => array_reverse($attachments), ]; } } diff --git a/src/Controller/UserPanel.php b/src/Controller/UserPanel.php index 23291d8d57..6f6962cffb 100644 --- a/src/Controller/UserPanel.php +++ b/src/Controller/UserPanel.php @@ -115,7 +115,7 @@ class UserPanel extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); - $sfile = $file_title = null; + $sfile = null; if (isset($data['hidden'])) { // Cropped client side $matches = []; @@ -125,12 +125,7 @@ class UserPanel extends AbstractController $data_user = base64_decode($data_user); $filename = tempnam('/tmp/', 'avatar'); file_put_contents($filename, $data_user); - try { - $sfile = new SymfonyFile($filename); - $file_title = $data['avatar']->getFilename(); - } finally { - // fclose($tmp_file); - } + $sfile = new SymfonyFile($filename); } else { Log::info('Avatar upload got an invalid encoding, something\'s fishy and/or wrong'); } @@ -143,7 +138,7 @@ class UserPanel extends AbstractController } $actor = Common::actor(); $actor_id = $actor->getId(); - $file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $file_title, $is_local = true, $use_unique = $actor_id); + $file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id); $old_file = null; $avatar = DB::find('avatar', ['gsactor_id' => $actor_id]); // Must get old id before inserting another one diff --git a/templates/network/public.html.twig b/templates/network/public.html.twig index 6f6c0c48b6..c59ad26c17 100644 --- a/templates/network/public.html.twig +++ b/templates/network/public.html.twig @@ -52,9 +52,11 @@ -
- {{ form(post_form) }} -
+ {% if post_form is defined %} +
+ {{ form(post_form) }} +
+ {% endif %}
{% if notes is defined %} @@ -63,6 +65,12 @@ {{ note.getActorNickname() }}'s avatar {{ note.getActorNickname() }}
{{ note.getContent() }}
+ {% set id = note.getId() - 1 %} + {% for attachment in attachments[id] %} +
+ {{ attachment.getTitle() }} +
+ {% endfor %}
{% endfor %} {% else %}