]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Delivery.php
Merge pull request #5780 from annando/fix-relocation
[friendica.git] / src / Worker / Delivery.php
index a996f85feed6b4d725ab999f280fbd31327d2465..3a93d92f7b37366047dfc551e4c792081c9fb65e 100644 (file)
@@ -7,6 +7,7 @@ namespace Friendica\Worker;
 use Friendica\BaseObject;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
@@ -59,7 +60,7 @@ class Delivery extends BaseObject
                        }
                        $parent_id = intval($item['parent']);
 
-                       $condition = ['id' => [$item_id, $parent_id], 'visible' => true, 'moderated' => false];
+                       $condition = ['id' => [$item_id, $parent_id], 'moderated' => false];
                        $params = ['order' => ['id']];
                        $itemdata = Item::select([], $condition, $params);
 
@@ -75,6 +76,16 @@ class Delivery extends BaseObject
                        }
                        DBA::close($itemdata);
 
+                       if (empty($target_item)) {
+                               logger('Item ' . $item_id . "wasn't found. Quitting here.");
+                               return;
+                       }
+
+                       if (empty($parent)) {
+                               logger('Parent ' . $parent_id . ' for item ' . $item_id . "wasn't found. Quitting here.");
+                               return;
+                       }
+
                        $uid = $target_item['contact-uid'];
 
                        // avoid race condition with deleting entries
@@ -146,23 +157,23 @@ class Delivery extends BaseObject
 
                // Transmit via Diaspora if the thread had started as Diaspora post
                // This is done since the uri wouldn't match (Diaspora doesn't transmit it)
-               if (isset($parent) && ($parent['network'] == NETWORK_DIASPORA) && ($contact['network'] == NETWORK_DFRN)) {
-                       $contact['network'] = NETWORK_DIASPORA;
+               if (isset($parent) && ($parent['network'] == Protocol::DIASPORA) && ($contact['network'] == Protocol::DFRN)) {
+                       $contact['network'] = Protocol::DIASPORA;
                }
 
                logger("Delivering " . $cmd . " followup=$followup - via network " . $contact['network']);
 
                switch ($contact['network']) {
 
-                       case NETWORK_DFRN:
+                       case Protocol::DFRN:
                                self::deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
                                break;
 
-                       case NETWORK_DIASPORA:
+                       case Protocol::DIASPORA:
                                self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
                                break;
 
-                       case NETWORK_OSTATUS:
+                       case Protocol::OSTATUS:
                                // Do not send to otatus if we are not configured to send to public networks
                                if ($owner['prvnets']) {
                                        break;
@@ -175,7 +186,7 @@ class Delivery extends BaseObject
                                // This is done in "notifier.php" (See "url_recipients" and "push_notify")
                                break;
 
-                       case NETWORK_MAIL:
+                       case Protocol::MAIL:
                                self::deliverMail($cmd, $contact, $owner, $target_item);
                                break;
 
@@ -252,23 +263,12 @@ class Delivery extends BaseObject
                                }
                        }
 
-                       // We now have some contact, so we fetch it
-                       $target_importer = DBA::fetchFirst("SELECT *, `name` as `senderName`
-                                                       FROM `contact`
-                                                       WHERE NOT `blocked` AND `id` = ? LIMIT 1",
-                                                       $cid);
-
-                       // This should never fail
-                       if (!DBA::isResult($target_importer)) {
+                       $target_importer = DFRN::getImporter($cid, $target_uid);
+                       if (empty($target_importer)) {
+                               // This should never happen
                                return;
                        }
 
-                       $user = DBA::selectFirst('user', [], ['uid' => $target_uid]);
-
-                       $target_importer = array_merge($target_importer, $user);
-
-                       // Set the user id. This is important if this is a public contact
-                       $target_importer['importer_uid']  = $target_uid;
                        DFRN::import($atom, $target_importer);
                        return;
                }
@@ -277,7 +277,7 @@ class Delivery extends BaseObject
                // Se we transmit with the new method and via Diaspora as a fallback
                if (!empty($items) && (($items[0]['uid'] == 0) || ($contact['uid'] == 0))) {
                        // Transmit in public if it's a relay post
-                       $public_dfrn = ($contact['contact-type'] == ACCOUNT_TYPE_RELAY);
+                       $public_dfrn = ($contact['contact-type'] == Contact::ACCOUNT_TYPE_RELAY);
 
                        $deliver_status = DFRN::transmit($owner, $contact, $atom, $public_dfrn);
 
@@ -292,15 +292,17 @@ class Delivery extends BaseObject
                                self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
                                return;
                        }
-               } else {
+               } elseif ($cmd != self::RELOCATION) {
                        $deliver_status = DFRN::deliver($owner, $contact, $atom);
+               } else {
+                       $deliver_status = DFRN::deliver($owner, $contact, $atom, false, true);
                }
 
                logger('Delivery to ' . $contact["url"] . ' with guid ' . $target_item["guid"] . ' returns ' . $deliver_status);
 
                if ($deliver_status < 0) {
                        logger('Delivery failed: queuing message ' . $target_item["guid"] );
-                       Queue::add($contact['id'], NETWORK_DFRN, $atom, false, $target_item['guid']);
+                       Queue::add($contact['id'], Protocol::DFRN, $atom, false, $target_item['guid']);
                }
 
                if (($deliver_status >= 200) && ($deliver_status <= 299)) {
@@ -331,7 +333,7 @@ class Delivery extends BaseObject
        private static function deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
        {
                // We don't treat Forum posts as "wall-to-wall" to be able to post them via Diaspora
-               $walltowall = $top_level && ($owner['id'] != $items[0]['contact-id']) & ($owner['account-type'] != ACCOUNT_TYPE_COMMUNITY);
+               $walltowall = $top_level && ($owner['id'] != $items[0]['contact-id']) & ($owner['account-type'] != Contact::ACCOUNT_TYPE_COMMUNITY);
 
                if ($public_message) {
                        $loc = 'public batch ' . $contact['batch'];
@@ -444,25 +446,30 @@ class Delivery extends BaseObject
                        if (($target_item["thr-parent"] != "") && ($target_item["thr-parent"] != $target_item["parent-uri"])) {
                                $headers .= " <".Email::iri2msgid($target_item["thr-parent"]).">";
                        }
+
                        $headers .= "\n";
 
                        if (empty($target_item['title'])) {
                                $condition = ['uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
                                $title = Item::selectFirst(['title'], $condition);
+
                                if (DBA::isResult($title) && ($title['title'] != '')) {
                                        $subject = $title['title'];
                                } else {
                                        $condition = ['parent-uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
                                        $title = Item::selectFirst(['title'], $condition);
+
                                        if (DBA::isResult($title) && ($title['title'] != '')) {
                                                $subject = $title['title'];
                                        }
                                }
                        }
+
                        if (strncasecmp($subject, 'RE:', 3)) {
                                $subject = 'Re: ' . $subject;
                        }
                }
+
                Email::send($addr, $subject, $headers, $target_item);
        }
 }