Skip to content

Commit 2d0f4e6

Browse files
authored
feat: added option to store email of senders (#58)
1 parent 80faa28 commit 2d0f4e6

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

blueprints/sections/komments.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fields:
1212
kommentsInbox:
1313
type: structure
1414
label: Inbox
15+
# translate: false
1516
fields:
1617
author:
1718
type: text
@@ -25,6 +26,10 @@ fields:
2526
type: url
2627
label: Author Url
2728
width: 2/3
29+
authorEmail:
30+
type: email
31+
label: Author email
32+
width: 1/3
2833
kommentType:
2934
type: select
3035
label: Type

docs/options.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ You can fine tune the komments plugin to behave as you whish. Use these options
4343

4444
## Moderation settings
4545

46-
| Option | Default | Description |
47-
| --------------------------------------- | --------- | ----------------------------------------------------------- |
48-
| `komment-auto-publish` | `false` | When you receive a komment set status to published |
49-
| `auto-publish-verified` | `true` | New comments by verified users are automatically published |
50-
| `moderation.autoPublish` | `[]` | An array of email addresses which comments will be published without moderation |
46+
| Option | Default | Description |
47+
| ------------------------ | ------- | ------------------------------------------------------------------------------- |
48+
| `komment-auto-publish` | `false` | When you receive a komment set status to published |
49+
| `auto-publish-verified` | `true` | New comments by verified users are automatically published |
50+
| `moderation.autoPublish` | `[]` | An array of email addresses which comments will be published without moderation |
51+
52+
## Privacy settings
53+
54+
| Option | Default | Description |
55+
| -------------------- | ------- | -------------------------------------------------------------------------- |
56+
| `privacy.storeEmail` | `false` | Enable to also store the email address of the comment sender in plain text |

tests/utils/ReceiveKommentTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,53 @@ public function testAutoPublishUnknownAddress()
4444

4545
$this->assertFalse($isVerified);
4646
}
47+
48+
public function testSetEmailAddressDisabled()
49+
{
50+
$kommentReceiver = new KommentReceiver();
51+
$result = $kommentReceiver->setEmail('[email protected]');
52+
53+
$this->assertNull($result);
54+
}
55+
56+
public function testSetConvertToWebmention()
57+
{
58+
$commentMock = Array
59+
(
60+
'komment' => 'This is my comment',
61+
'url' => '',
62+
'email' => '[email protected]',
63+
'author' => 'John Doe',
64+
'author_url' => 'https://web.site',
65+
'wmSource' => 'https://komments.test:8890/en/phpunit',
66+
'wmTarget' => 'https://komments.test:8890/en/phpunit',
67+
'wmProperty' => 'komment',
68+
'quote' => '',
69+
'replyTo' => '',
70+
'replyHandle' => '',
71+
'cts' => 10,
72+
);
73+
74+
$expectedResult = [
75+
'type' => 'KOMMENT',
76+
'target' => '/en/phpunit',
77+
'source' => '/en/phpunit',
78+
'mentionOf' => null,
79+
'published' => date('c'),
80+
'content' => 'This is my comment',
81+
'quote' => '',
82+
'author' => [
83+
'type' => 'card',
84+
'name' => 'John Doe',
85+
'avatar' => 'https://www.gravatar.com/avatar/cc1bdde8cb8ec60527b5102beafc99a6',
86+
'url' => 'https://web.site',
87+
'email' => null,
88+
]
89+
];
90+
91+
$kommentReceiver = new KommentReceiver();
92+
$webmention = $kommentReceiver->convertToWebmention($commentMock, page('phpunit'));
93+
94+
$this->assertEquals($expectedResult, $webmention);
95+
}
4796
}

utils/receiveKomment.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function convertToWebmention($formData, $targetPage)
3838
'name' => $this->setAuthorName($formData['author']),
3939
'avatar' => $this->setAvatarFromEmail($formData['email']),
4040
'url' => $this->setUrl($formData['author_url']),
41+
'email' => $this->setEmail($formData['email']),
4142
]
4243
];
4344
}
@@ -51,6 +52,7 @@ public function createKomment($webmention, $spamlevel = 0, $isVerified = false,
5152
'avatar' => $this->setUrl($webmention['author']['avatar']),
5253
'author' => $webmention['author']['name'],
5354
'authorUrl' => $this->setUrl($webmention['author']['url']),
55+
'authorEmail' => $this->setEmail($webmention['author']['email']),
5456
'source' => $this->setUrl($webmention['source']),
5557
'target' => $this->setUrl($webmention['target']),
5658
'mentionOf' => (!isset($webmention['mentionOf']) || is_null($webmention['mentionOf'])) ? $this->setUrl($webmention['target']) : $webmention['mentionOf'],
@@ -186,6 +188,21 @@ public function setAvatarFromEmail(string $email)
186188
return null;
187189
}
188190

191+
public function setEmail(string | null $email) {
192+
193+
if(is_null($email)) {
194+
return null;
195+
}
196+
197+
if(option('mauricerenck.komments.privacy.storeEmail', false)) {
198+
if (V::email($email)) {
199+
return $email;
200+
}
201+
}
202+
203+
return null;
204+
}
205+
189206
public function sendReponseToClient(string $headlineTranslationString, string $messageTranslationString, int $httpCode, bool $shouldReturnJson)
190207
{
191208
if ($shouldReturnJson) {

0 commit comments

Comments
 (0)