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();
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
namespace Lewisdale\Webmentions\Gateways;
|
||||
|
||||
use Exception;
|
||||
use Lewisdale\Webmentions\Models\Author;
|
||||
use Lewisdale\Webmentions\Models\MentionType;
|
||||
use Lewisdale\Webmentions\Models\Webmention;
|
||||
use PDO;
|
||||
|
||||
class SqliteGateway extends WebmentionGatewayInterface {
|
||||
class SqliteGateway extends WebmentionGatewayInterface
|
||||
{
|
||||
private readonly PDO $connection;
|
||||
|
||||
function __construct(string $name)
|
||||
@@ -16,7 +17,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
|
||||
$this->up();
|
||||
}
|
||||
|
||||
protected function up() : void
|
||||
protected function up(): void
|
||||
{
|
||||
// Create Webmention table
|
||||
$sql = <<<SQL
|
||||
@@ -26,7 +27,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
|
||||
source TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
content TEXT,
|
||||
author TEXT
|
||||
author INTEGER
|
||||
);
|
||||
SQL;
|
||||
|
||||
@@ -48,7 +49,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
|
||||
$row["source"],
|
||||
MentionType::from($row["type"]),
|
||||
$row["content"],
|
||||
$row["author"]
|
||||
new Author((int)$row["author"])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -70,7 +71,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
|
||||
$row["source"],
|
||||
MentionType::from($row["type"]),
|
||||
$row["content"],
|
||||
$row["author"]
|
||||
new Author((int)$row["author"])
|
||||
);
|
||||
}
|
||||
$statement->closeCursor();
|
||||
@@ -94,11 +95,11 @@ class SqliteGateway extends WebmentionGatewayInterface {
|
||||
"source" => $webmention->source,
|
||||
"type" => $webmention->type->toString(),
|
||||
"content" => $webmention->content,
|
||||
"author" => $webmention->author,
|
||||
"author" => $webmention->author->id,
|
||||
]);
|
||||
$statement->closeCursor();
|
||||
|
||||
return $success ? (int) $this->connection->lastInsertId() : null;
|
||||
|
||||
return $success ? (int)$this->connection->lastInsertId() : null;
|
||||
}
|
||||
|
||||
public function delete(Webmention $webmention): void
|
||||
@@ -109,9 +110,9 @@ class SqliteGateway extends WebmentionGatewayInterface {
|
||||
$statement->closeCursor();
|
||||
}
|
||||
|
||||
public function find(array $values) : array
|
||||
public function find(array $values): array
|
||||
{
|
||||
$keys = implode(" AND ", array_map(function($v) {
|
||||
$keys = implode(" AND ", array_map(function ($v) {
|
||||
return "$v=:$v";
|
||||
}, array_keys($values)));
|
||||
$sql = <<<SQL
|
||||
@@ -125,4 +126,5 @@ class SqliteGateway extends WebmentionGatewayInterface {
|
||||
return $statement->fetchAll();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lewisdale\Webmentions\Models;
|
||||
|
||||
class Author
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $id = null,
|
||||
public ?string $name = "",
|
||||
public ?string $url = "",
|
||||
public ?string $photo = "",
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,23 @@
|
||||
|
||||
namespace Lewisdale\Webmentions\Models;
|
||||
|
||||
class Webmention {
|
||||
class Webmention
|
||||
{
|
||||
function __construct(
|
||||
public ?int $id,
|
||||
public string $target, // The target post
|
||||
public string $source,
|
||||
public ?int $id,
|
||||
public string $target, // The target post
|
||||
public string $source,
|
||||
public MentionType $type,
|
||||
public ?string $content,
|
||||
public ?string $author, // TODO: Should be reference to another model
|
||||
public ?string $content,
|
||||
public Author $author, // TODO: Should be reference to another model
|
||||
)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return "Webmention (id: {$this->id}, target: {$this->target}, source: {$this->source}, content: {$this->content}, author: {$this->author})";
|
||||
public function __toString()
|
||||
{
|
||||
return "Webmention (id: {$this->id}, target: {$this->target}, source: {$this->source}, content: {$this->content}, author: {$this->author->name})";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in new issue
Block a user