From b00185ddbac9ac3de975a3954f2ede2f24458f6a Mon Sep 17 00:00:00 2001 From: winter Date: Sun, 8 Dec 2024 18:04:37 +0000 Subject: implement notes --- WpfTest/Model/Note.php | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 WpfTest/Model/Note.php (limited to 'WpfTest/Model/Note.php') diff --git a/WpfTest/Model/Note.php b/WpfTest/Model/Note.php new file mode 100644 index 0000000..85b3567 --- /dev/null +++ b/WpfTest/Model/Note.php @@ -0,0 +1,95 @@ + + */ + public array $formattedContent = []; + public string $language; + /** + * @var Actor[] + */ + public array $mentions = []; + public ?Note $inReplyTo; + public ?Note $threadApex; + public NotePrivacy $privacy; + /** + * @var NoteAttachment[] + */ + public array $attachments = []; + public array $extensions = []; + + protected function hydrate(array $row) { + $this->privacy = NotePrivacy::findWhere('note_id = ?', [$this->id]); + $this->formattedContent = $this->findFormattedContents(); + $this->mentions = $this->findMentions(); + $this->attachments = NoteAttachment::findAllWhere('note_id = ?', [$this->id]); + } + + private function findFormattedContents(): array { + $pdo = Db::getInstance()->getPdo(); + $stmt = $pdo->prepare('SELECT mimetype, body FROM note_formatted_content WHERE note_id = ?'); + $stmt->execute([$this->id]); + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + } + + private function findMentions(): array { + $pdo = Db::getInstance()->getPdo(); + $stmt = $pdo->prepare('SELECT actor_id FROM note_mention WHERE note_id = ?'); + $stmt->execute([$this->id]); + $actors = array_map(function ($actorId) { + return Actor::find($actorId); + }, $stmt->fetchAll(\PDO::FETCH_COLUMN, 0)); + return $actors; + } + + public static function findAllWithAuthor(Actor $author): array { + return self::findAllWhere('author = ?', [$author->id]); + } + + public function jsonSerialize(): array { + return [ + 'type' => 'note', + 'self' => path_to_uri("/post/$this->id"), + 'created' => $this->created->format('c'), + 'modified' => $this->modified?->format('c'), + 'author' => $this->author->uri, + 'summary' => $this->summary, + 'plainContent' => $this->plainContent, + 'formattedContent' => $this->formattedContent, + 'language' => $this->language, + 'mentions' => array_map(function (Actor $actor) { + return $actor->uri; + }, $this->mentions), + 'inReplyTo' => $this->inReplyTo?->uri, + 'threadApex' => $this->threadApex?->uri, + 'privacy' => [ + 'scope' => $this->privacy->scope->name, + 'alsoVisibleTo' => $this->privacy->alsoVisibleTo, + 'indexable' => $this->privacy->indexable, + 'canReshare' => $this->privacy->canReshare->name, + 'canReply' => $this->privacy->canReply->name, + 'canOtherInteract' => $this->privacy->canOtherInteract->name + ], + 'attachments' => array_map(function (NoteAttachment $attachment) { + return [ + 'type' => $attachment->type, + 'href' => $attachment->href, + 'description' => $attachment->description + ]; + }, $this->attachments), + //'extensions' => [] + ]; + } +} \ No newline at end of file -- cgit v1.3