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 League\Uri\Uri;
use Lewisdale\Webmentions\Exceptions\SourceNotFoundException; use Lewisdale\Webmentions\Exceptions\SourceNotFoundException;
use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException; use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException;
use Lewisdale\Webmentions\Models\MentionType;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
@ -56,7 +57,7 @@ class Endpoint {
$content = $this->parseContent($response->getContent(), $target); $content = $this->parseContent($response->getContent(), $target);
$author = $this->parseAuthor($response->getContent()); $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); $this->gateway->save($webmention);
} else { } else {
throw new SourceNotFoundException(); throw new SourceNotFoundException();

View File

@ -3,6 +3,7 @@
namespace Lewisdale\Webmentions\Gateways; namespace Lewisdale\Webmentions\Gateways;
use Exception; use Exception;
use Lewisdale\Webmentions\Models\MentionType;
use Lewisdale\Webmentions\Models\Webmention; use Lewisdale\Webmentions\Models\Webmention;
use PDO; use PDO;
@ -23,6 +24,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
target TEXT NOT NULL, target TEXT NOT NULL,
source TEXT NOT NULL, source TEXT NOT NULL,
type TEXT NOT NULL,
content TEXT, content TEXT,
author TEXT author TEXT
); );
@ -44,6 +46,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
$row["id"], $row["id"],
$row["target"], $row["target"],
$row["source"], $row["source"],
MentionType::from($row["type"]),
$row["content"], $row["content"],
$row["author"] $row["author"]
); );
@ -65,6 +68,7 @@ class SqliteGateway extends WebmentionGatewayInterface {
$row["id"], $row["id"],
$row["target"], $row["target"],
$row["source"], $row["source"],
MentionType::from($row["type"]),
$row["content"], $row["content"],
$row["author"] $row["author"]
); );
@ -78,13 +82,20 @@ class SqliteGateway extends WebmentionGatewayInterface {
{ {
$sql = <<< SQL $sql = <<< SQL
INSERT INTO webmentions INSERT INTO webmentions
(id, target, source, content, author) (id, target, source, type, content, author)
VALUES VALUES
(:id, :target, :source, :content, :author); (:id, :target, :source, :type, :content, :author);
SQL; SQL;
$statement = $this->connection->prepare($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(); $statement->closeCursor();
return $success ? (int) $this->connection->lastInsertId() : null; 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 ?int $id,
public string $target, // The target post public string $target, // The target post
public string $source, public string $source,
public MentionType $type,
public ?string $content, public ?string $content,
public ?string $author, // TODO: Should be reference to another model 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\Gateways\SqliteGateway;
use Lewisdale\Webmentions\Models\MentionType;
use Lewisdale\Webmentions\Models\Webmention; use Lewisdale\Webmentions\Models\Webmention;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -24,6 +25,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-post", "https://lewisdale.dev/post/a-post",
"https://a-source.url", "https://a-source.url",
MentionType::Like,
"No content", "No content",
"Some Author Name" "Some Author Name"
); );
@ -38,6 +40,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-post", "https://lewisdale.dev/post/a-post",
"https://a-source.url", "https://a-source.url",
MentionType::Like,
"No content", "No content",
"Some Author Name" "Some Author Name"
); );
@ -55,6 +58,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-post", "https://lewisdale.dev/post/a-post",
"https://a-source.url", "https://a-source.url",
MentionType::Like,
"No content", "No content",
"Some Author Name" "Some Author Name"
); );
@ -74,6 +78,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-new-post", "https://lewisdale.dev/post/a-new-post",
"https://a-source.url", "https://a-source.url",
MentionType::Comment,
"No content", "No content",
"Some Author Name" "Some Author Name"
)); ));
@ -84,6 +89,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-different-post", "https://lewisdale.dev/post/a-different-post",
"https://a-source.url", "https://a-source.url",
MentionType::Like,
"No content", "No content",
"Some Author Name" "Some Author Name"
)); ));
@ -100,6 +106,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-new-post", "https://lewisdale.dev/post/a-new-post",
"https://a-source.url", "https://a-source.url",
MentionType::Reply,
"No content", "No content",
"Some Author Name" "Some Author Name"
)); ));
@ -108,6 +115,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-new-post", "https://lewisdale.dev/post/a-new-post",
"https://a-different-source.url", "https://a-different-source.url",
MentionType::Like,
"No content", "No content",
"Some Author Name" "Some Author Name"
)); ));
@ -116,6 +124,7 @@ class SqliteGatewayTest extends TestCase
null, null,
"https://lewisdale.dev/post/a-new-post", "https://lewisdale.dev/post/a-new-post",
"https://a-source.url", "https://a-source.url",
MentionType::Comment,
"Some content", "Some content",
"Some Author Name" "Some Author Name"
)); ));