aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-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
-rw-r--r--migrations/20241213_164314_create_instance.php33
-rw-r--r--routes/nodeinfo.php19
-rw-r--r--routes/webfinger.php3
7 files changed, 132 insertions, 6 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
diff --git a/migrations/20241213_164314_create_instance.php b/migrations/20241213_164314_create_instance.php
new file mode 100644
index 0000000..fdd2f22
--- /dev/null
+++ b/migrations/20241213_164314_create_instance.php
@@ -0,0 +1,33 @@
+<?php
+
+use \Digitigrade\Db\Migrator;
+
+Migrator::getInstance()->register(20241213_164314, function (PDO $db) {
+ // table of instance information
+ // also decided to move (un)subscribe endpoints to be global instead of actor-specific
+ // hope to yourself that there wasn't any data in those because im too lazy to move it properly lol
+ $db->beginTransaction();
+ $db->exec(<<<END
+ CREATE TABLE instance (
+ id bigserial primary key,
+ domain text not null unique,
+ name text not null,
+ description text,
+ icon text,
+ software_name text not null,
+ software_description text,
+ software_version text,
+ software_homepage text
+ );
+ CREATE TABLE instance_endpoints (
+ instance_id bigint not null references instance,
+ auth text,
+ report text,
+ subscribe text,
+ unsubscribe text
+ );
+ ALTER TABLE actor_endpoints DROP COLUMN subscribe;
+ ALTER TABLE actor_endpoints DROP COLUMN unsubscribe;
+ END);
+ $db->commit();
+});
diff --git a/routes/nodeinfo.php b/routes/nodeinfo.php
index c29ac0a..93f103c 100644
--- a/routes/nodeinfo.php
+++ b/routes/nodeinfo.php
@@ -37,10 +37,21 @@ Router::getInstance()->mount('/nodeinfo/2.1', function (array $args) {
'pawpub' => [
'extensions' => [
// 'https://whatever.example/ext/meow',
- ],
- // 'pushEndpoint' => path_to_uri('/push'),
- // 'authEndpoint' => path_to_uri('/auth')
- ],
+ ]
+ ]
]
], 'application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.1#"');
+});
+
+Router::getInstance()->mount('/.well-known/pawpub-instance', function (array $args) {
+ json_response([
+ 'name' => 'A Digitigrade server',
+ 'description' => 'metadata is hardcoded for now',
+ 'softwareName' => 'Digitigrade',
+ 'softwareVersion' => '0.1.0',
+ 'softwareDescription' => 'An experimental PawPub server',
+ 'endpoints' => [
+
+ ]
+ ]);
}); \ No newline at end of file
diff --git a/routes/webfinger.php b/routes/webfinger.php
index cfccb22..541fb06 100644
--- a/routes/webfinger.php
+++ b/routes/webfinger.php
@@ -4,6 +4,7 @@ use Digitigrade\GlobalConfig;
use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\HttpResponseStatus\NotFound;
use Digitigrade\Model\Actor;
+use Digitigrade\Model\ActorWebfinger;
use Digitigrade\Router;
Router::getInstance()->mount('/.well-known/webfinger', function (array $args) {
@@ -27,7 +28,7 @@ Router::getInstance()->mount('/.well-known/webfinger', function (array $args) {
'subject' => "acct:$actor->handle@$remotePart",
'links' => [
[
- 'rel' => Actor::REL_URI,
+ 'rel' => ActorWebfinger::REL_URI,
'href' => path_to_uri("/user/$actor->handle"),
'type' => 'application/json'
]