Skip to content

Commit

Permalink
fix: handle empty mail address in webmentions #60
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Oct 29, 2023
1 parent b00d9aa commit 8c5b3e8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
49 changes: 45 additions & 4 deletions tests/utils/ReceiveKommentTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use mauricerenck\Komments\KommentReceiver;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -55,8 +56,7 @@ public function testSetEmailAddressDisabled()

public function testSetConvertToWebmention()
{
$commentMock = Array
(
$commentMock = [
'komment' => 'This is my comment',
'url' => '',
'email' => '[email protected]',
Expand All @@ -69,7 +69,7 @@ public function testSetConvertToWebmention()
'replyTo' => '',
'replyHandle' => '',
'cts' => 10,
);
];

$expectedResult = [
'type' => 'KOMMENT',
Expand All @@ -90,7 +90,48 @@ public function testSetConvertToWebmention()

$kommentReceiver = new KommentReceiver();
$webmention = $kommentReceiver->convertToWebmention($commentMock, page('phpunit'));

$this->assertEquals($expectedResult, $webmention);
}

public function testGetAuthorData()
{
$kommentReceiver = new KommentReceiver();

$expectedResult = [
'name' => 'John Doe',
'avatar' => 'https://web.site/avatar.jpg',
'url' => 'https://web.site',
'email' => '[email protected]'
];

$author = $kommentReceiver->getAuthorData([
'name' => 'John Doe',
'avatar' => 'https://web.site/avatar.jpg',
'url' => 'https://web.site',
'email' => '[email protected]'
]);

$this->assertEquals($expectedResult, $author);
}

public function testGetAuthorDataHandlesMissingData()
{
$kommentReceiver = new KommentReceiver();

$expectedResult = [
'name' => 'John Doe',
'avatar' => 'https://web.site/avatar.jpg',
'url' => null,
'email' => null
];

$author = $kommentReceiver->getAuthorData([
'name' => 'John Doe',
'avatar' => 'https://web.site/avatar.jpg',
'email' => ''
]);

$this->assertEquals($expectedResult, $author);
}
}
20 changes: 15 additions & 5 deletions utils/receiveKomment.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public function convertToWebmention($formData, $targetPage)
public function createKomment($webmention, $spamlevel = 0, $isVerified = false, $autoPublish = false)
{
$publishComments = $isVerified || $autoPublish;
$author = $this->getAuthorData($webmention['author']);

return [
'id' => md5($webmention['target'] . $webmention['author']['name'] . $webmention['published']),
'avatar' => $this->setUrl($webmention['author']['avatar']),
'author' => $webmention['author']['name'],
'authorUrl' => $this->setUrl($webmention['author']['url']),
'authorEmail' => $this->setEmail($webmention['author']['email']),
'id' => md5($webmention['target'] . $author['name'] . $webmention['published']),
'avatar' => $this->setUrl($author['avatar']),
'author' => $author['name'],
'authorUrl' => $this->setUrl($author['url']),
'authorEmail' => $this->setEmail($author['email']),
'source' => $this->setUrl($webmention['source']),
'target' => $this->setUrl($webmention['target']),
'mentionOf' => (!isset($webmention['mentionOf']) || is_null($webmention['mentionOf'])) ? $this->setUrl($webmention['target']) : $webmention['mentionOf'],
Expand All @@ -66,6 +67,15 @@ public function createKomment($webmention, $spamlevel = 0, $isVerified = false,
];
}

public function getAuthorData(array $author) {
return [
'name' => $author['name'] ?? null,
'avatar' => $author['avatar'] ?? null,
'url' => $author['url'] ?? null,
'email' => $author['email'] ?? null,
];
}

public function getPageFromUrl(string $url)
{
if (V::url($url)) {
Expand Down

0 comments on commit 8c5b3e8

Please sign in to comment.