@@ -84,17 +84,15 @@ protected function doSend(MessageInterface $message): SentMessage
84
84
default => throw new UnsupportedRecipientTypeException ($ message ->getRecipientId ()[0 ]),
85
85
};
86
86
87
+ $ options ['body ' ] = $ message ->getSubject ();
87
88
if ('org.matrix.custom.html ' === $ options ['format ' ]) {
88
89
$ options ['formatted_body ' ] = $ message ->getSubject ();
89
90
$ options ['body ' ] = strip_tags ($ message ->getSubject ());
90
- } else {
91
- $ options ['body ' ] = $ message ->getSubject ();
92
91
}
93
92
94
- $ uri = '/_matrix/client/v3/rooms/%s/send/%s/%s ' ;
95
- $ response = $ this ->connect (
93
+ $ response = $ this ->request (
96
94
method: 'PUT ' ,
97
- uri: \sprintf ($ uri , $ recipient , 'm.room.message ' , Uuid::v4 ()),
95
+ uri: \sprintf (' /_matrix/client/v3/rooms/%s/send/%s/%s ' , $ recipient , 'm.room.message ' , Uuid::v4 ()),
98
96
options: [
99
97
'json ' => $ options ,
100
98
]
@@ -107,21 +105,17 @@ protected function doSend(MessageInterface $message): SentMessage
107
105
return $ sentMessage ;
108
106
}
109
107
110
- private function getRoomFromAlias (
111
- string $ alias ,
112
- ): string {
113
- $ uri = '/_matrix/client/v3/directory/room/%s ' ;
114
- $ response = $ this ->connect ('GET ' , \sprintf ($ uri , urlencode ($ alias )));
108
+ private function getRoomFromAlias (string $ alias ): string
109
+ {
110
+ $ response = $ this ->request ('GET ' , \sprintf ('/_matrix/client/v3/directory/room/%s ' , urlencode ($ alias )));
115
111
116
112
return $ response ->toArray ()['room_id ' ];
117
113
}
118
114
119
- private function createPrivateChannel (
120
- string $ recipientId ,
121
- ): ?array {
122
- $ uri = '/_matrix/client/v3/createRoom ' ;
115
+ private function createPrivateChannel (string $ recipientId ): ?array
116
+ {
123
117
$ invites [] = $ recipientId ;
124
- $ response = $ this ->connect ('POST ' , $ uri , [
118
+ $ response = $ this ->request ('POST ' , ' /_matrix/client/v3/createRoom ' , [
125
119
'json ' => [
126
120
'creation_content ' => [
127
121
'm.federate ' => false ,
@@ -135,9 +129,8 @@ private function createPrivateChannel(
135
129
return $ response ->toArray ();
136
130
}
137
131
138
- private function getDirectMessageChannel (
139
- string $ recipientId ,
140
- ): ?string {
132
+ private function getDirectMessageChannel (string $ recipientId ): ?string
133
+ {
141
134
$ response = $ this ->getAccountData ($ this ->getWhoami ()['user_id ' ], 'm.direct ' );
142
135
if (!isset ($ response [$ recipientId ])) {
143
136
$ roomid = $ this ->createPrivateChannel ($ recipientId )['room_id ' ];
@@ -150,15 +143,11 @@ private function getDirectMessageChannel(
150
143
return $ response [$ recipientId ][0 ];
151
144
}
152
145
8000
code>
153
- private function updateAccountData (
154
- string $ userId ,
155
- string $ type ,
156
- array $ data ,
157
- ): void {
158
- $ uri = '/_matrix/client/v3/user/%s/account_data/%s ' ;
159
- $ response = $ this ->connect (
146
+ private function updateAccountData (string $ userId , string $ type , array $ data ): void
147
+ {
148
+ $ response = $ this ->request (
160
149
method: 'PUT ' ,
161
- uri: \sprintf ($ uri , urlencode ($ userId ), $ type ),
150
+ uri: \sprintf (' /_matrix/client/v3/user/%s/account_data/%s ' , urlencode ($ userId ), $ type ),
162
151
options: [
163
152
'json ' => $ data ,
164
153
]);
@@ -167,52 +156,42 @@ private function updateAccountData(
167
156
}
168
157
}
169
158
170
- private function getAccountData (
171
- string $ userId ,
172
- string $ type ,
173
- ): ?array {
174
- $ uri = '/_matrix/client/v3/user/%s/account_data/%s ' ;
175
- $ response = $ this ->connect (
159
+ private function getAccountData (string $ userId , string $ type ): ?array
160
+ {
161
+ $ response = $ this ->request (
176
162
method: 'GET ' ,
177
- uri: \sprintf ($ uri , urlencode ($ userId ), $ type ));
163
+ uri: \sprintf (' /_matrix/client/v3/user/%s/account_data/%s ' , urlencode ($ userId ), $ type ));
178
164
179
165
return $ response ->toArray ();
180
166
}
181
167
182
- protected function getWhoami (): ?array
168
+ private function getWhoami (): ?array
183
169
{
184
- $ uri = '/_matrix/client/v3/account/whoami ' ;
185
- $ response = $ this ->connect (
170
+ $ response = $ this ->request (
186
171
method: 'GET ' ,
187
- uri: $ uri ,
172
+ uri: ' /_matrix/client/v3/account/whoami ' ,
188
173
);
189
174
190
175
return $ response ->toArray ();
191
176
}
192
177
193
- protected function getEndpoint (
194
- bool $ full = false ,
195
- ): string {
178
+ protected function getEndpoint (bool $ full = false ): string {
196
179
return rtrim (
197
180
($ full ? $ this ->getScheme ().':// ' : '' ).$ this ->host .($ this ->port ? ': ' .$ this ->port : '' ),
198
181
'/ ' );
199
182
}
200
183
201
- protected function getScheme (): string
184
+ private function getScheme (): string
202
185
{
203
186
return $ this ->ssl ? 'https ' : 'http ' ;
204
187
}
205
188
206
- protected function connect (
207
- string $ method ,
208
- string $ uri ,
209
- ?array $ options = [],
210
- ): ResponseInterface {
189
+ private function request (string $ method , string $ uri , ?array $ options = []): ResponseInterface
190
+ {
211
191
$ options += [
212
192
'auth_bearer ' => $ this ->accessToken ,
213
193
];
214
- $ url = $ this ->getEndpoint (true ).$ uri ;
215
- $ response = $ this ->client ->request ($ method , $ url , $ options );
194
+ $ response = $ this ->client ->request ($ method , $ this ->getEndpoint (true ).$ uri , $ options );
216
195
217
196
try {
218
197
$ statusCode = $ response ->getStatusCode ();
0 commit comments