]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
A server is only reachable when the network can be detected
[friendica.git] / src / Model / GServer.php
index e93c347627d5bd269f2b6a72418c71a50ab95acc..b0a9dd5c9a590210d233d2161932df2f6239e2f8 100644 (file)
@@ -167,27 +167,43 @@ class GServer
        /**
         * Checks if the given server is reachable
         *
-        * @param string  $profile URL of the given profile
-        * @param string  $server  URL of the given server (If empty, taken from profile)
-        * @param string  $network Network value that is used, when detection failed
-        * @param boolean $force   Force an update.
+        * @param array $contact Contact that should be checked
         *
         * @return boolean 'true' if server seems vital
         */
-       public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
+       public static function reachable(array $contact): bool
        {
-               if ($server == '') {
-                       $contact = Contact::getByURL($profile, null, ['baseurl']);
-                       if (!empty($contact['baseurl'])) {
-                               $server = $contact['baseurl'];
-                       }
+               if (!empty($contact['gsid'])) {
+                       $gsid = $contact['gsid'];
+               } elseif (!empty($contact['baseurl'])) {
+                       $server = $contact['baseurl'];
+               } elseif ($contact['network'] == Protocol::DIASPORA) {
+                       $parts = parse_url($contact['url']);
+                       unset($parts['path']);
+                       $server = (string)Uri::fromParts($parts);
+               } else {
+                       return true;
                }
 
-               if ($server == '') {
-                       return true;
+               if (!empty($gsid)) {
+                       $condition = ['id' => $gsid];
+               } else {
+                       $condition = ['nurl' => Strings::normaliseLink($server)];
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], $condition);
+               if (empty($gserver)) {
+                       $reachable = true;
+               } else {
+                       $reachable = !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
+                       $server    = $gserver['url'];
+               }
+
+               if (!empty($server) && (empty($gserver) || strtotime($gserver['next_contact']) < time())) {
+                       Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $server, false);
                }
 
-               return self::check($server, $network, $force);
+               return $reachable;
        }
 
        /**