From 51cc1f679f525c1ab3bea71c4c1026e1ad1386d4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 24 Jul 2022 14:26:06 +0000 Subject: [PATCH] Added trust / isActivityGone --- database.sql | 3 ++- doc/database/db_inbox-entry.md | 31 +++++++++++++------------- src/Protocol/ActivityPub/Processor.php | 4 ++-- src/Protocol/ActivityPub/Queue.php | 5 +++-- src/Protocol/ActivityPub/Receiver.php | 12 +++++----- static/dbstructure.config.php | 3 ++- 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/database.sql b/database.sql index 42624d4d1d..6b8d7df2ff 100644 --- a/database.sql +++ b/database.sql @@ -739,7 +739,8 @@ CREATE TABLE IF NOT EXISTS `inbox-entry` ( `received` datetime COMMENT 'Receiving date', `activity` mediumtext COMMENT 'The JSON activity', `signer` varchar(255) COMMENT '', - `push` boolean NOT NULL DEFAULT '0' COMMENT '', + `push` boolean COMMENT 'Is the entry pushed or have pulled it?', + `trust` boolean COMMENT 'Do we trust this entry?', `wid` int unsigned COMMENT 'Workerqueue id', PRIMARY KEY(`id`), UNIQUE INDEX `activity-id` (`activity-id`), diff --git a/doc/database/db_inbox-entry.md b/doc/database/db_inbox-entry.md index 32dd1203dc..d10d3f242b 100644 --- a/doc/database/db_inbox-entry.md +++ b/doc/database/db_inbox-entry.md @@ -6,21 +6,22 @@ Incoming activity Fields ------ -| Field | Description | Type | Null | Key | Default | Extra | -| ------------------ | ------------------------------------ | -------------- | ---- | --- | ------- | -------------- | -| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | -| activity-id | id of the incoming activity | varbinary(255) | YES | | NULL | | -| object-id | | varbinary(255) | YES | | NULL | | -| in-reply-to-id | | varbinary(255) | YES | | NULL | | -| conversation | | varbinary(255) | YES | | NULL | | -| type | Type of the activity | varchar(64) | YES | | NULL | | -| object-type | Type of the object activity | varchar(64) | YES | | NULL | | -| object-object-type | Type of the object's object activity | varchar(64) | YES | | NULL | | -| received | Receiving date | datetime | YES | | NULL | | -| activity | The JSON activity | mediumtext | YES | | NULL | | -| signer | | varchar(255) | YES | | NULL | | -| push | | boolean | NO | | 0 | | -| wid | Workerqueue id | int unsigned | YES | | NULL | | +| Field | Description | Type | Null | Key | Default | Extra | +| ------------------ | -------------------------------------- | -------------- | ---- | --- | ------- | -------------- | +| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | +| activity-id | id of the incoming activity | varbinary(255) | YES | | NULL | | +| object-id | | varbinary(255) | YES | | NULL | | +| in-reply-to-id | | varbinary(255) | YES | | NULL | | +| conversation | | varbinary(255) | YES | | NULL | | +| type | Type of the activity | varchar(64) | YES | | NULL | | +| object-type | Type of the object activity | varchar(64) | YES | | NULL | | +| object-object-type | Type of the object's object activity | varchar(64) | YES | | NULL | | +| received | Receiving date | datetime | YES | | NULL | | +| activity | The JSON activity | mediumtext | YES | | NULL | | +| signer | | varchar(255) | YES | | NULL | | +| push | Is the entry pushed or have pulled it? | boolean | YES | | NULL | | +| trust | Do we trust this entry? | boolean | YES | | NULL | | +| wid | Workerqueue id | int unsigned | YES | | NULL | | Indexes ------------ diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index d1c3994e2a..1ca6fce902 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -303,7 +303,7 @@ class Processor Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]); if ($recursion_depth < 10) { $result = self::fetchMissingActivity($activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO); - if (empty($result) && self::ActivityIsGone($activity['reply-to-id'])) { + if (empty($result) && self::isActivityGone($activity['reply-to-id'])) { // Recursively delete this and all depending entries Queue::deleteById($activity['entry-id']); return []; @@ -466,7 +466,7 @@ class Processor * * @return boolean */ - private static function ActivityIsGone(string $url): bool + private static function isActivityGone(string $url): bool { $curlResult = HTTPSignature::fetchRaw($url, 0); diff --git a/src/Protocol/ActivityPub/Queue.php b/src/Protocol/ActivityPub/Queue.php index 011101eed7..faaf0aa3bc 100644 --- a/src/Protocol/ActivityPub/Queue.php +++ b/src/Protocol/ActivityPub/Queue.php @@ -42,7 +42,7 @@ class Queue * @param boolean $push * @return array */ - public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push): array + public static function add(array $activity, string $type, int $uid, string $http_signer, bool $push, bool $trust_source): array { $fields = [ 'activity-id' => $activity['id'], @@ -52,6 +52,7 @@ class Queue 'activity' => json_encode($activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), 'received' => DateTimeFormat::utcNow(), 'push' => $push, + 'trust' => $trust_source, ]; if (!empty($activity['reply-to-id'])) { @@ -204,7 +205,7 @@ class Queue */ public static function processAll() { - $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`wid` IS NULL"], ['order' => ['id' => true]]); + $entries = DBA::select('inbox-entry', ['id', 'type', 'object-type', 'object-id', 'in-reply-to-id'], ["`trust` AND `wid` IS NULL"], ['order' => ['id' => true]]); while ($entry = DBA::fetch($entries)) { // We don't need to process entries that depend on already existing entries. if (!empty($entry['in-reply-to-id']) && DBA::exists('inbox-entry', ["`id` != ? AND `object-id` = ?", $entry['id'], $entry['in-reply-to-id']])) { diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 3e34510749..84d1197514 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -530,11 +530,6 @@ class Receiver $type = $object_data['type']; } - if (!$trust_source) { - Logger::info('Activity trust could not be achieved.', ['id' => $object_data['object_id'], 'type' => $type, 'signer' => $signer, 'actor' => $actor, 'attributedTo' => $attributed_to]); - return; - } - if (!empty($body) && empty($object_data['raw'])) { $object_data['raw'] = $body; } @@ -561,7 +556,12 @@ class Receiver $object_data['object_activity'] = $activity; } - $object_data = Queue::add($object_data, $type, $uid, $http_signer, $push); + $object_data = Queue::add($object_data, $type, $uid, $http_signer, $push, $trust_source); + + if (!$trust_source) { + Logger::info('Activity trust could not be achieved.', ['id' => $object_data['object_id'], 'type' => $type, 'signer' => $signer, 'actor' => $actor, 'attributedTo' => $attributed_to]); + return; + } if (!empty($activity['recursion-depth'])) { $object_data['recursion-depth'] = $activity['recursion-depth']; diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 0b6af9b13a..53ceaa7ece 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -798,7 +798,8 @@ return [ "received" => ["type" => "datetime", "comment" => "Receiving date"], "activity" => ["type" => "mediumtext", "comment" => "The JSON activity"], "signer" => ["type" => "varchar(255)", "comment" => ""], - "push" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], + "push" => ["type" => "boolean", "comment" => "Is the entry pushed or have pulled it?"], + "trust" => ["type" => "boolean", "comment" => "Do we trust this entry?"], "wid" => ["type" => "int unsigned", "foreign" => ["workerqueue" => "id"], "comment" => "Workerqueue id"], ], "indexes" => [ "PRIMARY" => ["id"], -- 2.39.5