From 7dcd02938de26ba6cb612b88d3988c868d09a82b Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Jul 2022 05:37:40 +0000 Subject: [PATCH] Remove entries from queue / relay detection --- src/Model/APContact.php | 18 +++++++++++++++++- src/Protocol/ActivityPub/Processor.php | 6 ++++++ src/Protocol/ActivityPub/Receiver.php | 24 +++++++++++++++--------- src/Util/XML.php | 4 ++-- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/Model/APContact.php b/src/Model/APContact.php index baa364e94..ba2e3c973 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -26,7 +26,6 @@ use Friendica\Core\Cache\Enum\Duration; use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\Database\DBA; -use Friendica\Database\DBStructure; use Friendica\DI; use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPException; @@ -539,4 +538,21 @@ class APContact HTTPSignature::setInboxStatus($url, true, $shared); } + + public static function isRelay(array $apcontact): bool + { + if ($apcontact['nick'] != 'relay') { + return false; + } + + if ($apcontact['type'] == 'Application') { + return true; + } + + if (in_array($apcontact['type'], ['Group', 'Service']) && ($apcontact['nick'] == 'relay') && is_null($apcontact['outbox'])) { + return true; + } + + return false; + } } diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 6e81cb767..e6ff43f06 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -546,6 +546,7 @@ class Processor Logger::debug('Add post to featured collection', ['uri-id' => $uriid]); Post\Collection::add($uriid, Post\Collection::FEATURED); + Receiver::removeFromQueue($activity); } /** @@ -563,6 +564,7 @@ class Processor Logger::debug('Remove post from featured collection', ['uri-id' => $uriid]); Post\Collection::remove($uriid, Post\Collection::FEATURED); + Receiver::removeFromQueue($activity); } /** @@ -1464,6 +1466,7 @@ class Processor $condition = ['id' => $cid]; Contact::update($fields, $condition); Logger::info('Accept contact request', ['contact' => $cid, 'user' => $uid]); + Receiver::removeFromQueue($activity); } /** @@ -1497,6 +1500,7 @@ class Processor } else { Logger::info('Rejected contact request', ['contact' => $cid, 'user' => $uid]); } + Receiver::removeFromQueue($activity); } /** @@ -1522,6 +1526,7 @@ class Processor } Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]); + Receiver::removeFromQueue($activity); } /** @@ -1558,6 +1563,7 @@ class Processor Contact::removeFollower($contact); Logger::info('Undo following request', ['contact' => $cid, 'user' => $uid]); + Receiver::removeFromQueue($activity); } /** diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 11e7151e1..4d7309001 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -105,7 +105,7 @@ class Receiver if (empty($apcontact)) { Logger::notice('Unable to retrieve AP contact for actor - message is discarded', ['actor' => $actor]); return; - } elseif ($apcontact['type'] == 'Application' && $apcontact['nick'] == 'relay') { + } elseif (APContact::isRelay($apcontact)) { self::processRelayPost($ldactivity, $actor); return; } else { @@ -208,18 +208,18 @@ class Receiver { $type = JsonLD::fetchElement($activity, '@type'); if (!$type) { - Logger::info('Empty type', ['activity' => $activity]); + Logger::info('Empty type', ['activity' => $activity, 'actor' => $actor]); return; } if ($type != 'as:Announce') { - Logger::info('Not an announcement', ['activity' => $activity]); + Logger::info('Not an announcement', ['activity' => $activity, 'actor' => $actor]); return; } $object_id = JsonLD::fetchElement($activity, 'as:object', '@id'); if (empty($object_id)) { - Logger::info('No object id found', ['activity' => $activity]); + Logger::info('No object id found', ['activity' => $activity, 'actor' => $actor]); return; } @@ -234,11 +234,11 @@ class Receiver return; } - Logger::info('Got relayed message id', ['id' => $object_id]); + Logger::info('Got relayed message id', ['id' => $object_id, 'actor' => $actor]); $item_id = Item::searchByLink($object_id); if ($item_id) { - Logger::info('Relayed message already exists', ['id' => $object_id, 'item' => $item_id]); + Logger::info('Relayed message already exists', ['id' => $object_id, 'item' => $item_id, 'actor' => $actor]); return; } @@ -246,7 +246,7 @@ class Receiver $id = Processor::fetchMissingActivity($fetchQueue, $object_id, [], $actor, self::COMPLETION_RELAY); if (empty($id)) { - Logger::notice('Relayed message had not been fetched', ['id' => $object_id]); + Logger::notice('Relayed message had not been fetched', ['id' => $object_id, 'actor' => $actor]); return; } @@ -254,9 +254,9 @@ class Receiver $item_id = Item::searchByLink($object_id); if ($item_id) { - Logger::info('Relayed message had been fetched and stored', ['id' => $object_id, 'item' => $item_id]); + Logger::info('Relayed message had been fetched and stored', ['id' => $object_id, 'item' => $item_id, 'actor' => $actor]); } else { - Logger::notice('Relayed message had not been stored', ['id' => $object_id]); + Logger::notice('Relayed message had not been stored', ['id' => $object_id, 'actor' => $actor]); } } @@ -615,6 +615,7 @@ class Receiver ActivityPub\Processor::postItem($object_data, $item); } elseif (in_array($object_data['object_type'], ['pt:CacheFile'])) { // Unhandled Peertube activity + self::removeFromQueue($object_data); } else { self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer); } @@ -706,6 +707,7 @@ class Receiver ActivityPub\Processor::updatePerson($object_data); } elseif (in_array($object_data['object_type'], ['pt:CacheFile'])) { // Unhandled Peertube activity + self::removeFromQueue($object_data); } else { self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer); } @@ -791,9 +793,11 @@ class Receiver } elseif (in_array($object_data['object_type'], array_merge(self::ACTIVITY_TYPES, ['as:Announce', 'as:Create', ''])) && empty($object_data['object_object_type'])) { // We cannot detect the target object. So we can ignore it. + self::removeFromQueue($object_data); } elseif (in_array($object_data['object_type'], ['as:Create']) && in_array($object_data['object_object_type'], ['pt:CacheFile'])) { // Unhandled Peertube activity + self::removeFromQueue($object_data); } else { self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer); } @@ -804,6 +808,7 @@ class Receiver ActivityPub\Processor::createActivity($fetchQueue, $object_data, Activity::VIEW); } elseif ($object_data['object_type'] == '') { // The object type couldn't be determined. Most likely we don't have it here. We ignore this activity. + self::removeFromQueue($object_data); } else { self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer); } @@ -843,6 +848,7 @@ class Receiver private static function storeUnhandledActivity(bool $unknown, string $type, array $object_data, array $activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = []) { if (!DI::config()->get('debug', 'ap_log_unknown')) { + self::removeFromQueue($activity); return; } diff --git a/src/Util/XML.php b/src/Util/XML.php index e371ea3cd..a6e6cd5ba 100644 --- a/src/Util/XML.php +++ b/src/Util/XML.php @@ -141,9 +141,9 @@ class XML * @param string $elementname Name of the XML element of the target * @return void */ - public static function copy(&$source, &$target, string $elementname) + public static function copy(&$source, &$target, $elementname) { - if (is_string($source)) { + if (count($source->children()) == 0) { $target->addChild($elementname, self::escape($source)); } else { $child = $target->addChild($elementname);