Merge pull request #4691 from annando/relais-discovery

Added server discovery for their relais configuration
This commit is contained in:
Hypolite Petovan 2018-03-27 08:50:27 -04:00 committed by GitHub
commit 27155fead7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 4 deletions

View file

@ -41,7 +41,7 @@ define('FRIENDICA_PLATFORM', 'Friendica');
define('FRIENDICA_CODENAME', 'The Tazmans Flax-lily');
define('FRIENDICA_VERSION', '2018-05-dev');
define('DFRN_PROTOCOL_VERSION', '2.23');
define('DB_UPDATE_VERSION', 1258);
define('DB_UPDATE_VERSION', 1259);
define('NEW_UPDATE_ROUTINE_VERSION', 1170);
/**

View file

@ -1,6 +1,6 @@
-- ------------------------------------------
-- Friendica 2018-05-dev (The Tazmans Flax-lily)
-- DB_UPDATE_VERSION 1258
-- DB_UPDATE_VERSION 1259
-- ------------------------------------------
@ -397,6 +397,8 @@ CREATE TABLE IF NOT EXISTS `gserver` (
`noscrape` varchar(255) NOT NULL DEFAULT '' COMMENT '',
`network` char(4) NOT NULL DEFAULT '' COMMENT '',
`platform` varchar(255) NOT NULL DEFAULT '' COMMENT '',
`relay-subscribe` boolean NOT NULL DEFAULT '0' COMMENT 'Has the server subscribed to the relay system',
`relay-scope` varchar(10) NOT NULL DEFAULT '' COMMENT 'The scope of messages that the server wants to get',
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`last_poco_query` datetime DEFAULT '0001-01-01 00:00:00' COMMENT '',
`last_contact` datetime DEFAULT '0001-01-01 00:00:00' COMMENT '',
@ -405,6 +407,16 @@ CREATE TABLE IF NOT EXISTS `gserver` (
UNIQUE INDEX `nurl` (`nurl`(190))
) DEFAULT COLLATE utf8mb4_general_ci;
--
-- TABLE gserver-tag
--
CREATE TABLE IF NOT EXISTS `gserver-tag` (
`gserver-id` int unsigned NOT NULL DEFAULT 0 COMMENT 'The id of the gserver',
`tag` varchar(100) NOT NULL DEFAULT '' COMMENT 'Tag that the server has subscribed',
PRIMARY KEY(`gserver-id`,`tag`),
INDEX `tag` (`tag`)
) DEFAULT COLLATE utf8mb4_general_ci;
--
-- TABLE hook
--

View file

@ -1074,6 +1074,8 @@ class DBStructure
"noscrape" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
"network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""],
"platform" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
"relay-subscribe" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Has the server subscribed to the relay system"],
"relay-scope" => ["type" => "varchar(10)", "not null" => "1", "default" => "", "comment" => "The scope of messages that the server wants to get"],
"created" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => ""],
"last_poco_query" => ["type" => "datetime", "default" => NULL_DATE, "comment" => ""],
"last_contact" => ["type" => "datetime", "default" => NULL_DATE, "comment" => ""],
@ -1084,6 +1086,17 @@ class DBStructure
"nurl" => ["UNIQUE", "nurl(190)"],
]
];
$database["gserver-tag"] = [
"comment" => "Tags that the server has subscribed",
"fields" => [
"gserver-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "relation" => ["gserver" => "id"], "primary" => "1", "comment" => "The id of the gserver"],
"tag" => ["type" => "varchar(100)", "not null" => "1", "default" => "", "primary" => "1", "comment" => "Tag that the server has subscribed"],
],
"indexes" => [
"PRIMARY" => ["gserver-id", "tag"],
"tag" => ["tag"],
]
];
$database["hook"] = [
"comment" => "addon hook registry",
"fields" => [

View file

@ -1377,11 +1377,48 @@ class PortableContact
$fields['created'] = DateTimeFormat::utcNow();
dba::insert('gserver', $fields);
}
if (!$failure && in_array($fields['network'], [NETWORK_DFRN, NETWORK_DIASPORA])) {
self::discoverRelay($server_url);
}
logger("End discovery for server " . $server_url, LOGGER_DEBUG);
return !$failure;
}
private static function discoverRelay($server_url)
{
logger("Discover relay data for server " . $server_url, LOGGER_DEBUG);
$serverret = Network::curl($server_url."/.well-known/x-social-relay");
if (!$serverret["success"]) {
return;
}
$data = json_decode($serverret['body']);
if (!is_object($data)) {
return;
}
$gserver = dba::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => normalise_link($server_url)]);
if (!DBM::is_result($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') {
foreach ($data->tags as $tag) {
dba::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag]);
}
}
}
/**
* @brief Returns a list of all known servers
* @return array List of server urls
@ -1463,8 +1500,8 @@ class PortableContact
$header = ['Authorization: Bearer '.$accesstoken];
$serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
if ($serverdata['success']) {
$servers = json_decode($serverdata['body']);
foreach ($servers->instances as $server) {
$servers = json_decode($serverdata['body']);
foreach ($servers->instances as $server) {
$url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
}