From 9d9e465a3749897726cff3e6c0133a601bdb1747 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Tue, 14 Mar 2023 08:43:25 +0000 Subject: [PATCH] Add a type to webmentions --- src/Endpoint.php | 3 ++- src/Gateways/SqliteGateway.php | 17 ++++++++++++--- src/Models/MentionType.php | 31 ++++++++++++++++++++++++++++ src/Models/Webmention.php | 1 + tests/Gateways/SqliteGatewayTest.php | 9 ++++++++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/Models/MentionType.php diff --git a/src/Endpoint.php b/src/Endpoint.php index 60865d9..acdcd26 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -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(); diff --git a/src/Gateways/SqliteGateway.php b/src/Gateways/SqliteGateway.php index 63ff0fc..f88d261 100644 --- a/src/Gateways/SqliteGateway.php +++ b/src/Gateways/SqliteGateway.php @@ -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; diff --git a/src/Models/MentionType.php b/src/Models/MentionType.php new file mode 100644 index 0000000..95d40b6 --- /dev/null +++ b/src/Models/MentionType.php @@ -0,0 +1,31 @@ + "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; + } + } +} +?> \ No newline at end of file diff --git a/src/Models/Webmention.php b/src/Models/Webmention.php index 7e49dc0..fcad793 100644 --- a/src/Models/Webmention.php +++ b/src/Models/Webmention.php @@ -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 ) diff --git a/tests/Gateways/SqliteGatewayTest.php b/tests/Gateways/SqliteGatewayTest.php index 1f62aba..811d760 100644 --- a/tests/Gateways/SqliteGatewayTest.php +++ b/tests/Gateways/SqliteGatewayTest.php @@ -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" ));