8000 test msgtype support in API · symfony/symfony@9cf0cdf · GitHub
[go: up one dir, main page]

Skip to content

Commit 9cf0cdf

Browse files
committed
test msgtype support in API
1 parent cd47767 commit 9cf0cdf

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
< 10000 /div>
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Matrix\Exception;
13+
14+
use Symfony\Component\Notifier\Bridge\Matrix\MatrixTransport;
15+
use Symfony\Component\Notifier\Exception\LogicException;
16+
17+
class UnsupportesMsgTypeByAPIException extends LogicException
18+
{
19+
public function __construct(string $given)
20+
{
21+
parent::__construct(\sprintf('Unsupported message type: "%s". Only %s are supported by Matrix Synapse Client-Server API v3', $given, implode(', ',MatrixTransport::SUPPORTED_MSG_TYPES_BY_API)));
22+
}
23+
}

src/Symfony/Component/Notifier/Bridge/Matrix/MatrixTransport.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\Matrix;
1313

1414
use Symfony\Component\Notifier\Bridge\Matrix\Exception\UnsupportedRecipientTypeException;
15+
use Symfony\Component\Notifier\Bridge\Matrix\Exception\UnsupportesMsgTypeByAPIException;
1516
use Symfony\Component\Notifier\Exception\LogicException;
1617
use Symfony\Component\Notifier\Exception\TransportException;
1718
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
@@ -29,6 +30,8 @@
2930
*/
3031
final class MatrixTransport exten 10000 ds AbstractTransport
3132
{
33+
// not all Message Types are supported by Symfony API
34+
public const SUPPORTED_MSG_TYPES_BY_API = ['m.text', 'm.emote', 'm.notice', 'm.image', 'm.file', 'm.audio', 'm.video', 'm.key.verification'];
3235
public function __construct(
3336
#[\SensitiveParameter] private string $accessToken,
3437
private bool $ssl,
@@ -54,6 +57,20 @@ protected function doSend(MessageInterface $message): SentMessage
5457
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
5558
}
5659

60+
if (($opts = $message->getOptions()) && !$message->getOptions() instanceof MatrixOptions) {
61+
throw new UnsupportedOptionsException(__CLASS__, MatrixOptions::class, $opts);
62+
}
63+
64+
$options = $opts ? $opts->toArray() : [];
65+
66+
// Defaulting msgtype to 'm.text' if msgtype is empty or not set;
67+
$options['msgtype'] = $options['msgtype'] ?? 'm.text';
68+
69+
// check if msgtype is supported by API
70+
if (!in_array($options['msgtype'], self::SUPPORTED_MSG_TYPES_BY_API, true)) {
71+
throw new UnsupportesMsgTypeByAPIException($options['msgtype']);
72+
}
73+
5774
if (null === $message->getRecipientId()) {
5875
throw new LogicException('Recipient id is required.');
5976
}

src/Symfony/Component/Notifier/Bridge/Matrix/Tests/MatrixTransportTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
1515
use Symfony\Component\Notifier\Bridge\Matrix\Exception\UnsupportedRecipientTypeException;
16+
use Symfony\Component\Notifier\Bridge\Matrix\Exception\UnsupportesMsgTypeByAPIException;
1617
use Symfony\Component\Notifier\Bridge\Matrix\MatrixOptions;
1718
use Symfony\Component\Notifier\Bridge\Matrix\MatrixTransport;
1819
use Symfony\Component\Notifier\Message\ChatMessage;
@@ -51,4 +52,11 @@ public function testUnsupportedRecipients()
5152
$this->expectExceptionMessage('The "Symfony\Component\Notifier\Bridge\Matrix\MatrixTransport" transport only supports recipients starting with "!","@","#" ("%" given).');
5253
$transport->send(new ChatMessage('Hello!', new MatrixOptions(['recipient_id' => '%testchannelalias:matrix.io'])));
5354
}
55+
56+
public function testUnsupportedMsgType(){
57+
$transport = self::createTransport();
58+
$this->expectException(UnsupportesMsgTypeByAPIException::class);
59+
$transport->send(new ChatMessage('Hello!', new MatrixOptions(['recipient_id' => '@user:matrix.io', 'msgtype' => 'm.anything'])));
60+
}
61+
5462
}

0 commit comments

Comments
 (0)
0