]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Merge pull request #9586 from annando/inbox-status
[friendica.git] / src / Model / GServer.php
index 07a5eaad3ec93374362c155bbfb1ff12555d25b4..713d6f114601d901b53036b7a46ade4b83805a0e 100644 (file)
@@ -27,12 +27,12 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
+use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Module\Register;
 use Friendica\Network\CurlResult;
-use Friendica\Protocol\Diaspora;
-use Friendica\Protocol\PortableContact;
+use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
@@ -111,7 +111,10 @@ class GServer
        public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false)
        {
                if ($server == '') {
-                       $server = GContact::getBasepath($profile);
+                       $contact = Contact::getByURL($profile, null, ['baseurl']);
+                       if (!empty($contact['baseurl'])) {
+                               $server = $contact['baseurl'];
+                       }
                }
 
                if ($server == '') {
@@ -309,7 +312,7 @@ class GServer
 
                // When a nodeinfo is present, we don't need to dig further
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpRequest()->get($url . '/.well-known/nodeinfo', false, ['timeout' => $xrd_timeout]);
+               $curlResult = DI::httpRequest()->get($url . '/.well-known/nodeinfo', ['timeout' => $xrd_timeout]);
                if ($curlResult->isTimeout()) {
                        self::setFailure($url);
                        return false;
@@ -342,7 +345,7 @@ class GServer
                                        $basedata = ['detection-method' => self::DETECT_MANUAL];
                                }
 
-                               $curlResult = DI::httpRequest()->get($baseurl, false, ['timeout' => $xrd_timeout]);
+                               $curlResult = DI::httpRequest()->get($baseurl, ['timeout' => $xrd_timeout]);
                                if ($curlResult->isSuccess()) {
                                        $basedata = self::analyseRootHeader($curlResult, $basedata);
                                        $basedata = self::analyseRootBody($curlResult, $basedata, $baseurl);
@@ -359,7 +362,7 @@ class GServer
                                        // When the base path doesn't seem to contain a social network we try the complete path.
                                        // Most detectable system have to be installed in the root directory.
                                        // We checked the base to avoid false positives.
-                                       $curlResult = DI::httpRequest()->get($url, false, ['timeout' => $xrd_timeout]);
+                                       $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout]);
                                        if ($curlResult->isSuccess()) {
                                                $urldata = self::analyseRootHeader($curlResult, $serverdata);
                                                $urldata = self::analyseRootBody($curlResult, $urldata, $url);
@@ -471,10 +474,9 @@ class GServer
                }
 
                if (!empty($serverdata['network']) && !empty($id) && ($serverdata['network'] != Protocol::PHANTOM)) {
-                       $gcontacts = DBA::count('gcontact', ['gsid' => $id]);
                        $apcontacts = DBA::count('apcontact', ['gsid' => $id]);
                        $contacts = DBA::count('contact', ['uid' => 0, 'gsid' => $id]);
-                       $max_users = max($gcontacts, $apcontacts, $contacts, $registeredUsers);
+                       $max_users = max($apcontacts, $contacts, $registeredUsers);
                        if ($max_users > $registeredUsers) {
                                Logger::info('Update registered users', ['id' => $id, 'url' => $serverdata['nurl'], 'registered-users' => $max_users]);
                                DBA::update('gserver', ['registered-users' => $max_users], ['id' => $id]);
@@ -517,7 +519,7 @@ class GServer
                        $data['tags'] = [];
                }
 
-               $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
+               $gserver = DBA::selectFirst('gserver', ['id', 'url', 'network', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
                if (!DBA::isResult($gserver)) {
                        return;
                }
@@ -540,7 +542,7 @@ class GServer
                        }
 
                        foreach ($tags as $tag) {
-                               DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true);
+                               DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], Database::INSERT_IGNORE);
                        }
                }
 
@@ -566,8 +568,22 @@ class GServer
                                        $fields['batch'] = $data['protocols']['dfrn'];
                                }
                        }
+
+                       if (isset($data['protocols']['activitypub'])) {
+                               $fields['network'] = Protocol::ACTIVITYPUB;
+
+                               if (!empty($data['protocols']['activitypub']['actor'])) {
+                                       $fields['url'] = $data['protocols']['activitypub']['actor'];
+                               }
+                               if (!empty($data['protocols']['activitypub']['receive'])) {
+                                       $fields['batch'] = $data['protocols']['activitypub']['receive'];
+                               }
+                       }
                }
-               Diaspora::setRelayContact($server_url, $fields);
+
+               Logger::info('Discovery ended', ['server' => $server_url, 'data' => $fields]);
+
+               Relay::updateContact($gserver, $fields);
        }
 
        /**
@@ -912,7 +928,7 @@ class GServer
        private static function validHostMeta(string $url)
        {
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpRequest()->get($url . '/.well-known/host-meta', false, ['timeout' => $xrd_timeout]);
+               $curlResult = DI::httpRequest()->get($url . '/.well-known/host-meta', ['timeout' => $xrd_timeout]);
                if (!$curlResult->isSuccess()) {
                        return false;
                }
@@ -959,12 +975,6 @@ class GServer
        {
                $contacts = [];
 
-               $gcontacts = DBA::select('gcontact', ['url', 'nurl'], ['server_url' => [$url, $serverdata['nurl']]]);
-               while ($gcontact = DBA::fetch($gcontacts)) {
-                       $contacts[$gcontact['nurl']] = $gcontact['url'];
-               }
-               DBA::close($gcontacts);
-
                $apcontacts = DBA::select('apcontact', ['url'], ['baseurl' => [$url, $serverdata['nurl']]]);
                while ($apcontact = DBA::fetch($apcontacts)) {
                        $contacts[Strings::normaliseLink($apcontact['url'])] = $apcontact['url'];
@@ -1556,20 +1566,6 @@ class GServer
                return !strpos($body, '>');
        }
 
-       /**
-        * 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']);
-               }
-       }
-
        /**
         * Update GServer entries
         */
@@ -1588,7 +1584,7 @@ class GServer
 
                $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
 
-               $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`
+               $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`, `directory-type`
                        FROM `gserver`
                        WHERE NOT `failed`
                        AND `directory-type` != ?
@@ -1658,7 +1654,7 @@ class GServer
                if (!empty($accesstoken)) {
                        $api = 'https://instances.social/api/1.0/instances/list?count=0';
                        $header = ['Authorization: Bearer '.$accesstoken];
-                       $curlResult = DI::httpRequest()->get($api, false, ['headers' => $header]);
+                       $curlResult = DI::httpRequest()->get($api, ['header' => $header]);
 
                        if ($curlResult->isSuccess()) {
                                $servers = json_decode($curlResult->getBody(), true);