Browse Source

Code transitioning from PortableContacts.php to GServer.php

tags/2019.12
Michael 1 year ago
parent
commit
d9a74bd5dd
6 changed files with 135 additions and 954 deletions
  1. +1
    -1
      src/Model/Contact.php
  2. +2
    -2
      src/Model/GContact.php
  3. +120
    -0
      src/Model/GServer.php
  4. +6
    -947
      src/Protocol/PortableContact.php
  5. +4
    -3
      src/Worker/DiscoverPoCo.php
  6. +2
    -1
      src/Worker/PubSubPublish.php

+ 1
- 1
src/Model/Contact.php View File

@@ -1481,7 +1481,7 @@ class Contact extends BaseObject
$data = Probe::uri($url, "", $uid);
// Ensure that there is a gserver entry
if (!empty($data['baseurl']) && ($data['network'] != Protocol::PHANTOM)) {
PortableContact::checkServer($data['baseurl']);
GServer::check($data['baseurl']);
}
}



+ 2
- 2
src/Model/GContact.php View File

@@ -231,7 +231,7 @@ class GContact
}

// The server URL doesn't seem to be valid, so we don't store it.
if (!PortableContact::checkServer($gcontact['server_url'], $gcontact['network'])) {
if (!GServer::check($gcontact['server_url'])) {
$gcontact['server_url'] = "";
}

@@ -541,7 +541,7 @@ class GContact
$j = json_decode($x);
if (!empty($j->entries)) {
foreach ($j->entries as $entry) {
PortableContact::checkServer($entry->url);
GServer::check($entry->url);

$url = $entry->url . '/poco';
if (!in_array($url, $done)) {


+ 120
- 0
src/Model/GServer.php View File

@@ -17,12 +17,55 @@ use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
use Friendica\Util\XML;
use Friendica\Core\Logger;
use Friendica\Protocol\PortableContact;
use Friendica\Protocol\Diaspora;

/**
* @brief This class handles GServer related functions
*/
class GServer
{
public static function check($server_url, $force = false)
{
// Unify the server address
$server_url = trim($server_url, '/');
$server_url = str_replace('/index.php', '', $server_url);

if ($server_url == '') {
return false;
}

$gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]);
if (DBA::isResult($gserver)) {
if ($gserver['created'] <= DBA::NULL_DATETIME) {
$fields = ['created' => DateTimeFormat::utcNow()];
$condition = ['nurl' => Strings::normaliseLink($server_url)];
DBA::update('gserver', $fields, $condition);
}

$last_contact = $gserver["last_contact"];
$last_failure = $gserver["last_failure"];

// See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
// It can happen that a zero date is in the database, but storing it again is forbidden.
if ($last_contact < DBA::NULL_DATETIME) {
$last_contact = DBA::NULL_DATETIME;
}

if ($last_failure < DBA::NULL_DATETIME) {
$last_failure = DBA::NULL_DATETIME;
}

if (!$force && !PortableContact::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
Logger::info('Use cached data', ['server' => $server_url]);
return ($last_contact >= $last_failure);
}
}
Logger::info('Server is outdated or unknown. Start discovery.', ['Server' => $server_url, 'Force' => $force, 'Created' => $gserver['created'], 'Failure' => $last_failure, 'Contact' => $last_contact]);

return self::detect($server_url);
}

/**
* Detect server data (type, protocol, version number, ...)
* The detected data is then updated or inserted in the gserver table.
@@ -133,9 +176,86 @@ class GServer
$ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]);
}

if (in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
self::discoverRelay($url);
}

return $ret;
}

/**
* @brief Fetch relay data from a given server url
*
* @param string $server_url address of the server
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function discoverRelay($server_url)
{
Logger::info('Discover relay data', ['server' => $server_url]);

$curlResult = Network::curl($server_url . '/.well-known/x-social-relay');
if (!$curlResult->isSuccess()) {
return;
}

$data = json_decode($curlResult->getBody(), true);
if (!is_array($data)) {
return;
}

$gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
if (!DBA::isResult($gserver)) {
return;
}

if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
$fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
DBA::update('gserver', $fields, ['id' => $gserver['id']]);
}

DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);

if ($data['scope'] == 'tags') {
// Avoid duplicates
$tags = [];
foreach ($data['tags'] as $tag) {
$tag = mb_strtolower($tag);
if (strlen($tag) < 100) {
$tags[$tag] = $tag;
}
}

foreach ($tags as $tag) {
DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true);
}
}

// Create or update the relay contact
$fields = [];
if (isset($data['protocols'])) {
if (isset($data['protocols']['diaspora'])) {
$fields['network'] = Protocol::DIASPORA;

if (isset($data['protocols']['diaspora']['receive'])) {
$fields['batch'] = $data['protocols']['diaspora']['receive'];
} elseif (is_string($data['protocols']['diaspora'])) {
$fields['batch'] = $data['protocols']['diaspora'];
}
}

if (isset($data['protocols']['dfrn'])) {
$fields['network'] = Protocol::DFRN;

if (isset($data['protocols']['dfrn']['receive'])) {
$fields['batch'] = $data['protocols']['dfrn']['receive'];
} elseif (is_string($data['protocols']['dfrn'])) {
$fields['batch'] = $data['protocols']['dfrn'];
}
}
}
Diaspora::setRelayContact($server_url, $fields);
}

private static function fetchStatistics($url)
{
$curlResult = Network::curl($url . '/statistics.json');


+ 6
- 947
src/Protocol/PortableContact.php
File diff suppressed because it is too large
View File


+ 4
- 3
src/Worker/DiscoverPoCo.php View File

@@ -12,6 +12,7 @@ use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Model\GContact;
use Friendica\Model\Contact;
use Friendica\Model\GServer;
use Friendica\Network\Probe;
use Friendica\Protocol\PortableContact;
use Friendica\Util\DateTimeFormat;
@@ -86,7 +87,7 @@ class DiscoverPoCo
return;
}
$result = "Checking server ".$server_url." - ";
$ret = PortableContact::checkServer($server_url);
$ret = GServer::check($server_url);
if ($ret) {
$result .= "success";
} else {
@@ -186,7 +187,7 @@ class DiscoverPoCo
$server_url = $user["server_url"];
}

if ((($server_url == "") && ($user["network"] == Protocol::FEED)) || $force_update || PortableContact::checkServer($server_url, $user["network"])) {
if ((($server_url == "") && ($user["network"] == Protocol::FEED)) || $force_update || GServer::check($server_url)) {
Logger::log('Check profile '.$user["url"]);
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "check_profile", $user["url"]);

@@ -237,7 +238,7 @@ class DiscoverPoCo

$server_url = Contact::getBasepath($jj->url);
if ($server_url != '') {
if (!PortableContact::checkServer($server_url)) {
if (!GServer::check($server_url)) {
Logger::log("Friendica server ".$server_url." doesn't answer.", Logger::DEBUG);
continue;
}


+ 2
- 1
src/Worker/PubSubPublish.php View File

@@ -9,6 +9,7 @@ use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model\PushSubscriber;
use Friendica\Model\GServer;
use Friendica\Protocol\OStatus;
use Friendica\Util\Network;

@@ -30,7 +31,7 @@ class PubSubPublish
return;
}

/// @todo Check server status with PortableContact::checkServer()
/// @todo Check server status with GServer::check()
// Before this can be done we need a way to safely detect the server url.

Logger::log("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], Logger::DEBUG);


Loading…
Cancel
Save