]> git.mxchange.org Git - friendica.git/commitdiff
Remove entries from queue / relay detection
authorMichael <heluecht@pirati.ca>
Wed, 20 Jul 2022 05:37:40 +0000 (05:37 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 20 Jul 2022 05:37:40 +0000 (05:37 +0000)
src/Model/APContact.php
src/Protocol/ActivityPub/Processor.php
src/Protocol/ActivityPub/Receiver.php
src/Util/XML.php

index baa364e940636a4e1d39ea7e40cadd5fed0e7bdb..ba2e3c9734fdff020a3dcc6e35958cb0f4f44ce1 100644 (file)
@@ -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;
+       }
 }
index 6e81cb767f69a1e925225dc432c84cc12739f973..e6ff43f06acd807e53c1a260e21a5c26931d0a45 100644 (file)
@@ -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);
        }
 
        /**
index 11e7151e1e9f57e7bff9b0cd1d86390bce6a8d12..4d7309001604e36e72378960b30c267661c18eb8 100644 (file)
@@ -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;
                }
 
index e371ea3cd628df93f6396027fcc595691fd2156e..a6e6cd5bab9d0c0417490e7304463150dfca4891 100644 (file)
@@ -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);