- Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id']]);
- /**
- * Instead of calling recursively self::fetchMissingActivity which can hit PHP's default function nesting
- * limit of 256 recursive calls, we push the parent activity fetch parameters in this queue. The initial
- * caller is responsible for processing the remaining queue once the original activity has been processed.
- */
- $fetchQueue->push(new FetchQueueItem($activity['reply-to-id'], $activity));
+ $recursion_depth = $activity['recursion-depth'] ?? 0;
+ 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::isActivityGone($activity['reply-to-id'])) {
+ // Recursively delete this and all depending entries
+ Queue::deleteById($activity['entry-id']);
+ return [];
+ }
+ $fetch_by_worker = empty($result);
+ } else {
+ Logger::notice('Recursion level is too high.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+ $fetch_by_worker = true;
+ }
+
+ if ($fetch_by_worker && Queue::hasWorker($activity)) {
+ Logger::notice('There is already a worker task to fetch the post.', ['id' => $activity['id'], 'parent' => $activity['reply-to-id']]);
+ $fetch_by_worker = false;
+ if (!empty($conversation)) {
+ return [];
+ }
+ }
+
+ if ($fetch_by_worker) {
+ Logger::notice('Fetching is done by worker.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+ $activity['recursion-depth'] = 0;
+ $wid = Worker::add(PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
+ Queue::setWorkerId($activity, $wid);
+ if (!empty($conversation)) {
+ return [];
+ }
+ } elseif (!empty($result)) {
+ if (($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
+ $item['thr-parent'] = $result;
+ }
+ }