Add subscribe URL retrieval to server poll

This commit is contained in:
Hypolite Petovan 2022-05-07 16:04:12 -04:00
parent 7d5012e72e
commit 8988ad9f9d

View file

@ -147,6 +147,10 @@ class Server
$addons = $probe_result['data']['plugins'];
}
if ($probe_result['data']['admin']['profile']) {
$subscribe = $this->getSubscribeUrl($probe_result['data']['url'], $probe_result['data']['admin']['profile']);
}
$this->atlas->perform(
'UPDATE `server`
SET `available` = 1,
@ -161,6 +165,7 @@ class Server
`admin_name` = :admin_name,
`admin_profile` = :admin_profile,
`noscrape_url` = :noscrape_url,
`subscribe_url` = :subscribe_url,
`ssl_state` = :ssl_state
WHERE `id` = :server_id',
[
@ -175,6 +180,7 @@ class Server
'admin_name' => $probe_result['data']['admin']['name'],
'admin_profile' => $probe_result['data']['admin']['profile'],
'noscrape_url' => $probe_result['data']['no_scrape_url'] ?? null,
'subscribe_url' => $subscribe ?? null,
'ssl_state' => $probe_result['ssl_state']
]
);
@ -452,4 +458,38 @@ class Server
}
}
}
public function getSubscribeUrl($base_url, $profile)
{
$xrdRequest = new WebRequest($base_url . '/xrd');
$xrdRequest->addRequestHeader('Accept', 'application/jrd+json');
$xrdJsonData = $xrdRequest->get(['uri' => $profile]);
$this->logger->debug('WebRequest: ' . $xrdRequest->getLastFetchedUrl() . ' Status: ' . $xrdRequest->getLastStatus());
if ($xrdRequest->getLastStatus() != 200) {
$this->logger->info('Unsuccessful XRD request: ' . $xrdRequest->getLastFetchedUrl());
return null;
}
try {
$xrdData = json_decode($xrdJsonData);
} catch (\Throwable $e) {
$this->logger->notice('Invalid JSON string for XRD URL: ' . $xrdRequest->getLastFetchedUrl());
return null;
}
if (!isset($xrdData->links)) {
$this->logger->notice('Invalid JSON structure for XRD URL: ' . $xrdRequest->getLastFetchedUrl());
return null;
}
foreach ($xrdData->links as $link) {
if ($link->rel == 'http://ostatus.org/schema/1.0/subscribe') {
return $link->template ?? null;
}
}
return null;
}
}