From 721e37882db20045b128bbec8dd46970cdb1854b Mon Sep 17 00:00:00 2001 From: Yannis Vogel Date: Thu, 10 Apr 2025 09:02:45 +0200 Subject: [PATCH] revert to single-connector support - removed support for multiple connector-plugins - fixed issue where outbox wasn't properly retrieved from contentnation --- htdocs/index.html | 12 +++--- php/federator/api.php | 17 ++------- php/federator/api/apiinterface.php | 3 +- php/federator/api/fedusers.php | 14 +++---- .../api/fedusers/fedusersinterface.php | 6 +-- php/federator/api/fedusers/outbox.php | 7 ++-- php/federator/api/v1/dummy.php | 3 +- php/federator/api/wellknown.php | 6 +-- php/federator/api/wellknown/nodeinfo.php | 5 +-- php/federator/api/wellknown/webfinger.php | 2 +- php/federator/connector/connector.php | 6 --- php/federator/dio/stats.php | 4 +- php/federator/dio/user.php | 2 +- php/federator/main.php | 37 +++++++++--------- plugins/federator/contentnation.php | 38 ++++++------------- plugins/federator/dummyconnector.php | 12 +----- plugins/federator/mastodon.php | 13 +------ plugins/federator/rediscache.php | 9 ----- 18 files changed, 64 insertions(+), 132 deletions(-) diff --git a/htdocs/index.html b/htdocs/index.html index a3e7f67..7347200 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -14,7 +14,7 @@
-
+
@@ -38,7 +38,7 @@

Response:

-

Waiting for response

+

Waiting for response

@@ -94,11 +94,11 @@ const container = document.getElementById("request-container"); const requestBox = container.firstElementChild.cloneNode(true); - requestBox.querySelector(".target-link-input").value = "federator/v1/dummy/moo"; + requestBox.querySelector(".target-link-input").value = "federator/fedusers/grumpydevelop@contentnation.net/outbox?page=0"; requestBox.querySelector(".request-type-input").value = "GET"; - requestBox.querySelector(".session-input").value = "somethingvalider"; - requestBox.querySelector(".profile-input").value = "ihaveone"; - requestBox.querySelector(".response").textContent = ""; + requestBox.querySelector(".session-input").value = ""; + requestBox.querySelector(".profile-input").value = ""; + requestBox.querySelector(".response").textContent = "Waiting for response"; requestBox.querySelector(".send-btn").addEventListener("click", function () { sendRequest(this); diff --git a/php/federator/api.php b/php/federator/api.php index 91117ec..5b6e99a 100644 --- a/php/federator/api.php +++ b/php/federator/api.php @@ -75,20 +75,9 @@ class Api extends Main $this->openDatabase(); $this->loadPlugins(); - $host = 'dummy'; // fallback - - // Check if path matches something like fedusers/username@domain.tld - if (preg_match("#^fedusers/([^@]+)@([^/]+)/.*$#", $this->path, $matches) === 1) { - $host = strtolower($matches[2]); // extract domain - } else { - $host = 'dummy'; - echo "using dummy host in API::run\n"; - } - - $connector = $this->getConnectorForHost($host); $retval = ""; $handler = null; - if ($connector === null) { + if ($this->connector === null) { http_response_code(500); return; } @@ -97,7 +86,7 @@ class Api extends Main $this->dbh, $_SERVER['HTTP_X_SESSION'], $_SERVER['HTTP_X_PROFILE'], - $connector, + $this->connector, $this->cache ); if ($this->user === false) { @@ -124,7 +113,7 @@ class Api extends Main $printresponse = true; if ($handler !== null) { try { - $printresponse = $handler->exec($this->paths, $this->user, $connector); + $printresponse = $handler->exec($this->paths, $this->user); if ($printresponse) { $retval = $handler->toJson(); } diff --git a/php/federator/api/apiinterface.php b/php/federator/api/apiinterface.php index a12ee74..b8cf7fe 100644 --- a/php/federator/api/apiinterface.php +++ b/php/federator/api/apiinterface.php @@ -19,10 +19,9 @@ interface APIInterface * @param array $paths path array split by / * * @param \Federator\Data\User|false $user user who is calling us - * @param \Federator\Connector\Connector $connector connector to use * @return bool true on success */ - public function exec($paths, $user, $connector); + public function exec($paths, $user); /** * get internal represenation as json string diff --git a/php/federator/api/fedusers.php b/php/federator/api/fedusers.php index 401c5be..d384366 100644 --- a/php/federator/api/fedusers.php +++ b/php/federator/api/fedusers.php @@ -43,10 +43,9 @@ class FedUsers implements APIInterface * * @param array $paths path array split by / * @param \Federator\Data\User|false $user user who is calling us @unused-param - * @param \Federator\Connector\Connector $connector connector to use * @return bool true on success */ - public function exec($paths, $user, $connector) + public function exec($paths, $user) { $method = $_SERVER["REQUEST_METHOD"]; $handler = null; @@ -54,7 +53,7 @@ class FedUsers implements APIInterface case 2: if ($method === 'GET') { // /users/username or /@username - return $this->returnUserProfile($paths[1], $connector); + return $this->returnUserProfile($paths[1]); } break; case 3: @@ -83,10 +82,10 @@ class FedUsers implements APIInterface $ret = false; switch ($method) { case 'GET': - $ret = $handler->get($paths[1], $connector); + $ret = $handler->get($paths[1]); break; case 'POST': - $ret = $handler->post($paths[1], $connector); + $ret = $handler->post($paths[1]); break; } if ($ret !== false) { @@ -103,15 +102,14 @@ class FedUsers implements APIInterface * return user profile * * @param string $_name - * @param \Federator\Connector\Connector $connector connector to use * @return boolean true on success */ - private function returnUserProfile($_name, $connector) + private function returnUserProfile($_name) { $user = \Federator\DIO\User::getUserByName( $this->main->getDatabase(), $_name, - $connector, + $this->main->getConnector(), $this->main->getCache() ); if ($user === false || $user->id === null) { diff --git a/php/federator/api/fedusers/fedusersinterface.php b/php/federator/api/fedusers/fedusersinterface.php index 934f95c..1782323 100644 --- a/php/federator/api/fedusers/fedusersinterface.php +++ b/php/federator/api/fedusers/fedusersinterface.php @@ -14,17 +14,15 @@ interface FedUsersInterface * get call for user * * @param string $_user user to fetch data for - * @param \Federator\Connector\Connector $connector connector to use * @return string|false response or false in case of error */ - public function get($_user, $connector); + public function get($_user); /** * post call for user * * @param string $_user user to add data to - * @param \Federator\Connector\Connector $connector connector to use * @return string|false response or false in case of error */ - public function post($_user, $connector); + public function post($_user); } diff --git a/php/federator/api/fedusers/outbox.php b/php/federator/api/fedusers/outbox.php index ffdcf60..8b11d0e 100644 --- a/php/federator/api/fedusers/outbox.php +++ b/php/federator/api/fedusers/outbox.php @@ -33,13 +33,13 @@ class Outbox implements \Federator\Api\FedUsers\FedUsersInterface * handle get call * * @param string $_user user to fetch outbox for - * @param \Federator\Connector\Connector $connector connector to use * @return string|false response */ - public function get($_user, $connector) + public function get($_user) { $dbh = $this->main->getDatabase(); $cache = $this->main->getCache(); + $connector = $this->main->getConnector(); // get user $user = \Federator\DIO\User::getUserByName( @@ -84,10 +84,9 @@ class Outbox implements \Federator\Api\FedUsers\FedUsersInterface * handle post call * * @param string $_user user to add data to outbox @unused-param - * @param \Federator\Connector\Connector $connector connector to use * @return string|false response */ - public function post($_user, $connector) + public function post($_user) { return false; } diff --git a/php/federator/api/v1/dummy.php b/php/federator/api/v1/dummy.php index 97a9e0a..6914691 100644 --- a/php/federator/api/v1/dummy.php +++ b/php/federator/api/v1/dummy.php @@ -42,10 +42,9 @@ class Dummy implements \Federator\Api\APIInterface * * @param array $paths path array split by / * @param \Federator\Data\User|false $user user who is calling us - * @param \Federator\Connector\Connector $connector connector to use * @return bool true on success */ - public function exec($paths, $user, $connector) : bool + public function exec($paths, $user) : bool { // only for user with the 'publish' permission if ($user === false || $user->hasPermission('publish') === false) { diff --git a/php/federator/api/wellknown.php b/php/federator/api/wellknown.php index 752f33b..6b1e76d 100644 --- a/php/federator/api/wellknown.php +++ b/php/federator/api/wellknown.php @@ -57,7 +57,7 @@ class WellKnown implements APIInterface * @param \Federator\Data\User|false $user user who is calling us @unused-param * @return bool true on success */ - public function exec($paths, $user, $connector) + public function exec($paths, $user) { $method = $_SERVER["REQUEST_METHOD"]; switch ($method) { @@ -66,14 +66,14 @@ class WellKnown implements APIInterface case 2: if ($paths[0] === 'nodeinfo') { $ni = new WellKnown\NodeInfo($this, $this->main); - return $ni->exec($paths, $connector); + return $ni->exec($paths); } switch ($paths[1]) { case 'host-meta': return $this->hostMeta(); case 'nodeinfo': $ni = new WellKnown\NodeInfo($this, $this->main); - return $ni->exec($paths, $connector); + return $ni->exec($paths); case 'webfinger': $wf = new WellKnown\WebFinger($this, $this->main); return $wf->exec(); diff --git a/php/federator/api/wellknown/nodeinfo.php b/php/federator/api/wellknown/nodeinfo.php index 5bcef42..0d5b251 100644 --- a/php/federator/api/wellknown/nodeinfo.php +++ b/php/federator/api/wellknown/nodeinfo.php @@ -41,10 +41,9 @@ class NodeInfo * handle nodeinfo request * * @param string[] $paths path of us - * @param \Federator\Connector\Connector $connector connector to use * @return bool true on success */ - public function exec($paths, $connector) + public function exec($paths) { $data = [ 'fqdn' => $_SERVER['SERVER_NAME'] @@ -65,7 +64,7 @@ class NodeInfo default: $template = 'nodeinfo2.0.json'; } - $stats = \Federator\DIO\Stats::getStats($this->main, $connector); + $stats = \Federator\DIO\Stats::getStats($this->main); echo "fetch usercount via connector\n"; $data['usercount'] = $stats->userCount; $data['postcount'] = $stats->postCount; diff --git a/php/federator/api/wellknown/webfinger.php b/php/federator/api/wellknown/webfinger.php index b817401..61dadc3 100644 --- a/php/federator/api/wellknown/webfinger.php +++ b/php/federator/api/wellknown/webfinger.php @@ -53,7 +53,7 @@ class WebFinger $user = \Federator\DIO\User::getUserByName( $this->main->getDatabase(), $matches[1], - $this->main->getConnectorForHost($domain), + $this->main->getConnector(), $this->main->getCache() ); if ($user->id == 0) { diff --git a/php/federator/connector/connector.php b/php/federator/connector/connector.php index d1f41ac..aab10e7 100644 --- a/php/federator/connector/connector.php +++ b/php/federator/connector/connector.php @@ -13,12 +13,6 @@ namespace Federator\Connector; */ interface Connector { - /** - * get the host this connector is dedicated to - * - * @return string - */ - public function getHost(); /** * get posts by given user * diff --git a/php/federator/dio/stats.php b/php/federator/dio/stats.php index 9aa3fd4..bf85909 100644 --- a/php/federator/dio/stats.php +++ b/php/federator/dio/stats.php @@ -18,10 +18,9 @@ class Stats * get remote stats * * @param \Federator\Main $main main instance - * @param \Federator\Connector\Connector $connector connector to use * @return \Federator\Data\Stats */ - public static function getStats($main, $connector) + public static function getStats($main) { $cache = $main->getCache(); // ask cache @@ -31,6 +30,7 @@ class Stats return $stats; } } + $connector = $main->getConnector(); // ask connector for stats $stats = $connector->getRemoteStats(); if ($cache !== null && $stats !== false) { diff --git a/php/federator/dio/user.php b/php/federator/dio/user.php index 9470198..ac936e7 100644 --- a/php/federator/dio/user.php +++ b/php/federator/dio/user.php @@ -192,7 +192,7 @@ class User if ($user->id === null && $user->externalid !== null) { self::addLocalUser($dbh, $user, $_name); } - $cache->saveRemoteUserByName($_name, user: $user); + $cache->saveRemoteUserByName($_name, $user); } return $user; } diff --git a/php/federator/main.php b/php/federator/main.php index 6ec0637..ddb5acf 100644 --- a/php/federator/main.php +++ b/php/federator/main.php @@ -28,11 +28,11 @@ class Main */ protected $config; /** - * remote connectors + * remote connector * - * @var array $connectors + * @var Connector\Connector $connector */ - protected $connectors = []; + protected $connector = null; /** * response content type * @@ -139,17 +139,14 @@ class Main { return $this->cache; } - /** - * Get the connector for a given remote host - * - * @param string $remoteHost The host from the actor URL (e.g. mastodon.social) - * @return Connector\Connector|null - */ - public function getConnectorForHost(string $remoteHost): ?Connector\Connector + * get connector + * + * @return \Federator\Connector\Connector + */ + public function getConnector() { - $host = strtolower(parse_url($remoteHost, PHP_URL_HOST) ?? $remoteHost); - return $this->connectors[$host] ?? null; + return $this->connector; } /** @@ -234,6 +231,8 @@ class Main /** * set cache + * + * @param \Federator\Cache\Cache $cache the new cache */ public function setCache(Cache\Cache $cache): void { @@ -241,16 +240,16 @@ class Main } /** - * Set a connector for a specific remote host + * set connector * - * @param string $remoteURL The remote host (like mastodon.social or contentnation.net) - * @param Connector\Connector $connector The connector instance + * @param \Federator\Connector\Connector $connector the new connector */ - public function addConnector(string $remoteURL, Connector\Connector $connector): void + public function setConnector(Connector\Connector $connector) : void { - // Normalize the host (no scheme, lowercase) - $host = strtolower(parse_url($remoteURL, PHP_URL_HOST) ?? $remoteURL); - $this->connectors[$host] = $connector; + if ($this->connector) { + echo "main::setConnector Setting new connector will override old one.\n"; // TODO CHANGE TO LOG WARNING + } + $this->connector = $connector; } /** diff --git a/plugins/federator/contentnation.php b/plugins/federator/contentnation.php index b7d4ac0..77826bc 100644 --- a/plugins/federator/contentnation.php +++ b/plugins/federator/contentnation.php @@ -49,15 +49,6 @@ class ContentNation implements Connector $this->main = $main; } - /** - * get the host this connector is dedicated to - * - * @return string - */ - public function getHost() { - return "contentnation.net"; - } - /** * get posts by given user * @@ -68,7 +59,10 @@ class ContentNation implements Connector */ public function getRemotePostsByUser($userId, $min, $max) { - $remoteURL = $this->service . '/api/profile/' . $userId . '/activities'; + if (preg_match("#^([^@]+)@([^/]+)#", $userId, $matches) === 1) { + $userId = $matches[1]; + } + $remoteURL = $this->service . '/api/profile/' . urlencode($userId) . '/activities'; if ($min !== '') { $remoteURL .= '&minTS=' . urlencode($min); } @@ -133,15 +127,16 @@ class ContentNation implements Connector $apArticle->setID($idurl) ->setURL($idurl); $image = $activity['image'] ?? $activity['profileimg']; - // $mediaType = @mime_content_type($imgpath . $activity['profile'] . '/' . $image) | 'text/plain'; // old approach, using local copy of images - $imgUrl = $userdata . '/' . $activity['profile'] . $image; - $mediaType = $this->getRemoteMimeType($imgUrl) ?? 'text/plain'; + $path = $imgpath . $activity['profile'] . '/' . $image; + $mediaType = (file_exists($path) && ($type = mime_content_type($path)) && !str_starts_with($type, 'text/')) + ? $type + : 'image/jpeg'; $img = new \Federator\Data\ActivityPub\Common\Image(); $img->setMediaType($mediaType) ->setName($articleimage) ->setURL($userdata . '/' . $activity['profile'] . $image); $apArticle->addImage($img); - $create->setObject(object: $apArticle); + $create->setObject($apArticle); $posts[] = $create; break; // Article case 'Comment': @@ -200,18 +195,6 @@ class ContentNation implements Connector } return $posts; } - - /** - * Get the MIME type of a remote file by its URL. - * - * @param string $_url The URL of the remote file. - * @return string|false The MIME type if found, or false on failure. - */ - public function getRemoteMimeType($url) - { - $headers = get_headers($url, 1); - return $headers['Content-Type'] ?? 'unknown'; - } /** * get statistics from remote system @@ -326,5 +309,6 @@ namespace Federator; function contentnation_load($main) { $cn = new Connector\ContentNation($main); - $main->addConnector($cn->getHost(), $cn); + echo "contentnation::contentnation_load Loaded new connector, adding to main\n"; // TODO change to proper log + $main->setConnector($cn); } diff --git a/plugins/federator/dummyconnector.php b/plugins/federator/dummyconnector.php index d5acad4..ed699bf 100644 --- a/plugins/federator/dummyconnector.php +++ b/plugins/federator/dummyconnector.php @@ -18,15 +18,6 @@ class DummyConnector implements Connector public function __construct() { } - - /** - * get the host this connector is dedicated to - * - * @return string - */ - public function getHost() { - return "dummy"; - } /** * get posts by given user @@ -96,5 +87,6 @@ namespace Federator; function dummy_load($main) { $dummy = new Connector\DummyConnector(); - $main->addConnector($dummy->getHost(), $dummy); + echo "dummyconnector::dummy_load Loaded new connector, adding to main\n"; // TODO change to proper log + $main->setConnector($dummy); } diff --git a/plugins/federator/mastodon.php b/plugins/federator/mastodon.php index 3a9c3f1..be9f5e0 100644 --- a/plugins/federator/mastodon.php +++ b/plugins/federator/mastodon.php @@ -49,16 +49,6 @@ class Mastodon implements Connector $this->main = $main; } - /** - * get the host this connector is dedicated to - * - * @return string - */ - public function getHost() - { - return "mastodon.social"; - } - /** * get posts by given user * @@ -363,5 +353,6 @@ namespace Federator; function mastodon_load($main) { $mast = new Connector\Mastodon($main); - $main->addConnector($mast->getHost(), $mast); + echo "mastodon::mastodon_load Loaded new connector, adding to main\n"; // TODO change to proper log + $main->setConnector($mast); } diff --git a/plugins/federator/rediscache.php b/plugins/federator/rediscache.php index 78b16d4..5c96eeb 100644 --- a/plugins/federator/rediscache.php +++ b/plugins/federator/rediscache.php @@ -53,15 +53,6 @@ class RedisCache implements Cache } } - /** - * get the host this connector is dedicated to - * - * @return string - */ - public function getHost() { - return "redis"; - } - /** * connect to redis * @return void