Move webmention parsing and authorship into the app
The jonnybarnes/webmentions-parser package is now archived, and its guzzlehttp/guzzle ^6|^7 constraint was the only thing holding Guzzle back from 8. Port the parts we use into App\Services\WebMention and drop the package. Parser keeps getMentionType and the checkInReplyTo/LikeOf/RepostOf checks unchanged. Authorship follows the same authorship algorithm, with some fixes: - fetch the author page with Laravel's Http client, so tests can fake it, and treat connection failures as AuthorshipParserException too - keep state in locals so one instance can be reused safely - make the url == uid == author-page check actually compare the h-card's uid, rather than an undefined variable Add direct tests for both, and mark the library as archived on the projects page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
79c112bb86
commit
df9b0dc490
8 changed files with 526 additions and 64 deletions
9
app/Exceptions/AuthorshipParserException.php
Normal file
9
app/Exceptions/AuthorshipParserException.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class AuthorshipParserException extends Exception {}
|
||||
9
app/Exceptions/InvalidMentionException.php
Normal file
9
app/Exceptions/InvalidMentionException.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class InvalidMentionException extends Exception {}
|
||||
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Exceptions\AuthorshipParserException;
|
||||
use App\Models\Like;
|
||||
use App\Services\WebMention\Authorship;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
|
@ -12,8 +14,6 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Jonnybarnes\WebmentionsParser\Authorship;
|
||||
use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException;
|
||||
|
||||
class ProcessLike implements ShouldQueue
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,17 +4,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Exceptions\InvalidMentionException;
|
||||
use App\Exceptions\RemoteContentNotFoundException;
|
||||
use App\Models\Note;
|
||||
use App\Models\WebMention;
|
||||
use App\Services\WebMention\Parser;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Jonnybarnes\WebmentionsParser\Exceptions\InvalidMentionException;
|
||||
use Jonnybarnes\WebmentionsParser\Parser;
|
||||
use Mf2;
|
||||
|
||||
class ProcessWebMention implements ShouldQueue
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Exceptions\AuthorshipParserException;
|
||||
use App\Services\WebMention\Authorship;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
|
|
@ -12,8 +14,6 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Jonnybarnes\WebmentionsParser\Authorship;
|
||||
use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException;
|
||||
|
||||
class SaveProfileImage implements ShouldQueue
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Services\WebMention\Authorship;
|
||||
use App\Traits\FilterHtml;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
||||
|
|
@ -13,7 +14,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Jonnybarnes\WebmentionsParser\Authorship;
|
||||
|
||||
#[Table('webmentions')]
|
||||
#[Guarded(['id'])]
|
||||
|
|
|
|||
152
app/Services/WebMention/Authorship.php
Normal file
152
app/Services/WebMention/Authorship.php
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\WebMention;
|
||||
|
||||
use App\Exceptions\AuthorshipParserException;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
use function Mf2\parse;
|
||||
|
||||
/**
|
||||
* Determine the author of a post from its parsed microformats, following
|
||||
* the IndieWeb authorship algorithm: https://indieweb.org/authorship
|
||||
*
|
||||
* Originally from the jonnybarnes/webmentions-parser package.
|
||||
*/
|
||||
class Authorship
|
||||
{
|
||||
/**
|
||||
* Find the author of the first h-entry (or h-feed) in the microformats.
|
||||
*
|
||||
* Returns the author's h-card, or their name as a string if that's all
|
||||
* we have, or false if the author can't be determined.
|
||||
*
|
||||
* @throws AuthorshipParserException
|
||||
*/
|
||||
public function findAuthor(array $mf, bool $permalink = true): array|string|false
|
||||
{
|
||||
$hEntry = null;
|
||||
$hFeed = null;
|
||||
foreach ($mf['items'] ?? [] as $item) {
|
||||
foreach ($item['type'] ?? [] as $type) {
|
||||
if ($type === 'h-entry') {
|
||||
$hEntry = $item;
|
||||
} elseif ($type === 'h-feed') {
|
||||
$hFeed = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($hEntry === null && $hFeed === null) {
|
||||
throw new AuthorshipParserException('No h-entry or h-feed found');
|
||||
}
|
||||
|
||||
// If the h-entry has an author property use that
|
||||
$author = $hEntry['properties']['author'] ?? null;
|
||||
|
||||
// A parent h-feed with an h-card takes precedence
|
||||
foreach ($hFeed['children'] ?? [] as $child) {
|
||||
if (($child['type'][0] ?? null) === 'h-card') {
|
||||
$author = $child;
|
||||
}
|
||||
}
|
||||
|
||||
$authorPage = null;
|
||||
|
||||
if ($author !== null) {
|
||||
// Flatten a single string entry
|
||||
if (is_array($author) && count($author) === 1 && is_string($author[0] ?? null)) {
|
||||
$author = $author[0];
|
||||
}
|
||||
|
||||
// A URL author is the author-page, anything else is the author itself
|
||||
if (is_string($author) && filter_var($author, FILTER_VALIDATE_URL)) {
|
||||
$authorPage = $author;
|
||||
} else {
|
||||
return $this->normalise($author);
|
||||
}
|
||||
}
|
||||
|
||||
// If no author-page and h-entry is a permalink then use the rel-author link
|
||||
if ($authorPage === null && $permalink && isset($mf['rels']['author'])) {
|
||||
$authorPage = is_array($mf['rels']['author']) ? $mf['rels']['author'][0] : $mf['rels']['author'];
|
||||
}
|
||||
|
||||
if ($authorPage === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->findAuthorOnPage($authorPage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the author-page and look for a representative h-card on it.
|
||||
*
|
||||
* @throws AuthorshipParserException
|
||||
*/
|
||||
private function findAuthorOnPage(string $authorPage): array|false
|
||||
{
|
||||
try {
|
||||
$response = Http::get($authorPage);
|
||||
} catch (ConnectionException) {
|
||||
throw new AuthorshipParserException('Unable to connect to the author’s page');
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new AuthorshipParserException('Unable to get the content from the author’s page');
|
||||
}
|
||||
|
||||
$authorMf2 = parse($response->body(), $authorPage);
|
||||
$hCards = array_filter(
|
||||
$authorMf2['items'] ?? [],
|
||||
fn (array $item): bool => in_array('h-card', $item['type'] ?? [], true)
|
||||
);
|
||||
|
||||
// Use the first h-card where url == uid == author-page
|
||||
foreach ($hCards as $hCard) {
|
||||
if (
|
||||
in_array($authorPage, $hCard['properties']['url'] ?? [], true)
|
||||
&& in_array($authorPage, $hCard['properties']['uid'] ?? [], true)
|
||||
) {
|
||||
return $this->normalise($hCard);
|
||||
}
|
||||
}
|
||||
|
||||
// Else the first h-card with a url that matches a rel-me link on the page
|
||||
$relMeLinks = $authorMf2['rels']['me'] ?? [];
|
||||
foreach ($hCards as $hCard) {
|
||||
foreach ($hCard['properties']['url'] ?? [] as $url) {
|
||||
if (in_array($url, $relMeLinks, true)) {
|
||||
return $this->normalise($hCard);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Else the first h-card with url == author-page
|
||||
foreach ($hCards as $hCard) {
|
||||
if (in_array($authorPage, $hCard['properties']['url'] ?? [], true)) {
|
||||
return $this->normalise($hCard);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function normalise(array|string $author): array|string
|
||||
{
|
||||
if (is_array($author) && array_key_exists(0, $author)) {
|
||||
$author = $author[0];
|
||||
}
|
||||
|
||||
if (is_string($author)) {
|
||||
return $author;
|
||||
}
|
||||
|
||||
unset($author['value'], $author['children']);
|
||||
|
||||
return $author;
|
||||
}
|
||||
}
|
||||
86
app/Services/WebMention/Parser.php
Normal file
86
app/Services/WebMention/Parser.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\WebMention;
|
||||
|
||||
use App\Exceptions\InvalidMentionException;
|
||||
|
||||
/**
|
||||
* Inspect the parsed microformats of a webmention source.
|
||||
*
|
||||
* Originally from the jonnybarnes/webmentions-parser package.
|
||||
*/
|
||||
class Parser
|
||||
{
|
||||
/**
|
||||
* Return the type of mention.
|
||||
*
|
||||
* @throws InvalidMentionException when the mention type can't be determined
|
||||
*/
|
||||
public function getMentionType(array $microformats): string
|
||||
{
|
||||
foreach (['in-reply-to', 'like-of', 'repost-of'] as $type) {
|
||||
if ($this->arrayKeyExistsRecursive($type, $microformats)) {
|
||||
return $type;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidMentionException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a reply is to the intended target.
|
||||
*/
|
||||
public function checkInReplyTo(array $microformats, string $target): bool
|
||||
{
|
||||
foreach ($microformats['items'] ?? [] as $item) {
|
||||
$inReplyTo = $item['properties']['in-reply-to'] ?? null;
|
||||
if ($inReplyTo === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The reply context may be an h-cite rather than a plain URL
|
||||
if (is_array($inReplyTo[0]) && ($inReplyTo[0]['properties']['url'][0] ?? null) === $target) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (in_array($target, $inReplyTo, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the microformats contain a like of the target.
|
||||
*/
|
||||
public function checkLikeOf(array $microformats, string $target): bool
|
||||
{
|
||||
return in_array($target, $microformats['items'][0]['properties']['like-of'] ?? [], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the microformats contain a repost of the target.
|
||||
*/
|
||||
public function checkRepostOf(array $microformats, string $target): bool
|
||||
{
|
||||
return in_array($target, $microformats['items'][0]['properties']['repost-of'] ?? [], true);
|
||||
}
|
||||
|
||||
private function arrayKeyExistsRecursive(string $needle, array $haystack): bool
|
||||
{
|
||||
if (array_key_exists($needle, $haystack)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($haystack as $value) {
|
||||
if (is_array($value) && $this->arrayKeyExistsRecursive($needle, $value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue