-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlueskyTransport.php
More file actions
331 lines (283 loc) · 11.9 KB
/
Copy pathBlueskyTransport.php
File metadata and controls
331 lines (283 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Bridge\Bluesky;
use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class BlueskyTransport extends AbstractTransport
{
private const TAG_MAX_BYTES = 640;
private const TAG_MAX_GRAPHEMES = 64;
private array $authSession = [];
private ClockInterface $clock;
public function __construct(
#[\SensitiveParameter] private string $user,
#[\SensitiveParameter] private string $password,
private LoggerInterface $logger,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
?ClockInterface $clock = null,
) {
parent::__construct($client, $dispatcher);
$this->clock = $clock ?? Clock::get();
}
public function __toString(): string
{
return \sprintf('bluesky://%s', $this->getEndpoint());
}
public function supports(MessageInterface $message): bool
{
return $message instanceof ChatMessage;
}
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
}
if ([] === $this->authSession) {
$this->authenticate();
}
$post = [
'$type' => 'app.bsky.feed.post',
'text' => $message->getSubject(),
'createdAt' => $this->clock->now()->format('Y-m-d\\TH:i:s.u\\Z'),
];
if ([] !== $facets = $this->parseFacets($post['text'])) {
$post['facets'] = $facets;
}
$options = $message->getOptions()?->toArray() ?? [];
$options['repo'] = $this->authSession['did'] ?? null;
$options['collection'] = 'app.bsky.feed.post';
$options['record'] = $post;
if (isset($options['attach'])) {
$options['record']['embed'] = [
'$type' => 'app.bsky.embed.images',
'images' => $this->uploadMedia($options['attach']),
];
unset($options['attach']);
}
if (isset($options['external'])) {
$uploadedMedia = $this->uploadMedia([
[
'file' => $options['external']['thumb'],
'description' => $options['external']['description'],
],
]);
$options['record']['embed'] = [
'$type' => 'app.bsky.embed.external',
'external' => [
'uri' => $options['external']['uri'],
'title' => $options['external']['title'],
'description' => $options['external']['description'],
'thumb' => $uploadedMedia[array_key_first($uploadedMedia)]['image'],
],
];
unset($options['external']);
}
$response = $this->client->request('POST', \sprintf('https://%s/xrpc/com.atproto.repo.createRecord', $this->getEndpoint()), [
'auth_bearer' => $this->authSession['accessJwt'] ?? null,
'json' => $options,
]);
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote bluesky server.', $response, 0, $e);
}
if (200 === $statusCode) {
$content = $response->toArray();
$sentMessage = new SentMessage($message, (string) $this, ['cid' => $content['cid']]);
$sentMessage->setMessageId($content['uri']);
return $sentMessage;
}
try {
$content = $response->toArray(false);
} catch (DecodingExceptionInterface $e) {
throw new TransportException('Unexpected response from bluesky server.', $response, 0, $e);
}
$title = $content['error'] ?? '';
$errorDescription = $content['message'] ?? '';
throw new TransportException(\sprintf('Unable to send message to Bluesky: Status code %d (%s) with message "%s".', $statusCode, $title, $errorDescription), $response);
}
private function authenticate(): void
{
$response = $this->client->request('POST', \sprintf('https://%s/xrpc/com.atproto.server.createSession', $this->getEndpoint()), [
'json' => [
'identifier' => $this->user,
'password' => $this->password,
],
]);
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote bluesky server.', $response, 0, $e);
}
if (200 !== $statusCode) {
throw new TransportException('Could not authenticate with the remote bluesky server.', $response);
}
try {
$this->authSession = $response->toArray(false) ?? [];
} catch (DecodingExceptionInterface $e) {
throw new TransportException('Unexpected response from bluesky server.', $response, 0, $e);
}
}
private function parseFacets(string $input): array
{
$facets = [];
// regex based on: https://bluesky.com/specs/handle#handle-identifier-syntax
$regex = '#[$|\W](@([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)#';
foreach ($this->getMatchAndPosition($input, $regex) as $match) {
$response = $this->client->request('GET', \sprintf('https://%s/xrpc/com.atproto.identity.resolveHandle', $this->getEndpoint()), [
'query' => [
'handle' => ltrim($match['match'], '@'),
],
]);
try {
if (200 !== $response->getStatusCode()) {
continue;
}
} catch (TransportExceptionInterface $e) {
$this->logger->error('Could not reach the remote bluesky server. Tried to lookup username.', ['exception' => $e]);
throw $e;
}
$did = $response->toArray(false)['did'] ?? null;
if (null === $did) {
$this->logger->error('Could not get a good response from bluesky server. Tried to lookup username.');
continue;
}
$facets[] = [
'index' => [
'byteStart' => $match['start'],
'byteEnd' => $match['end'],
],
'features' => [
[
'$type' => 'app.bsky.richtext.facet#mention',
'did' => $did,
],
],
];
}
// partial/naive URL regex based on: https://stackoverflow.com/a/3809435
// tweaked to disallow some trailing punctuation
$regex = ';[$|\W](https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?);';
foreach ($this->getMatchAndPosition($input, $regex) as $match) {
$facets[] = [
'index' => [
'byteStart' => $match['start'],
'byteEnd' => $match['end'],
],
'features' => [
[
'$type' => 'app.bsky.richtext.facet#link',
'uri' => $match['match'],
],
],
];
}
// tag regex based on: https://github.com/bluesky-social/atproto/blob/main/packages/api/src/rich-text/detection.ts
// a tag needs at least one character that is neither a digit nor punctuation, and trailing punctuation is left out
$regex = '/(?:^|\s)([##][^\s]*[^\s\d\p{P}][^\s\p{P}]*)/u';
foreach ($this->getMatchAndPosition($input, $regex) as $match) {
// the marker is "#" or the fullwidth "#", which is 3 bytes
$tag = preg_replace('/^[##]/u', '', $match['match']);
// the lexicon caps the tag at 640 bytes and 64 graphemes; emitting a longer one makes the whole post fail
if (self::TAG_MAX_BYTES < \strlen($tag) || self::TAG_MAX_GRAPHEMES < preg_match_all('/\X/u', $tag)) {
continue;
}
$facets[] = [
'index' => [
'byteStart' => $match['start'],
'byteEnd' => $match['end'],
],
'features' => [
[
'$type' => 'app.bsky.richtext.facet#tag',
'tag' => $tag,
],
],
];
}
usort($facets, static fn (array $a, array $b) => $a['index']['byteStart'] <=> $b['index']['byteStart']);
return $facets;
}
/**
* @return list<array{start: int, end: int, match: string}>
*/
private function getMatchAndPosition(string $text, string $regex): array
{
// preg_* work on bytes, so the captured offsets already are the byte offsets facets are indexed by
if (!preg_match_all($regex, $text, $matches, \PREG_OFFSET_CAPTURE)) {
return [];
}
$output = [];
foreach ($matches[1] as [$match, $start]) {
$output[] = [
'start' => $start,
'end' => $start + \strlen($match),
'match' => $match,
];
}
return $output;
}
/**
* @param array<array{file: File, description: string}> $media
*
* @return array<array{alt: string, image: array{$type: string, ref: array{$link: string}, mimeType: string, size: int}}>
*/
private function uploadMedia(array $media): array
{
$pool = [];
foreach ($media as ['file' => $file, 'description' => $description]) {
$pool[] = [
'description' => $description,
'response' => $this->client->request('POST', \sprintf('https://%s/xrpc/com.atproto.repo.uploadBlob', $this->getEndpoint()), [
'auth_bearer' => $this->authSession['accessJwt'] ?? null,
'headers' => [
'Content-Type: '.$file->getContentType(),
],
'body' => fopen($file->getPath(), 'r'),
]),
];
}
$embeds = [];
try {
foreach ($pool as $i => ['description' => $description, 'response' => $response]) {
unset($pool[$i]);
$result = $response->toArray(false);
if (300 <= $response->getStatusCode()) {
throw new TransportException('Unable to embed medias.', $response);
}
$embeds[] = [
'alt' => $description,
'image' => $result['blob'],
];
}
} finally {
foreach ($pool as ['response' => $response]) {
$response->cancel();
}
}
return $embeds;
}
}