[UI][NOTE] Post and see attachments

This commit is contained in:
Hugo Sales 2020-08-20 00:40:06 +00:00 committed by Hugo Sales
parent 9a0c64c3d1
commit 1b0cab6dc8
4 changed files with 55 additions and 18 deletions

View File

@ -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

View File

@ -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),
];
}
}

View File

@ -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

View File

@ -52,9 +52,11 @@
</li>
</ul>
</nav>
<div class="note-post">
{{ form(post_form) }}
</div>
{% if post_form is defined %}
<div class="note-post">
{{ form(post_form) }}
</div>
{% endif %}
<div class="timeline-nav">
<div class="notes">
{% if notes is defined %}
@ -63,6 +65,12 @@
<img src="{{ note.getAvatarUrl() }}" alt="{{ note.getActorNickname() }}'s avatar" width="64px">
<b>{{ note.getActorNickname() }}</b>
<div>{{ note.getContent() }}</div>
{% set id = note.getId() - 1 %}
{% for attachment in attachments[id] %}
<div>
<i> {{ attachment.getTitle() }} </i>
</div>
{% endfor %}
</div>
{% endfor %}
{% else %}