Work on parsing authors and content from mentions
This commit is contained in:
1 parent
a5e0902366
commit
a429befc94
6 files changed
+282
-77
No files matched your search
+37
-35
@@ -3,44 +3,46 @@
|
||||
namespace Lewisdale\Webmentions;
|
||||
|
||||
use League\Uri\Exceptions\SyntaxError;
|
||||
use League\Uri\Uri;
|
||||
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
|
||||
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
|
||||
use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
|
||||
use Lewisdale\Webmentions\Models\Webmention;
|
||||
use Symfony\Component\HttpClient\HttpClient;
|
||||
use League\Uri\Uri;
|
||||
use Lewisdale\Webmentions\Exceptions\SourceNotFoundException;
|
||||
use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException;
|
||||
use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
|
||||
use Lewisdale\Webmentions\Models\Author;
|
||||
use Lewisdale\Webmentions\Models\MentionType;
|
||||
use Lewisdale\Webmentions\Models\Webmention;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class Endpoint {
|
||||
class Endpoint
|
||||
{
|
||||
|
||||
function __construct(
|
||||
private readonly HttpClientInterface $httpClient,
|
||||
private readonly WebmentionGatewayInterface $gateway
|
||||
private readonly HttpClientInterface $httpClient,
|
||||
private readonly WebmentionGatewayInterface $gateway,
|
||||
)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
public function validateUrl(string $url) : bool {
|
||||
public function validateUrl(string $url): bool
|
||||
{
|
||||
try {
|
||||
$uri = Uri::createFromString($url);
|
||||
$scheme = $uri->getScheme();
|
||||
$schemeValid = in_array($scheme, ["http", "https"]);
|
||||
|
||||
return $schemeValid && !!filter_var($url, FILTER_VALIDATE_URL);
|
||||
|
||||
} catch (SyntaxError $e)
|
||||
{
|
||||
|
||||
} catch (SyntaxError $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function receiveWebmention(string $source, string $target) : void
|
||||
|
||||
public function receiveWebmention(string $source, string $target): void
|
||||
{
|
||||
// Validate that both source and target are actual domains
|
||||
if (!$this->validateUrl($source) || !$this->validateUrl($target))
|
||||
{
|
||||
if (!$this->validateUrl($source) || !$this->validateUrl($target)) {
|
||||
throw new InvalidUrlException();
|
||||
}
|
||||
|
||||
@@ -52,12 +54,10 @@ class Endpoint {
|
||||
// Parse content from the source
|
||||
$response = $this->httpClient->request('GET', $source);
|
||||
|
||||
if ($response->getStatusCode() < 400)
|
||||
{
|
||||
if ($response->getStatusCode() < 400) {
|
||||
$document = new Crawler($response->getContent());
|
||||
|
||||
if (!$this->hasMention($target, $document))
|
||||
{
|
||||
if (!$this->hasMention($target, $document)) {
|
||||
throw new TargetNotMentionedException();
|
||||
}
|
||||
|
||||
@@ -66,26 +66,26 @@ class Endpoint {
|
||||
$content = $this->parseContent($target, $container, $type);
|
||||
$author = $this->parseAuthor($container);
|
||||
|
||||
$webmention = new Webmention(null, $target, $source, $type, null, $author);
|
||||
$webmention = new Webmention(null, $target, $source, $type, $content, $author);
|
||||
$this->gateway->save($webmention);
|
||||
} else {
|
||||
throw new SourceNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
private function hasMention(string $target, Crawler $document) : bool
|
||||
private function hasMention(string $target, Crawler $document): bool
|
||||
{
|
||||
return $document->filter('a[href="' . $target . '"]')->count() > 0;
|
||||
}
|
||||
|
||||
private function getContainer(string $target, Crawler $document) : Crawler
|
||||
private function getContainer(string $target, Crawler $document): Crawler
|
||||
{
|
||||
return $document->filter('a[href="' . $target . '"]')->closest('.h-entry') ?? $document;
|
||||
}
|
||||
|
||||
private function parseMentionType(string $target, Crawler $document) : MentionType
|
||||
private function parseMentionType(string $target, Crawler $document): MentionType
|
||||
{
|
||||
$class = $document->filter('a[href="'. $target . '"]')->attr('class');
|
||||
$class = $document->filter('a[href="' . $target . '"]')->attr('class');
|
||||
|
||||
if (str_contains($class, "u-like-of")) {
|
||||
return MentionType::Like;
|
||||
@@ -98,28 +98,30 @@ class Endpoint {
|
||||
return MentionType::Mention;
|
||||
}
|
||||
|
||||
private function parseContent(string $target, Crawler $document, MentionType $type) : ?string
|
||||
private function parseContent(string $target, Crawler $document, MentionType $type): ?string
|
||||
{
|
||||
return match ($type) {
|
||||
MentionType::Like => "Liked this post",
|
||||
MentionType::Reply => $document->innerText(),
|
||||
MentionType::Reply, MentionType::Mention => $document->text(),
|
||||
MentionType::Repost => "Reposted this post",
|
||||
MentionType::Mention => $document->innerText(),
|
||||
};
|
||||
}
|
||||
|
||||
private function parseAuthor(Crawler $document) : ?string
|
||||
private function parseAuthor(Crawler $document): Author
|
||||
{
|
||||
$card = $document->filter('.p-author.h-card')->eq(0);
|
||||
|
||||
if ($card->count())
|
||||
{
|
||||
$card = $document->filter('.h-card');
|
||||
|
||||
if (!$card->count()) {
|
||||
$card = $document->closest('.h-card');
|
||||
}
|
||||
|
||||
if ($card && $card->count()) {
|
||||
$name = $card->filter('.p-name')?->text("");
|
||||
$url = $card->filter('.u-url')?->text("");
|
||||
$url = $card->filter('.u-url')->count() ? $card->filter('.u-url')->attr('href') : "";
|
||||
$photo = $card->filter('.u-photo')->count() ? $card->filter('.u-photo')->attr('src') : "";
|
||||
|
||||
return implode(", ", [$name, $url, $photo]);
|
||||
return new Author(null, $name, $url, $photo);
|
||||
}
|
||||
return null;
|
||||
return new Author();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user