Parse likes and reply types using microformats
This commit is contained in:
1 parent
9d9e465a37
commit
0c316d18a2
2 files changed
+174
-11
No files matched your search
+18
-6
@@ -16,10 +16,10 @@ use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class Endpoint {
|
||||
private readonly WebmentionGatewayInterface $gateway;
|
||||
|
||||
function __construct(
|
||||
private readonly HttpClientInterface $httpClient
|
||||
private readonly HttpClientInterface $httpClient,
|
||||
private readonly WebmentionGatewayInterface $gateway
|
||||
)
|
||||
{}
|
||||
|
||||
@@ -54,17 +54,18 @@ class Endpoint {
|
||||
|
||||
if ($response->getStatusCode() < 400)
|
||||
{
|
||||
$content = $this->parseContent($response->getContent(), $target);
|
||||
[$type, $content] = $this->parseContent($response->getContent(), $target);
|
||||
$author = $this->parseAuthor($response->getContent());
|
||||
|
||||
$webmention = new Webmention(null, $target, $source, MentionType::from(""), $content, $author);
|
||||
$webmention = new Webmention(null, $target, $source, $type, $content, $author);
|
||||
$this->gateway->save($webmention);
|
||||
} else {
|
||||
throw new SourceNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
private function parseContent(string $content, string $target) : ?string
|
||||
|
||||
private function parseContent(string $content, string $target) : array
|
||||
{
|
||||
$body = new Crawler($content);
|
||||
$anchors = $body->filter('a[href="'. $target . '"]');
|
||||
@@ -72,8 +73,19 @@ class Endpoint {
|
||||
if (!$anchors->count()) {
|
||||
throw new TargetNotMentionedException();
|
||||
}
|
||||
$type = $this->classToMentionType($anchors->attr('class'));
|
||||
return [$type, null];
|
||||
}
|
||||
|
||||
return null;
|
||||
private function classToMentionType(string $class = "") : MentionType
|
||||
{
|
||||
if (str_contains($class, "u-like-of")) {
|
||||
return MentionType::Like;
|
||||
} else if (str_contains($class, "u-reply-to")) {
|
||||
return MentionType::Reply;
|
||||
}
|
||||
|
||||
return MentionType::Mention;
|
||||
}
|
||||
|
||||
private function parseAuthor(string $author) : ?string
|
||||
|
||||
Reference in new issue
Block a user