From dde59ffbe5583d28d323d3206251bd4948bcc325 Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Wed, 26 Jun 2024 07:03:38 +1000 Subject: [PATCH] add alsoKnownAs entities to actor record. --- src/Lib/Activity.php | 30 +++++++++- src/Lib/Channel.php | 5 ++ src/Module/Nomad_gateway.php | 109 +++++++++++++++++++++++++++++++++++ src/Module/Well_known.php | 7 +++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/Module/Nomad_gateway.php diff --git a/src/Lib/Activity.php b/src/Lib/Activity.php index 5d02ef7e8..e38376fb7 100644 --- a/src/Lib/Activity.php +++ b/src/Lib/Activity.php @@ -1702,16 +1702,42 @@ class Activity } $auto_follow = intval(PConfig::Get($channel['channel_id'],'system','autoperms')); } - + $aka = []; if ($channel) { $locations = self::nomadic_locations(['author_xchan' => $channel['channel_hash']]); + $aplocations = self::nomadic_locations(['author_xchan' => Channel::getDid($channel)]); + $aka[] = Channel::getDidResolver($channel, true); + $aka[] = z_root() . '/.well-known/nomad-gateway/' . $channel['xchan_hash'] . '/actor'; + $aka[] = z_root() . '/channel/' . $channel['channel_address']; + $gateways[] = z_root(); if ($locations) { foreach ($locations as $location) { - $gateways[] = $location['hubloc_url']; + if ($location['hubloc_url'] === z_root()) { + continue; + } + if (! in_array($location['hubloc_url'], $gateways)) { + $gateways[] = $location['hubloc_url']; + } + if (! in_array($location['hubloc_url'] . '/.well-known/nomad-gateway/' . $location['hubloc_hash'] . '/actor', $aka)) { + $aka[] = $location['hubloc_url'] . '/.well-known/nomad-gateway/' . $location['hubloc_hash'] . '/actor'; + } + if (! in_array($location['hubloc_id_url'], $aka)) { + $aka[] = $location['hubloc_id_url']; + } } + foreach ($aplocations as $location) { + if (! in_array($location['hubloc_url'], $gateways)) { + $gateways[] = $location['hubloc_url']; + } + if (! in_array($location['hubloc_id_url'], $aka)) { + $aka[] = $location['hubloc_id_url']; + } + } + $actor->setAlsoKnownAs($aka); $actor->setGateways($gateways); } $actor->setId($nomadic ? Channel::getDidResolver($channel, true) : Channel::url($channel)); + } else { $actor->setId((str_starts_with($p['xchan_hash'], 'http')) ? $p['xchan_hash'] : $current_url); } diff --git a/src/Lib/Channel.php b/src/Lib/Channel.php index 16a4a97d5..640356f6f 100644 --- a/src/Lib/Channel.php +++ b/src/Lib/Channel.php @@ -2092,6 +2092,11 @@ class Channel return z_root() . '/.well-known/apgateway/' . self::getDid($channel) . (($isActor) ? '/actor' : ''); } + public static function getNomadResolver($channel, $isActor = false) + { + return z_root() . '/.well-known/nomad-gateway/' . $channel['channel_hash']. (($isActor) ? '/actor' : ''); + } + /** * @brief Get a channel array by a channel_hash. * diff --git a/src/Module/Nomad_gateway.php b/src/Module/Nomad_gateway.php new file mode 100644 index 000000000..2035c36f9 --- /dev/null +++ b/src/Module/Nomad_gateway.php @@ -0,0 +1,109 @@ +mapObject(str_replace($key, '', rtrim($url, '/')), $query[0]); + $localPath = ltrim($mappedPath, '/'); + App::$cmd = $localPath; + App::$argv = explode('/', App::$cmd); + + App::$argc = count(App::$argv); + if ((array_key_exists('0', App::$argv)) && strlen(App::$argv[0])) { + if (strpos(App::$argv[0],'.')) { + $_REQUEST['module_format'] = substr(App::$argv[0],strpos(App::$argv[0], '.') + 1); + App::$argv[0] = substr(App::$argv[0], 0, strpos(App::$argv[0], '.')); + } + + App::$module = str_replace(".", "_", App::$argv[0]); + App::$module = str_replace("-", "_", App::$module); + if (str_starts_with(App::$module, '_')) { + App::$module = substr(App::$module, 1); + } + } + else { + App::$argc = 1; + App::$argv = ['home']; + App::$module = 'home'; + } + header('Link: ' . '<' . $url . '>; rel="alternate"', false); + // recursively call the router. + App::$module_loaded = false; + $router = new Router(); + $router->Dispatch(); + + } + + protected function mapObject($path, $channel) + { + // lookup abstract paths + $systemPaths = [ + '' => '/channel/' . $channel['channel_address'], + '/inbox' => '/inbox/' . $channel['channel_address'], + '/outbox' => '/outbox/' . $channel['channel_address'], + '/followers' => '/followers/' . $channel['channel_address'], + '/following' => '/following/' . $channel['channel_address'], + '/permissions' => '/permissions/' . $channel['channel_address'], + '/actor' => '/channel/' . $channel['channel_address'], + '/actor/inbox' => '/inbox/' . $channel['channel_address'], + '/actor/outbox' => '/outbox/' . $channel['channel_address'], + '/actor/followers' => '/followers/' . $channel['channel_address'], + '/actor/following' => '/following/' . $channel['channel_address'], + '/actor/permissions' => '/permissions/' . $channel['channel_address'], + ]; + + $partialPaths = [ + '/files/' => '/cloud/' . $channel['channel_address'], + '/photos/' => '/photos/' . $channel['channel_address'], + '/album/' => '/album/' . $channel['channel_address'], + ]; + + foreach ($systemPaths as $index => $localPath) { + if ($path === $index) { + return $localPath; + } + } + + foreach ($partialPaths as $index => $localPath) { + if (str_starts_with($path, $index)) { + $suffix = substr($path, strlen($index)); + return $localPath . '/' . ltrim($suffix, '/'); + } + } + return $path; + } + + +} \ No newline at end of file diff --git a/src/Module/Well_known.php b/src/Module/Well_known.php index 7049cb794..c00c8d396 100644 --- a/src/Module/Well_known.php +++ b/src/Module/Well_known.php @@ -46,6 +46,13 @@ class Well_known extends Controller $module = new Apgateway(); $module->init(); break; + case 'nomad-gateway': + App::$argc -= 1; + array_shift(App::$argv); + App::$argv[0] = 'nomad-gateway'; + $module = new Nomad_gateway(); + $module->init(); + break; case 'oauth-authorization-server': case 'openid-configuration': App::$argc -= 1;