1
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
+
2
12
namespace Symfony \Component \Notifier \Bridge \Matrix ;
3
13
4
- use Symfony \Component \Notifier \Bridge \Matrix \Exception \UnsupportedRecipiantTypeException ;
14
+ use Symfony \Component \Notifier \Bridge \Matrix \Exception \UnsupportedRecipientTypeException ;
5
15
use Symfony \Component \Notifier \Exception \LogicException ;
6
16
use Symfony \Component \Notifier \Exception \TransportException ;
7
17
use Symfony \Component \Notifier \Exception \UnsupportedMessageTypeException ;
8
18
use Symfony \Component \Notifier \Message \ChatMessage ;
9
19
use Symfony \Component \Notifier \Message \MessageInterface ;
10
20
use Symfony \Component \Notifier \Message \SentMessage ;
11
21
use Symfony \Component \Notifier \Transport \AbstractTransport ;
22
+ use Symfony \Component \Uid \Uuid ;
12
23
use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
13
24
use Symfony \Contracts \HttpClient \HttpClientInterface ;
14
- use Symfony \Component \Uid \Uuid ;
15
25
use Symfony \Contracts \HttpClient \ResponseInterface ;
16
26
27
+ /**
28
+ * @author Frank Schulze <frank@akiber.de>
29
+ */
17
30
final class MatrixTransport extends AbstractTransport
18
31
{
19
32
public function __construct (
20
33
#[\SensitiveParameter] private string $ accessToken ,
21
34
private bool $ ssl ,
22
35
?HttpClientInterface $ client = null ,
23
36
?EventDispatcherInterface $ dispatcher = null
24
- ){
37
+ ) {
25
38
parent ::__construct ($ client , $ dispatcher );
26
39
}
27
40
28
41
public function __toString (): string
29
42
{
30
- return \sprintf ( 'matrix://%s ' , $ this ->getEndpoint (false ));
43
+ return \sprintf ('matrix://%s ' , $ this ->getEndpoint (false ));
31
44
}
32
45
33
46
public function supports (MessageInterface $ message ): bool
@@ -38,41 +51,40 @@ public function supports(MessageInterface $message): bool
38
51
protected function doSend (MessageInterface $ message ): SentMessage
39
52
{
40
53
if (!$ message instanceof ChatMessage) {
41
- throw new UnsupportedMessageTypeException (__CLASS__ , ChatMessage::class, $ message );
54
+ throw new UnsupportedMessageTypeException (__CLASS__ , ChatMessage::class, $ message );
42
55
}
43
56
44
- if ($ message ->getRecipientId () === null ){
57
+ if (null === $ message ->getRecipientId ()){
45
58
throw new LogicException ('Recipient id is required. ' );
46
59
}
47
60
48
- $ recipient = match (mb_substr ($ message ->getRecipientId (),0 , 1 )){
61
+ $ recipient = match (mb_substr ($ message ->getRecipientId (), 0 , 1 )){
49
62
"@ " => $ this ->getDirectMessageChannel ($ message ->getRecipientId ()),
50
63
"! " => $ message ->getRecipientId (),
51
64
"# " => $ this ->getRoomFromAlias ($ message ->getRecipientId ()),
52
- default => throw new UnsupportedRecipiantTypeException (__CLASS__ , mb_substr ($ message ->getRecipientId (),0 , 1 )),
65
+ default => throw new UnsupportedRecipientTypeException (__CLASS__ , mb_substr ($ message ->getRecipientId (), 0 , 1 )),
53
66
};
67
+
54
68
return $ this ->sendMessage ($ recipient , $ message );
55
69
}
56
70
57
71
protected function sendMessage (
58
72
string $ recipientId ,
59
73
MessageInterface $ message ,
60
- ): SentMessage
61
- {
74
+ ): SentMessage {
62
75
/** @var MatrixOptions $options */
63
76
$ options = $ message ->getOptions ();
64
77
$ uri = '/_matrix/client/v3/rooms/%s/send/%s/%s ' ;
65
78
66
79
$ content ['msgtype ' ] = $ options ->getMsgType ();
67
- if ($ options -> getFormat () === 'org.matrix.custom.html ' ) {
80
+ if ('org.matrix.custom.html ' === $ options -> getFormat () ) {
68
81
$ content ['format ' ] = $ options ->getFormat ();
69
82
$ content ['formatted_body ' ] = $ message ->getSubject ();
70
83
$ content ['body ' ] = strip_tags ($ message ->getSubject ());
71
84
} else {
72
85
$ content ['body ' ] = $ message ->getSubject ();
73
86
}
74
87
75
-
76
88
$ response = $ this ->connect (
77
89
method: 'PUT ' ,
78
90
uri: \sprintf ($ uri , $ recipientId , 'm.room.message ' , Uuid::v4 ()),
@@ -83,49 +95,50 @@ protected function sendMessage(
83
95
$ success = $ response ->toArray (false );
84
96
$ sentMessage = new SentMessage ($ message , (string ) $ this );
85
97
$ sentMessage ->setMessageId ($ success ['event_id ' ]);
98
+
86
99
return $ sentMessage ;
87
100
}
88
101
89
102
protected function getRoomFromAlias (
90
103
string $ alias ,
91
- ): string
92
- {
104
+ ): string {
93
105
$ uri = '/_matrix/client/v3/directory/room/%s ' ;
94
106
$ response = $ this ->connect ('GET ' , \sprintf ($ uri , urlencode ($ alias )));
107
+
95
108
return $ response ->toArray ()['room_id ' ];
96
109
}
97
110
98
111
protected function createPrivateChannel (
99
112
string $ recipientId ,
100
- ): array | null
101
- {
113
+ ): array | null {
102
114
$ uri = '/_matrix/client/v3/createRoom ' ;
103
115
$ invites [] = $ recipientId ;
104
116
$ response = $ this ->connect ('POST ' , $ uri , [
105
117
'json ' => [
106
- " creation_content " => [
107
- " m.federate " => false
118
+ ' creation_content ' => [
119
+ ' m.federate ' => false
108
120
],
109
- " is_direct " => true ,
110
- " preset " => " trusted_private_chat " ,
111
- " invite " => $ invites
112
- ]
121
+ ' is_direct ' => true ,
122
+ ' preset ' => ' trusted_private_chat ' ,
123
+ ' invite ' => $ invites
124
+ ],
113
125
]);
126
+
114
127
return $ response ->toArray ();
115
128
}
116
129
117
130
protected function getDirectMessageChannel (
118
131
string $ recipientId
119
- ): string | null
120
- {
132
+ ): string | null {
121
133
$ response = $ this ->getAccountData (
122
134
userId: $ this ->getWhoami ()['user_id ' ],
123
- type:'m.direct '
135
+ type: 'm.direct '
124
136
);
125
- if (!isset ($ response [$ recipientId ])){
137
+ if (!isset ($ response [$ recipientId ])) {
126
138
$ roomid = $ this ->createPrivateChannel ($ recipientId )['room_id ' ];
127
139
$ response [$ recipientId ] = [$ roomid ];
128
140
$ this ->updateAccountData ('m.direct ' , $ response );
141
+
129
142
return $ roomid ;
130
143
}
131
144
@@ -135,23 +148,22 @@ protected function getDirectMessageChannel(
135
148
protected function updateAccountData (
136
149
string $ type ,
137
150
array $ option
138
- ): array | null
139
- {
151
+ ): ?array {
140
152
$ uri = '/_matrix/client/v3/user/%s/account_data/%s ' ;
141
153
$ response = $ this ->connect (
142
154
method: 'PUT ' ,
143
- uri: \sprintf ($ uri ,urlencode ($ this ->getWhoami ()['user_id ' ]), $ type ),
155
+ uri: \sprintf ($ uri , urlencode ($ this ->getWhoami ()['user_id ' ]), $ type ),
144
156
options: [
145
- 'json ' => $ option ,
157
+ 'json ' => $ option ,
146
158
]);
159
+
147
160
return $ response ->toArray ();
148
161
}
149
162
150
163
protected function getAccountData (
151
164
string $ userId ,
152
165
string $ type ,
153
- ): array |null
154
- {
166
+ ): ?array {
155
167
$ uri = '/_matrix/client/v3/user/%s/account_data/%s ' ;
156
168
$ response = $ this ->connect (
157
169
method: 'GET ' ,
@@ -160,7 +172,7 @@ protected function getAccountData(
160
172
return $ response ->toArray ();
161
173
}
162
174
163
- protected function getWhoami (): array | null
175
+ protected function getWhoami (): ? array
164
176
{
165
177
$ uri = '/_matrix/client/v3/account/whoami ' ;
166
178
$ response = $ this ->connect (
@@ -172,24 +184,23 @@ protected function getWhoami(): array|null
172
184
}
173
185
174
186
protected function getEndpoint (
175
- bool $ full =false
176
- ): string
177
- {
187
+ bool $ full = false
188
+ ): string {
178
189
return rtrim (
179
- ($ full? $ this ->getScheme ().':// ' : '' ).$ this ->host .($ this ->port ? ': ' .$ this ->port : '' ),
190
+ ($ full ? $ this ->getScheme ().':// ' : '' ).$ this ->host .($ this ->port ? ': ' .$ this ->port : '' ),
180
191
'/ ' );
181
192
}
193
+
182
194
protected function getScheme (): string
183
195
{
184
- return $ this ->ssl ? 'https ' : 'http ' ;
196
+ return $ this ->ssl ? 'https ' : 'http ' ;
185
197
}
186
198
187
199
protected function connect (
188
200
string $ method ,
189
201
string $ uri ,
190
202
?array $ options = [],
191
- ): ResponseInterface
192
- {
203
+ ): ResponseInterface {
193
204
$ options += [
194
205
'auth_bearer ' => $ this ->accessToken ,
195
206
];
@@ -204,17 +215,12 @@ protected function connect(
204
215
if (400 == $ statusCode ) {
205
216
$ result = $ response ->toArray (false );
206
217
207
- throw new TransportException (
208
- \sprintf (
209
- 'Error: Matrix responded with "%s (%s)" ' ,
210
- $ result ['error ' ],
211
- $ result ['errcode ' ]
212
- ),
213
- $ response );
218
+ throw new TransportException (\sprintf ('Error: Matrix responded with "%s (%s)" ' , $ result ['error ' ], $ result ['errcode ' ]), $ response );
214
219
}
215
- if (!$ response instanceof ResponseInterface) {
216
- throw new LogicException ('Expected response to be an instance of ResponseInterface ' );
220
+ if (!$ response instanceof ResponseInterface) {
221
+ throw new LogicException ('Expected response to be an instance of ResponseInterface. ' );
217
222
}
223
+
218
224
return $ response ;
219
225
}
220
226
}
0 commit comments