diff options
| author | winter | 2024-12-17 23:05:43 +0000 |
|---|---|---|
| committer | winter | 2024-12-17 23:05:43 +0000 |
| commit | 6f844ff40d936fb6591c2469dd9ff922bc4e575f (patch) | |
| tree | 007dc0372c2c999bdcf6ea1ba80f6eb93a161a7f /Digitigrade/Model/Interaction.php | |
| parent | 801778a295df2b026391483faa390cf87e68b1ad (diff) | |
implement interactions
Diffstat (limited to 'Digitigrade/Model/Interaction.php')
| -rw-r--r-- | Digitigrade/Model/Interaction.php | 62 |
1 files changed, 62 insertions, 0 deletions
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 @@ +<?php +namespace Digitigrade\Model; + +class Interaction extends PushableModel { + public ?int $id; + public string $uri; + public \DateTimeImmutable $created; + public ?\DateTimeImmutable $modified; + public Actor $author; + public InteractionKind $kind; + public Note $target; + public array $extensions = []; + + public static function create(Actor $author, InteractionKind $kind, Note $target) { + $inter = new self(); + $inter->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 |
