diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 0f47146eb7..07a5eaad3e 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -1591,9 +1591,9 @@ class GServer $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco` FROM `gserver` WHERE NOT `failed` - AND `poco` != '' + AND `directory-type` != ? AND `last_poco_query` < ? - ORDER BY RAND()", $last_update + ORDER BY RAND()", self::DT_NONE, $last_update ); while ($gserver = DBA::fetch($gservers)) { @@ -1604,9 +1604,15 @@ class GServer continue; } + Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]); + Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']); + Logger::info('Update directory', ['server' => $gserver['url'], 'id' => $gserver['id']]); Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver); + $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; + DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]); + if (--$no_of_queries == 0) { break; } diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index cfc140d66d..8109ef2373 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -330,18 +330,11 @@ class PortableContact } } - $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; - DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]); - return true; } else { // If the server hadn't replied correctly, then force a sanity check GServer::check($server["url"], $server["network"], true); - // If we couldn't reach the server, we will try it some time later - $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; - DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]); - return false; } } diff --git a/src/Worker/UpdateServerPeers.php b/src/Worker/UpdateServerPeers.php new file mode 100644 index 0000000000..ff0cdfa730 --- /dev/null +++ b/src/Worker/UpdateServerPeers.php @@ -0,0 +1,66 @@ +. + * + */ + +namespace Friendica\Worker; + +use Friendica\Core\Logger; +use Friendica\Core\Worker; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Util\Strings; + +class UpdateServerPeers +{ + /** + * Query the given server for their known peers + * @param string $gserver Server URL + */ + public static function execute(string $url) + { + $ret = DI::httpRequest()->get($url . '/api/v1/instance/peers'); + if (!$ret->isSuccess() || empty($ret->getBody())) { + Logger::info('Server is not reachable or does not offer the "peers" endpoint', ['url' => $url]); + return; + } + + $peers = json_decode($ret->getBody()); + if (empty($peers) || !is_array($peers)) { + Logger::info('Server does not have any peers listed', ['url' => $url]); + return; + } + + Logger::info('Server peer update start', ['url' => $url]); + + $total = 0; + $added = 0; + foreach ($peers as $peer) { + ++$total; + if (DBA::exists('gserver', ['nurl' => Strings::normaliseLink('http://' . $peer)])) { + // We already know this server + continue; + } + // This endpoint doesn't offer the schema. So we assume that it is HTTPS. + Worker::add(PRIORITY_LOW, 'UpdateGServer', 'https://' . $peer); + ++$added; + } + Logger::info('Server peer update ended', ['total' => $total, 'added' => $added, 'url' => $url]); + } +}