From e65bd0325eb35c96138fe3339721a385dae9942a Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 26 Mar 2018 05:44:53 +0000 Subject: [PATCH] Added server discovery for their relais configuration --- boot.php | 2 +- database.sql | 14 +++++++++++- src/Database/DBStructure.php | 13 +++++++++++ src/Protocol/PortableContact.php | 37 ++++++++++++++++++++++++++++++-- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/boot.php b/boot.php index 71050fe4d7..e3b94a8c9a 100644 --- a/boot.php +++ b/boot.php @@ -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); /** diff --git a/database.sql b/database.sql index e8662f95c8..aa87247db3 100644 --- a/database.sql +++ b/database.sql @@ -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 -- diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 5d527aac60..67c8d7b8a6 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -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" => [ diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index ff2b2fca61..c7f58b9fb2 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -1377,11 +1377,44 @@ class PortableContact $fields['created'] = DateTimeFormat::utcNow(); dba::insert('gserver', $fields); } + + if (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'], ['nurl' => normalise_link($server_url)]); + if (!DBM::is_result($gserver)) { + return; + } + + $fields = ['relay-subscribe' => $data->subscribe, 'relay-scope' => $data->scope]; + dba::update('gserver', $fields, ['id' => $gserver['id']]); + + dba::delete('gserver-tag', ['gserver-id' => $gserver['id']]); + 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 +1496,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); }