Skip to content

Commit

Permalink
Add metadata event
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner committed Oct 10, 2024
1 parent c05c2ca commit cdce9ec
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/Commands/SyncMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function handle(): void
'collection:id,collection_chain_id',
];
foreach ($query->with($withs)->lazy(config('enjin-platform.sync_metadata.data_chunk_size')) as $attribute) {
SyncMetadataJob::dispatch($attribute->value_string);
SyncMetadataJob::dispatch(
$attribute->collection->collection_chain_id,
$attribute->token?->token_chain_id,
$attribute->value_string
);
$progress->advance();
}

Expand Down
29 changes: 29 additions & 0 deletions src/Events/Substrate/MultiTokens/MetadataUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Enjin\Platform\Events\Substrate\MultiTokens;

use Enjin\Platform\Channels\PlatformAppChannel;
use Enjin\Platform\Events\PlatformBroadcastEvent;
use Illuminate\Broadcasting\Channel;

class MetadataUpdated extends PlatformBroadcastEvent
{
/**
* Create a new event instance.
*/
public function __construct(string $collectionId, ?string $tokenId = null)
{
parent::__construct();

$this->broadcastData = [
'collectionId' => $collectionId,
'tokenId' => $tokenId,
];

$this->broadcastChannels = [
new Channel("collection;{$collectionId}"),
new Channel("token;{$collectionId}-{$tokenId}"),
new PlatformAppChannel(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function resolve(
)
)
->get()
->each(fn ($attribute) => $metadataService->fetchAndCache($attribute->value_string));
->each(fn ($attribute) => $metadataService->fetchAttributeWithEvent($attribute));

return true;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Jobs/SyncMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Enjin\Platform\Jobs;

use Enjin\Platform\Services\Database\MetadataService;
use Enjin\Platform\Support\Hex;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
Expand All @@ -22,19 +22,17 @@ class SyncMetadata implements ShouldQueue
/**
* Create a new job instance.
*/
public function __construct(protected string $url) {}
public function __construct(protected Model $attribute) {}

/**
* Execute the job.
*/
public function handle(MetadataService $service): void
{
try {
$service->fetchAndCache(
Hex::isHexEncoded($this->url) ? Hex::safeConvertToString($this->url) : $this->url
);
$service->fetchAttributeWithEvent($this->attribute);
} catch (Throwable $e) {
Log::error("Unable to sync metadata for url {$this->url}", $e->getMessage());
Log::error("Unable to sync metadata for url {$this->attribute->value_string}", $e->getMessage());
}
}
}
15 changes: 15 additions & 0 deletions src/Services/Database/MetadataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enjin\Platform\Services\Database;

use Enjin\Platform\Clients\Implementations\MetadataHttpClient;
use Enjin\Platform\Events\Substrate\MultiTokens\MetadataUpdated;
use Enjin\Platform\Models\Laravel\Attribute;
use Enjin\Platform\Support\Hex;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -41,6 +42,20 @@ public function fetchUrl(string $url): mixed
return $this->client->fetch($url) ?: null;
}

public function fetchAttributeWithEvent(Attribute $attribute): mixed
{
$old = $this->getCache($url = $attribute->value_string);
$new = $this->fetchAndCache($url);
if ($old !== $new) {
event(new MetadataUpdated(
$attribute->collection->collection_chain_id,
$attribute->token?->token_chain_id,
));
}

return $new;
}

public function fetchAndCache(string $url, bool $forget = true): mixed
{
$url = $this->convertHexToString($url);
Expand Down

0 comments on commit cdce9ec

Please sign in to comment.