Update or create relay contact from discovery / process new protocol values

This commit is contained in:
Michael 2018-04-30 05:33:47 +00:00
parent aa4945f4a0
commit d0dd5c44d9
2 changed files with 44 additions and 26 deletions

View file

@ -134,55 +134,58 @@ class Diaspora
*/ */
private static function getRelayContact($server_url) private static function getRelayContact($server_url)
{ {
$batch = $server_url . '/receive/public';
$fields = ['batch', 'id', 'name', 'network', 'archive', 'blocked']; $fields = ['batch', 'id', 'name', 'network', 'archive', 'blocked'];
// Fetch the relay contact // Fetch the relay contact
$condition = ['uid' => 0, 'network' => NETWORK_DIASPORA, 'batch' => $batch, $condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
'contact-type' => ACCOUNT_TYPE_RELAY]; 'contact-type' => ACCOUNT_TYPE_RELAY];
$contact = dba::selectFirst('contact', $fields, $condition); $contact = dba::selectFirst('contact', $fields, $condition);
// If there is nothing found, we check if there is some unmarked relay
// This code segment can be removed before the release 2018-05
if (!DBM::is_result($contact)) {
$condition = ['uid' => 0, 'network' => NETWORK_DIASPORA, 'batch' => $batch,
'name' => 'relay', 'nick' => 'relay', 'url' => $server_url];
$contact = dba::selectFirst('contact', $fields, $condition);
if (DBM::is_result($contact)) {
// Mark the relay account as a relay account
$fields = ['contact-type' => ACCOUNT_TYPE_RELAY];
dba::update('contact', $fields, ['id' => $contact['id']]);
}
}
if (DBM::is_result($contact)) { if (DBM::is_result($contact)) {
if ($contact['archive'] || $contact['blocked']) { if ($contact['archive'] || $contact['blocked']) {
return false; return false;
} }
return $contact; return $contact;
} else { } else {
$fields = ['uid' => 0, 'created' => DateTimeFormat::utcNow(), self::setRelayContact($server_url);
'name' => 'relay', 'nick' => 'relay',
'url' => $server_url, 'nurl' => normalise_link($server_url),
'batch' => $batch, 'network' => NETWORK_DIASPORA,
'rel' => CONTACT_IS_FOLLOWER, 'blocked' => false,
'contact-type' => ACCOUNT_TYPE_RELAY,
'pending' => false, 'writable' => true];
dba::insert('contact', $fields);
$fields = ['batch', 'id', 'name', 'network'];
$contact = dba::selectFirst('contact', $fields, $condition); $contact = dba::selectFirst('contact', $fields, $condition);
if (DBM::is_result($contact)) { if (DBM::is_result($contact)) {
return $contact; return $contact;
} }
} }
// It should never happen that we arrive here // It should never happen that we arrive here
return []; return [];
} }
/**
* @brief Update or insert a relay contact
*
* @param string $server_url The url of the server
* @param array $network_fields Optional network specific fields
*/
public static function setRelayContact($server_url, $network_fields = [])
{
$fields = ['created' => DateTimeFormat::utcNow(),
'name' => 'relay', 'nick' => 'relay',
'url' => $server_url, 'network' => NETWORK_DIASPORA,
'batch' => $server_url . '/receive/public',
'rel' => CONTACT_IS_FOLLOWER, 'blocked' => false,
'pending' => false, 'writable' => true];
$fields = array_merge($fields, $network_fields);
$condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
'contact-type' => ACCOUNT_TYPE_RELAY];
if (dba::exists('contact', $condition)) {
unset($fields['created']);
}
dba::update('contact', $fields, $condition, true);
}
/** /**
* @brief Return a list of participating contacts for a thread * @brief Return a list of participating contacts for a thread
* *

View file

@ -18,6 +18,7 @@ use Friendica\Model\Profile;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Protocol\Diaspora;
use dba; use dba;
use DOMDocument; use DOMDocument;
use DOMXPath; use DOMXPath;
@ -1429,6 +1430,20 @@ class PortableContact
dba::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag]); dba::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag]);
} }
} }
// Create or update the relay contact
$fields = [];
if (isset($data->protocols)) {
if (isset($data->protocols->diaspora)) {
$fields['network'] = NETWORK_DIASPORA;
$fields['batch'] = $data->protocols->diaspora;
}
if (isset($data->protocols->dfrn)) {
$fields['network'] = NETWORK_DFRN;
$fields['batch'] = $data->protocols->dfrn;
}
}
Diaspora::setRelayContact($server_url, $fields);
} }
/** /**