Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
fix(posts): cache the link card as an array, not the DTO
Paulo Castellano8866e4ed4673
diff --git a/app/Services/Social/LinkCard/LinkCardFetcher.php b/app/Services/Social/LinkCard/LinkCardFetcher.phpindex b4985799d..d987cf42d 100644--- a/app/Services/Social/LinkCard/LinkCardFetcher.php+++ b/app/Services/Social/LinkCard/LinkCardFetcher.php@@ -32,11 +32,16 @@ class LinkCardFetcherreturn null;}- return Cache::remember(+ // Cache a plain array, never the DTO: a rich object does not round-trip+ // cleanly through every cache driver (it comes back as an incomplete+ // class), whereas primitives always do.+ $data = Cache::remember('link_card:'.sha1($url),now()->addMinutes(self::CACHE_MINUTES),- fn (): ?LinkCardMetadata => $this->build($url),+ fn (): ?array => $this->build($url)?->toArray(),);++ return $data === null ? null : LinkCardMetadata::fromArray($data);}private function build(string $url): ?LinkCardMetadatadiff --git a/app/Services/Social/LinkCard/LinkCardMetadata.php b/app/Services/Social/LinkCard/LinkCardMetadata.phpindex 53760b73f..95d50346e 100644--- a/app/Services/Social/LinkCard/LinkCardMetadata.php+++ b/app/Services/Social/LinkCard/LinkCardMetadata.php@@ -16,6 +16,22 @@ final readonly class LinkCardMetadatapublic ?string $imageUrl,) {}+ /**+ * Rebuild the DTO from its cached array form (the inverse of toArray, minus+ * the derived `domain`, which toArray recomputes).+ *+ * @param array{uri?: string, title?: string, description?: string, image?: ?string} $data+ */+ public static function fromArray(array $data): self+ {+ return new self(+ uri: (string) data_get($data, 'uri', ''),+ title: (string) data_get($data, 'title', ''),+ description: (string) data_get($data, 'description', ''),+ imageUrl: data_get($data, 'image'),+ );+ }+/*** @return array{uri: string, domain: string, title: string, description: string, image: ?string}*/diff --git a/tests/Feature/Services/Social/LinkCard/LinkCardFetcherTest.php b/tests/Feature/Services/Social/LinkCard/LinkCardFetcherTest.phpindex 95d148104..ca024fdf6 100644--- a/tests/Feature/Services/Social/LinkCard/LinkCardFetcherTest.php+++ b/tests/Feature/Services/Social/LinkCard/LinkCardFetcherTest.php@@ -3,6 +3,8 @@declare(strict_types=1);use App\Services\Social\LinkCard\LinkCardFetcher;+use App\Services\Social\LinkCard\LinkCardMetadata;+use Illuminate\Support\Facades\Cache;use Illuminate\Support\Facades\Http;// example.com resolves to a public IP so the SafeHttpFetcher SSRF guard passes;@@ -50,8 +52,16 @@ test('caches the result so a repeated url is fetched once', function () {]);$fetcher = app(LinkCardFetcher::class);- $fetcher->fetch('https://example.com');- $fetcher->fetch('https://example.com');+ $first = $fetcher->fetch('https://example.com');+ $second = $fetcher->fetch('https://example.com');Http::assertSentCount(1);++ // The cache must hold a primitive array, not the DTO — a rich object does+ // not round-trip through every cache driver (it comes back as an incomplete+ // class). A cache hit still reconstructs a proper DTO.+ expect(Cache::get('link_card:'.sha1('https://example.com')))->toBeArray();+ expect($second)->toBeInstanceOf(LinkCardMetadata::class)+ ->and($second->uri)->toBe($first->uri)+ ->and($second->title)->toBe('Cached');});