mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-12-22 07:42:41 +00:00
Update ActivityPubFetchService, reduce cache ttl from 1 hour to 7.5 mins and add uncached fetchRequest method
This commit is contained in:
parent
edad436d4d
commit
21da2b642c
1 changed files with 58 additions and 53 deletions
|
@ -25,59 +25,8 @@ class ActivityPubFetchService
|
|||
$urlKey = hash('sha256', $url);
|
||||
$key = self::CACHE_KEY.$domainKey.':'.$urlKey;
|
||||
|
||||
return Cache::remember($key, 3600, function () use ($url) {
|
||||
$baseHeaders = [
|
||||
'Accept' => 'application/activity+json',
|
||||
];
|
||||
|
||||
$headers = HttpSignature::instanceActorSign($url, false, $baseHeaders, 'get');
|
||||
$headers['Accept'] = 'application/activity+json';
|
||||
$headers['User-Agent'] = 'PixelFedBot/1.0.0 (Pixelfed/'.config('pixelfed.version').'; +'.config('app.url').')';
|
||||
|
||||
try {
|
||||
$res = Http::withOptions([
|
||||
'allow_redirects' => [
|
||||
'max' => 2,
|
||||
'protocols' => ['https'],
|
||||
]])
|
||||
->withHeaders($headers)
|
||||
->timeout(30)
|
||||
->connectTimeout(5)
|
||||
->retry(3, 500)
|
||||
->get($url);
|
||||
} catch (RequestException $e) {
|
||||
return;
|
||||
} catch (ConnectionException $e) {
|
||||
return;
|
||||
} catch (Exception $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $res->ok()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $res->hasHeader('Content-Type')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$acceptedTypes = [
|
||||
'application/activity+json; charset=utf-8',
|
||||
'application/activity+json',
|
||||
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
||||
];
|
||||
|
||||
$contentType = $res->getHeader('Content-Type')[0];
|
||||
|
||||
if (! $contentType) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! in_array($contentType, $acceptedTypes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $res->body();
|
||||
return Cache::remember($key, 450, function () use ($url) {
|
||||
return self::fetchRequest($url);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -130,4 +79,60 @@ class ActivityPubFetchService
|
|||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public static function fetchRequest($url, $returnJsonFormat = false)
|
||||
{
|
||||
$baseHeaders = [
|
||||
'Accept' => 'application/activity+json',
|
||||
];
|
||||
|
||||
$headers = HttpSignature::instanceActorSign($url, false, $baseHeaders, 'get');
|
||||
$headers['Accept'] = 'application/activity+json';
|
||||
$headers['User-Agent'] = 'PixelFedBot/1.0.0 (Pixelfed/'.config('pixelfed.version').'; +'.config('app.url').')';
|
||||
|
||||
try {
|
||||
$res = Http::withOptions([
|
||||
'allow_redirects' => [
|
||||
'max' => 2,
|
||||
'protocols' => ['https'],
|
||||
]])
|
||||
->withHeaders($headers)
|
||||
->timeout(30)
|
||||
->connectTimeout(5)
|
||||
->retry(3, 500)
|
||||
->get($url);
|
||||
} catch (RequestException $e) {
|
||||
return;
|
||||
} catch (ConnectionException $e) {
|
||||
return;
|
||||
} catch (Exception $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $res->ok()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $res->hasHeader('Content-Type')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$acceptedTypes = [
|
||||
'application/activity+json; charset=utf-8',
|
||||
'application/activity+json',
|
||||
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
||||
];
|
||||
|
||||
$contentType = $res->getHeader('Content-Type')[0];
|
||||
|
||||
if (! $contentType) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! in_array($contentType, $acceptedTypes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $returnJsonFormat ? $res->json() : $res->body();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue