Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lazka committed Aug 29, 2024
1 parent 1821773 commit ef165b1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/Blob/BlobSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
use Dbp\Relay\CabinetBundle\TypesenseSync\DocumentTranslator;
use Dbp\Relay\CabinetBundle\TypesenseSync\TypesenseSync;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

class BlobSubscriber implements EventSubscriberInterface
{
private ConfigurationService $config;
private DocumentTranslator $translator;
private TypesenseSync $typesenseSync;
private MessageBusInterface $messageBus;

public function __construct(ConfigurationService $config, DocumentTranslator $translator, TypesenseSync $typesenseSync)
public function __construct(ConfigurationService $config, DocumentTranslator $translator, TypesenseSync $typesenseSync, MessageBusInterface $messageBus)
{
$this->config = $config;
$this->translator = $translator;
$this->typesenseSync = $typesenseSync;
$this->messageBus = $messageBus;
}

private function isForCabinet(FileData $fileData): bool
Expand Down Expand Up @@ -70,7 +74,7 @@ public function onFileAdded(AddFileDataByPostSuccessEvent $event)
}

$translated = $this->translateMetadata($fileData);
$this->typesenseSync->upsertPartialFile($translated);
$this->messageBus->dispatch(new TypesenseTask('upsert', $translated));
}

public function onFileChanged(ChangeFileDataByPatchSuccessEvent $event)
Expand All @@ -81,7 +85,7 @@ public function onFileChanged(ChangeFileDataByPatchSuccessEvent $event)
}

$translated = $this->translateMetadata($fileData);
$this->typesenseSync->upsertPartialFile($translated);
$this->messageBus->dispatch(new TypesenseTask('upsert', $translated));
}

public function onFileRemoved(DeleteFileDataByDeleteSuccessEvent $event)
Expand All @@ -92,6 +96,18 @@ public function onFileRemoved(DeleteFileDataByDeleteSuccessEvent $event)
}

$translated = $this->translateMetadata($fileData);
$this->typesenseSync->removePartialFile($translated);
$this->messageBus->dispatch(new TypesenseTask('delete', $translated));
}

#[AsMessageHandler]
public function handleTypesenseTask(TypesenseTask $task): void
{
if ($task->getAction() === 'upsert') {
$this->typesenseSync->upsertPartialFile($task->getDocument());
} elseif ($task->getAction() === 'delete') {
$this->typesenseSync->deletePartialFile($task->getDocument());
} else {
throw new \RuntimeException('unsupported task: ' . $task->getAction());
}
}
}
19 changes: 19 additions & 0 deletions src/Blob/TypesenseTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Dbp\Relay\CabinetBundle\Blob;

class TypesenseTask
{
public function __construct(private string $action, private array $document) {
}

public function getAction(): string
{
return $this->action;
}

public function getDocument(): array
{
return $this->document;
}
}
3 changes: 3 additions & 0 deletions src/DependencyInjection/DbpRelayCabinetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Dbp\Relay\CabinetBundle\DependencyInjection;

use Dbp\Relay\CabinetBundle\Authorization\AuthorizationService;
use Dbp\Relay\CabinetBundle\Blob\TypesenseTask;
use Dbp\Relay\CabinetBundle\Service\ConfigurationService;
use Dbp\Relay\CoreBundle\Extension\ExtensionTrait;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -88,5 +89,7 @@ public function prepend(ContainerBuilder $container): void
'Dbp\Relay\CabinetBundle\Migrations' => __DIR__.'/../Migrations',
],
]);

$this->addQueueMessageClass($container, TypesenseTask::class);
}
}
2 changes: 1 addition & 1 deletion src/TypesenseSync/TypesenseSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function upsertPartialFile(array $partialFileDocument): void
$this->searchIndex->addDocumentsToCollection($collectionName, [$partialFileDocument]);
}

public function removePartialFile(array $partialFileDocument): void
public function deletePartialFile(array $partialFileDocument): void
{
$collectionName = $this->searchIndex->getAliasName();
$id = $partialFileDocument['id'];
Expand Down

0 comments on commit ef165b1

Please sign in to comment.