From d12c9a8f581a22f2f9d04b73c85dce890fb037a1 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 28 Jul 2022 19:05:04 +0000 Subject: [PATCH 1/2] Caching for fetched activities --- src/Protocol/ActivityPub/Processor.php | 47 ++++++++++++++++---------- src/Protocol/ActivityPub/Receiver.php | 8 ++--- src/Util/HTTPSignature.php | 2 +- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 421f2d4c0c..bfff5f84bb 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1201,6 +1201,33 @@ class Processor Logger::info('Fetched featured posts', ['new' => $new, 'old' => $old, 'contact' => $url]); } + public static function fetchCachedActivity(string $url, int $uid): array + { + $cachekey = self::CACHEKEY_FETCH_ACTIVITY . $uid . ':' . $url; + $object = DI::cache()->get($cachekey); + + if (!is_null($object)) { + Logger::debug('Fetch from cache', ['url' => $url, 'uid' => $uid]); + return $object; + } + + $object = ActivityPub::fetchContent($url, $uid); + if (empty($object)) { + Logger::notice('Activity was not fetchable, aborting.', ['url' => $url, 'uid' => $uid]); + return []; + } + + if (empty($object['id'])) { + Logger::notice('Activity has got not id, aborting. ', ['url' => $url, 'object' => $object]); + return []; + } + DI::cache()->set($cachekey, $object, Duration::FIVE_MINUTES); + + Logger::debug('Activity was fetched successfully', ['url' => $url, 'uid' => $uid]); + + return $object; + } + /** * Fetches missing posts * @@ -1220,23 +1247,9 @@ class Processor $uid = 0; } - $cachekey = self::CACHEKEY_FETCH_ACTIVITY . $url; - $object = DI::cache()->get($cachekey); - - if (is_null($object)) { - $object = ActivityPub::fetchContent($url, $uid); - if (empty($object)) { - Logger::notice('Activity was not fetchable, aborting.', ['url' => $url, 'uid' => $uid]); - return ''; - } - - if (empty($object['id'])) { - Logger::notice('Activity has got not id, aborting. ', ['url' => $url, 'object' => $object]); - return ''; - } - DI::cache()->set($cachekey, $object, Duration::FIVE_MINUTES); - } else { - Logger::debug('Fetch from cache', ['url' => $url]); + $object = self::fetchCachedActivity($url, $uid); + if (empty($object)) { + return ''; } $signer = []; diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index af6eca61e8..f09541b13d 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -241,7 +241,7 @@ class Receiver return 'as:' . $profile['type']; } - $data = ActivityPub::fetchContent($object_id, $uid); + $data = Processor::fetchCachedActivity($object_id, $uid); if (!empty($data)) { $object = JsonLD::compact($data); $type = JsonLD::fetchElement($object, '@type'); @@ -284,7 +284,7 @@ class Receiver if (!empty($id) && !$trust_source) { $fetch_uid = $uid ?: self::getBestUserForActivity($activity); - $fetched_activity = ActivityPub::fetchContent($fetch_id, $fetch_uid); + $fetched_activity = Processor::fetchCachedActivity($fetch_id, $fetch_uid); if (!empty($fetched_activity)) { $object = JsonLD::compact($fetched_activity); @@ -358,7 +358,7 @@ class Receiver // Fetch the activity on Lemmy "Announce" messages (announces of activities) if (($type == 'as:Announce') && in_array($object_type, array_merge(self::ACTIVITY_TYPES, ['as:Delete', 'as:Undo', 'as:Update']))) { - $data = ActivityPub::fetchContent($object_id, $fetch_uid); + $data = Processor::fetchCachedActivity($object_id, $fetch_uid); if (!empty($data)) { $type = $object_type; $activity = JsonLD::compact($data); @@ -1273,7 +1273,7 @@ class Receiver $type = JsonLD::fetchElement($object, '@type'); if (!$trust_source || empty($type)) { - $data = ActivityPub::fetchContent($object_id, $uid); + $data = Processor::fetchCachedActivity($object_id, $uid); if (!empty($data)) { $object = JsonLD::compact($data); Logger::info('Fetched content for ' . $object_id); diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 9f732acb6e..f4f0a4e85c 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -508,7 +508,7 @@ class HTTPSignature return false; } - $actor = JsonLD::fetchElement($object, 'actor', 'id'); + $actor = JsonLD::fetchElement($object, 'actor', 'id') ?? ''; } else { $actor = ''; } From 1aacff4ce1cafcb3ab1e232f9c01dd3daa770d15 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 28 Jul 2022 19:35:25 +0000 Subject: [PATCH 2/2] Fix fatal error --- src/Protocol/OStatus.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 64e32f2ac4..6531d75af1 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -797,6 +797,7 @@ class OStatus } if ($xml != '') { + $hub = ''; self::process($xml, $importer, $contact, $hub, $stored, false, Conversation::PULL); } else { Logger::info('XML could not be fetched for URI: ' . $related_uri . ' - href: ' . $related);