-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMastodonTransport.php
More file actions
155 lines (129 loc) · 5.16 KB
/
MastodonTransport.php
File metadata and controls
155 lines (129 loc) · 5.16 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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Bridge\Mastodon;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Component\Notifier\Exception\RuntimeException;
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\ExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* @author Quentin Dequippe <[email protected]>
*
* @see https://docs.joinmastodon.org
*/
final class MastodonTransport extends AbstractTransport
{
public function __construct(
#[\SensitiveParameter] private readonly string $accessToken,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}
public function __toString(): string
{
return \sprintf('mastodon://%s', $this->getEndpoint());
}
public function supports(MessageInterface $message): bool
{
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof MastodonOptions);
}
public function request(string $method, string $url, array $options): ResponseInterface
{
$url = \sprintf('https://%s%s', $this->getEndpoint(), $url);
$options['auth_bearer'] = $this->accessToken;
return $this->client->request($method, $url, $options);
}
/**
* @see https://docs.joinmastodon.org/methods/statuses/
*/
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
}
$options = $message->getOptions()?->toArray() ?? [];
$options['status'] = $message->getSubject();
$response = null;
try {
if (isset($options['attach'])) {
$options['media_ids'] = $this->uploadMedia($options['attach']);
unset($options['attach']);
}
$response = $this->request('POST', '/api/v1/statuses', ['json' => $options]);
$statusCode = $response->getStatusCode();
$result = $response->toArray(false);
} catch (ExceptionInterface $e) {
if (null !== $response) {
throw new TransportException($e->getMessage(), $response, 0, $e);
}
throw new RuntimeException($e->getMessage(), 0, $e);
}
if (200 !== $statusCode) {
throw new TransportException(\sprintf('Unable to post the Mastodon message: "%s" (%s).', $result['error_description'], $result['error']), $response);
}
$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($result['id']);
return $sentMessage;
}
/**
* @param array<array{file: File, thumbnail: File|null, description: string|null, focus: string}> $media
*/
private function uploadMedia(array $media): array
{
$responses = [];
foreach ($media as [
'file' => $file,
'thumbnail' => $thumbnail,
'description' => $description,
'focus' => $focus,
]) {
$formDataPart = new FormDataPart(array_filter([
'file' => new DataPart($file),
'thumbnail' => $thumbnail ? new DataPart($thumbnail) : null,
'description' => $description,
'focus' => $focus,
]));
$headers = [];
foreach ($formDataPart->getPreparedHeaders()->all() as $header) {
$headers[] = $header->toString();
}
$responses[] = $this->request('POST', '/api/v2/media', [
'headers' => $headers,
'body' => $formDataPart->bodyToIterable(),
]);
}
$mediaIds = [];
try {
foreach ($responses as $i => $response) {
unset($responses[$i]);
$result = $response->toArray(false);
if (300 <= $response->getStatusCode()) {
throw new TransportException(\sprintf('Unable to upload media as attachment: "%s" (%s).', $result['error_description'], $result['error']), $response);
}
$mediaIds[] = $result['id'];
}
} finally {
foreach ($responses as $response) {
$response->cancel();
}
}
return $mediaIds;
}
}