8000 implemented some suggestions · symfony/symfony@62f60de · GitHub
[go: up one dir, main page]

Skip to content

Commit 62f60de

Browse files
committed
implemented some suggestions
1 parent f20d386 commit 62f60de

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
lines changed

src/Symfony/Component/Notifier/Bridge/Matrix/Exception/UnsupportedRecipientTypeException.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515

1616
class UnsupportedRecipientTypeException extends LogicException
10000 1717
{
18-
public function __construct(string $transport, string $given)
18+
public function __construct(string $given)
1919
{
20-
$message = \sprintf(
21-
'The "%s" transport only supports recipients starting with "!","@","#" ("%s" given).',
22-
$transport,
23-
$given
24-
);
25-
2620
parent::__construct(\sprintf('Only recipients starting with "!","@","#" are supported ("%s" given).', $given));
2721
}
2822
}

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Notifier\Exception\LogicException;
1717
use Symfony\Component\Notifier\Exception\TransportException;
1818
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
19+
use Symfony\Component\Notifier\Exception\UnsupportedOptionsException;
1920
use Symfony\Component\Notifier\Message\ChatMessage;
2021
use Symfony\Component\Notifier\Message\MessageInterface;
2122
use Symfony\Component\Notifier\Message\SentMessage;
@@ -86,25 +87,26 @@ protected function doSend(MessageInterface $message): SentMessage
8687
$options['formatted_body'] = $message->getSubject();
8788
$options['body'] = strip_tags($message->getSubject());
8889
} else {
89-
$content['body'] = $message->getSubject();
90+
$options['body'] = $message->getSubject();
9091
}
9192

9293
$uri = '/_matrix/client/v3/rooms/%s/send/%s/%s';
9394
$response = $this->connect(
9495
method: 'PUT',
95-
uri: \sprintf($uri, $recipientId, 'm.room.message', Uuid::v4()),
96+
uri: \sprintf($uri, $recipient, 'm.room.message', Uuid::v4()),
9697
options: [
97-
'json' => $content,
98+
'json' => $options,
9899
]
99100
);
101+
100102
$success = $response->toArray(false);
101103
$sentMessage = new SentMessage($message, (string) $this);
102104
$sentMessage->setMessageId($success['event_id']);
103105

104106
return $sentMessage;
105107
}
106108

107-
protected function getRoomFromAlias(
109+
private function getRoomFromAlias(
108110
string $alias,
109111
): string {
110112
$uri = '/_matrix/client/v3/directory/room/%s';
@@ -113,7 +115,7 @@ protected function getRoomFromAlias(
113115
return $response->toArray()['room_id'];
114116
}
115117

116-
protected function createPrivateChannel(
118+
private function createPrivateChannel(
117119
string $recipientId,
118120
): ?array {
119121
$uri = '/_matrix/client/v3/createRoom';
@@ -132,40 +134,39 @@ protected function createPrivateChannel(
132134
return $response->toArray();
133135
}
134136

135-
protected function getDirectMessageChannel(
137+
private function getDirectMessageChannel(
136138
string $recipientId,
137139
): ?string {
138-
$response = $this->getAccountData(
139-
userId: $this->getWhoami()['user_id'],
140-
type: 'm.direct'
141-
);
140+
$response = $this->getAccountData($this->getWhoami()['user_id'],'m.direct');
142141
if (!isset($response[$recipientId])) {
143142
$roomid = $this->createPrivateChannel($recipientId)['room_id'];
144143
$response[$recipientId] = [$roomid];
145-
$this->updateAccountData('m.direct', $response);
144+
$this->updateAccountData($this->getWhoami(), 'm.direct', $response);
146145

147146
return $roomid;
148147
}
149148

150149
return $response[$recipientId][0];
151150
}
152151

153-
protected function updateAccountData(
152+
private function updateAccountData(
153+
string $userId,
154154
string $type,
155-
array $option,
156-
): ?array {
155+
array $data,
156+
): void {
157157
$uri = '/_matrix/client/v3/user/%s/account_data/%s';
158158
$response = $this->connect(
159159
method: 'PUT',
160-
uri: \sprintf($uri, urlencode($this->getWhoami()['user_id']), $type),
160+
uri: \sprintf($uri, urlencode($userId), $type),
161161
options: [
162-
'json' => $option,
162+
'json' => $data,
163163
]);
164-
165-
return $response->toArray();
164+
if($response->toArray()!== []) {
165+
throw new TransportException('Unable to update account data.', $response);
166+
}
166167
}
167168

168-
protected function getAccountData(
169+
private function getAccountData(
169170
string $userId,
170171
string $type,
171172
): ?array {
@@ -211,20 +212,17 @@ protected function connect(
211212
];
212213
$url = $this->getEndpoint(true).$uri;
213214
$response = $this->client->request($method, $url, $options);
215+
214216
try {
215217
$statusCode = $response->getStatusCode();
216218
} catch (TransportException $e) {
217219
throw new TransportException('Could not reach the Matrix server.', $response, 0, $e);
218220
}
219221

220-
if (400 == $statusCode) {
222+
if (in_array($statusCode,[400,403,405])) {
221223
$result = $response->toArray(false);
222-
223224
throw new TransportException(\sprintf('Error: Matrix responded with "%s (%s)"', $result['error'], $result['errcode']), $response);
224225
}
225-
if (!$response instanceof ResponseInterface) {
226-
throw new LogicException('Expected response to be an instance of ResponseInterface.');
227-
}
228226

229227
return $response;
230228
}

src/Symfony/Component/Notifier/Bridge/Matrix/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ Resources
3030
[send Pull Requests](https://github.com/symfony/symfony/pulls)
3131
in the [main Symfony repository](https://github.com/symfony/symfony)
3232
* [Matrix Playground](https://playground.matrix.org)
33+
* [Matrix Client-Server API](https://spec.matrix.org/latest/client-server-api/)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function testUnsupportedRecipients()
4949
{
5050
$transport = self::createTransport();
5151
$this->expectException(UnsupportedRecipientTypeException::class);
52-
$this->expectExceptionMessage('The "Symfony\Component\Notifier\Bridge\Matrix\MatrixTransport" transport only supports recipients starting with "!","@","#" ("%" given).');
53-
$transport->send(new ChatMessage('Hello!', new MatrixOptions(['recipient_id' => '%testchannelalias:matrix.io'])));
52+
$this->expectExceptionMessage('Only recipients starting with "!","@","#" are supported ("+" given).');
53+
$transport->send(new ChatMessage('Hello!', new MatrixOptions(['recipient_id' => '+testchannelalias:matrix.io'])));
5454
}
5555

5656
public function testUnsupportedMsgType(){

src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ class UnsupportedSchemeException extends LogicException
148148
'class' => Bridge\Mastodon\MastodonTransportFactory::class,
149149
'package' => 'symfony/mastodon-notifier',
150150
],
151+
'matrix' => [
152+
'class' => Bridge\Matrix\MatrixTransportFactory::class,
153+
'package' => 'symfony/matrix-notifier',
154+
],
151155
'mattermost' => [
152156
'class' => Bridge\Mattermost\MattermostTransportFactory::class,
153157
'package' => 'symfony/mattermost-notifier',

src/Symfony/Component/Notifier/Tests/Exception/UnsupportedSchemeExceptionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static function setUpBeforeClass(): void
5858
Bridge\Lox24\Lox24TransportFactory::class => false,
5959
Bridge\Mailjet\MailjetTransportFactory::class => false,
6060
Bridge\Mastodon\MastodonTransportFactory::class => false,
61+
Bridge\Matrix\MatrixTransportFactory::class => false,
6162
Bridge\Mattermost\MattermostTransportFactory::class => false,
6263
Bridge\Mercure\MercureTransportFactory::class => false,
6364
Bridge\MessageBird\MessageBirdTransportFactory::class => false,
@@ -155,6 +156,7 @@ public static function messageWhereSchemeIsPartOfSchemeToPackageMapProvider(): \
155156
yield ['lox24', 'symfony/lox24-notifier'];
156157
yield ['mailjet', 'symfony/mailjet-notifier'];
157158
yield ['mastodon', 'symfony/mastodon-notifier'];
159+
yield ['matrix', 'symfony/matrix-notifier'];
158160
yield ['mattermost', 'symfony/mattermost-notifier'];
159161
yield ['mercure', 'symfony/mercure-notifier'];
160162
yield ['messagebird', 'symfony/message-bird-notifier'];

0 commit comments

Comments
 (0)
0