Skip to content

Commit

Permalink
Improve search experience when searching for AP urls (#1244)
Browse files Browse the repository at this point in the history
Co-authored-by: Melroy van den Berg <melroy@melroy.org>
  • Loading branch information
BentiGorlich and melroy89 authored Nov 25, 2024
1 parent dd6ad9a commit 834e802
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use App\ActivityPub\ActorHandle;
use App\Entity\Magazine;
use App\Entity\User;
use App\Message\ActivityPub\Inbox\ActivityMessage;
use App\Message\ActivityPub\Inbox\CreateMessage;
use App\MessageHandler\ActivityPub\Inbox\CreateHandler;
use App\Service\ActivityPub\ApHttpClient;
use App\Service\ActivityPubManager;
use App\Service\SearchManager;
Expand All @@ -16,18 +17,17 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;

class SearchController extends AbstractController
{
public function __construct(
private readonly SearchManager $manager,
private readonly ActivityPubManager $activityPubManager,
private readonly MessageBusInterface $bus,
private readonly ApHttpClient $apHttpClient,
private readonly SubjectOverviewManager $overviewManager,
private readonly SettingsManager $settingsManager,
private readonly LoggerInterface $logger,
private readonly CreateHandler $createHandler,
) {
}

Expand Down Expand Up @@ -62,10 +62,21 @@ public function __invoke(Request $request): Response

// looking up object by AP id (i.e. urls)
if (false !== filter_var($query, FILTER_VALIDATE_URL)) {
$this->logger->debug('Query is a valid url');
$objects = $this->manager->findByApId($query);
if (!$objects) {
$body = $this->apHttpClient->getActivityObject($query, false);
$this->bus->dispatch(new ActivityMessage($body));
if (0 === \sizeof($objects)) {
$body = $this->apHttpClient->getActivityObject($query);
// the returned id could be different from the query url.
$postId = $body['id'];
$objects = $this->manager->findByApId($postId);
if (0 === \sizeof($objects)) {
try {
$this->createHandler->doWork(new CreateMessage($body));
$objects = $this->manager->findByApId($postId);
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
}
}
}
}

Expand Down

0 comments on commit 834e802

Please sign in to comment.