|
|
|
@ -7,6 +7,7 @@ namespace App\Twig; |
|
|
|
use App\Service\CacheService; |
|
|
|
use App\Service\CacheService; |
|
|
|
use App\Service\NostrPathHelper; |
|
|
|
use App\Service\NostrPathHelper; |
|
|
|
use Symfony\Component\Asset\Packages; |
|
|
|
use Symfony\Component\Asset\Packages; |
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
use Throwable; |
|
|
|
use Throwable; |
|
|
|
use Twig\Extension\AbstractExtension; |
|
|
|
use Twig\Extension\AbstractExtension; |
|
|
|
use Twig\TwigFunction; |
|
|
|
use Twig\TwigFunction; |
|
|
|
@ -22,6 +23,8 @@ final class ArticleCardCoverExtension extends AbstractExtension |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private const DEFAULT_PACKAGE_IMAGE = 'icons/favicon-96x96.png'; |
|
|
|
private const DEFAULT_PACKAGE_IMAGE = 'icons/favicon-96x96.png'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private const OG_FALLBACK_PACKAGE_IMAGE = 'og-image.jpg'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var array<string, string> lowercase 64-hex pubkey → resolved cover URL (author picture or site default) |
|
|
|
* @var array<string, string> lowercase 64-hex pubkey → resolved cover URL (author picture or site default) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -31,6 +34,7 @@ final class ArticleCardCoverExtension extends AbstractExtension |
|
|
|
private readonly CacheService $cacheService, |
|
|
|
private readonly CacheService $cacheService, |
|
|
|
private readonly NostrPathHelper $nostrPathHelper, |
|
|
|
private readonly NostrPathHelper $nostrPathHelper, |
|
|
|
private readonly Packages $packages, |
|
|
|
private readonly Packages $packages, |
|
|
|
|
|
|
|
private readonly RequestStack $requestStack, |
|
|
|
) { |
|
|
|
) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -38,9 +42,63 @@ final class ArticleCardCoverExtension extends AbstractExtension |
|
|
|
{ |
|
|
|
{ |
|
|
|
return [ |
|
|
|
return [ |
|
|
|
new TwigFunction('article_card_cover', $this->articleCardCover(...)), |
|
|
|
new TwigFunction('article_card_cover', $this->articleCardCover(...)), |
|
|
|
|
|
|
|
new TwigFunction('article_og_image', $this->articleOgImage(...)), |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Absolute URL + whether to emit og:image:width/height (1200×630) for the site default OG JPEG only. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return array{href: string, use_default_dimensions: bool} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function articleOgImage(?string $articleImage, ?string $pubkeyHex): array |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$cover = $this->articleCardCover($articleImage, $pubkeyHex); |
|
|
|
|
|
|
|
$defaultCover = $this->defaultSiteImageUrl(); |
|
|
|
|
|
|
|
$useDefaultDimensions = false; |
|
|
|
|
|
|
|
$chosen = $cover; |
|
|
|
|
|
|
|
if ($chosen === $defaultCover) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
$chosen = $this->packages->getUrl(self::OG_FALLBACK_PACKAGE_IMAGE); |
|
|
|
|
|
|
|
$useDefaultDimensions = true; |
|
|
|
|
|
|
|
} catch (Throwable) { |
|
|
|
|
|
|
|
$useDefaultDimensions = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
|
|
|
|
|
|
'href' => $this->absolutizeForOpenGraph($chosen), |
|
|
|
|
|
|
|
'use_default_dimensions' => $useDefaultDimensions, |
|
|
|
]; |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Crawlers require absolute https URLs; article/profile images are often https, //, or site-relative. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function absolutizeForOpenGraph(string $urlOrPath): string |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$u = trim($urlOrPath); |
|
|
|
|
|
|
|
if ($u === '') { |
|
|
|
|
|
|
|
return ''; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (preg_match('#^https?://#i', $u) === 1) { |
|
|
|
|
|
|
|
return $u; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (str_starts_with($u, '//')) { |
|
|
|
|
|
|
|
return 'https:'.$u; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$request = $this->requestStack->getMainRequest(); |
|
|
|
|
|
|
|
if ($request === null) { |
|
|
|
|
|
|
|
return $u; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$base = $request->getSchemeAndHttpHost().$request->getBaseUrl(); |
|
|
|
|
|
|
|
if (str_starts_with($u, '/')) { |
|
|
|
|
|
|
|
return $base.$u; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $base.'/'.ltrim($u, '/'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param string|null $articleImage Cover URL stored on the article, if any |
|
|
|
* @param string|null $articleImage Cover URL stored on the article, if any |
|
|
|
* @param string|null $pubkeyHex 64-char hex (lowercase) Nostr public key, if any |
|
|
|
* @param string|null $pubkeyHex 64-char hex (lowercase) Nostr public key, if any |
|
|
|
|