aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade
diff options
context:
space:
mode:
authorwinter2024-12-13 23:53:23 +0000
committerwinter2024-12-13 23:53:23 +0000
commit2713c1f73a39d4106936d4c52bf8f18d810d7f3c (patch)
treeca3ab4c78b7a45fe4ddc218ce4e45f181cd179e2 /Digitigrade
parentae37b73d1f15ffe5c4c9a32c5de349cd746ebc48 (diff)
add instance information
Diffstat (limited to 'Digitigrade')
-rw-r--r--Digitigrade/Model.php5
-rw-r--r--Digitigrade/Model/FetchableModel.php7
-rw-r--r--Digitigrade/Model/Instance.php48
-rw-r--r--Digitigrade/Model/InstanceEndpoints.php23
4 files changed, 82 insertions, 1 deletions
diff --git a/Digitigrade/Model.php b/Digitigrade/Model.php
index 8a593eb..ffc2157 100644
--- a/Digitigrade/Model.php
+++ b/Digitigrade/Model.php
@@ -146,6 +146,11 @@ abstract class Model {
}, $stmt->fetchAll(\PDO::FETCH_ASSOC));
}
+ public static function findAll(): array {
+ // FIXME: anything but this...
+ return static::findAllWhere('1 = 1', []);
+ }
+
public static function find(int $id): ?static {
return static::findWhere('id = ?', [$id]);
}
diff --git a/Digitigrade/Model/FetchableModel.php b/Digitigrade/Model/FetchableModel.php
index 30b68e7..0b67599 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;
}
- private static function fetchFromRemote(string $uri, bool $autoSave): ?static {
+ protected static function fetchFromRemote(string $uri, bool $autoSave): ?static {
// fetch the object
$data = @file_get_contents($uri, context: stream_context_create(['http' => [
'header' => 'Accept: application/json'
@@ -58,6 +58,8 @@ abstract class FetchableModel extends Model implements RemoteFetchable {
}
}
+ $obj->beforeSave($uri);
+
if ($autoSave) {
$obj->save();
}
@@ -65,6 +67,9 @@ abstract class FetchableModel extends Model implements RemoteFetchable {
return $obj;
}
+ protected function beforeSave(string $uri) {
+ }
+
public static function findByUri(string $uri, bool $autoSave = true, bool $forceRefetch = false): ?static {
if (!$forceRefetch) {
$obj = static::findWhere('uri = ?', [$uri]);
diff --git a/Digitigrade/Model/Instance.php b/Digitigrade/Model/Instance.php
new file mode 100644
index 0000000..2924f71
--- /dev/null
+++ b/Digitigrade/Model/Instance.php
@@ -0,0 +1,48 @@
+<?php
+namespace Digitigrade\Model;
+
+use Digitigrade\Model;
+
+class Instance extends FetchableModel {
+ public ?int $id;
+ public string $domain;
+ public string $name;
+ public ?string $description;
+ public ?string $icon; // uri
+ public string $softwareName;
+ public ?string $softwareDescription;
+ public ?string $softwareVersion;
+ public ?string $softwareHomepage;
+
+ public InstanceEndpoints $endpoints;
+
+ protected function getUpdateWhereClause(\PDO $db): ?string {
+ if (self::findWhere('domain = ?', [$this->domain]) != null)
+ return 'domain = ' . $db->quote($this->domain);
+ if (self::findWhere('id = ?', [$this->id]) != null)
+ return "id = $this->id";
+ return null;
+ }
+
+ public static function findByDomain(string $domain): ?self {
+ return self::findByUri("https://$domain/.well-known/pawpub-instance");
+ }
+
+ public function hydrate() {
+ $this->endpoints = InstanceEndpoints::findWhere('instance_id = ?', [$this->id]);
+ }
+
+ protected function beforeSave(string $uri) {
+ $this->domain = explode('/', $uri)[2];
+ }
+
+ public static function findByUri(string $uri, bool $autoSave = true, bool $forceRefetch = false): ?static {
+ if (!$forceRefetch) {
+ $domain = explode('/', $uri)[2];
+ $obj = self::findWhere('domain = ?', [$domain]);
+ if ($obj != null)
+ return $obj;
+ }
+ return self::fetchFromRemote($uri, $autoSave);
+ }
+} \ No newline at end of file
diff --git a/Digitigrade/Model/InstanceEndpoints.php b/Digitigrade/Model/InstanceEndpoints.php
new file mode 100644
index 0000000..d2b0b24
--- /dev/null
+++ b/Digitigrade/Model/InstanceEndpoints.php
@@ -0,0 +1,23 @@
+<?php
+namespace Digitigrade\Model;
+
+use Digitigrade\Model;
+
+class InstanceEndpoints extends Model {
+ public ?int $instanceId;
+ public ?string $auth;
+ public ?string $report;
+ public ?string $subscribe;
+ public ?string $unsubscribe;
+
+ protected function setOwnerId(int $id) {
+ $this->instanceId = $id;
+ }
+
+ protected function getUpdateWhereClause($db): ?string {
+ if (self::findWhere('instance_id = ?', [$this->instanceId]) != null) {
+ return "instance_id = $this->instanceId";
+ }
+ return null;
+ }
+} \ No newline at end of file