From e591374bb4b7d231fadf99d2e278806e53971919 Mon Sep 17 00:00:00 2001 From: winter Date: Tue, 28 Jan 2025 20:34:49 +0000 Subject: display attachments on notes --- Digitigrade/Model/Note.php | 13 +++++++++++++ Digitigrade/Model/NoteAttachment.php | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'Digitigrade') diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php index 0164e7d..a4df99d 100644 --- a/Digitigrade/Model/Note.php +++ b/Digitigrade/Model/Note.php @@ -82,10 +82,14 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable { $this->mentions = $this->findMentions(); $this->attachments = NoteAttachment::findAllWhere('note_id = ?', [$this->id]); $this->extensions = $this->findExtensions(); + + // put attachments back in the right order + usort($this->attachments, fn($a, $b) => $a->index <=> $b->index); } protected function dehydrate() { $this->saveMentions(); + $this->saveAttachments(); $this->saveExtensions(); } @@ -130,6 +134,15 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable { } } + private function saveAttachments() { + $index = 0; + foreach ($this->attachments as $att) { + $att->index = $index++; + $att->setOwnerId($this->id); + $att->save(); + } + } + private function saveExtensions() { $pdo = Db::getInstance()->getPdo(); foreach ($this->extensions as $uri => $obj) { diff --git a/Digitigrade/Model/NoteAttachment.php b/Digitigrade/Model/NoteAttachment.php index c47ec64..9e6a3d3 100644 --- a/Digitigrade/Model/NoteAttachment.php +++ b/Digitigrade/Model/NoteAttachment.php @@ -10,13 +10,13 @@ class NoteAttachment extends Model { public string $href; public ?string $description; - protected function setOwnerId(int $id) { + public function setOwnerId(int $id) { $this->noteId = $id; } protected function getUpdateWhereClause($db): ?string { - if (self::findWhere('note_id = ?', [$this->noteId]) != null) { - return "note_id = $this->noteId"; + if (self::findWhere('note_id = ? AND index = ?', [$this->noteId, $this->index]) != null) { + return "note_id = $this->noteId AND index = $this->index"; } return null; } -- cgit v1.3