]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Delivery.php
New post class in protocol and worker classes
[friendica.git] / src / Worker / Delivery.php
index 8a84f34a2f07f0c562da6d28b06b151e1b2561ab..3c6b8775b8cef459d5d4844e83c16b42e144050a 100644 (file)
@@ -74,7 +74,7 @@ class Delivery
                        $uid = $target_id;
                        $target_item = [];
                } else {
-                       $item = Model\Item::selectFirst(['parent'], ['id' => $target_id]);
+                       $item = Model\Post::selectFirst(['parent'], ['id' => $target_id]);
                        if (!DBA::isResult($item) || empty($item['parent'])) {
                                return;
                        }
@@ -82,9 +82,9 @@ class Delivery
 
                        $condition = ['id' => [$target_id, $parent_id], 'visible' => true, 'moderated' => false];
                        $params = ['order' => ['id']];
-                       $itemdata = Model\Item::select([], $condition, $params);
+                       $itemdata = Model\Post::select([], $condition, $params);
 
-                       while ($item = Model\Item::fetch($itemdata)) {
+                       while ($item = Model\Post::fetch($itemdata)) {
                                if ($item['verb'] == Activity::ANNOUNCE) {
                                        continue;
                                }
@@ -121,7 +121,7 @@ class Delivery
                        }
 
                        $condition = ['uri' => $target_item['thr-parent'], 'uid' => $target_item['uid']];
-                       $thr_parent = Model\Item::selectFirst(['network', 'object'], $condition);
+                       $thr_parent = Model\Post::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.
@@ -141,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!!!!
 
@@ -208,10 +202,13 @@ class Delivery
                        return;
                }
 
+               $protocol = Model\GServer::getProtocol($contact['gsid'] ?? 0);
+
                // 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;
                }
 
@@ -224,7 +221,7 @@ class Delivery
 
                switch ($contact['network']) {
                        case Protocol::DFRN:
-                               self::deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
+                               self::deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup, $protocol);
                                break;
 
                        case Protocol::DIASPORA:
@@ -260,18 +257,19 @@ class Delivery
        /**
         * Deliver content via DFRN
         *
-        * @param string  $cmd            Command
-        * @param array   $contact        Contact record of the receiver
-        * @param array   $owner          Owner record of the sender
-        * @param array   $items          Item record of the content and the parent
-        * @param array   $target_item    Item record of the content
-        * @param boolean $public_message Is the content public?
-        * @param boolean $top_level      Is it a thread starter?
-        * @param boolean $followup       Is it an answer to a remote post?
+        * @param string  $cmd             Command
+        * @param array   $contact         Contact record of the receiver
+        * @param array   $owner           Owner record of the sender
+        * @param array   $items           Item record of the content and the parent
+        * @param array   $target_item     Item record of the content
+        * @param boolean $public_message  Is the content public?
+        * @param boolean $top_level       Is it a thread starter?
+        * @param boolean $followup        Is it an answer to a remote post?
+        * @param int     $server_protocol The protocol of the server
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup)
+       private static function deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup, $server_protocol)
        {
                // Transmit Diaspora reshares via Diaspora if the Friendica contact support Diaspora
                if (Diaspora::isReshare($target_item['body']) && !empty(FContact::getByURL($contact['addr'], false))) {
@@ -339,7 +337,7 @@ class Delivery
                                return;
                        }
 
-                       DFRN::import($atom, $target_importer, false, Conversation::PARCEL_LOCAL_DFRN);
+                       DFRN::import($atom, $target_importer, Conversation::PARCEL_LOCAL_DFRN, Conversation::PUSH);
 
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::DFRN);
@@ -365,6 +363,8 @@ class Delivery
                                if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                        if (($deliver_status >= 200) && ($deliver_status <= 299)) {
                                                Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol);
+
+                                               Model\GServer::setProtocol($contact['gsid'] ?? 0, $protocol);
                                        } else {
                                                Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']);
                                        }
@@ -372,7 +372,7 @@ class Delivery
                                return;
                        }
 
-                       if (($deliver_status < 200) || ($deliver_status > 299)) {
+                       if ((($deliver_status < 200) || ($deliver_status > 299)) && (empty($server_protocol) || ($server_protocol == Model\Post\DeliveryData::LEGACY_DFRN))) {
                                // Transmit via Diaspora if not possible via Friendica
                                self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup);
                                return;
@@ -380,7 +380,7 @@ class Delivery
                } elseif ($cmd != self::RELOCATION) {
                        // DFRN payload over Diaspora transport layer
                        $deliver_status = DFRN::transmit($owner, $contact, $atom);
-                       if ($deliver_status < 200) {
+                       if (($deliver_status < 200) && (empty($server_protocol) || ($server_protocol == Model\Post\DeliveryData::LEGACY_DFRN))) {
                                // Legacy DFRN
                                $deliver_status = DFRN::deliver($owner, $contact, $atom);
                                $protocol = Model\Post\DeliveryData::LEGACY_DFRN;
@@ -396,6 +396,8 @@ class Delivery
                        // We successfully delivered a message, the contact is alive
                        Model\Contact::unmarkForArchival($contact);
 
+                       Model\GServer::setProtocol($contact['gsid'] ?? 0, $protocol);
+
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol);
                        }
@@ -481,6 +483,8 @@ class Delivery
                        // We successfully delivered a message, the contact is alive
                        Model\Contact::unmarkForArchival($contact);
 
+                       Model\GServer::setProtocol($contact['gsid'] ?? 0, Model\Post\DeliveryData::DIASPORA);
+
                        if (in_array($cmd, [Delivery::POST, Delivery::POKE])) {
                                Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::DIASPORA);
                        }
@@ -589,13 +593,13 @@ class Delivery
 
                        if (empty($target_item['title'])) {
                                $condition = ['uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
-                               $title = Model\Item::selectFirst(['title'], $condition);
+                               $title = Model\Post::selectFirst(['title'], $condition);
 
                                if (DBA::isResult($title) && ($title['title'] != '')) {
                                        $subject = $title['title'];
                                } else {
                                        $condition = ['parent-uri' => $target_item['parent-uri'], 'uid' => $owner['uid']];
-                                       $title = Model\Item::selectFirst(['title'], $condition);
+                                       $title = Model\Post::selectFirst(['title'], $condition);
 
                                        if (DBA::isResult($title) && ($title['title'] != '')) {
                                                $subject = $title['title'];