]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Notifier.php
Moved updating personal contacts to updatecontact
[friendica.git] / src / Worker / Notifier.php
index ec038d2313325ed5af55d083e4e59b1498ad95cb..7359ad1d47b64fee1bea3e8212a525947e019d90 100644 (file)
@@ -40,6 +40,7 @@ use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
+use Friendica\Protocol\Relay;
 use Friendica\Protocol\Salmon;
 
 /*
@@ -179,7 +180,7 @@ class Notifier
 
                        // Only deliver threaded replies (comment to a comment) to Diaspora
                        // when the original comment author does support the Diaspora protocol.
-                       if ($target_item['parent-uri'] != $target_item['thr-parent']) {
+                       if ($thr_parent['author-link'] && $target_item['parent-uri'] != $target_item['thr-parent']) {
                                $diaspora_delivery = Diaspora::isSupportedByContactUrl($thr_parent['author-link']);
                                Logger::info('Threaded comment', ['diaspora_delivery' => (int)$diaspora_delivery]);
                        }
@@ -414,12 +415,10 @@ class Notifier
 
                                $relay_list_stmt = DBA::p(
                                        "SELECT
-                                               `batch`,
+                                               `batch`, `network`, `protocol`,
                                                ANY_VALUE(`id`) AS `id`,
                                                ANY_VALUE(`url`) AS `url`,
-                                               ANY_VALUE(`name`) AS `name`,
-                                               ANY_VALUE(`network`) AS `network`,
-                                               ANY_VALUE(`protocol`) AS `protocol`
+                                               ANY_VALUE(`name`) AS `name`
                                        FROM `contact`
                                        WHERE `network` = ?
                                        AND `batch` != ''
@@ -428,7 +427,7 @@ class Notifier
                                        AND NOT `blocked`
                                        AND NOT `pending`
                                        AND NOT `archive`
-                                       GROUP BY `batch`",
+                                       GROUP BY `batch`, `network`, `protocol`",
                                        Protocol::DIASPORA,
                                        $owner['uid'],
                                        Contact::SHARING
@@ -442,7 +441,7 @@ class Notifier
                                // Add the relay to the list, avoid duplicates.
                                // Don't send community posts to the relay. Forum posts via the Diaspora protocol are looking ugly.
                                if (!$followup && !Item::isForumPost($target_item, $owner) && !self::isForumPost($target_item)) {
-                                       $relay_list = Diaspora::relayList($target_id, $relay_list);
+                                       $relay_list = Relay::getList($target_id, $relay_list, [Protocol::DFRN, Protocol::DIASPORA]);
                                }
                        }
 
@@ -461,7 +460,7 @@ class Notifier
                                        }
 
                                        if (!empty($rr['addr']) && ($rr['network'] == Protocol::ACTIVITYPUB) && !DBA::exists('fcontact', ['addr' => $rr['addr']])) {
-                                               Logger::info('Contact is AP omly, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $rr['url']]);
+                                               Logger::info('Contact is AP only, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $rr['url']]);
                                                continue;
                                        }
 
@@ -475,8 +474,8 @@ class Notifier
                                                continue;
                                        }
 
-                                       if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $cmd)) {
-                                               Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['id' => $target_id, 'url' => $rr['url']]);
+                                       if (self::skipDFRN($rr, $target_item, $parent, $thr_parent, $owner, $cmd)) {
+                                               Logger::info('Content will be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['id' => $target_id, 'url' => $rr['url']]);
                                                continue;
                                        }
 
@@ -516,7 +515,7 @@ class Notifier
                        }
 
                        if (!empty($contact['addr']) && ($contact['network'] == Protocol::ACTIVITYPUB) && !DBA::exists('fcontact', ['addr' => $contact['addr']])) {
-                               Logger::info('Contact is AP omly, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $contact['url']]);
+                               Logger::info('Contact is AP only, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'contact' => $contact['url']]);
                                continue;
                        }
 
@@ -530,8 +529,8 @@ class Notifier
                                continue;
                        }
 
-                       if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $cmd)) {
-                               Logger::info('Contact can be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'url' => $contact['url']]);
+                       if (self::skipDFRN($contact, $target_item, $parent, $thr_parent, $owner, $cmd)) {
+                               Logger::info('Content will be delivered via AP, so skip delivery via legacy DFRN/Diaspora', ['target' => $target_id, 'url' => $contact['url']]);
                                continue;
                        }
 
@@ -629,12 +628,12 @@ class Notifier
                }
 
                // Skip the delivery to Diaspora if the item is from an ActivityPub author
-               if ($item['author-network'] == Protocol::ACTIVITYPUB) {
+               if (!empty($item['author-network']) && ($item['author-network'] == Protocol::ACTIVITYPUB)) {
                        return true;
                }
-               
+
                // Skip the delivery to Diaspora if the thread parent is from an ActivityPub author
-               if ($thr_parent['author-network'] == Protocol::ACTIVITYPUB) {
+               if (!empty($thr_parent['author-network']) && ($thr_parent['author-network'] == Protocol::ACTIVITYPUB)) {
                        return true;
                }
 
@@ -648,12 +647,13 @@ class Notifier
         * @param array  $item       The post
         * @param array  $parent     The parent
         * @param array  $thr_parent The thread parent
+        * @param array  $owner      Owner array
         * @param string $cmd        Notifier command
         * @return bool
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function skipDFRN($contact, $item, $parent, $thr_parent, $cmd)
+       private static function skipDFRN($contact, $item, $parent, $thr_parent, $owner, $cmd)
        {
                if (empty($parent['network'])) {
                        return false;
@@ -689,6 +689,12 @@ class Notifier
                        return true;
                }
 
+               // For the time being we always deliver forum post via DFRN if possible
+               // This can be removed possible at the end of 2020 when hopefully most system can process AP forum posts
+               if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
+                       return false;
+               }
+
                // Skip DFRN when the item will be (forcefully) delivered via AP
                if (DI::config()->get('debug', 'total_ap_delivery') && ($contact['network'] == Protocol::DFRN) && !empty(APContact::getByURL($contact['url'], false))) {
                        return true;
@@ -774,11 +780,18 @@ class Notifier
                }
 
                $inboxes = [];
+               $relay_inboxes = [];
 
                $uid = $target_item['contact-uid'] ?: $target_item['uid'];
 
                if ($target_item['origin']) {
                        $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid);
+
+                       if (in_array($target_item['private'], [Item::PUBLIC])) {
+                               $inboxes = ActivityPub\Transmitter::addRelayServerInboxesForItem($target_item['id'], $inboxes);
+                               $relay_inboxes = ActivityPub\Transmitter::addRelayServerInboxes();
+                       }
+
                        Logger::log('Origin item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
                } elseif (Item::isForumPost($target_item, $owner)) {
                        $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($target_item, $uid, false, 0, true);
@@ -790,10 +803,16 @@ class Notifier
                        // Remote items are transmitted via the personal inboxes.
                        // Doing so ensures that the dedicated receiver will get the message.
                        $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true, $target_item['id']);
+
+                       if (in_array($target_item['private'], [Item::PUBLIC])) {
+                               $inboxes = ActivityPub\Transmitter::addRelayServerInboxesForItem($parent['id'], $inboxes);
+                               $relay_inboxes = ActivityPub\Transmitter::addRelayServerInboxes([]);
+                       }
+
                        Logger::log('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' will be distributed.', Logger::DEBUG);
                }
 
-               if (empty($inboxes)) {
+               if (empty($inboxes) && empty($relay_inboxes)) {
                        Logger::log('No inboxes found for item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . '. It will not be distributed.', Logger::DEBUG);
                        return 0;
                }
@@ -812,6 +831,15 @@ class Notifier
                        }
                }
 
+               // We deliver posts to relay servers slightly delayed to priorize the direct delivery
+               foreach ($relay_inboxes as $inbox) {
+                       Logger::info('Delivery to relay servers via ActivityPub', ['cmd' => $cmd, 'id' => $target_item['id'], 'inbox' => $inbox]);
+
+                       if (Worker::add(['priority' => $priority, 'dont_fork' => true], 'APDelivery', $cmd, $target_item['id'], $inbox, $uid)) {
+                               $delivery_queue_count++;
+                       }
+               }
+
                return $delivery_queue_count;
        }