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/Interaction.php | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Digitigrade/Model/Interaction.php (limited to 'Digitigrade/Model/Interaction.php') 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 -- cgit v1.3