From 6f844ff40d936fb6591c2469dd9ff922bc4e575f Mon Sep 17 00:00:00 2001 From: winter Date: Tue, 17 Dec 2024 23:05:43 +0000 Subject: implement interactions --- Digitigrade/Model/FetchableModel.php | 7 ++-- Digitigrade/Model/Interaction.php | 62 +++++++++++++++++++++++++++++++++++ Digitigrade/Model/InteractionKind.php | 8 +++++ Digitigrade/Model/Note.php | 4 +++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 Digitigrade/Model/Interaction.php create mode 100644 Digitigrade/Model/InteractionKind.php (limited to 'Digitigrade') diff --git a/Digitigrade/Model/FetchableModel.php b/Digitigrade/Model/FetchableModel.php index 33e1151..1120823 100644 --- a/Digitigrade/Model/FetchableModel.php +++ b/Digitigrade/Model/FetchableModel.php @@ -13,7 +13,7 @@ abstract class FetchableModel extends Model implements RemoteFetchable { self::$mapper->bStrictObjectTypeChecking = false; } - protected static function createFromJson(\stdClass $data, bool $autoSave): static { + protected static function createFromJson(\stdClass $data, bool $autoSave, ?string $uri = null): static { // default everything nullable to null $obj = new static(); $reflProps = (new \ReflectionClass(static::class))->getProperties(); @@ -62,9 +62,12 @@ abstract class FetchableModel extends Model implements RemoteFetchable { if ($data === null) return null; - $obj = static::createFromJson($data, $autoSave); + $obj = static::createFromJson($data, $autoSave, $uri); $obj->finaliseAfterFetch($uri); + if (!$obj->validate()) { + return null; // and don't save + } if ($autoSave) { $obj->save(); $obj->finaliseAfterSave(); diff --git a/Digitigrade/Model/Interaction.php b/Digitigrade/Model/Interaction.php new file mode 100644 index 0000000..bf0745e --- /dev/null +++ b/Digitigrade/Model/Interaction.php @@ -0,0 +1,62 @@ +uri = "UNKNOWN"; + $inter->created = new \DateTimeImmutable(); + $inter->author = $author; + $inter->kind = $kind; + $inter->target = $target; + + $inter->save(); + $inter->uri = path_to_uri("/interaction/$inter->id"); + $inter->save(); + + return $inter; + } + + protected function getUpdateWhereClause(\PDO $db): ?string { + if (self::findWhere('uri = ?', [$this->uri]) != null) + return 'uri = ' . $db->quote($this->uri); + if (self::findWhere('id = ?', [$this->id]) != null) + return "id = $this->id"; + return null; + } + + protected function validate(): bool { + // author has to be from the same instance as the interaction itself + return hostname_from_uri($this->author->uri) == hostname_from_uri($this->uri); + } + + public static function findAllWithAuthor(Actor $author): array { + return self::findAllWhere('author = ?', [$author->id]); + } + + public static function findAllWithTarget(Note $target): array { + return self::findAllWhere('target = ?', [$target->id]); + } + + public function jsonSerialize(): array { + return [ + 'type' => 'interaction', + 'self' => path_to_uri("/interaction/$this->id"), + 'created' => $this->created->format('c'), + 'modified' => $this->modified?->format('c'), + 'author' => $this->author->uri, + 'kind' => $this->kind->value, + 'target' => $this->target->uri, + //'extensions' => [] + ]; + } +} \ No newline at end of file diff --git a/Digitigrade/Model/InteractionKind.php b/Digitigrade/Model/InteractionKind.php new file mode 100644 index 0000000..2fd09c9 --- /dev/null +++ b/Digitigrade/Model/InteractionKind.php @@ -0,0 +1,8 @@ +id]); } + public function getInteractions(): array { + return Interaction::findAllWithTarget($this); + } + public function jsonSerialize(): array { return [ 'type' => 'note', -- cgit v1.3