From 012a3f9f8dccd60b4c7decaf06668363cdbf6791 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 18 Nov 2018 10:36:03 -0500 Subject: [PATCH] Add poco retrieval during server poll - Closes https://github.com/friendica/friendica-directory/issues/7 --- src/classes/Pollers/Server.php | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/classes/Pollers/Server.php b/src/classes/Pollers/Server.php index efeadff..1b96264 100644 --- a/src/classes/Pollers/Server.php +++ b/src/classes/Pollers/Server.php @@ -2,6 +2,8 @@ namespace Friendica\Directory\Pollers; +use ByJG\Util\WebRequest; + /** * @author Hypolite Petovan */ @@ -179,6 +181,8 @@ class Server $result = $this->profilePollQueueModel->add($probe_result['data']['admin']['profile']); $this->logger->debug('Profile queue add URL: ' . $probe_result['data']['admin']['profile'] . ' - ' . $result); } + + $this->discoverPoco($base_url); } if ($server) { @@ -371,4 +375,46 @@ class Server return max(min($max_health, $original_health + $delta), -100); } + + function discoverPoco($base_url): void + { + $pocoUrl = $base_url . '/poco'; + + $webrequest = new WebRequest($pocoUrl); + $pocoJsonData = $webrequest->get(['fields' => 'urls', 'count' => 1000]); + + $this->logger->debug('WebRequest: ' . $webrequest->getLastFetchedUrl() . ' Status: ' . $webrequest->getLastStatus()); + + if ($webrequest->getLastStatus() != 200) { + $this->logger->info('Unsuccessful poco request: ' . $webrequest->getLastFetchedUrl()); + return; + } + + try { + $pocoFetchData = json_decode($pocoJsonData); + } catch (\Throwable $e) { + $this->logger->notice('Invalid JSON string for PoCo URL: ' . $webrequest->getLastFetchedUrl()); + return; + } + + if (!isset($pocoFetchData->entry)) { + $this->logger->notice('Invalid JSON structure for PoCo URL: ' . $webrequest->getLastFetchedUrl()); + return; + } + + foreach($pocoFetchData->entry as $entry) { + if (empty($entry->urls)) { + continue; + } + + foreach ($entry->urls as $url) { + if (!empty($url->type) && !empty($url->value) && $url->type == 'profile') { + $result = $this->profilePollQueueModel->add($url->value); + if ($result === 0) { + $this->logger->info('Discovered profile URL ' . $url->value); + } + } + } + } + } }