Add poco retrieval during server poll

- Closes https://github.com/friendica/friendica-directory/issues/7
This commit is contained in:
Hypolite Petovan 2018-11-18 10:36:03 -05:00
parent 26b4299b68
commit 012a3f9f8d
1 changed files with 46 additions and 0 deletions

View File

@ -2,6 +2,8 @@
namespace Friendica\Directory\Pollers;
use ByJG\Util\WebRequest;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
@ -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);
}
}
}
}
}
}