]> git.mxchange.org Git - friendica.git/commitdiff
Improved bulk delivery / don't redistribute activities
authorMichael <heluecht@pirati.ca>
Thu, 3 Nov 2022 04:03:39 +0000 (04:03 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 3 Nov 2022 04:03:39 +0000 (04:03 +0000)
src/Core/Worker/Cron.php
src/Protocol/ActivityPub/Delivery.php
src/Worker/Notifier.php
static/defaults.config.php

index 11631e02b341e4594bd6774d1919c446aa4914d0..4aef72e5f5112345aa58bf89ed8ff37f84e83d5b 100644 (file)
@@ -159,8 +159,13 @@ class Cron
         */
        private static function deliverPosts()
        {
-               $deliveries = DBA::p("SELECT `item-uri`.`uri` AS `inbox`, MAX(`failed`) AS `failed` FROM `post-delivery` INNER JOIN `item-uri` ON `item-uri`.`id` = `post-delivery`.`inbox-id` GROUP BY `inbox`");
+               $deliveries = DBA::p("SELECT `item-uri`.`uri` AS `inbox`, MAX(`failed`) AS `failed` FROM `post-delivery` INNER JOIN `item-uri` ON `item-uri`.`id` = `post-delivery`.`inbox-id` GROUP BY `inbox` ORDER BY RAND()");
                while ($delivery = DBA::fetch($deliveries)) {
+                       if ($delivery['failed'] > 0) {
+                               Logger::info('Removing failed deliveries', ['inbox' => $delivery['inbox'], 'failed' => $delivery['failed']]);
+                               Post\Delivery::removeFailed($delivery['inbox']);
+                       }
+                               
                        if ($delivery['failed'] == 0) {
                                $result = ActivityPub\Delivery::deliver($delivery['inbox']);
                                Logger::info('Directly deliver inbox', ['inbox' => $delivery['inbox'], 'result' => $result['success']]);
@@ -175,15 +180,17 @@ class Cron
                                $priority = Worker::PRIORITY_NEGLIGIBLE;
                        }
 
-                       if ($delivery['failed'] >= DI::config()->get('system', 'worker_defer_limit')) {
-                               Logger::info('Removing failed deliveries', ['inbox' => $delivery['inbox'], 'failed' => $delivery['failed']]);
-                               Post\Delivery::removeFailed($delivery['inbox']);
-                       }
-
-                       if (Worker::add($priority, 'APDelivery', '', 0, $delivery['inbox'], 0)) {
+                       if (Worker::add(['priority' => $priority, 'force_priority' => true], 'APDelivery', '', 0, $delivery['inbox'], 0)) {
                                Logger::info('Missing APDelivery worker added for inbox', ['inbox' => $delivery['inbox'], 'failed' => $delivery['failed'], 'priority' => $priority]);
                        }
                }
+
+               // Optimizing this table only last seconds
+               if (DI::config()->get('system', 'optimize_tables')) {
+                       Logger::info('Optimize start');
+                       DBA::e("OPTIMIZE TABLE `post-delivery`");
+                       Logger::info('Optimize end');
+               }
        }
 
        /**
index 3b8e539e9e3bc8d4986e719accf05c96a05f088a..add898888f9d5f994932d6691ded4841718e9f67 100644 (file)
@@ -27,6 +27,7 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\GServer;
+use Friendica\Model\Item;
 use Friendica\Model\Post;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Util\HTTPSignature;
@@ -82,11 +83,15 @@ class Delivery
        public static function deliverToInbox(string $cmd, int $item_id, string $inbox, int $uid, array $receivers, int $uri_id): array
        {
                if (empty($item_id) && !empty($uri_id) && !empty($uid)) {
-                       $item = Post::selectFirst(['id', 'parent', 'origin'], ['uri-id' => $uri_id, 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
+                       $item = Post::selectFirst(['id', 'parent', 'origin', 'gravity', 'verb'], ['uri-id' => $uri_id, 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
                        if (empty($item['id'])) {
                                Logger::warning('Item not found, removing delivery', ['uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
                                Post\Delivery::remove($uri_id, $inbox);
                                return ['success' => true, 'serverfailure' => false, 'drop' => false];
+                       } elseif (!DI::config()->get('system', 'redistribute_activities') && !$item['origin'] && ($item['gravity'] == Item::GRAVITY_ACTIVITY)) {
+                               Logger::notice('Activities are not relayed, removing delivery', ['verb' => $item['verb'], 'uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]);
+                               Post\Delivery::remove($uri_id, $inbox);
+                               return ['success' => true, 'serverfailure' => false, 'drop' => false];
                        } else {
                                $item_id = $item['id'];
                        }
@@ -162,6 +167,8 @@ class Delivery
                                                Post\Delivery::incrementFailed($uri_id, $inbox);
                                        }
                                }
+                       } elseif ($uri_id) {
+                               Post\Delivery::remove($uri_id, $inbox);
                        }
                }
 
index 9898eed1a015cc36c2c29219ee9e51d2910ff2e5..be08ad7064d86dd8cef5597cc0066ff3bb45f40b 100644 (file)
@@ -775,7 +775,7 @@ class Notifier
                } elseif (!Post\Activity::exists($target_item['uri-id'])) {
                        Logger::info('Remote item ' . $target_item['id'] . ' with URL ' . $target_item['uri'] . ' is no AP post. It will not be distributed.');
                        return ['count' => 0, 'contacts' => []];
-               } elseif ($parent['origin']) {
+               } elseif ($parent['origin'] && (($target_item['gravity'] != Item::GRAVITY_ACTIVITY) || DI::config()->get('system', 'redistribute_activities'))) {
                        $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, false, $target_item['id']);
 
                        if (in_array($target_item['private'], [Item::PUBLIC])) {
index 647fb73b13ffc325cf689a7171e45a7230daee59..83c0d371fe82fa75a128a90758071355e844c1b8 100644 (file)
@@ -525,6 +525,10 @@ return [
                // The authentication password for the redis database
                'redis_password' => null,
 
+               // redistribute_activities (Boolean)
+               // Redistribute incoming activities via ActivityPub
+               'redistribute_activities' => true,
+
                // relay_deny_languages (Array)
                // Array of languages (two digit format) that are rejected.
                'relay_deny_languages' => [],