aboutsummaryrefslogtreecommitdiffhomepage
path: root/WpfTest/Model
diff options
context:
space:
mode:
authorwinter2024-12-08 18:04:37 +0000
committerwinter2024-12-08 18:04:37 +0000
commitb00185ddbac9ac3de975a3954f2ede2f24458f6a (patch)
tree265ccaae497e2788ed7de8f6b08a0d5a77ea16dc /WpfTest/Model
parent13647d55bd8085a2b3a686b8aad3b28b0faf693a (diff)
implement notes
Diffstat (limited to 'WpfTest/Model')
-rw-r--r--WpfTest/Model/Actor.php25
-rw-r--r--WpfTest/Model/Note.php95
-rw-r--r--WpfTest/Model/NoteAttachment.php11
-rw-r--r--WpfTest/Model/NotePrivacy.php16
-rw-r--r--WpfTest/Model/NotePrivacyInteractors.php9
-rw-r--r--WpfTest/Model/NotePrivacyScope.php9
6 files changed, 162 insertions, 3 deletions
diff --git a/WpfTest/Model/Actor.php b/WpfTest/Model/Actor.php
index cf8d7ca..eaef99c 100644
--- a/WpfTest/Model/Actor.php
+++ b/WpfTest/Model/Actor.php
@@ -3,9 +3,9 @@ namespace WpfTest\Model;
use WpfTest\Model;
-class Actor extends Model {
- public int $id;
- public string $self;
+class Actor extends Model implements \JsonSerializable {
+ public ?int $id;
+ public string $uri;
public bool $isLocal = false;
public \DateTimeImmutable $created;
public ?\DateTimeImmutable $modified;
@@ -23,4 +23,23 @@ class Actor extends Model {
public static function findLocalByHandle(string $handle): ?self {
return self::findWhere('is_local = true AND handle = ?', [$handle]);
}
+
+ public function jsonSerialize(): array {
+ return [
+ 'type' => 'actor',
+ 'self' => path_to_uri("/user/$this->handle"),
+ 'created' => $this->created?->format('c'),
+ 'modified' => $this->modified?->format('c'),
+ 'homepage' => path_to_uri("/$this->handle"),
+ 'handle' => $this->handle,
+ 'displayName' => $this->displayName,
+ 'bio' => $this->bio,
+ 'pronouns' => $this->pronouns,
+ 'automated' => $this->automated,
+ 'endpoints' => [
+ 'basicFeed' => path_to_uri("/user/$this->handle/basicFeed"),
+ 'fullFeed' => path_to_uri("/user/$this->handle/fullFeed")
+ ]
+ ];
+ }
} \ No newline at end of file
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 @@
+<?php
+namespace WpfTest\Model;
+
+use WpfTest\Db;
+use WpfTest\Model;
+
+class Note extends Model implements \JsonSerializable {
+ public ?int $id;
+ public string $uri;
+ public \DateTimeImmutable $created;
+ public ?\DateTimeImmutable $modified;
+ public Actor $author;
+ public ?string $summary;
+ public string $plainContent;
+ /**
+ * @var array<string, string>
+ */
+ 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
diff --git a/WpfTest/Model/NoteAttachment.php b/WpfTest/Model/NoteAttachment.php
new file mode 100644
index 0000000..b07d595
--- /dev/null
+++ b/WpfTest/Model/NoteAttachment.php
@@ -0,0 +1,11 @@
+<?php
+namespace WpfTest\Model;
+
+use WpfTest\Model;
+
+class NoteAttachment extends Model {
+ public int $index;
+ public string $type;
+ public string $href;
+ public ?string $description;
+} \ No newline at end of file
diff --git a/WpfTest/Model/NotePrivacy.php b/WpfTest/Model/NotePrivacy.php
new file mode 100644
index 0000000..b7ed58a
--- /dev/null
+++ b/WpfTest/Model/NotePrivacy.php
@@ -0,0 +1,16 @@
+<?php
+namespace WpfTest\Model;
+
+use WpfTest\Model;
+
+class NotePrivacy extends Model {
+ public NotePrivacyScope $scope;
+ /**
+ * @var Actor[]
+ */
+ public array $alsoVisibleTo = [];
+ public bool $indexable;
+ public NotePrivacyInteractors $canReshare = NotePrivacyInteractors::all;
+ public NotePrivacyInteractors $canReply = NotePrivacyInteractors::all;
+ public NotePrivacyInteractors $canOtherInteract = NotePrivacyInteractors::all;
+} \ No newline at end of file
diff --git a/WpfTest/Model/NotePrivacyInteractors.php b/WpfTest/Model/NotePrivacyInteractors.php
new file mode 100644
index 0000000..ff4d7fa
--- /dev/null
+++ b/WpfTest/Model/NotePrivacyInteractors.php
@@ -0,0 +1,9 @@
+<?php
+namespace WpfTest\Model;
+
+enum NotePrivacyInteractors {
+ case all;
+ case followers;
+ case mutuals;
+ case none;
+} \ No newline at end of file
diff --git a/WpfTest/Model/NotePrivacyScope.php b/WpfTest/Model/NotePrivacyScope.php
new file mode 100644
index 0000000..3134c26
--- /dev/null
+++ b/WpfTest/Model/NotePrivacyScope.php
@@ -0,0 +1,9 @@
+<?php
+namespace WpfTest\Model;
+
+enum NotePrivacyScope {
+ case public;
+ case followers;
+ case mutuals;
+ case none;
+} \ No newline at end of file