From a662245c744f4d2faa1b533977f7d21b4de6a724 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 13 May 2022 05:52:05 +0000 Subject: [PATCH] We now store the receivers as well --- database.sql | 1 + doc/database/db_post-delivery.md | 17 +++++++++-------- src/Model/Post/Delivery.php | 20 +++++++++++++++++--- src/Worker/APDelivery.php | 19 ++++--------------- src/Worker/Notifier.php | 4 ++-- static/dbstructure.config.php | 1 + 6 files changed, 34 insertions(+), 28 deletions(-) diff --git a/database.sql b/database.sql index 1508f358d..d13a3330c 100644 --- a/database.sql +++ b/database.sql @@ -1127,6 +1127,7 @@ CREATE TABLE IF NOT EXISTS `post-delivery` ( `created` datetime DEFAULT '0001-01-01 00:00:00' COMMENT '', `command` varbinary(32) COMMENT '', `failed` tinyint DEFAULT 0 COMMENT 'Number of times the delivery has failed', + `receivers` mediumtext COMMENT 'JSON encoded array with the receiving contacts', PRIMARY KEY(`uri-id`,`inbox-id`), INDEX `inbox-id_created` (`inbox-id`,`created`), INDEX `uid` (`uid`), diff --git a/doc/database/db_post-delivery.md b/doc/database/db_post-delivery.md index 8b149b571..2a766eea5 100644 --- a/doc/database/db_post-delivery.md +++ b/doc/database/db_post-delivery.md @@ -6,14 +6,15 @@ Delivery data for posts for the batch processing Fields ------ -| Field | Description | Type | Null | Key | Default | Extra | -| -------- | --------------------------------------------------------- | ------------------ | ---- | --- | ------------------- | ----- | -| uri-id | Id of the item-uri table entry that contains the item uri | int unsigned | NO | PRI | NULL | | -| inbox-id | Item-uri id of inbox url | int unsigned | NO | PRI | NULL | | -| uid | Delivering user | mediumint unsigned | YES | | NULL | | -| created | | datetime | YES | | 0001-01-01 00:00:00 | | -| command | | varbinary(32) | YES | | NULL | | -| failed | Number of times the delivery has failed | tinyint | YES | | 0 | | +| Field | Description | Type | Null | Key | Default | Extra | +| --------- | --------------------------------------------------------- | ------------------ | ---- | --- | ------------------- | ----- | +| uri-id | Id of the item-uri table entry that contains the item uri | int unsigned | NO | PRI | NULL | | +| inbox-id | Item-uri id of inbox url | int unsigned | NO | PRI | NULL | | +| uid | Delivering user | mediumint unsigned | YES | | NULL | | +| created | | datetime | YES | | 0001-01-01 00:00:00 | | +| command | | varbinary(32) | YES | | NULL | | +| failed | Number of times the delivery has failed | tinyint | YES | | 0 | | +| receivers | JSON encoded array with the receiving contacts | mediumtext | YES | | NULL | | Indexes ------------ diff --git a/src/Model/Post/Delivery.php b/src/Model/Post/Delivery.php index 8d19f8c94..52c1bf8e0 100644 --- a/src/Model/Post/Delivery.php +++ b/src/Model/Post/Delivery.php @@ -35,14 +35,16 @@ class Delivery * @param integer $uri_id * @param string $inbox * @param string $created + * @param array %receivers */ - public static function add(int $uri_id, int $uid, string $inbox, string $created, string $command) + public static function add(int $uri_id, int $uid, string $inbox, string $created, string $command, array $receivers) { if (empty($uri_id)) { throw new BadMethodCallException('Empty URI_id'); } - $fields = ['uri-id' => $uri_id, 'uid' => $uid, 'inbox-id' => ItemURI::getIdByURI($inbox), 'created' => $created, 'command' => $command]; + $fields = ['uri-id' => $uri_id, 'uid' => $uid, 'inbox-id' => ItemURI::getIdByURI($inbox), + 'created' => $created, 'command' => $command, 'receivers' => json_encode($receivers)]; DBA::insert('post-delivery', $fields, Database::INSERT_IGNORE); } @@ -81,6 +83,18 @@ class Delivery public static function selectForInbox(string $inbox) { - return DBA::selectToArray('post-delivery', [], ['inbox-id' => ItemURI::getIdByURI($inbox)], ['order' => ['created']]); + $rows = DBA::select('post-delivery', [], ['inbox-id' => ItemURI::getIdByURI($inbox)], ['order' => ['created']]); + $deliveries = []; + while ($row = DBA::fetch($rows)) { + if (!empty($row['receivers'])) { + $row['receivers'] = json_decode($row['receivers'], true); + } else { + $row['receivers'] = []; + } + $deliveries[] = $row; + } + DBA::close($rows); + + return $deliveries; } } diff --git a/src/Worker/APDelivery.php b/src/Worker/APDelivery.php index 641f27fce..6995b0edf 100644 --- a/src/Worker/APDelivery.php +++ b/src/Worker/APDelivery.php @@ -25,7 +25,6 @@ use Friendica\Core\Logger; use Friendica\Core\Worker; use Friendica\Model\Contact; use Friendica\Model\GServer; -use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Protocol\ActivityPub; use Friendica\Util\HTTPSignature; @@ -93,7 +92,7 @@ class APDelivery $posts = Post\Delivery::selectForInbox($inbox); foreach ($posts as $post) { - if (!self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], [], $post['uri-id'])) { + if (!self::deliverToInbox($post['command'], 0, $inbox, $post['uid'], $post['receivers'], $post['uri-id'])) { $uri_ids[] = $post['uri-id']; } } @@ -106,22 +105,12 @@ class APDelivery { 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_id = $item['id'] ?? 0; - if (empty($receivers) && !empty($item)) { - $parent = Post::selectFirst(Item::DELIVER_FIELDLIST, ['id' => $item['parent']]); - - $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid); - $receivers = $inboxes[$inbox] ?? []; - - // When we haven't fetched the receiver list, it can be a personal inbox - if (empty($receivers)) { - $inboxes = ActivityPub\Transmitter::fetchTargetInboxes($parent, $uid, true); - $receivers = $inboxes[$inbox] ?? []; - } - } elseif (empty($item_id)) { + if (empty($item['id'])) { Logger::debug('Item not found, removing delivery', ['uri-id' => $uri_id, 'uid' => $uid, 'cmd' => $cmd, 'inbox' => $inbox]); Post\Delivery::remove($uri_id, $inbox); return true; + } else { + $item_id = $item['id']; } } diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 66c3cadbd..e248c944d 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -788,7 +788,7 @@ class Notifier if (DI::config()->get('system', 'bulk_delivery')) { $delivery_queue_count++; - Post\Delivery::add($target_item['uri-id'], $uid, $inbox, $target_item['created'], $cmd); + Post\Delivery::add($target_item['uri-id'], $uid, $inbox, $target_item['created'], $cmd, $receivers); Worker::add(PRIORITY_HIGH, 'APDelivery', '', 0, $inbox, 0); } else { if (Worker::add(['priority' => $priority, 'created' => $created, 'dont_fork' => true], @@ -804,7 +804,7 @@ class Notifier if (DI::config()->get('system', 'bulk_delivery')) { $delivery_queue_count++; - Post\Delivery::add($target_item['uri-id'], $uid, $inbox, $target_item['created'], $cmd); + Post\Delivery::add($target_item['uri-id'], $uid, $inbox, $target_item['created'], $cmd, []); Worker::add(PRIORITY_MEDIUM, 'APDelivery', '', 0, $inbox, 0); } else { if (Worker::add(['priority' => $priority, 'dont_fork' => true], 'APDelivery', $cmd, $target_item['id'], $inbox, $uid, [], $target_item['uri-id'])) { diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 101e3da34..44b2fc1f8 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -1166,6 +1166,7 @@ return [ "created" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => ""], "command" => ["type" => "varbinary(32)", "comment" => ""], "failed" => ["type" => "tinyint", "default" => 0, "comment" => "Number of times the delivery has failed"], + "receivers" => ["type" => "mediumtext", "comment" => "JSON encoded array with the receiving contacts"], ], "indexes" => [ "PRIMARY" => ["uri-id", "inbox-id"],