|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Bluesky; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 16 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 17 | +use Symfony\Component\Notifier\Message\ChatMessage; |
| 18 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 19 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 20 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 21 | +use Symfony\Component\String\AbstractString; |
| 22 | +use Symfony\Component\String\ByteString; |
| 23 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 24 | +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; |
| 25 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 26 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Tobias Nyholm <[email protected]> |
| 30 | + */ |
| 31 | +final class BlueskyTransport extends AbstractTransport |
| 32 | +{ |
| 33 | + private array $authSession = []; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + #[\SensitiveParameter] |
| 37 | + private string $user, |
| 38 | + #[\SensitiveParameter] |
| 39 | + private string $password, |
| 40 | + private LoggerInterface $logger, |
| 41 | + HttpClientInterface $client = null, |
| 42 | + EventDispatcherInterface $dispatcher = null, |
| 43 | + ) { |
| 44 | + parent::__construct($client, $dispatcher); |
| 45 | + } |
| 46 | + |
| 47 | + public function __toString(): string |
| 48 | + { |
| 49 | + return sprintf('bluesky://%s', $this->getEndpoint()); |
| 50 | + } |
| 51 | + |
| 52 | + public function supports(MessageInterface $message): bool |
| 53 | + { |
| 54 | + return $message instanceof ChatMessage; |
| 55 | + } |
| 56 | + |
| 57 | + protected function doSend(MessageInterface $message): SentMessage |
| 58 | + { |
| 59 | + if (!$message instanceof ChatMessage) { |
| 60 | + throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); |
| 61 | + } |
| 62 | + |
| 63 | + if ([] === $this->authSession) { |
| 64 | + $this->authenticate(); |
| 65 | + } |
| 66 | + |
| 67 | + $post = [ |
| 68 | + '$type' => 'app.bsky.feed.post', |
| 69 | + 'text' => $message->getSubject(), |
| 70 | + 'createdAt' => (new \DateTimeImmutable())->format('Y-m-d\\TH:i:s.u\\Z'), |
| 71 | + ]; |
| 72 | + if ([] !== $facets = $this->parseFacets($post['text'])) { |
| 73 | + $post['facets'] = $facets; |
| 74 | + } |
| 75 | + |
| 76 | + $response = $this->client->request('POST', sprintf('https://%s/xrpc/com.atproto.repo.createRecord', $this->getEndpoint()), [ |
| 77 | + 'auth_bearer' => $this->authSession['accessJwt'] ?? null, |
| 78 | + 'json' => [ |
| 79 | + 'repo' => $this->authSession['did'] ?? null, |
| 80 | + 'collection' => 'app.bsky.feed.post', |
| 81 | + 'record' => $post, |
| 82 | + ], |
| 83 | + ]); |
| 84 | + |
| 85 | + try { |
| 86 | + $statusCode = $response->getStatusCode(); |
| 87 | + } catch (TransportExceptionInterface $e) { |
| 88 | + throw new TransportException('Could not reach the remote bluesky server.', $response, 0, $e); |
| 89 | + } |
| 90 | + |
| 91 | + if (200 === $statusCode) { |
| 92 | + $content = $response->toArray(); |
| 93 | + $sentMessage = new SentMessage($message, (string) $this); |
| 94 | + $sentMessage->setMessageId($content['cid']); |
| 95 | + |
| 96 | + return $sentMessage; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + $content = $response->toArray(false); |
| 101 | + } catch (DecodingExceptionInterface $e) { |
| 102 | + throw new TransportException('Unexpected response from bluesky server.', $response, 0, $e); |
| 103 | + } |
| 104 | + |
| 105 | + $title = $content['error'] ?? ''; |
| 106 | + $errorDescription = $content['message'] ?? ''; |
| 107 | + |
| 108 | + throw new TransportException(sprintf('Unable to send message to Bluesky: Status code %d (%s) with message "%s".', $statusCode, $title, $errorDescription), $response); |
| 109 | + } |
| 110 | + |
| 111 | + private function authenticate(): void |
| 112 | + { |
| 113 | + $response = $this->client->request('POST', sprintf('https://%s/xrpc/com.atproto.server.createSession', $this->getEndpoint()), [ |
| 114 | + 'json' => [ |
| 115 | + 'identifier' => $this->user, |
| 116 | + 'password' => $this->password, |
| 117 | + ], |
| 118 | + ]); |
| 119 | + |
| 120 | + try { |
| 121 | + $statusCode = $response->getStatusCode(); |
| 122 | + } catch (TransportExceptionInterface $e) { |
| 123 | + throw new TransportException('Could not reach the remote bluesky server.', $response, 0, $e); |
| 124 | + } |
| 125 | + |
| 126 | + if (200 !== $statusCode) { |
| 127 | + throw new TransportException('Could not authenticate with the remote bluesky server.', $response); |
| 128 | + } |
| 129 | + |
| 130 | + try { |
| 131 | + $this->authSession = $response->toArray(false) ?? []; |
| 132 | + } catch (DecodingExceptionInterface $e) { |
| 133 | + throw new TransportException('Unexpected response from bluesky server.', $response, 0, $e); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private function parseFacets(string $input): array |
| 138 | + { |
| 139 | + $facets = []; |
| 140 | + $text = new ByteString($input); |
| 141 | + |
| 142 | + // regex based on: https://bluesky.com/specs/handle#handle-identifier-syntax |
| 143 | + $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])?)#'; |
| 144 | + foreach ($this->getMatchAndPosition($text, $regex) as $match) { |
| 145 | + $response = $this->client->request('GET', sprintf('https://%s/xrpc/com.atproto.identity.resolveHandle', $this->getEndpoint()), [ |
| 146 | + 'query' => [ |
| 147 | + 'handle' => ltrim($match['match'], '@'), |
| 148 | + ], |
| 149 | + ]); |
| 150 | + try { |
| 151 | + if (200 !== $response->getStatusCode()) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + } catch (TransportExceptionInterface $e) { |
| 155 | + $this->logger->error('Could not reach the remote bluesky server. Tried to lookup username.', ['exception' => $e]); |
| 156 | + throw $e; |
| 157 | + } |
| 158 | + |
| 159 | + $did = $response->toArray(false)['did'] ?? null; |
| 160 | + if (null === $did) { |
| 161 | + $this->logger->error('Could not get a good response from bluesky server. Tried to lookup username.'); |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + $facets[] = [ |
| 166 | + 'index' => [ |
| 167 | + 'byteStart' => $match['start'], |
| 168 | + 'byteEnd' => $match['end'], |
| 169 | + ], |
| 170 | + 'features' => [ |
| 171 | + [ |
| 172 | + '$type' => 'app.bsky.richtext.facet#mention', |
| 173 | + 'did' => $did, |
| 174 | + ], |
| 175 | + ], |
| 176 | + ]; |
| 177 | + } |
| 178 | + |
| 179 | + // partial/naive URL regex based on: https://stackoverflow.com/a/3809435 |
| 180 | + // tweaked to disallow some trailing punctuation |
| 181 | + $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@%_\+~#//=])?);'; |
| 182 | + foreach ($this->getMatchAndPosition($text, $regex) as $match) { |
| 183 | + $facets[] = [ |
| 184 | + 'index' => [ |
| 185 | + 'byteStart' => $match['start'], |
| 186 | + 'byteEnd' => $match['end'], |
| 187 | + ], |
| 188 | + 'features' => [ |
| 189 | + [ |
| 190 | + '$type' => 'app.bsky.richtext.facet#link', |
| 191 | + 'uri' => $match['match'], |
| 192 | + ], |
| 193 | + ], |
| 194 | + ]; |
| 195 | + } |
| 196 | + |
| 197 | + return $facets; |
| 198 | + } |
| 199 | + |
| 200 | + private function getMatchAndPosition(AbstractString $text, string $regex): array |
| 201 | + { |
| 202 | + $output = []; |
| 203 | + $handled = []; |
| 204 | + $matches = $text->match($regex, \PREG_PATTERN_ORDER); |
| 205 | + if ([] === $matches) { |
| 206 | + return $output; |
| 207 | + } |
| 208 | + |
| 209 | + $length = $text->length(); |
| 210 | + foreach ($matches[1] as $match) { |
| 211 | + if (isset($handled[$match])) { |
| 212 | + continue; |
| 213 | + } |
| 214 | + $handled[$match] = true; |
| 215 | + $end = -1; |
| 216 | + while (null !== $start = $text->indexOf($match, min($length, $end + 1))) { |
| 217 | + $output[] = [ |
| 218 | + 'start' => $start, |
| 219 | + 'end' => $end = $start + (new ByteString($match))->length(), |
| 220 | + 'match' => $match, |
| 221 | + ]; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return $output; |
| 226 | + } |
| 227 | +} |
0 commit comments