]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Delivery.php
version 2021.03-dev
[friendica.git] / src / Worker / Delivery.php
index 19035ac0a3bbd03efa55e358f7da7ef88c840c01..1d1e8702b28c60390eea80d29e67fe0d5aa1ed12 100644 (file)
@@ -1,10 +1,26 @@
 <?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\Core\Config;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
@@ -17,6 +33,9 @@ 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
 {
@@ -42,14 +61,12 @@ class Delivery
                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'];
@@ -59,7 +76,6 @@ class Delivery
                } 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']);
@@ -69,6 +85,10 @@ class Delivery
                        $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;
                                }
@@ -81,13 +101,12 @@ class Delivery
 
                        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;
                        }
 
@@ -97,7 +116,7 @@ class Delivery
                                $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;
                        }
 
@@ -111,7 +130,7 @@ class Delivery
 
                        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;
                        }
 
@@ -122,13 +141,7 @@ class Delivery
                                }
                        }
 
-                       // 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!!!!
 
@@ -160,7 +173,7 @@ class Delivery
                                && empty($parent['allow_gid'])
                                && empty($parent['deny_cid'])
                                && empty($parent['deny_gid'])
-                               && !$parent["private"]) {
+                               && ($parent["private"] != Model\Item::PRIVATE)) {
                                $public_message = true;
                        }
                }
@@ -171,28 +184,29 @@ class Delivery
 
                $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;
                }
 
@@ -226,16 +240,16 @@ class Delivery
        /**
         * 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']);
        }
 
        /**
@@ -255,7 +269,7 @@ class Delivery
        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(Diaspora::personByHandle($contact['addr'], false))) {
+               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;
@@ -277,13 +291,17 @@ class Delivery
                        $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);
@@ -316,16 +334,16 @@ class Delivery
                                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
@@ -341,9 +359,9 @@ class Delivery
 
                                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;
@@ -360,11 +378,11 @@ class Delivery
                        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]);
@@ -374,7 +392,7 @@ class Delivery
                        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"
@@ -382,7 +400,7 @@ class Delivery
 
                        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']);
                        }
                }
        }
@@ -459,7 +477,7 @@ class Delivery
                        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"
@@ -467,17 +485,17 @@ class Delivery
 
                        // 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']);
                        }
                }
        }
@@ -548,7 +566,8 @@ class Delivery
                                $headers  = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $local_user['email'] . '>' . "\n";
                        }
                } else {
-                       $headers  = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' <noreply@' . DI::baseUrl()->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";
@@ -586,7 +605,7 @@ class Delivery
 
                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]);
        }