]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Delivery.php
Delivery of reshares
[friendica.git] / src / Worker / Delivery.php
index 97a0968510c5cf945cfd1d3f8915a5e90d7c86dc..e59ea37aad35a4d0bd7c678de53e512c0c327677 100644 (file)
@@ -15,6 +15,7 @@ use Friendica\Model;
 use Friendica\Protocol\DFRN;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\Email;
+use Friendica\Protocol\Activity;
 use Friendica\Util\Strings;
 use Friendica\Util\Network;
 use Friendica\Core\Worker;
@@ -33,7 +34,7 @@ class Delivery extends BaseObject
 
        public static function execute($cmd, $target_id, $contact_id)
        {
-               Logger::log('Invoked: ' . $cmd . ': ' . $target_id . ' to ' . $contact_id, Logger::DEBUG);
+               Logger::info('Invoked', ['cmd' => $cmd, 'target' => $target_id, 'contact' => $contact_id]);
 
                $top_level = false;
                $followup = false;
@@ -43,12 +44,14 @@ class Delivery extends BaseObject
                if ($cmd == self::MAIL) {
                        $target_item = DBA::selectFirst('mail', [], ['id' => $target_id]);
                        if (!DBA::isResult($target_item)) {
+                               self::setFailedQueue($cmd, $target_id);
                                return;
                        }
                        $uid = $target_item['uid'];
                } elseif ($cmd == self::SUGGESTION) {
                        $target_item = DBA::selectFirst('fsuggest', [], ['id' => $target_id]);
                        if (!DBA::isResult($target_item)) {
+                               self::setFailedQueue($cmd, $target_id);
                                return;
                        }
                        $uid = $target_item['uid'];
@@ -58,6 +61,7 @@ class Delivery extends BaseObject
                } else {
                        $item = Model\Item::selectFirst(['parent'], ['id' => $target_id]);
                        if (!DBA::isResult($item) || empty($item['parent'])) {
+                               self::setFailedQueue($cmd, $target_id);
                                return;
                        }
                        $parent_id = intval($item['parent']);
@@ -79,11 +83,13 @@ class Delivery extends BaseObject
 
                        if (empty($target_item)) {
                                Logger::log('Item ' . $target_id . "wasn't found. Quitting here.");
+                               self::setFailedQueue($cmd, $target_id);
                                return;
                        }
 
                        if (empty($parent)) {
                                Logger::log('Parent ' . $parent_id . ' for item ' . $target_id . "wasn't found. Quitting here.");
+                               self::setFailedQueue($cmd, $target_id);
                                return;
                        }
 
@@ -93,6 +99,21 @@ class Delivery extends BaseObject
                                $uid = $target_item['uid'];
                        } else {
                                Logger::log('Only public users for item ' . $target_id, Logger::DEBUG);
+                               self::setFailedQueue($cmd, $target_id);
+                               return;
+                       }
+
+                       $condition = ['uri' => $target_item['thr-parent'], 'uid' => $target_item['uid']];
+                       $thr_parent = Model\Item::selectFirst(['network', 'object'], $condition);
+                       if (!DBA::isResult($thr_parent)) {
+                               // Shouldn't happen. But when this does, we just take the parent as thread parent.
+                               // That's totally okay for what we use this variable here.
+                               $thr_parent = $parent;
+                       }
+
+                       if (!empty($contact_id) && Model\Contact::isArchived($contact_id)) {
+                               Logger::info('Contact is archived', ['id' => $contact_id, 'cmd' => $cmd, 'item' => $target_item['id']]);
+                               self::setFailedQueue($cmd, $target_id);
                                return;
                        }
 
@@ -152,6 +173,7 @@ class Delivery extends BaseObject
 
                $owner = Model\User::getOwnerDataById($uid);
                if (!DBA::isResult($owner)) {
+                       self::setFailedQueue($cmd, $target_id);
                        return;
                }
 
@@ -160,23 +182,25 @@ class Delivery extends BaseObject
                        ['id' => $contact_id, 'blocked' => false, 'pending' => false, 'self' => false]
                );
                if (!DBA::isResult($contact)) {
+                       self::setFailedQueue($cmd, $target_id);
                        return;
                }
 
                if (Network::isUrlBlocked($contact['url'])) {
+                       self::setFailedQueue($cmd, $target_id);
                        return;
                }
 
-               // Transmit via Diaspora if the thread had started as Diaspora post
+               // Transmit via Diaspora if the thread had started as Diaspora post.
+               // Also transmit via Diaspora if this is a direct answer to a Diaspora comment.
                // This is done since the uri wouldn't match (Diaspora doesn't transmit it)
-               if (isset($parent) && ($parent['network'] == Protocol::DIASPORA) && ($contact['network'] == Protocol::DFRN)) {
+               if (!empty($parent) && !empty($thr_parent) && in_array(Protocol::DIASPORA, [$parent['network'], $thr_parent['network']])) {
                        $contact['network'] = Protocol::DIASPORA;
                }
 
-               Logger::log("Delivering " . $cmd . " followup=$followup - via network " . $contact['network']);
+               Logger::notice('Delivering', ['cmd' => $cmd, 'target' => $target_id, 'followup' => $followup, 'network' => $contact['network']]);
 
                switch ($contact['network']) {
-
                        case Protocol::DFRN:
                                self::deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
                                break;
@@ -185,21 +209,8 @@ class Delivery extends BaseObject
                                self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
                                break;
 
-                       case Protocol::OSTATUS:
-                               // Do not send to otatus if we are not configured to send to public networks
-                               if ($owner['prvnets']) {
-                                       break;
-                               }
-                               if (Config::get('system','ostatus_disabled') || Config::get('system','dfrn_only')) {
-                                       break;
-                               }
-
-                               // There is currently no code here to distribute anything to OStatus.
-                               // This is done in "notifier.php" (See "url_recipients" and "push_notify")
-                               break;
-
                        case Protocol::MAIL:
-                               self::deliverMail($cmd, $contact, $owner, $target_item);
+                               self::deliverMail($cmd, $contact, $owner, $target_item, $thr_parent);
                                break;
 
                        default:
@@ -209,6 +220,21 @@ class Delivery extends BaseObject
                return;
        }
 
+       /**
+        * Increased the "failed" counter in the item delivery data
+        *
+        * @param string  $cmd Command
+        * @param integer $id  Item id
+        */
+       private static function setFailedQueue(string $cmd, int $id)
+       {
+               if (!in_array($cmd, [Delivery::POST, Delivery::POKE])) {
+                       return;
+               }
+
+               Model\ItemDeliveryData::incrementQueueFailed($id);
+       }
+
        /**
         * @brief Deliver content via DFRN
         *
@@ -225,7 +251,18 @@ class Delivery extends BaseObject
         */
        private static function deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
        {
-               Logger::log('Deliver ' . defaults($target_item, 'guid', $target_item['id']) . ' via DFRN to ' . (empty($contact['addr']) ? $contact['url'] : $contact['addr']));
+/*
+               if (Diaspora::isReshare($target_item['body'])) {
+                       // Transmit Diaspora reshares only via Diaspora
+                       self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
+                       return;
+               }
+
+               if (ActivityPub\Transmitter::::isAnnounce($target_item) && getby) {
+                       return;
+               }
+*/
+               Logger::info('Deliver ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' via DFRN to ' . (($contact['addr'] ?? '') ?: $contact['url']));
 
                if ($cmd == self::MAIL) {
                        $item = $target_item;
@@ -253,7 +290,7 @@ class Delivery extends BaseObject
                        $atom = DFRN::entries($msgitems, $owner);
                }
 
-               Logger::log('Notifier entry: ' . $contact["url"] . ' ' . defaults($target_item, 'guid', $target_item['id']) . ' entry: ' . $atom, Logger::DATA);
+               Logger::debug('Notifier entry: ' . $contact["url"] . ' ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' entry: ' . $atom);
 
                $basepath =  implode('/', array_slice(explode('/', $contact['url']), 0, 3));
 
@@ -286,12 +323,14 @@ class Delivery extends BaseObject
                        DFRN::import($atom, $target_importer);
 
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueDone($target_item['id']);
+                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DFRN);
                        }
 
                        return;
                }
 
+               $protocol = Model\ItemDeliveryData::DFRN;
+
                // We don't have a relationship with contacts on a public post.
                // Se we transmit with the new method and via Diaspora as a fallback
                if (!empty($items) && (($items[0]['uid'] == 0) || ($contact['uid'] == 0))) {
@@ -302,7 +341,15 @@ class Delivery extends BaseObject
 
                        // We never spool failed relay deliveries
                        if ($public_dfrn) {
-                               Logger::log('Relay delivery to ' . $contact["url"] . ' with guid ' . $target_item["guid"] . ' returns ' . $deliver_status);
+                               Logger::info('Relay delivery to ' . $contact["url"] . ' with guid ' . $target_item["guid"] . ' returns ' . $deliver_status);
+
+                               if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
+                                       if (($deliver_status >= 200) && ($deliver_status <= 299)) {
+                                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
+                                       } else {
+                                               Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
+                                       }
+                               }
                                return;
                        }
 
@@ -312,26 +359,35 @@ class Delivery extends BaseObject
                                return;
                        }
                } elseif ($cmd != self::RELOCATION) {
-                       $deliver_status = DFRN::deliver($owner, $contact, $atom);
+                       // DFRN payload over Diaspora transport layer
+                       $deliver_status = DFRN::transmit($owner, $contact, $atom);
+                       if ($deliver_status < 200) {
+                               // Legacy DFRN
+                               $deliver_status = DFRN::deliver($owner, $contact, $atom);
+                               $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
+                       }
                } else {
-                       $deliver_status = DFRN::deliver($owner, $contact, $atom, false, true);
+                       $deliver_status = DFRN::deliver($owner, $contact, $atom);
+                       $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
                }
 
-               Logger::info('DFRN Delivery', ['cmd' => $cmd, 'url' => $contact['url'], 'guid' => defaults($target_item, 'guid', $target_item['id']), 'return' => $deliver_status]);
+               Logger::info('DFRN Delivery', ['cmd' => $cmd, 'url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id'], 'return' => $deliver_status]);
 
                if (($deliver_status >= 200) && ($deliver_status <= 299)) {
                        // We successfully delivered a message, the contact is alive
                        Model\Contact::unmarkForArchival($contact);
 
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueDone($target_item['id']);
+                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
                        }
                } else {
                        // The message could not be delivered. We mark the contact as "dead"
                        Model\Contact::markForArchival($contact);
 
-                       Logger::info('Delivery failed: defer message', ['id' => defaults($target_item, 'guid', $target_item['id'])]);
-                       Worker::defer();
+                       Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
+                       if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) {
+                               Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
+                       }
                }
        }
 
@@ -360,11 +416,12 @@ class Delivery extends BaseObject
                        $loc = $contact['addr'];
                }
 
-               Logger::log('Deliver ' . defaults($target_item, 'guid', $target_item['id']) . ' via Diaspora to ' . $loc);
+               Logger::notice('Deliver via Diaspora', ['target' => $target_item['id'], 'guid' => $target_item['guid'], 'to' => $loc]);
 
                if (Config::get('system', 'dfrn_only') || !Config::get('system', 'diaspora_enabled')) {
                        return;
                }
+
                if ($cmd == self::MAIL) {
                        Diaspora::sendMail($target_item, $owner, $contact);
                        return;
@@ -373,6 +430,7 @@ class Delivery extends BaseObject
                if ($cmd == self::SUGGESTION) {
                        return;
                }
+
                if (!$contact['pubkey'] && !$public_message) {
                        return;
                }
@@ -405,18 +463,25 @@ class Delivery extends BaseObject
                        Model\Contact::unmarkForArchival($contact);
 
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueDone($target_item['id']);
+                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DIASPORA);
                        }
                } else {
                        // The message could not be delivered. We mark the contact as "dead"
                        Model\Contact::markForArchival($contact);
 
+                       // When it is delivered to the public endpoint, we do mark the relay contact for archival as well
+                       if ($public_message) {
+                               Diaspora::markRelayForArchival($contact);
+                       }
+
                        if (empty($contact['contact-type']) || ($contact['contact-type'] != Model\Contact::TYPE_RELAY)) {
-                               Logger::info('Delivery failed: defer message', ['id' => defaults($target_item, 'guid', $target_item['id'])]);
+                               Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
                                // defer message for redelivery
-                               Worker::defer();
+                               if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) {
+                                       Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
+                               }
                        } elseif (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueDone($target_item['id']);
+                               Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
                        }
                }
        }
@@ -428,15 +493,15 @@ class Delivery extends BaseObject
         * @param array  $contact     Contact record of the receiver
         * @param array  $owner       Owner record of the sender
         * @param array  $target_item Item record of the content
+        * @param array  $thr_parent  Item record of the direct parent in the thread
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function deliverMail($cmd, $contact, $owner, $target_item)
+       private static function deliverMail($cmd, $contact, $owner, $target_item, $thr_parent)
        {
                if (Config::get('system','dfrn_only')) {
                        return;
                }
-               // WARNING: does not currently convert to RFC2047 header encodings, etc.
 
                $addr = $contact['addr'];
                if (!strlen($addr)) {
@@ -447,12 +512,27 @@ class Delivery extends BaseObject
                        return;
                }
 
+               if ($target_item['verb'] != Activity::POST) {
+                       return;
+               }
+
+               if (!empty($thr_parent['object'])) {
+                       $data = json_decode($thr_parent['object'], true);
+                       if (!empty($data['reply_to'])) {
+                               $addr = $data['reply_to'][0]['mailbox'] . '@' . $data['reply_to'][0]['host'];
+                               Logger::info('Use "reply-to" address of the thread parent', ['addr' => $addr]);
+                       } elseif (!empty($data['from'])) {
+                               $addr = $data['from'][0]['mailbox'] . '@' . $data['from'][0]['host'];
+                               Logger::info('Use "from" address of the thread parent', ['addr' => $addr]);
+                       }
+               }
+
                $local_user = DBA::selectFirst('user', [], ['uid' => $owner['uid']]);
                if (!DBA::isResult($local_user)) {
                        return;
                }
 
-               Logger::log('Deliver ' . $target_item["guid"] . ' via mail to ' . $contact['addr']);
+               Logger::info('About to deliver via mail', ['guid' => $target_item['guid'], 'to' => $addr]);
 
                $reply_to = '';
                $mailacct = DBA::selectFirst('mailacct', ['reply_to'], ['uid' => $owner['uid']]);
@@ -466,10 +546,10 @@ class Delivery extends BaseObject
 
                if (($contact['rel'] == Model\Contact::FRIEND) && !$contact['blocked']) {
                        if ($reply_to) {
-                               $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $reply_to.'>' . "\n";
+                               $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $reply_to . '>' . "\n";
                                $headers .= 'Sender: ' . $local_user['email'] . "\n";
                        } else {
-                               $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8').' <' . $local_user['email'] . '>' . "\n";
+                               $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $local_user['email'] . '>' . "\n";
                        }
                } else {
                        $headers  = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' <noreply@' . self::getApp()->getHostName() . '>' . "\n";
@@ -478,11 +558,11 @@ class Delivery extends BaseObject
                $headers .= 'Message-Id: <' . Email::iri2msgid($target_item['uri']) . '>' . "\n";
 
                if ($target_item['uri'] !== $target_item['parent-uri']) {
-                       $headers .= "References: <" . Email::iri2msgid($target_item["parent-uri"]) . ">";
+                       $headers .= 'References: <' . Email::iri2msgid($target_item['parent-uri']) . '>';
 
-                       // If Threading is enabled, write down the correct parent
-                       if (($target_item["thr-parent"] != "") && ($target_item["thr-parent"] != $target_item["parent-uri"])) {
-                               $headers .= " <".Email::iri2msgid($target_item["thr-parent"]).">";
+                       // Export more references on deeper nested threads
+                       if (($target_item['thr-parent'] != '') && ($target_item['thr-parent'] != $target_item['parent-uri'])) {
+                               $headers .= ' <' . Email::iri2msgid($target_item['thr-parent']) . '>';
                        }
 
                        $headers .= "\n";
@@ -509,5 +589,9 @@ class Delivery extends BaseObject
                }
 
                Email::send($addr, $subject, $headers, $target_item);
+
+               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::MAIL);
+
+               Logger::info('Delivered via mail', ['guid' => $target_item['guid'], 'to' => $addr, 'subject' => $subject]);
        }
 }