From 5a8c661257539782d3bcb5531408f25794e8b814 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 17 Mar 2025 12:47:08 +0100 Subject: [PATCH] OembedExtractor.php: finalize --- inc/Services/Embed/OembedExtractor.php | 28 +++++++++++--------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/inc/Services/Embed/OembedExtractor.php b/inc/Services/Embed/OembedExtractor.php index e4706859..0de72ce3 100644 --- a/inc/Services/Embed/OembedExtractor.php +++ b/inc/Services/Embed/OembedExtractor.php @@ -7,6 +7,7 @@ use Vichan\Data\OembedResponse; class OembedExtractor { private const DEFAULT_CACHE_TIMEOUT = 3600; // 1 hour. + private const MIN_CACHE_TIMEOUT = 900; // 15 minutes. private CacheDriver $cache; private HttpDriver $http; @@ -41,30 +42,25 @@ class OembedExtractor { ); $json = \json_decode($body, null, 512, \JSON_THROW_ON_ERROR); - if (!isset($json['title'])) { - throw new \RuntimeException("Missing type in response from $provider_url"); - } $ret = [ - 'type' => $json['type'], - 'title' => $json['title'], - 'cache_age' => $json['cache_age'], - 'thumbnail_url' => $json['thumbnail_url'], - 'thumbnail_width' => $json['thumbnail_width'], - 'thumbnail_height' => $json['thumbnail_heigh'] + 'title' => $json['title'] ?? null, + 'thumbnail_url' => $json['thumbnail_url'] ?? null, ]; - $cache_timeout = $ret['cache_age'] ?? self::DEFAULT_CACHE_TIMEOUT; + $cache_timeout = self::DEFAULT_CACHE_TIMEOUT; + if (isset($json['cache_age'])) { + $cache_age = \intval($json['cache_age']); + if ($cache_age > 0) { + $cache_age = \max($cache_age, self::MIN_CACHE_TIMEOUT); + } + } $this->cache->set("oembed_embedder_$url$provider_url", $ret, $cache_timeout); } $resp = new OembedResponse(); - $resp->type = $ret['title']; - $resp->title = $ret['title'] ?? null; - $resp->cache_age = $ret['cache_age'] ?? null; - $resp->thumbnail_url = $ret['thumbnail_url'] ?? null; - $resp->thumbnail_width = $ret['thumbnail_width'] ?? null; - $resp->thumbnail_height = $ret['thumbnail_height'] ?? null; + $resp->title = $ret['title']; + $resp->thumbnail_url = $ret['thumbnail_url']; return $ret; } }