Merge pull request #7996 from annando/poco-cleanup

The PoCo-Workers had been split
This commit is contained in:
Hypolite Petovan 2019-12-24 05:13:36 -05:00 committed by GitHub
commit 94954c810b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 483 additions and 456 deletions

View file

@ -580,9 +580,6 @@ class GContact
public static function getId($contact)
{
$gcontact_id = 0;
$doprobing = false;
$last_failure_str = '';
$last_contact_str = '';
if (empty($contact['network'])) {
Logger::notice('Empty network', ['url' => $contact['url'], 'callstack' => System::callstack()]);
@ -613,15 +610,6 @@ class GContact
$gcnt = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($contact['url'])]);
if (DBA::isResult($gcnt)) {
$gcontact_id = $gcnt['id'];
// Update every 90 days
if (empty($gcnt['network']) || in_array($gcnt['network'], Protocol::FEDERATED)) {
$last_failure_str = $gcnt['last_failure'];
$last_failure = strtotime($gcnt['last_failure']);
$last_contact_str = $gcnt['last_contact'];
$last_contact = strtotime($gcnt['last_contact']);
$doprobing = (((time() - $last_contact) > (90 * 86400)) && ((time() - $last_failure) > (90 * 86400)));
}
} else {
$contact['location'] = $contact['location'] ?? '';
$contact['about'] = $contact['about'] ?? '';
@ -638,16 +626,10 @@ class GContact
$cnt = DBA::selectFirst('gcontact', ['id', 'network'], $condition, ['order' => ['id']]);
if (DBA::isResult($cnt)) {
$gcontact_id = $cnt['id'];
$doprobing = (empty($cnt['network']) || in_array($cnt['network'], Protocol::FEDERATED));
}
}
DBA::unlock();
if ($doprobing) {
Logger::notice('Probing', ['contact' => $last_contact_str, "failure" => $last_failure_str, "checking" => $contact['url']]);
Worker::add(PRIORITY_LOW, 'GProbe', $contact['url']);
}
return $gcontact_id;
}
@ -801,7 +783,7 @@ class GContact
return;
}
if (!$force && !PortableContact::updateNeeded($gcontact['created'], $gcontact['updated'], $gcontact['last_failure'], $gcontact['last_contact'])) {
if (!$force && !GServer::updateNeeded($gcontact['created'], $gcontact['updated'], $gcontact['last_failure'], $gcontact['last_contact'])) {
Logger::info("Don't update profile", ['url' => $data['url'], 'updated' => $gcontact['updated']]);
return;
}

View file

@ -26,6 +26,10 @@ use Friendica\Network\Probe;
*/
class GServer
{
// Directory types
const DT_NONE = 0;
const DT_POCO = 1;
const DT_MASTODON = 2;
/**
* Checks if the given server is reachable
*
@ -49,6 +53,61 @@ class GServer
return self::check($server, $network, $force);
}
/**
* Decides if a server needs to be updated, based upon several date fields
*
* @param date $created Creation date of that server entry
* @param date $updated When had the server entry be updated
* @param date $last_failure Last failure when contacting that server
* @param date $last_contact Last time the server had been contacted
*
* @return boolean Does the server record needs an update?
*/
public static function updateNeeded($created, $updated, $last_failure, $last_contact)
{
$now = strtotime(DateTimeFormat::utcNow());
if ($updated > $last_contact) {
$contact_time = strtotime($updated);
} else {
$contact_time = strtotime($last_contact);
}
$failure_time = strtotime($last_failure);
$created_time = strtotime($created);
// If there is no "created" time then use the current time
if ($created_time <= 0) {
$created_time = $now;
}
// If the last contact was less than 24 hours then don't update
if (($now - $contact_time) < (60 * 60 * 24)) {
return false;
}
// If the last failure was less than 24 hours then don't update
if (($now - $failure_time) < (60 * 60 * 24)) {
return false;
}
// If the last contact was less than a week ago and the last failure is older than a week then don't update
//if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
// return false;
// If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
return false;
}
// If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
return false;
}
return true;
}
/**
* Checks the state of the given server.
*
@ -89,7 +148,7 @@ class GServer
$last_failure = DBA::NULL_DATETIME;
}
if (!$force && !PortableContact::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
if (!$force && !self::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
Logger::info('No update needed', ['server' => $server_url]);
return ($last_contact >= $last_failure);
}
@ -112,8 +171,24 @@ class GServer
*/
public static function detect(string $url, string $network = '')
{
Logger::info('Detect server type', ['server' => $url]);
$serverdata = [];
$original_url = $url;
// Remove URL content that is not supposed to exist for a server url
$urlparts = parse_url($url);
unset($urlparts['user']);
unset($urlparts['pass']);
unset($urlparts['query']);
unset($urlparts['fragment']);
$url = Network::unparseURL($urlparts);
// If the URL missmatches, then we mark the old entry as failure
if ($url != $original_url) {
DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($original_url)]);
}
// When a nodeinfo is present, we don't need to dig further
$xrd_timeout = Config::get('system', 'xrd_timeout');
$curlResult = Network::curl($url . '/.well-known/nodeinfo', false, ['timeout' => $xrd_timeout]);
@ -183,7 +258,10 @@ class GServer
$serverdata = $nodeinfo;
}
// Detect the directory type
$serverdata['directory-type'] = self::DT_NONE;
$serverdata = self::checkPoCo($url, $serverdata);
$serverdata = self::checkMastodonDirectory($url, $serverdata);
// We can't detect the network type. Possibly it is some system that we don't know yet
if (empty($serverdata['network'])) {
@ -740,6 +818,8 @@ class GServer
*/
private static function checkPoCo(string $url, array $serverdata)
{
$serverdata['poco'] = '';
$curlResult = Network::curl($url. '/poco');
if (!$curlResult->isSuccess()) {
return $serverdata;
@ -753,9 +833,35 @@ class GServer
if (!empty($data['totalResults'])) {
$registeredUsers = $serverdata['registered-users'] ?? 0;
$serverdata['registered-users'] = max($data['totalResults'], $registeredUsers);
$serverdata['directory-type'] = self::DT_POCO;
$serverdata['poco'] = $url . '/poco';
} else {
$serverdata['poco'] = '';
}
return $serverdata;
}
/**
* Checks if the given server does have a Mastodon style directory endpoint.
*
* @param string $url URL of the given server
* @param array $serverdata array with server data
*
* @return array server data
*/
public static function checkMastodonDirectory(string $url, array $serverdata)
{
$curlResult = Network::curl($url . '/api/v1/directory?limit=1');
if (!$curlResult->isSuccess()) {
return $serverdata;
}
$data = json_decode($curlResult->getBody(), true);
if (empty($data)) {
return $serverdata;
}
if (count($data) == 1) {
$serverdata['directory-type'] = self::DT_MASTODON;
}
return $serverdata;
@ -1184,4 +1290,18 @@ class GServer
}
return $serverdata;
}
/**
* Update the user directory of a given gserver record
*
* @param array $gserver gserver record
*/
public static function updateDirectory(array $gserver)
{
/// @todo Add Mastodon API directory
if (!empty($gserver['poco'])) {
PortableContact::discoverSingleServer($gserver['id']);
}
}
}

View file

@ -1074,8 +1074,6 @@ class Profile
Logger::log('Not authenticated. Invoking reverse magic-auth for ' . $my_url, Logger::DEBUG);
Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
// Remove the "addr" parameter from the destination. It is later added as separate parameter again.
$addr_request = 'addr=' . urlencode($addr);
$query = rtrim(str_replace($addr_request, '', $a->query_string), '?&');