From: Michael <heluecht@pirati.ca>
Date: Wed, 3 Aug 2022 04:51:57 +0000 (+0000)
Subject: Don't always fetch parent posts
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3463e346932c27c4eb8b38fb3d1ecd0e83c519ad;p=friendica.git

Don't always fetch parent posts
---

diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php
index a409fec115..73f414d73c 100644
--- a/src/Protocol/ActivityPub/Processor.php
+++ b/src/Protocol/ActivityPub/Processor.php
@@ -265,12 +265,15 @@ class Processor
 	/**
 	 * Prepares data for a message
 	 *
-	 * @param array      $activity   Activity array
+	 * @param array $activity      Activity array
+	 * @param bool  $fetch_parents
+	 *
 	 * @return array Internal item
+	 *
 	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
 	 * @throws \ImagickException
 	 */
-	public static function createItem(array $activity): array
+	public static function createItem(array $activity, bool $fetch_parents = true): array
 	{
 		$item = [];
 		$item['verb'] = Activity::POST;
@@ -305,7 +308,7 @@ class Processor
 			return [];
 		}
 
-		if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
+		if ($fetch_parents && empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
 			$result = self::fetchParent($activity);
 			if (!empty($result)) {
 				if (($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
diff --git a/src/Protocol/ActivityPub/Queue.php b/src/Protocol/ActivityPub/Queue.php
index 751ee93bda..98c3b4d497 100644
--- a/src/Protocol/ActivityPub/Queue.php
+++ b/src/Protocol/ActivityPub/Queue.php
@@ -175,10 +175,11 @@ class Queue
 	 * Process the activity with the given id
 	 *
 	 * @param integer $id
+	 * @param bool    $fetch_parents
 	 *
 	 * @return bool
 	 */
-	public static function process(int $id): bool
+	public static function process(int $id, bool $fetch_parents = true): bool
 	{
 		$entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
 		if (empty($entry)) {
@@ -215,7 +216,7 @@ class Queue
 		}
 		DBA::close($receivers);
 
-		if (!Receiver::routeActivities($activity, $type, $push)) {
+		if (!Receiver::routeActivities($activity, $type, $push, $fetch_parents)) {
 			self::remove($activity);
 		}
 
@@ -236,7 +237,7 @@ class Queue
 				continue;
 			}
 			Logger::debug('Process leftover entry', $entry);
-			self::process($entry['id']);
+			self::process($entry['id'], false);
 		}
 		DBA::close($entries);
 	}
@@ -272,7 +273,7 @@ class Queue
 		$entries = DBA::select('inbox-entry', ['id'], ["`in-reply-to-id` = ? AND `object-id` != ?", $uri, $uri]);
 		while ($entry = DBA::fetch($entries)) {
 			$count += 1;
-			self::process($entry['id']);
+			self::process($entry['id'], false);
 		}
 		DBA::close($entries);
 		return $count;
diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php
index b3d67b2af7..1f7946af22 100644
--- a/src/Protocol/ActivityPub/Receiver.php
+++ b/src/Protocol/ActivityPub/Receiver.php
@@ -627,20 +627,21 @@ class Receiver
 	/**
 	 * Route activities
 	 *
-	 * @param array   $object_data
-	 * @param string  $type
-	 * @param boolean $push
+	 * @param array  $object_data
+	 * @param string $type
+	 * @param bool   $push
+	 * @param bool   $fetch_parents
 	 *
 	 * @return boolean Could the activity be routed?
 	 */
-	public static function routeActivities(array $object_data, string $type, bool $push): bool
+	public static function routeActivities(array $object_data, string $type, bool $push, bool $fetch_parents = true): bool
 	{
 		$activity = $object_data['object_activity']	?? [];
 
 		switch ($type) {
 			case 'as:Create':
 				if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
-					$item = ActivityPub\Processor::createItem($object_data);
+					$item = ActivityPub\Processor::createItem($object_data, $fetch_parents);
 					ActivityPub\Processor::postItem($object_data, $item);
 				} elseif (in_array($object_data['object_type'], ['pt:CacheFile'])) {
 					// Unhandled Peertube activity
@@ -652,7 +653,7 @@ class Receiver
 
 			case 'as:Invite':
 				if (in_array($object_data['object_type'], ['as:Event'])) {
-					$item = ActivityPub\Processor::createItem($object_data);
+					$item = ActivityPub\Processor::createItem($object_data, $fetch_parents);
 					ActivityPub\Processor::postItem($object_data, $item);
 				} else {
 					return false;
@@ -678,7 +679,7 @@ class Receiver
 					$object_data['thread-completion'] = Contact::getIdForURL($actor);
 					$object_data['completion-mode']   = self::COMPLETION_ANNOUCE;
 
-					$item = ActivityPub\Processor::createItem($object_data);
+					$item = ActivityPub\Processor::createItem($object_data, $fetch_parents);
 					if (empty($item)) {
 						return false;
 					}