Add a type to webmentions

This commit is contained in:
Lewis Dale 2023-03-14 08:43:25 +00:00
parent 38885beb74
commit 9d9e465a37
5 changed files with 57 additions and 4 deletions

View File

@ -11,6 +11,7 @@ use Symfony\Component\HttpClient\HttpClient;
use League\Uri\Uri;
use Lewisdale\Webmentions\Exceptions\SourceNotFoundException;
use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException;
use Lewisdale\Webmentions\Models\MentionType;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Contracts\HttpClient\HttpClientInterface;
@ -56,7 +57,7 @@ class Endpoint {
$content = $this->parseContent($response->getContent(), $target);
$author = $this->parseAuthor($response->getContent());
$webmention = new Webmention(null, $target, $source, $content, $author);
$webmention = new Webmention(null, $target, $source, MentionType::from(""), $content, $author);
$this->gateway->save($webmention);
} else {
throw new SourceNotFoundException();

View File

@ -3,6 +3,7 @@
namespace Lewisdale\Webmentions\Gateways;
use Exception;
use Lewisdale\Webmentions\Models\MentionType;
use Lewisdale\Webmentions\Models\Webmention;
use PDO;
@ -23,6 +24,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
id INTEGER PRIMARY KEY,
target TEXT NOT NULL,
source TEXT NOT NULL,
type TEXT NOT NULL,
content TEXT,
author TEXT
);
@ -44,6 +46,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
$row["id"],
$row["target"],
$row["source"],
MentionType::from($row["type"]),
$row["content"],
$row["author"]
);
@ -65,6 +68,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
$row["id"],
$row["target"],
$row["source"],
MentionType::from($row["type"]),
$row["content"],
$row["author"]
);
@ -78,13 +82,20 @@ class SqliteGateway extends WebmentionGatewayInterface {
{
$sql = <<< SQL
INSERT INTO webmentions
(id, target, source, content, author)
(id, target, source, type, content, author)
VALUES
(:id, :target, :source, :content, :author);
(:id, :target, :source, :type, :content, :author);
SQL;
$statement = $this->connection->prepare($sql);
$success = $statement->execute((array) $webmention);
$success = $statement->execute([
"id" => $webmention->id,
"target" => $webmention->target,
"source" => $webmention->source,
"type" => $webmention->type->toString(),
"content" => $webmention->content,
"author" => $webmention->author,
]);
$statement->closeCursor();
return $success ? (int) $this->connection->lastInsertId() : null;

View File

@ -0,0 +1,31 @@
<?php declare(strict_types=1);
namespace Lewisdale\Webmentions\Models;
enum MentionType {
case Like;
case Comment;
case Reply;
case Mention;
public function toString() : string
{
return match ($this) {
MentionType::Like => "like",
MentionType::Comment => "comment",
MentionType::Reply => "reply",
MentionType::Mention => "mention"
};
}
public static function from(string $string) : MentionType
{
switch($string) {
case "like": return MentionType::Like;
case "comment": return MentionType::Comment;
case "reply": return MentionType::Reply;
default: return MentionType::Mention;
}
}
}
?>

View File

@ -7,6 +7,7 @@ class Webmention {
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
)

View File

@ -5,6 +5,7 @@ error_reporting(E_ALL); ini_set('display_errors',1);
use Lewisdale\Webmentions\Gateways\SqliteGateway;
use Lewisdale\Webmentions\Models\MentionType;
use Lewisdale\Webmentions\Models\Webmention;
use PHPUnit\Framework\TestCase;
@ -24,6 +25,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-post",
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
);
@ -38,6 +40,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-post",
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
);
@ -55,6 +58,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-post",
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
);
@ -74,6 +78,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
MentionType::Comment,
"No content",
"Some Author Name"
));
@ -84,6 +89,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-different-post",
"https://a-source.url",
MentionType::Like,
"No content",
"Some Author Name"
));
@ -100,6 +106,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
MentionType::Reply,
"No content",
"Some Author Name"
));
@ -108,6 +115,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-different-source.url",
MentionType::Like,
"No content",
"Some Author Name"
));
@ -116,6 +124,7 @@ class SqliteGatewayTest extends TestCase
null,
"https://lewisdale.dev/post/a-new-post",
"https://a-source.url",
MentionType::Comment,
"Some content",
"Some Author Name"
));