Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org, webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id lookup, contact avatar/h-card fetch, profile image download, and the CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http, and enables Http::preventStrayRequests() globally in tests so any un-faked outbound call now fails loudly instead of silently hitting the network. The CloudConvert retry-until-finished middleware in AppServiceProvider is replaced by a plain polling loop in SaveScreenshot, which also fixes a latent bug where the old middleware decoded a response object instead of its body. Bridgy syndication jobs keep their tries=1/no-retry semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers (Header, UriResolver, Utils) stay in SendWebMentions since Http has no equivalent for them. Every affected test file's Guzzle MockHandler/HandlerStack boilerplate is replaced with Http::fake()/Http::sequence(). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
132 lines
3.5 KiB
PHP
132 lines
3.5 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Jobs;
|
||
|
||
use App\Models\Note;
|
||
use GuzzleHttp\Psr7\Header;
|
||
use GuzzleHttp\Psr7\UriResolver;
|
||
use GuzzleHttp\Psr7\Utils;
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Illuminate\Queue\SerializesModels;
|
||
use Illuminate\Support\Facades\Http;
|
||
use Illuminate\Support\Str;
|
||
use Mf2\Parser;
|
||
|
||
class SendWebMentions implements ShouldQueue
|
||
{
|
||
use InteractsWithQueue;
|
||
use Queueable;
|
||
use SerializesModels;
|
||
|
||
/**
|
||
* Create a new job instance.
|
||
*/
|
||
public function __construct(
|
||
protected Note $note
|
||
) {}
|
||
|
||
/**
|
||
* Execute the job.
|
||
*/
|
||
public function handle(): void
|
||
{
|
||
$urlsInReplyTo = explode(' ', $this->note->in_reply_to ?? '');
|
||
$urlsNote = $this->getLinks($this->note->note);
|
||
$urls = array_filter(array_merge($urlsInReplyTo, $urlsNote));
|
||
foreach ($urls as $url) {
|
||
$endpoint = $this->discoverWebmentionEndpoint($url);
|
||
if ($endpoint !== null) {
|
||
Http::asForm()->post($endpoint, [
|
||
'source' => $this->note->uri,
|
||
'target' => $url,
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Discover if a URL has a webmention endpoint.
|
||
*/
|
||
public function discoverWebmentionEndpoint(string $url): ?string
|
||
{
|
||
// let’s not send webmentions to myself
|
||
if (parse_url($url, PHP_URL_HOST) === parse_url(config('app.url'), PHP_URL_HOST)) {
|
||
return null;
|
||
}
|
||
if (Str::startsWith($url, '/notes/tagged/')) {
|
||
return null;
|
||
}
|
||
|
||
$endpoint = null;
|
||
|
||
$response = Http::get($url);
|
||
// check HTTP Headers for webmention endpoint
|
||
$links = Header::parse($response->header('Link'));
|
||
foreach ($links as $link) {
|
||
if (array_key_exists('rel', $link) && mb_stristr($link['rel'], 'webmention')) {
|
||
return $this->resolveUri(trim($link[0], '<>'), $url);
|
||
}
|
||
}
|
||
|
||
// failed to find a header so parse HTML
|
||
$html = $response->body();
|
||
|
||
if ($html === '') {
|
||
return null;
|
||
}
|
||
|
||
$mf2 = new Parser($html, $url);
|
||
$rels = $mf2->parseRelsAndAlternates();
|
||
if (array_key_exists('webmention', $rels[0])) {
|
||
$endpoint = $rels[0]['webmention'][0];
|
||
} elseif (array_key_exists('http://webmention.org/', $rels[0])) {
|
||
$endpoint = $rels[0]['http://webmention.org/'][0];
|
||
}
|
||
|
||
if ($endpoint === null) {
|
||
return null;
|
||
}
|
||
|
||
return $this->resolveUri($endpoint, $url);
|
||
}
|
||
|
||
/**
|
||
* Get the URLs from a note.
|
||
*/
|
||
public function getLinks(?string $html): array
|
||
{
|
||
if ($html === '' || is_null($html)) {
|
||
return [];
|
||
}
|
||
|
||
$urls = [];
|
||
$dom = new \DOMDocument;
|
||
$dom->loadHTML($html);
|
||
$anchors = $dom->getElementsByTagName('a');
|
||
foreach ($anchors as $anchor) {
|
||
$urls[] = ($anchor->hasAttribute('href')) ? $anchor->getAttribute('href') : false;
|
||
}
|
||
|
||
return $urls;
|
||
}
|
||
|
||
/**
|
||
* Resolve a URI if necessary.
|
||
*/
|
||
public function resolveUri(string $url, string $base): string
|
||
{
|
||
$endpoint = Utils::uriFor($url);
|
||
if ($endpoint->getScheme() !== '') {
|
||
return (string) $endpoint;
|
||
}
|
||
|
||
return (string) UriResolver::resolve(
|
||
Utils::uriFor($base),
|
||
$endpoint
|
||
);
|
||
}
|
||
}
|