]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Delivery.php
version 2021.03-dev
[friendica.git] / src / Worker / Delivery.php
index d9a2109ba3882d912c6278e163fb53899c788a83..1d1e8702b28c60390eea80d29e67fe0d5aa1ed12 100644 (file)
@@ -1,16 +1,30 @@
 <?php
 /**
- * @file src/Worker/Delivery.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
+
 namespace Friendica\Worker;
 
-use Friendica\BaseObject;
-use Friendica\Core\Config;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
-use Friendica\Core\System;
 use Friendica\Database\DBA;
+use Friendica\DI;
 use Friendica\Model;
 use Friendica\Protocol\DFRN;
 use Friendica\Protocol\Diaspora;
@@ -19,8 +33,11 @@ use Friendica\Protocol\Activity;
 use Friendica\Util\Strings;
 use Friendica\Util\Network;
 use Friendica\Core\Worker;
+use Friendica\Model\Conversation;
+use Friendica\Model\FContact;
+use Friendica\Protocol\Relay;
 
-class Delivery extends BaseObject
+class Delivery
 {
        const MAIL          = 'mail';
        const SUGGESTION    = 'suggest';
@@ -44,14 +61,12 @@ 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'];
@@ -61,7 +76,6 @@ 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']);
@@ -71,6 +85,10 @@ class Delivery extends BaseObject
                        $itemdata = Model\Item::select([], $condition, $params);
 
                        while ($item = Model\Item::fetch($itemdata)) {
+                               if ($item['verb'] == Activity::ANNOUNCE) {
+                                       continue;
+                               }
+       
                                if ($item['id'] == $parent_id) {
                                        $parent = $item;
                                }
@@ -83,13 +101,12 @@ 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);
+                               self::setFailedQueue($cmd, $target_item);
                                return;
                        }
 
@@ -99,7 +116,7 @@ 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);
+                               self::setFailedQueue($cmd, $target_item);
                                return;
                        }
 
@@ -113,7 +130,7 @@ class Delivery extends BaseObject
 
                        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);
+                               self::setFailedQueue($cmd, $target_item);
                                return;
                        }
 
@@ -124,13 +141,7 @@ class Delivery extends BaseObject
                                }
                        }
 
-                       // When commenting too fast after delivery, a post wasn't recognized as top level post.
-                       // The count then showed more than one entry. The additional check should help.
-                       // The check for the "count" should be superfluous, but I'm not totally sure by now, so we keep it.
-                       if ((($parent['id'] == $target_id) || (count($items) == 1)) && ($parent['uri'] === $parent['parent-uri'])) {
-                               Logger::log('Top level post');
-                               $top_level = true;
-                       }
+                       $top_level = $target_item['gravity'] == GRAVITY_PARENT;
 
                        // This is IMPORTANT!!!!
 
@@ -140,7 +151,7 @@ class Delivery extends BaseObject
                        // if $parent['wall'] == 1 we will already have the parent message in our array
                        // and we will relay the whole lot.
 
-                       $localhost = self::getApp()->getHostName();
+                       $localhost = DI::baseUrl()->getHostname();
                        if (strpos($localhost, ':')) {
                                $localhost = substr($localhost, 0, strpos($localhost, ':'));
                        }
@@ -162,7 +173,7 @@ class Delivery extends BaseObject
                                && empty($parent['allow_gid'])
                                && empty($parent['deny_cid'])
                                && empty($parent['deny_gid'])
-                               && !$parent["private"]) {
+                               && ($parent["private"] != Model\Item::PRIVATE)) {
                                $public_message = true;
                        }
                }
@@ -173,31 +184,37 @@ class Delivery extends BaseObject
 
                $owner = Model\User::getOwnerDataById($uid);
                if (!DBA::isResult($owner)) {
-                       self::setFailedQueue($cmd, $target_id);
+                       self::setFailedQueue($cmd, $target_item);
                        return;
                }
 
-               // We don't deliver our items to blocked or pending contacts, and not to ourselves either
+               // We don't deliver our items to blocked, archived or pending contacts, and not to ourselves either
                $contact = DBA::selectFirst('contact', [],
-                       ['id' => $contact_id, 'blocked' => false, 'pending' => false, 'self' => false]
+                       ['id' => $contact_id, 'archive' => false, 'blocked' => false, 'pending' => false, 'self' => false]
                );
                if (!DBA::isResult($contact)) {
-                       self::setFailedQueue($cmd, $target_id);
+                       self::setFailedQueue($cmd, $target_item);
                        return;
                }
 
                if (Network::isUrlBlocked($contact['url'])) {
-                       self::setFailedQueue($cmd, $target_id);
+                       self::setFailedQueue($cmd, $target_item);
                        return;
                }
 
                // 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 (!empty($parent) && !empty($thr_parent) && in_array(Protocol::DIASPORA, [$parent['network'], $thr_parent['network']])) {
+               // Also transmit relayed posts from Diaspora contacts via Diaspora.
+               if (!empty($parent) && !empty($thr_parent) && in_array(Protocol::DIASPORA, [$parent['network'], $thr_parent['network'], $target_item['network']])) {
                        $contact['network'] = Protocol::DIASPORA;
                }
 
+               // Ensure that local contacts are delivered locally
+               if (Model\Contact::isLocal($contact['url'])) {
+                       $contact['network'] = Protocol::DFRN;
+               }
+
                Logger::notice('Delivering', ['cmd' => $cmd, 'target' => $target_id, 'followup' => $followup, 'network' => $contact['network']]);
 
                switch ($contact['network']) {
@@ -223,20 +240,20 @@ class Delivery extends BaseObject
        /**
         * Increased the "failed" counter in the item delivery data
         *
-        * @param string  $cmd Command
-        * @param integer $id  Item id
+        * @param string $cmd  Command
+        * @param array  $item Item array
         */
-       private static function setFailedQueue(string $cmd, int $id)
+       private static function setFailedQueue(string $cmd, array $item)
        {
                if (!in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                        return;
                }
 
-               Model\ItemDeliveryData::incrementQueueFailed($id);
+               Model\Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? $item['id']);
        }
 
        /**
-        * @brief Deliver content via DFRN
+        * Deliver content via DFRN
         *
         * @param string  $cmd            Command
         * @param array   $contact        Contact record of the receiver
@@ -251,6 +268,13 @@ class Delivery extends BaseObject
         */
        private static function deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
        {
+               // Transmit Diaspora reshares via Diaspora if the Friendica contact support Diaspora
+               if (Diaspora::isReshare($target_item['body']) && !empty(FContact::getByURL($contact['addr'], false))) {
+                       Logger::info('Reshare will be transmitted via Diaspora', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
+                       self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
+                       return;
+               }
+
                Logger::info('Deliver ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' via DFRN to ' . (($contact['addr'] ?? '') ?: $contact['url']));
 
                if ($cmd == self::MAIL) {
@@ -267,13 +291,17 @@ class Delivery extends BaseObject
                        $msgitems = [$target_item];
                        $atom = DFRN::entries($msgitems, $owner);
                } else {
-                       $msgitems = [];
-                       foreach ($items as $item) {
-                               // Only add the parent when we don't delete other items.
-                               if (($target_item['id'] == $item['id']) || ($cmd != self::DELETION)) {
-                                       $item["entry:comment-allow"] = true;
-                                       $item["entry:cid"] = ($top_level ? $contact['id'] : 0);
-                                       $msgitems[] = $item;
+                       if ($target_item['deleted']) {
+                               $msgitems = [$target_item];
+                       } else {
+                               $msgitems = [];
+                               foreach ($items as $item) {
+                                       // Only add the parent when we don't delete other items.
+                                       if (($target_item['id'] == $item['id']) || ($cmd != self::DELETION)) {
+                                               $item["entry:comment-allow"] = true;
+                                               $item["entry:cid"] = ($top_level ? $contact['id'] : 0);
+                                               $msgitems[] = $item;
+                                       }
                                }
                        }
                        $atom = DFRN::entries($msgitems, $owner);
@@ -281,11 +309,8 @@ class Delivery extends BaseObject
 
                Logger::debug('Notifier entry: ' . $contact["url"] . ' ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' entry: ' . $atom);
 
-               $basepath =  implode('/', array_slice(explode('/', $contact['url']), 0, 3));
-
                // perform local delivery if we are on the same site
-
-               if (Strings::compareLink($basepath, System::baseUrl())) {
+               if (Model\Contact::isLocal($contact['url'])) {
                        $condition = ['nurl' => Strings::normaliseLink($contact['url']), 'self' => true];
                        $target_self = DBA::selectFirst('contact', ['uid'], $condition);
                        if (!DBA::isResult($target_self)) {
@@ -309,16 +334,16 @@ class Delivery extends BaseObject
                                return;
                        }
 
-                       DFRN::import($atom, $target_importer);
+                       DFRN::import($atom, $target_importer, false, Conversation::PARCEL_LOCAL_DFRN);
 
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DFRN);
+                               Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::DFRN);
                        }
 
                        return;
                }
 
-               $protocol = Model\ItemDeliveryData::DFRN;
+               $protocol = Model\Post\DeliveryData::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
@@ -334,9 +359,9 @@ class Delivery extends BaseObject
 
                                if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                        if (($deliver_status >= 200) && ($deliver_status <= 299)) {
-                                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
+                                               Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol);
                                        } else {
-                                               Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
+                                               Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']);
                                        }
                                }
                                return;
@@ -353,11 +378,11 @@ class Delivery extends BaseObject
                        if ($deliver_status < 200) {
                                // Legacy DFRN
                                $deliver_status = DFRN::deliver($owner, $contact, $atom);
-                               $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
+                               $protocol = Model\Post\DeliveryData::LEGACY_DFRN;
                        }
                } else {
                        $deliver_status = DFRN::deliver($owner, $contact, $atom);
-                       $protocol = Model\ItemDeliveryData::LEGACY_DFRN;
+                       $protocol = Model\Post\DeliveryData::LEGACY_DFRN;
                }
 
                Logger::info('DFRN Delivery', ['cmd' => $cmd, 'url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id'], 'return' => $deliver_status]);
@@ -367,7 +392,7 @@ class Delivery extends BaseObject
                        Model\Contact::unmarkForArchival($contact);
 
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol);
+                               Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol);
                        }
                } else {
                        // The message could not be delivered. We mark the contact as "dead"
@@ -375,13 +400,13 @@ class Delivery extends BaseObject
 
                        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']);
+                               Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']);
                        }
                }
        }
 
        /**
-        * @brief Deliver content via Diaspora
+        * Deliver content via Diaspora
         *
         * @param string  $cmd            Command
         * @param array   $contact        Contact record of the receiver
@@ -407,7 +432,7 @@ class Delivery extends BaseObject
 
                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')) {
+               if (DI::config()->get('system', 'dfrn_only') || !DI::config()->get('system', 'diaspora_enabled')) {
                        return;
                }
 
@@ -452,7 +477,7 @@ class Delivery extends BaseObject
                        Model\Contact::unmarkForArchival($contact);
 
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DIASPORA);
+                               Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::DIASPORA);
                        }
                } else {
                        // The message could not be delivered. We mark the contact as "dead"
@@ -460,23 +485,23 @@ class Delivery extends BaseObject
 
                        // When it is delivered to the public endpoint, we do mark the relay contact for archival as well
                        if ($public_message) {
-                               Diaspora::markRelayForArchival($contact);
+                               Relay::markForArchival($contact);
                        }
 
                        if (empty($contact['contact-type']) || ($contact['contact-type'] != Model\Contact::TYPE_RELAY)) {
                                Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]);
                                // defer message for redelivery
                                if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                                       Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
+                                       Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']);
                                }
                        } elseif (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
-                               Model\ItemDeliveryData::incrementQueueFailed($target_item['id']);
+                               Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']);
                        }
                }
        }
 
        /**
-        * @brief Deliver content via mail
+        * Deliver content via mail
         *
         * @param string $cmd         Command
         * @param array  $contact     Contact record of the receiver
@@ -488,7 +513,7 @@ class Delivery extends BaseObject
         */
        private static function deliverMail($cmd, $contact, $owner, $target_item, $thr_parent)
        {
-               if (Config::get('system','dfrn_only')) {
+               if (DI::config()->get('system','dfrn_only')) {
                        return;
                }
 
@@ -529,7 +554,7 @@ class Delivery extends BaseObject
                        $reply_to = $mailacct['reply_to'];
                }
 
-               $subject  = ($target_item['title'] ? Email::encodeHeader($target_item['title'], 'UTF-8') : L10n::t("\x28no subject\x29"));
+               $subject  = ($target_item['title'] ? Email::encodeHeader($target_item['title'], 'UTF-8') : DI::l10n()->t("\x28no subject\x29"));
 
                // only expose our real email address to true friends
 
@@ -541,7 +566,8 @@ class Delivery extends BaseObject
                                $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";
+                       $sender = DI::config()->get('config', 'sender_email', 'noreply@' . DI::baseUrl()->getHostname());
+                       $headers  = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' <' . $sender . '>' . "\n";
                }
 
                $headers .= 'Message-Id: <' . Email::iri2msgid($target_item['uri']) . '>' . "\n";
@@ -579,7 +605,7 @@ class Delivery extends BaseObject
 
                Email::send($addr, $subject, $headers, $target_item);
 
-               Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::MAIL);
+               Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::MAIL);
 
                Logger::info('Delivered via mail', ['guid' => $target_item['guid'], 'to' => $addr, 'subject' => $subject]);
        }