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
<?php
declare(strict_types=1);
namespace App\Services\Social\LinkCard;
use App\Services\Http\SafeHttpFetcher;
use App\Support\UrlDetector;
use Illuminate\Support\Facades\Cache;
/**
* Builds a link preview card (OpenGraph metadata) for the first URL in a piece
* of text. Shared by BlueskyPublisher (publish-time embed) and the editor
* preview endpoint. Returns null whenever a card cannot or should not be built,
* so callers degrade gracefully.
*/
class LinkCardFetcher
{
/** Cards for a public URL are identical for everyone; cache briefly and globally. */
private const int CACHE_MINUTES = 10;
/** OpenGraph metadata should occur near the document head; never buffer an unbounded page. */
private const int MAX_HTML_BYTES = 2 * 1024 * 1024;
public function __construct(
private readonly SafeHttpFetcher $http = new SafeHttpFetcher,
private readonly OpenGraphExtractor $extractor = new OpenGraphExtractor,
) {}
public function fetch(string $text): ?LinkCardMetadata
{
$url = UrlDetector::firstUrl($text);
if ($url === null) {
return null;
}
// 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 (): ?array => $this->build($url)?->toArray(),
);
return $data === null ? null : LinkCardMetadata::fromArray($data);
}
private function build(string $url): ?LinkCardMetadata
{
$html = $this->http->tryGetBody($url, self::MAX_HTML_BYTES);
if ($html === null) {
return null;
}
$meta = $this->extractor->extract($html, $url);
$title = data_get($meta, 'title');
$description = data_get($meta, 'description');
if ($title === null && $description === null) {
return null;
}
return new LinkCardMetadata(
uri: $url,
title: $title ?? '',
description: $description ?? '',
imageUrl: data_get($meta, 'image'),
);
}
}