]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Relay: Avoid empty tags / Always use the "relay account"
[friendica.git] / src / Protocol / Diaspora.php
index 23bc575dd071ac8ce5d75df0bf9fabdc5a049c65..79e7c096378a01a056b6297bfeb6bccd42efeaaa 100644 (file)
@@ -82,15 +82,19 @@ class Diaspora
 
                        // All servers who wants content with this tag
                        $tagserverlist = [];
-                       $tagserver = dba::select('gserver-tag', ['gserver-id'], ['tag' => $taglist]);
-                       while ($server = dba::fetch($tagserver)) {
-                               $tagserverlist[] = $server['gserver-id'];
+                       if (!empty($taglist)) {
+                               $tagserver = dba::select('gserver-tag', ['gserver-id'], ['tag' => $taglist]);
+                               while ($server = dba::fetch($tagserver)) {
+                                       $tagserverlist[] = $server['gserver-id'];
+                               }
                        }
 
                        // All adresses with the given id
-                       $servers = dba::select('gserver', ['url'], ['relay-subscribe' => true, 'relay-scope' => 'tags', 'id' => $tagserverlist]);
-                       while ($server = dba::fetch($servers)) {
-                               $serverlist[$server['url']] = $server['url'];
+                       if (!empty($tagserverlist)) {
+                               $servers = dba::select('gserver', ['url'], ['relay-subscribe' => true, 'relay-scope' => 'tags', 'id' => $tagserverlist]);
+                               while ($server = dba::fetch($servers)) {
+                                       $serverlist[$server['url']] = $server['url'];
+                               }
                        }
                }
 
@@ -99,7 +103,10 @@ class Diaspora
                foreach ($serverlist as $server_url) {
                        // We don't send messages to ourselves
                        if (!link_compare($server_url, System::baseUrl())) {
-                               $contacts[] = self::getRelayContactId($server_url);
+                               $cid = self::getRelayContactId($server_url);
+                               if (!is_bool($cid)) {
+                                       $contacts[] = $cid;
+                               }
                        }
                }
 
@@ -116,11 +123,30 @@ class Diaspora
        {
                $batch = $server_url . '/receive/public';
 
-               $fields = ['batch', 'id', 'name', 'network'];
+               $fields = ['batch', 'id', 'name', 'network', 'archive', 'blocked'];
+
+               // Fetch the relay contact
                $condition = ['uid' => 0, 'network' => NETWORK_DIASPORA, 'batch' => $batch,
-                               'archive' => false, 'blocked' => false];
+                       'contact-type' => ACCOUNT_TYPE_RELAY];
                $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 ($contact['archive'] || $contact['blocked']) {
+                               return false;
+                       }
                        return $contact;
                } else {
                        $fields = ['uid' => 0, 'created' => DateTimeFormat::utcNow(),
@@ -128,6 +154,7 @@ class Diaspora
                                '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);
 
@@ -3205,7 +3232,7 @@ class Diaspora
         *
         * @return string The message that will be transmitted to other servers
         */
-       private static function buildMessage($msg, $user, $contact, $prvkey, $pubkey, $public = false)
+       public static function buildMessage($msg, $user, $contact, $prvkey, $pubkey, $public = false)
        {
                // The message is put into an envelope with the sender's signature
                $envelope = self::buildMagicEnvelope($msg, $user);
@@ -3260,13 +3287,15 @@ class Diaspora
 
                $logid = random_string(4);
 
+               $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
+
                // We always try to use the data from the fcontact table.
                // This is important for transmitting data to Friendica servers.
-               if (!empty($contact['addr']) && ($contact['network'] != NETWORK_DIASPORA)) {
+               if (!empty($contact['addr'])) {
                        $fcontact = self::personByHandle($contact['addr']);
-                       $dest_url = ($public_batch ? $fcontact["batch"] : $fcontact["notify"]);
-               } else {
-                       $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
+                       if (!empty($fcontact)) {
+                               $dest_url = ($public_batch ? $fcontact["batch"] : $fcontact["notify"]);
+                       }
                }
 
                if (!$dest_url) {
@@ -3293,7 +3322,7 @@ class Diaspora
                logger("transmit: ".$logid."-".$guid." returns: ".$return_code);
 
                if (!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) {
-                       if (!$no_queue) {
+                       if (!$no_queue && ($contact['contact-type'] != ACCOUNT_TYPE_RELAY)) {
                                logger("queue message");
                                // queue message for redelivery
                                Queue::add($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch, $guid);