Skip to content

Commit e331bca

Browse files
Benmarkharding
authored andcommitted
[engine] Chat links front#6168
1 parent eed3a14 commit e331bca

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed

Core/Chat/Entities/ChatMessage.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTimeInterface;
66
use Minds\Core\Di\Di;
77
use Minds\Entities\EntityInterface;
8+
use Minds\Helpers\Export;
89

910
class ChatMessage implements EntityInterface
1011
{
@@ -84,8 +85,8 @@ public function export(): array
8485
'roomGuid' => $this->roomGuid,
8586
'type' => $this->getType(),
8687
'subtype' => $this->getSubtype(),
87-
'sender' => $sender->export(),
88-
'plainText' => $this->plainText,
88+
'sender' => $sender?->export(),
89+
'plainText' => Export::sanitizeString($this->plainText),
8990
'createdTimestampUnix' => $this->createdAt->getTimestamp()
9091
];
9192
}

Core/Chat/Types/ChatMessageNode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Minds\Core\Chat\Entities\ChatMessage;
55
use Minds\Core\Feeds\GraphQL\Types\UserEdge;
66
use Minds\Core\GraphQL\Types\NodeInterface;
7+
use Minds\Helpers\Export;
78
use TheCodingMachine\GraphQLite\Annotations\Field;
89
use TheCodingMachine\GraphQLite\Annotations\Type;
910
use TheCodingMachine\GraphQLite\Types\ID;
@@ -48,7 +49,7 @@ public function getRoomGuid(): string
4849
#[Field]
4950
public function getPlainText(): string
5051
{
51-
return $this->chatMessage->plainText;
52+
return Export::sanitizeString($this->chatMessage->plainText);
5253
}
5354

5455
/**

Helpers/Export.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public static function sanitize($array)
2424
} else {
2525
$return[$k] = (string) $v;
2626
}
27-
$return[$k] = htmlspecialchars($return[$k], ENT_NOQUOTES);
28-
$return[$k] = str_replace('&', '&', $return[$k]);
29-
$return[$k] = str_replace(' ', ' ', $return[$k]);
27+
$return[$k] = self::sanitizeString($return[$k]);
3028
} elseif (is_bool($v)) {
3129
$return[$k] = $v;
3230
} elseif (is_object($v) || is_array($v)) {
@@ -38,4 +36,15 @@ public static function sanitize($array)
3836

3937
return $return;
4038
}
39+
40+
/**
41+
* Sanitized a string for output
42+
*/
43+
public static function sanitizeString(string $input): string
44+
{
45+
$output = htmlspecialchars($input, ENT_NOQUOTES);
46+
$output = str_replace('&', '&', $output);
47+
$output = str_replace(' ', ' ', $output);
48+
return $output;
49+
}
4150
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Spec\Minds\Core\Chat\Entities;
4+
5+
use Minds\Core\Chat\Entities\ChatMessage;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class ChatMessageSpec extends ObjectBehavior
9+
{
10+
public function let()
11+
{
12+
$this->beConstructedWith(1, 2, 3, '');
13+
}
14+
15+
public function it_is_initializable()
16+
{
17+
$this->shouldHaveType(ChatMessage::class);
18+
}
19+
20+
public function it_should_export_sanitized_plaintext()
21+
{
22+
$plainText = 'just <b>for testing</b>';
23+
$this->beConstructedWith(1, 2, 3, $plainText);
24+
25+
$export = $this->export();
26+
$export['plainText']->shouldBe('just &lt;b&gt;for testing&lt;/b&gt;');
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Spec\Minds\Core\Chat\Types;
4+
5+
use Minds\Core\Chat\Entities\ChatMessage;
6+
use Minds\Core\Chat\Types\ChatMessageNode;
7+
use Minds\Core\Feeds\GraphQL\Types\UserEdge;
8+
use PhpSpec\ObjectBehavior;
9+
use PhpSpec\Wrapper\Collaborator;
10+
11+
class ChatMessageNodeSpec extends ObjectBehavior
12+
{
13+
private Collaborator $chatMessageMock;
14+
private Collaborator $userEdgeMock;
15+
16+
public function let(ChatMessage $chatMessageMock, UserEdge $userEdgeMock)
17+
{
18+
$this->beConstructedWith($chatMessageMock, $userEdgeMock);
19+
$this->chatMessageMock = $chatMessageMock;
20+
$this->userEdgeMock = $userEdgeMock;
21+
}
22+
23+
public function it_is_initializable()
24+
{
25+
$this->shouldHaveType(ChatMessageNode::class);
26+
}
27+
28+
public function it_should_return_sanitized_plaintext()
29+
{
30+
$plainText = 'just <b>for testing</b>';
31+
$chatMessage = new ChatMessage(1, 2, 3, $plainText);
32+
$this->beConstructedWith($chatMessage, $this->userEdgeMock);
33+
34+
$this->getPlainText()->shouldBe('just &lt;b&gt;for testing&lt;/b&gt;');
35+
}
36+
37+
}

0 commit comments

Comments
 (0)